OSDN Git Service

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