* inferiors.c (add_thread): Set last_status kind to
[external/binutils.git] / gdb / gdbserver / linux-x86-low.c
1 /* GNU/Linux/x86-64 specific low level interface, for the remote server
2    for GDB.
3    Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include <stddef.h>
22 #include <signal.h>
23 #include "server.h"
24 #include "linux-low.h"
25 #include "i387-fp.h"
26 #include "i386-low.h"
27
28 #include "gdb_proc_service.h"
29
30 /* Defined in auto-generated file i386-linux.c.  */
31 void init_registers_i386_linux (void);
32 /* Defined in auto-generated file amd64-linux.c.  */
33 void init_registers_amd64_linux (void);
34
35 #include <sys/reg.h>
36 #include <sys/procfs.h>
37 #include <sys/ptrace.h>
38
39 #ifndef PTRACE_GET_THREAD_AREA
40 #define PTRACE_GET_THREAD_AREA 25
41 #endif
42
43 /* This definition comes from prctl.h, but some kernels may not have it.  */
44 #ifndef PTRACE_ARCH_PRCTL
45 #define PTRACE_ARCH_PRCTL      30
46 #endif
47
48 /* The following definitions come from prctl.h, but may be absent
49    for certain configurations.  */
50 #ifndef ARCH_GET_FS
51 #define ARCH_SET_GS 0x1001
52 #define ARCH_SET_FS 0x1002
53 #define ARCH_GET_FS 0x1003
54 #define ARCH_GET_GS 0x1004
55 #endif
56
57 /* Per-process arch-specific data we want to keep.  */
58
59 struct arch_process_info
60 {
61   struct i386_debug_reg_state debug_reg_state;
62 };
63
64 /* Per-thread arch-specific data we want to keep.  */
65
66 struct arch_lwp_info
67 {
68   /* Non-zero if our copy differs from what's recorded in the thread.  */
69   int debug_registers_changed;
70 };
71
72 #ifdef __x86_64__
73
74 /* Mapping between the general-purpose registers in `struct user'
75    format and GDB's register array layout.
76    Note that the transfer layout uses 64-bit regs.  */
77 static /*const*/ int i386_regmap[] = 
78 {
79   RAX * 8, RCX * 8, RDX * 8, RBX * 8,
80   RSP * 8, RBP * 8, RSI * 8, RDI * 8,
81   RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
82   DS * 8, ES * 8, FS * 8, GS * 8
83 };
84
85 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
86
87 /* So code below doesn't have to care, i386 or amd64.  */
88 #define ORIG_EAX ORIG_RAX
89
90 static const int x86_64_regmap[] =
91 {
92   RAX * 8, RBX * 8, RCX * 8, RDX * 8,
93   RSI * 8, RDI * 8, RBP * 8, RSP * 8,
94   R8 * 8, R9 * 8, R10 * 8, R11 * 8,
95   R12 * 8, R13 * 8, R14 * 8, R15 * 8,
96   RIP * 8, EFLAGS * 8, CS * 8, SS * 8,
97   DS * 8, ES * 8, FS * 8, GS * 8,
98   -1, -1, -1, -1, -1, -1, -1, -1,
99   -1, -1, -1, -1, -1, -1, -1, -1,
100   -1, -1, -1, -1, -1, -1, -1, -1,
101   -1, -1, -1, -1, -1, -1, -1, -1, -1,
102   ORIG_RAX * 8
103 };
104
105 #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0]))
106
107 #else /* ! __x86_64__ */
108
109 /* Mapping between the general-purpose registers in `struct user'
110    format and GDB's register array layout.  */
111 static /*const*/ int i386_regmap[] = 
112 {
113   EAX * 4, ECX * 4, EDX * 4, EBX * 4,
114   UESP * 4, EBP * 4, ESI * 4, EDI * 4,
115   EIP * 4, EFL * 4, CS * 4, SS * 4,
116   DS * 4, ES * 4, FS * 4, GS * 4
117 };
118
119 #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0]))
120
121 #endif
122 \f
123 /* Called by libthread_db.  */
124
125 ps_err_e
126 ps_get_thread_area (const struct ps_prochandle *ph,
127                     lwpid_t lwpid, int idx, void **base)
128 {
129 #ifdef __x86_64__
130   int use_64bit = register_size (0) == 8;
131
132   if (use_64bit)
133     {
134       switch (idx)
135         {
136         case FS:
137           if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
138             return PS_OK;
139           break;
140         case GS:
141           if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
142             return PS_OK;
143           break;
144         default:
145           return PS_BADADDR;
146         }
147       return PS_ERR;
148     }
149 #endif
150
151   {
152     unsigned int desc[4];
153
154     if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
155                 (void *) (intptr_t) idx, (unsigned long) &desc) < 0)
156       return PS_ERR;
157
158     *(int *)base = desc[1];
159     return PS_OK;
160   }
161 }
162 \f
163 static int
164 i386_cannot_store_register (int regno)
165 {
166   return regno >= I386_NUM_REGS;
167 }
168
169 static int
170 i386_cannot_fetch_register (int regno)
171 {
172   return regno >= I386_NUM_REGS;
173 }
174
175 static void
176 x86_fill_gregset (struct regcache *regcache, void *buf)
177 {
178   int i;
179
180 #ifdef __x86_64__
181   if (register_size (0) == 8)
182     {
183       for (i = 0; i < X86_64_NUM_REGS; i++)
184         if (x86_64_regmap[i] != -1)
185           collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
186       return;
187     }
188 #endif
189
190   for (i = 0; i < I386_NUM_REGS; i++)
191     collect_register (regcache, i, ((char *) buf) + i386_regmap[i]);
192
193   collect_register_by_name (regcache, "orig_eax",
194                             ((char *) buf) + ORIG_EAX * 4);
195 }
196
197 static void
198 x86_store_gregset (struct regcache *regcache, const void *buf)
199 {
200   int i;
201
202 #ifdef __x86_64__
203   if (register_size (0) == 8)
204     {
205       for (i = 0; i < X86_64_NUM_REGS; i++)
206         if (x86_64_regmap[i] != -1)
207           supply_register (regcache, i, ((char *) buf) + x86_64_regmap[i]);
208       return;
209     }
210 #endif
211
212   for (i = 0; i < I386_NUM_REGS; i++)
213     supply_register (regcache, i, ((char *) buf) + i386_regmap[i]);
214
215   supply_register_by_name (regcache, "orig_eax",
216                            ((char *) buf) + ORIG_EAX * 4);
217 }
218
219 static void
220 x86_fill_fpregset (struct regcache *regcache, void *buf)
221 {
222 #ifdef __x86_64__
223   i387_cache_to_fxsave (regcache, buf);
224 #else
225   i387_cache_to_fsave (regcache, buf);
226 #endif
227 }
228
229 static void
230 x86_store_fpregset (struct regcache *regcache, const void *buf)
231 {
232 #ifdef __x86_64__
233   i387_fxsave_to_cache (regcache, buf);
234 #else
235   i387_fsave_to_cache (regcache, buf);
236 #endif
237 }
238
239 #ifndef __x86_64__
240
241 static void
242 x86_fill_fpxregset (struct regcache *regcache, void *buf)
243 {
244   i387_cache_to_fxsave (regcache, buf);
245 }
246
247 static void
248 x86_store_fpxregset (struct regcache *regcache, const void *buf)
249 {
250   i387_fxsave_to_cache (regcache, buf);
251 }
252
253 #endif
254
255 /* ??? The non-biarch i386 case stores all the i387 regs twice.
256    Once in i387_.*fsave.* and once in i387_.*fxsave.*.
257    This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
258    doesn't work.  IWBN to avoid the duplication in the case where it
259    does work.  Maybe the arch_setup routine could check whether it works
260    and update target_regsets accordingly, maybe by moving target_regsets
261    to linux_target_ops and set the right one there, rather than having to
262    modify the target_regsets global.  */
263
264 struct regset_info target_regsets[] =
265 {
266 #ifdef HAVE_PTRACE_GETREGS
267   { PTRACE_GETREGS, PTRACE_SETREGS, sizeof (elf_gregset_t),
268     GENERAL_REGS,
269     x86_fill_gregset, x86_store_gregset },
270 # ifndef __x86_64__
271 #  ifdef HAVE_PTRACE_GETFPXREGS
272   { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, sizeof (elf_fpxregset_t),
273     EXTENDED_REGS,
274     x86_fill_fpxregset, x86_store_fpxregset },
275 #  endif
276 # endif
277   { PTRACE_GETFPREGS, PTRACE_SETFPREGS, sizeof (elf_fpregset_t),
278     FP_REGS,
279     x86_fill_fpregset, x86_store_fpregset },
280 #endif /* HAVE_PTRACE_GETREGS */
281   { 0, 0, -1, -1, NULL, NULL }
282 };
283
284 static CORE_ADDR
285 x86_get_pc (struct regcache *regcache)
286 {
287   int use_64bit = register_size (0) == 8;
288
289   if (use_64bit)
290     {
291       unsigned long pc;
292       collect_register_by_name (regcache, "rip", &pc);
293       return (CORE_ADDR) pc;
294     }
295   else
296     {
297       unsigned int pc;
298       collect_register_by_name (regcache, "eip", &pc);
299       return (CORE_ADDR) pc;
300     }
301 }
302
303 static void
304 x86_set_pc (struct regcache *regcache, CORE_ADDR pc)
305 {
306   int use_64bit = register_size (0) == 8;
307
308   if (use_64bit)
309     {
310       unsigned long newpc = pc;
311       supply_register_by_name (regcache, "rip", &newpc);
312     }
313   else
314     {
315       unsigned int newpc = pc;
316       supply_register_by_name (regcache, "eip", &newpc);
317     }
318 }
319 \f
320 static const unsigned char x86_breakpoint[] = { 0xCC };
321 #define x86_breakpoint_len 1
322
323 static int
324 x86_breakpoint_at (CORE_ADDR pc)
325 {
326   unsigned char c;
327
328   (*the_target->read_memory) (pc, &c, 1);
329   if (c == 0xCC)
330     return 1;
331
332   return 0;
333 }
334 \f
335 /* Support for debug registers.  */
336
337 static unsigned long
338 x86_linux_dr_get (ptid_t ptid, int regnum)
339 {
340   int tid;
341   unsigned long value;
342
343   tid = ptid_get_lwp (ptid);
344
345   errno = 0;
346   value = ptrace (PTRACE_PEEKUSER, tid,
347                   offsetof (struct user, u_debugreg[regnum]), 0);
348   if (errno != 0)
349     error ("Couldn't read debug register");
350
351   return value;
352 }
353
354 static void
355 x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
356 {
357   int tid;
358
359   tid = ptid_get_lwp (ptid);
360
361   errno = 0;
362   ptrace (PTRACE_POKEUSER, tid,
363           offsetof (struct user, u_debugreg[regnum]), value);
364   if (errno != 0)
365     error ("Couldn't write debug register");
366 }
367
368 /* Update the inferior's debug register REGNUM from STATE.  */
369
370 void
371 i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
372 {
373   struct inferior_list_entry *lp;
374   CORE_ADDR addr;
375   /* Only need to update the threads of this process.  */
376   int pid = pid_of (get_thread_lwp (current_inferior));
377
378   if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
379     fatal ("Invalid debug register %d", regnum);
380
381   addr = state->dr_mirror[regnum];
382
383   for (lp = all_lwps.head; lp; lp = lp->next)
384     {
385       struct lwp_info *lwp = (struct lwp_info *) lp;
386
387       /* The actual update is done later, we just mark that the register
388          needs updating.  */
389       if (pid_of (lwp) == pid)
390         lwp->arch_private->debug_registers_changed = 1;
391     }
392 }
393
394 /* Update the inferior's DR7 debug control register from STATE.  */
395
396 void
397 i386_dr_low_set_control (const struct i386_debug_reg_state *state)
398 {
399   struct inferior_list_entry *lp;
400   /* Only need to update the threads of this process.  */
401   int pid = pid_of (get_thread_lwp (current_inferior));
402
403   for (lp = all_lwps.head; lp; lp = lp->next)
404     {
405       struct lwp_info *lwp = (struct lwp_info *) lp;
406
407       /* The actual update is done later, we just mark that the register
408          needs updating.  */
409       if (pid_of (lwp) == pid)
410         lwp->arch_private->debug_registers_changed = 1;
411     }
412 }
413
414 /* Get the value of the DR6 debug status register from the inferior
415    and record it in STATE.  */
416
417 void
418 i386_dr_low_get_status (struct i386_debug_reg_state *state)
419 {
420   struct lwp_info *lwp = get_thread_lwp (current_inferior);
421   ptid_t ptid = ptid_of (lwp);
422
423   state->dr_status_mirror = x86_linux_dr_get (ptid, DR_STATUS);
424 }
425 \f
426 /* Watchpoint support.  */
427
428 static int
429 x86_insert_point (char type, CORE_ADDR addr, int len)
430 {
431   struct process_info *proc = current_process ();
432   switch (type)
433     {
434     case '0':
435       return set_gdb_breakpoint_at (addr);
436     case '2':
437     case '3':
438     case '4':
439       return i386_low_insert_watchpoint (&proc->private->arch_private->debug_reg_state,
440                                          type, addr, len);
441     default:
442       /* Unsupported.  */
443       return 1;
444     }
445 }
446
447 static int
448 x86_remove_point (char type, CORE_ADDR addr, int len)
449 {
450   struct process_info *proc = current_process ();
451   switch (type)
452     {
453     case '0':
454       return delete_gdb_breakpoint_at (addr);
455     case '2':
456     case '3':
457     case '4':
458       return i386_low_remove_watchpoint (&proc->private->arch_private->debug_reg_state,
459                                          type, addr, len);
460     default:
461       /* Unsupported.  */
462       return 1;
463     }
464 }
465
466 static int
467 x86_stopped_by_watchpoint (void)
468 {
469   struct process_info *proc = current_process ();
470   return i386_low_stopped_by_watchpoint (&proc->private->arch_private->debug_reg_state);
471 }
472
473 static CORE_ADDR
474 x86_stopped_data_address (void)
475 {
476   struct process_info *proc = current_process ();
477   CORE_ADDR addr;
478   if (i386_low_stopped_data_address (&proc->private->arch_private->debug_reg_state,
479                                      &addr))
480     return addr;
481   return 0;
482 }
483 \f
484 /* Called when a new process is created.  */
485
486 static struct arch_process_info *
487 x86_linux_new_process (void)
488 {
489   struct arch_process_info *info = xcalloc (1, sizeof (*info));
490
491   i386_low_init_dregs (&info->debug_reg_state);
492
493   return info;
494 }
495
496 /* Called when a new thread is detected.  */
497
498 static struct arch_lwp_info *
499 x86_linux_new_thread (void)
500 {
501   struct arch_lwp_info *info = xcalloc (1, sizeof (*info));
502
503   info->debug_registers_changed = 1;
504
505   return info;
506 }
507
508 /* Called when resuming a thread.
509    If the debug regs have changed, update the thread's copies.  */
510
511 static void
512 x86_linux_prepare_to_resume (struct lwp_info *lwp)
513 {
514   ptid_t ptid = ptid_of (lwp);
515
516   if (lwp->arch_private->debug_registers_changed)
517     {
518       int i;
519       int pid = ptid_get_pid (ptid);
520       struct process_info *proc = find_process_pid (pid);
521       struct i386_debug_reg_state *state = &proc->private->arch_private->debug_reg_state;
522
523       for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
524         x86_linux_dr_set (ptid, i, state->dr_mirror[i]);
525
526       x86_linux_dr_set (ptid, DR_CONTROL, state->dr_control_mirror);
527
528       lwp->arch_private->debug_registers_changed = 0;
529     }
530
531   if (lwp->stopped_by_watchpoint)
532     x86_linux_dr_set (ptid, DR_STATUS, 0);
533 }
534 \f
535 /* When GDBSERVER is built as a 64-bit application on linux, the
536    PTRACE_GETSIGINFO data is always presented in 64-bit layout.  Since
537    debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same
538    as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
539    conversion in-place ourselves.  */
540
541 /* These types below (compat_*) define a siginfo type that is layout
542    compatible with the siginfo type exported by the 32-bit userspace
543    support.  */
544
545 #ifdef __x86_64__
546
547 typedef int compat_int_t;
548 typedef unsigned int compat_uptr_t;
549
550 typedef int compat_time_t;
551 typedef int compat_timer_t;
552 typedef int compat_clock_t;
553
554 struct compat_timeval
555 {
556   compat_time_t tv_sec;
557   int tv_usec;
558 };
559
560 typedef union compat_sigval
561 {
562   compat_int_t sival_int;
563   compat_uptr_t sival_ptr;
564 } compat_sigval_t;
565
566 typedef struct compat_siginfo
567 {
568   int si_signo;
569   int si_errno;
570   int si_code;
571
572   union
573   {
574     int _pad[((128 / sizeof (int)) - 3)];
575
576     /* kill() */
577     struct
578     {
579       unsigned int _pid;
580       unsigned int _uid;
581     } _kill;
582
583     /* POSIX.1b timers */
584     struct
585     {
586       compat_timer_t _tid;
587       int _overrun;
588       compat_sigval_t _sigval;
589     } _timer;
590
591     /* POSIX.1b signals */
592     struct
593     {
594       unsigned int _pid;
595       unsigned int _uid;
596       compat_sigval_t _sigval;
597     } _rt;
598
599     /* SIGCHLD */
600     struct
601     {
602       unsigned int _pid;
603       unsigned int _uid;
604       int _status;
605       compat_clock_t _utime;
606       compat_clock_t _stime;
607     } _sigchld;
608
609     /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
610     struct
611     {
612       unsigned int _addr;
613     } _sigfault;
614
615     /* SIGPOLL */
616     struct
617     {
618       int _band;
619       int _fd;
620     } _sigpoll;
621   } _sifields;
622 } compat_siginfo_t;
623
624 #define cpt_si_pid _sifields._kill._pid
625 #define cpt_si_uid _sifields._kill._uid
626 #define cpt_si_timerid _sifields._timer._tid
627 #define cpt_si_overrun _sifields._timer._overrun
628 #define cpt_si_status _sifields._sigchld._status
629 #define cpt_si_utime _sifields._sigchld._utime
630 #define cpt_si_stime _sifields._sigchld._stime
631 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
632 #define cpt_si_addr _sifields._sigfault._addr
633 #define cpt_si_band _sifields._sigpoll._band
634 #define cpt_si_fd _sifields._sigpoll._fd
635
636 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
637    In their place is si_timer1,si_timer2.  */
638 #ifndef si_timerid
639 #define si_timerid si_timer1
640 #endif
641 #ifndef si_overrun
642 #define si_overrun si_timer2
643 #endif
644
645 static void
646 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
647 {
648   memset (to, 0, sizeof (*to));
649
650   to->si_signo = from->si_signo;
651   to->si_errno = from->si_errno;
652   to->si_code = from->si_code;
653
654   if (to->si_code < 0)
655     {
656       to->cpt_si_ptr = (intptr_t) from->si_ptr;
657     }
658   else if (to->si_code == SI_USER)
659     {
660       to->cpt_si_pid = from->si_pid;
661       to->cpt_si_uid = from->si_uid;
662     }
663   else if (to->si_code == SI_TIMER)
664     {
665       to->cpt_si_timerid = from->si_timerid;
666       to->cpt_si_overrun = from->si_overrun;
667       to->cpt_si_ptr = (intptr_t) from->si_ptr;
668     }
669   else
670     {
671       switch (to->si_signo)
672         {
673         case SIGCHLD:
674           to->cpt_si_pid = from->si_pid;
675           to->cpt_si_uid = from->si_uid;
676           to->cpt_si_status = from->si_status;
677           to->cpt_si_utime = from->si_utime;
678           to->cpt_si_stime = from->si_stime;
679           break;
680         case SIGILL:
681         case SIGFPE:
682         case SIGSEGV:
683         case SIGBUS:
684           to->cpt_si_addr = (intptr_t) from->si_addr;
685           break;
686         case SIGPOLL:
687           to->cpt_si_band = from->si_band;
688           to->cpt_si_fd = from->si_fd;
689           break;
690         default:
691           to->cpt_si_pid = from->si_pid;
692           to->cpt_si_uid = from->si_uid;
693           to->cpt_si_ptr = (intptr_t) from->si_ptr;
694           break;
695         }
696     }
697 }
698
699 static void
700 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
701 {
702   memset (to, 0, sizeof (*to));
703
704   to->si_signo = from->si_signo;
705   to->si_errno = from->si_errno;
706   to->si_code = from->si_code;
707
708   if (to->si_code < 0)
709     {
710       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
711     }
712   else if (to->si_code == SI_USER)
713     {
714       to->si_pid = from->cpt_si_pid;
715       to->si_uid = from->cpt_si_uid;
716     }
717   else if (to->si_code == SI_TIMER)
718     {
719       to->si_timerid = from->cpt_si_timerid;
720       to->si_overrun = from->cpt_si_overrun;
721       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
722     }
723   else
724     {
725       switch (to->si_signo)
726         {
727         case SIGCHLD:
728           to->si_pid = from->cpt_si_pid;
729           to->si_uid = from->cpt_si_uid;
730           to->si_status = from->cpt_si_status;
731           to->si_utime = from->cpt_si_utime;
732           to->si_stime = from->cpt_si_stime;
733           break;
734         case SIGILL:
735         case SIGFPE:
736         case SIGSEGV:
737         case SIGBUS:
738           to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
739           break;
740         case SIGPOLL:
741           to->si_band = from->cpt_si_band;
742           to->si_fd = from->cpt_si_fd;
743           break;
744         default:
745           to->si_pid = from->cpt_si_pid;
746           to->si_uid = from->cpt_si_uid;
747           to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
748           break;
749         }
750     }
751 }
752
753 #endif /* __x86_64__ */
754
755 /* Convert a native/host siginfo object, into/from the siginfo in the
756    layout of the inferiors' architecture.  Returns true if any
757    conversion was done; false otherwise.  If DIRECTION is 1, then copy
758    from INF to NATIVE.  If DIRECTION is 0, copy from NATIVE to
759    INF.  */
760
761 static int
762 x86_siginfo_fixup (struct siginfo *native, void *inf, int direction)
763 {
764 #ifdef __x86_64__
765   /* Is the inferior 32-bit?  If so, then fixup the siginfo object.  */
766   if (register_size (0) == 4)
767     {
768       if (sizeof (struct siginfo) != sizeof (compat_siginfo_t))
769         fatal ("unexpected difference in siginfo");
770
771       if (direction == 0)
772         compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
773       else
774         siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
775
776       return 1;
777     }
778 #endif
779
780   return 0;
781 }
782 \f
783 /* Initialize gdbserver for the architecture of the inferior.  */
784
785 static void
786 x86_arch_setup (void)
787 {
788 #ifdef __x86_64__
789   int pid = pid_of (get_thread_lwp (current_inferior));
790   char *file = linux_child_pid_to_exec_file (pid);
791   int use_64bit = elf_64_file_p (file);
792
793   free (file);
794
795   if (use_64bit < 0)
796     {
797       /* This can only happen if /proc/<pid>/exe is unreadable,
798          but "that can't happen" if we've gotten this far.
799          Fall through and assume this is a 32-bit program.  */
800     }
801   else if (use_64bit)
802     {
803       init_registers_amd64_linux ();
804
805       /* Amd64 doesn't have HAVE_LINUX_USRREGS.  */
806       the_low_target.num_regs = -1;
807       the_low_target.regmap = NULL;
808       the_low_target.cannot_fetch_register = NULL;
809       the_low_target.cannot_store_register = NULL;
810
811       /* Amd64 has 16 xmm regs.  */
812       num_xmm_registers = 16;
813
814       return;
815     }
816 #endif
817
818   /* Ok we have a 32-bit inferior.  */
819
820   init_registers_i386_linux ();
821
822   the_low_target.num_regs = I386_NUM_REGS;
823   the_low_target.regmap = i386_regmap;
824   the_low_target.cannot_fetch_register = i386_cannot_fetch_register;
825   the_low_target.cannot_store_register = i386_cannot_store_register;
826
827   /* I386 has 8 xmm regs.  */
828   num_xmm_registers = 8;
829 }
830
831 /* This is initialized assuming an amd64 target.
832    x86_arch_setup will correct it for i386 or amd64 targets.  */
833
834 struct linux_target_ops the_low_target =
835 {
836   x86_arch_setup,
837   -1,
838   NULL,
839   NULL,
840   NULL,
841   x86_get_pc,
842   x86_set_pc,
843   x86_breakpoint,
844   x86_breakpoint_len,
845   NULL,
846   1,
847   x86_breakpoint_at,
848   x86_insert_point,
849   x86_remove_point,
850   x86_stopped_by_watchpoint,
851   x86_stopped_data_address,
852   /* collect_ptrace_register/supply_ptrace_register are not needed in the
853      native i386 case (no registers smaller than an xfer unit), and are not
854      used in the biarch case (HAVE_LINUX_USRREGS is not defined).  */
855   NULL,
856   NULL,
857   /* need to fix up i386 siginfo if host is amd64 */
858   x86_siginfo_fixup,
859   x86_linux_new_process,
860   x86_linux_new_thread,
861   x86_linux_prepare_to_resume
862 };