OSDN Git Service

Update copyright year in most headers.
[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, 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 reg-i386-linux.c.  */
31 void init_registers_i386_linux (void);
32 /* Defined in auto-generated file reg-x86-64-linux.c.  */
33 void init_registers_x86_64_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 (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 (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 (i, ((char *) buf) + i386_regmap[i]);
192
193   collect_register_by_name ("orig_eax", ((char *) buf) + ORIG_EAX * 4);
194 }
195
196 static void
197 x86_store_gregset (const void *buf)
198 {
199   int i;
200
201 #ifdef __x86_64__
202   if (register_size (0) == 8)
203     {
204       for (i = 0; i < X86_64_NUM_REGS; i++)
205         if (x86_64_regmap[i] != -1)
206           supply_register (i, ((char *) buf) + x86_64_regmap[i]);
207       return;
208     }
209 #endif
210
211   for (i = 0; i < I386_NUM_REGS; i++)
212     supply_register (i, ((char *) buf) + i386_regmap[i]);
213
214   supply_register_by_name ("orig_eax", ((char *) buf) + ORIG_EAX * 4);
215 }
216
217 static void
218 x86_fill_fpregset (void *buf)
219 {
220 #ifdef __x86_64__
221   i387_cache_to_fxsave (buf);
222 #else
223   i387_cache_to_fsave (buf);
224 #endif
225 }
226
227 static void
228 x86_store_fpregset (const void *buf)
229 {
230 #ifdef __x86_64__
231   i387_fxsave_to_cache (buf);
232 #else
233   i387_fsave_to_cache (buf);
234 #endif
235 }
236
237 #ifndef __x86_64__
238
239 static void
240 x86_fill_fpxregset (void *buf)
241 {
242   i387_cache_to_fxsave (buf);
243 }
244
245 static void
246 x86_store_fpxregset (const void *buf)
247 {
248   i387_fxsave_to_cache (buf);
249 }
250
251 #endif
252
253 /* ??? The non-biarch i386 case stores all the i387 regs twice.
254    Once in i387_.*fsave.* and once in i387_.*fxsave.*.
255    This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS
256    doesn't work.  IWBN to avoid the duplication in the case where it
257    does work.  Maybe the arch_setup routine could check whether it works
258    and update target_regsets accordingly, maybe by moving target_regsets
259    to linux_target_ops and set the right one there, rather than having to
260    modify the target_regsets global.  */
261
262 struct regset_info target_regsets[] =
263 {
264 #ifdef HAVE_PTRACE_GETREGS
265   { PTRACE_GETREGS, PTRACE_SETREGS, sizeof (elf_gregset_t),
266     GENERAL_REGS,
267     x86_fill_gregset, x86_store_gregset },
268 # ifndef __x86_64__
269 #  ifdef HAVE_PTRACE_GETFPXREGS
270   { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, sizeof (elf_fpxregset_t),
271     EXTENDED_REGS,
272     x86_fill_fpxregset, x86_store_fpxregset },
273 #  endif
274 # endif
275   { PTRACE_GETFPREGS, PTRACE_SETFPREGS, sizeof (elf_fpregset_t),
276     FP_REGS,
277     x86_fill_fpregset, x86_store_fpregset },
278 #endif /* HAVE_PTRACE_GETREGS */
279   { 0, 0, -1, -1, NULL, NULL }
280 };
281
282 static CORE_ADDR
283 x86_get_pc (void)
284 {
285   int use_64bit = register_size (0) == 8;
286
287   if (use_64bit)
288     {
289       unsigned long pc;
290       collect_register_by_name ("rip", &pc);
291       return (CORE_ADDR) pc;
292     }
293   else
294     {
295       unsigned int pc;
296       collect_register_by_name ("eip", &pc);
297       return (CORE_ADDR) pc;
298     }
299 }
300
301 static void
302 x86_set_pc (CORE_ADDR pc)
303 {
304   int use_64bit = register_size (0) == 8;
305
306   if (use_64bit)
307     {
308       unsigned long newpc = pc;
309       supply_register_by_name ("rip", &newpc);
310     }
311   else
312     {
313       unsigned int newpc = pc;
314       supply_register_by_name ("eip", &newpc);
315     }
316 }
317 \f
318 static const unsigned char x86_breakpoint[] = { 0xCC };
319 #define x86_breakpoint_len 1
320
321 static int
322 x86_breakpoint_at (CORE_ADDR pc)
323 {
324   unsigned char c;
325
326   read_inferior_memory (pc, &c, 1);
327   if (c == 0xCC)
328     return 1;
329
330   return 0;
331 }
332 \f
333 /* Support for debug registers.  */
334
335 static unsigned long
336 x86_linux_dr_get (ptid_t ptid, int regnum)
337 {
338   int tid;
339   unsigned long value;
340
341   tid = ptid_get_lwp (ptid);
342
343   errno = 0;
344   value = ptrace (PTRACE_PEEKUSER, tid,
345                   offsetof (struct user, u_debugreg[regnum]), 0);
346   if (errno != 0)
347     error ("Couldn't read debug register");
348
349   return value;
350 }
351
352 static void
353 x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
354 {
355   int tid;
356
357   tid = ptid_get_lwp (ptid);
358
359   errno = 0;
360   ptrace (PTRACE_POKEUSER, tid,
361           offsetof (struct user, u_debugreg[regnum]), value);
362   if (errno != 0)
363     error ("Couldn't write debug register");
364 }
365
366 /* Update the inferior's debug register REGNUM from STATE.  */
367
368 void
369 i386_dr_low_set_addr (const struct i386_debug_reg_state *state, int regnum)
370 {
371   struct inferior_list_entry *lp;
372   CORE_ADDR addr;
373   /* Only need to update the threads of this process.  */
374   int pid = pid_of (get_thread_lwp (current_inferior));
375
376   if (! (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR))
377     fatal ("Invalid debug register %d", regnum);
378
379   addr = state->dr_mirror[regnum];
380
381   for (lp = all_lwps.head; lp; lp = lp->next)
382     {
383       struct lwp_info *lwp = (struct lwp_info *) lp;
384
385       /* The actual update is done later, we just mark that the register
386          needs updating.  */
387       if (pid_of (lwp) == pid)
388         lwp->arch_private->debug_registers_changed = 1;
389     }
390 }
391
392 /* Update the inferior's DR7 debug control register from STATE.  */
393
394 void
395 i386_dr_low_set_control (const struct i386_debug_reg_state *state)
396 {
397   struct inferior_list_entry *lp;
398   /* Only need to update the threads of this process.  */
399   int pid = pid_of (get_thread_lwp (current_inferior));
400
401   for (lp = all_lwps.head; lp; lp = lp->next)
402     {
403       struct lwp_info *lwp = (struct lwp_info *) lp;
404
405       /* The actual update is done later, we just mark that the register
406          needs updating.  */
407       if (pid_of (lwp) == pid)
408         lwp->arch_private->debug_registers_changed = 1;
409     }
410 }
411
412 /* Get the value of the DR6 debug status register from the inferior
413    and record it in STATE.  */
414
415 void
416 i386_dr_low_get_status (struct i386_debug_reg_state *state)
417 {
418   struct lwp_info *lwp = get_thread_lwp (current_inferior);
419   ptid_t ptid = ptid_of (lwp);
420
421   state->dr_status_mirror = x86_linux_dr_get (ptid, DR_STATUS);
422 }
423 \f
424 /* Watchpoint support.  */
425
426 static int
427 x86_insert_point (char type, CORE_ADDR addr, int len)
428 {
429   struct process_info *proc = current_process ();
430   switch (type)
431     {
432     case '2':
433     case '3':
434     case '4':
435       return i386_low_insert_watchpoint (&proc->private->arch_private->debug_reg_state,
436                                          type, addr, len);
437     default:
438       /* Unsupported.  */
439       return 1;
440     }
441 }
442
443 static int
444 x86_remove_point (char type, CORE_ADDR addr, int len)
445 {
446   struct process_info *proc = current_process ();
447   switch (type)
448     {
449     case '2':
450     case '3':
451     case '4':
452       return i386_low_remove_watchpoint (&proc->private->arch_private->debug_reg_state,
453                                          type, addr, len);
454     default:
455       /* Unsupported.  */
456       return 1;
457     }
458 }
459
460 static int
461 x86_stopped_by_watchpoint (void)
462 {
463   struct process_info *proc = current_process ();
464   return i386_low_stopped_by_watchpoint (&proc->private->arch_private->debug_reg_state);
465 }
466
467 static CORE_ADDR
468 x86_stopped_data_address (void)
469 {
470   struct process_info *proc = current_process ();
471   CORE_ADDR addr;
472   if (i386_low_stopped_data_address (&proc->private->arch_private->debug_reg_state,
473                                      &addr))
474     return addr;
475   return 0;
476 }
477 \f
478 /* Called when a new process is created.  */
479
480 static struct arch_process_info *
481 x86_linux_new_process (void)
482 {
483   struct arch_process_info *info = xcalloc (1, sizeof (*info));
484
485   i386_low_init_dregs (&info->debug_reg_state);
486
487   return info;
488 }
489
490 /* Called when a new thread is detected.  */
491
492 static struct arch_lwp_info *
493 x86_linux_new_thread (void)
494 {
495   struct arch_lwp_info *info = xcalloc (1, sizeof (*info));
496
497   info->debug_registers_changed = 1;
498
499   return info;
500 }
501
502 /* Called when resuming a thread.
503    If the debug regs have changed, update the thread's copies.  */
504
505 static void
506 x86_linux_prepare_to_resume (struct lwp_info *lwp)
507 {
508   if (lwp->arch_private->debug_registers_changed)
509     {
510       int i;
511       ptid_t ptid = ptid_of (lwp);
512       int pid = ptid_get_pid (ptid);
513       struct process_info *proc = find_process_pid (pid);
514       struct i386_debug_reg_state *state = &proc->private->arch_private->debug_reg_state;
515
516       for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
517         x86_linux_dr_set (ptid, i, state->dr_mirror[i]);
518
519       x86_linux_dr_set (ptid, DR_CONTROL, state->dr_control_mirror);
520
521       lwp->arch_private->debug_registers_changed = 0;
522     }
523 }
524 \f
525 /* When GDBSERVER is built as a 64-bit application on linux, the
526    PTRACE_GETSIGINFO data is always presented in 64-bit layout.  Since
527    debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same
528    as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit
529    conversion in-place ourselves.  */
530
531 /* These types below (compat_*) define a siginfo type that is layout
532    compatible with the siginfo type exported by the 32-bit userspace
533    support.  */
534
535 #ifdef __x86_64__
536
537 typedef int compat_int_t;
538 typedef unsigned int compat_uptr_t;
539
540 typedef int compat_time_t;
541 typedef int compat_timer_t;
542 typedef int compat_clock_t;
543
544 struct compat_timeval
545 {
546   compat_time_t tv_sec;
547   int tv_usec;
548 };
549
550 typedef union compat_sigval
551 {
552   compat_int_t sival_int;
553   compat_uptr_t sival_ptr;
554 } compat_sigval_t;
555
556 typedef struct compat_siginfo
557 {
558   int si_signo;
559   int si_errno;
560   int si_code;
561
562   union
563   {
564     int _pad[((128 / sizeof (int)) - 3)];
565
566     /* kill() */
567     struct
568     {
569       unsigned int _pid;
570       unsigned int _uid;
571     } _kill;
572
573     /* POSIX.1b timers */
574     struct
575     {
576       compat_timer_t _tid;
577       int _overrun;
578       compat_sigval_t _sigval;
579     } _timer;
580
581     /* POSIX.1b signals */
582     struct
583     {
584       unsigned int _pid;
585       unsigned int _uid;
586       compat_sigval_t _sigval;
587     } _rt;
588
589     /* SIGCHLD */
590     struct
591     {
592       unsigned int _pid;
593       unsigned int _uid;
594       int _status;
595       compat_clock_t _utime;
596       compat_clock_t _stime;
597     } _sigchld;
598
599     /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
600     struct
601     {
602       unsigned int _addr;
603     } _sigfault;
604
605     /* SIGPOLL */
606     struct
607     {
608       int _band;
609       int _fd;
610     } _sigpoll;
611   } _sifields;
612 } compat_siginfo_t;
613
614 #define cpt_si_pid _sifields._kill._pid
615 #define cpt_si_uid _sifields._kill._uid
616 #define cpt_si_timerid _sifields._timer._tid
617 #define cpt_si_overrun _sifields._timer._overrun
618 #define cpt_si_status _sifields._sigchld._status
619 #define cpt_si_utime _sifields._sigchld._utime
620 #define cpt_si_stime _sifields._sigchld._stime
621 #define cpt_si_ptr _sifields._rt._sigval.sival_ptr
622 #define cpt_si_addr _sifields._sigfault._addr
623 #define cpt_si_band _sifields._sigpoll._band
624 #define cpt_si_fd _sifields._sigpoll._fd
625
626 /* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
627    In their place is si_timer1,si_timer2.  */
628 #ifndef si_timerid
629 #define si_timerid si_timer1
630 #endif
631 #ifndef si_overrun
632 #define si_overrun si_timer2
633 #endif
634
635 static void
636 compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
637 {
638   memset (to, 0, sizeof (*to));
639
640   to->si_signo = from->si_signo;
641   to->si_errno = from->si_errno;
642   to->si_code = from->si_code;
643
644   if (to->si_code < 0)
645     {
646       to->cpt_si_ptr = (intptr_t) from->si_ptr;
647     }
648   else if (to->si_code == SI_USER)
649     {
650       to->cpt_si_pid = from->si_pid;
651       to->cpt_si_uid = from->si_uid;
652     }
653   else if (to->si_code == SI_TIMER)
654     {
655       to->cpt_si_timerid = from->si_timerid;
656       to->cpt_si_overrun = from->si_overrun;
657       to->cpt_si_ptr = (intptr_t) from->si_ptr;
658     }
659   else
660     {
661       switch (to->si_signo)
662         {
663         case SIGCHLD:
664           to->cpt_si_pid = from->si_pid;
665           to->cpt_si_uid = from->si_uid;
666           to->cpt_si_status = from->si_status;
667           to->cpt_si_utime = from->si_utime;
668           to->cpt_si_stime = from->si_stime;
669           break;
670         case SIGILL:
671         case SIGFPE:
672         case SIGSEGV:
673         case SIGBUS:
674           to->cpt_si_addr = (intptr_t) from->si_addr;
675           break;
676         case SIGPOLL:
677           to->cpt_si_band = from->si_band;
678           to->cpt_si_fd = from->si_fd;
679           break;
680         default:
681           to->cpt_si_pid = from->si_pid;
682           to->cpt_si_uid = from->si_uid;
683           to->cpt_si_ptr = (intptr_t) from->si_ptr;
684           break;
685         }
686     }
687 }
688
689 static void
690 siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
691 {
692   memset (to, 0, sizeof (*to));
693
694   to->si_signo = from->si_signo;
695   to->si_errno = from->si_errno;
696   to->si_code = from->si_code;
697
698   if (to->si_code < 0)
699     {
700       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
701     }
702   else if (to->si_code == SI_USER)
703     {
704       to->si_pid = from->cpt_si_pid;
705       to->si_uid = from->cpt_si_uid;
706     }
707   else if (to->si_code == SI_TIMER)
708     {
709       to->si_timerid = from->cpt_si_timerid;
710       to->si_overrun = from->cpt_si_overrun;
711       to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
712     }
713   else
714     {
715       switch (to->si_signo)
716         {
717         case SIGCHLD:
718           to->si_pid = from->cpt_si_pid;
719           to->si_uid = from->cpt_si_uid;
720           to->si_status = from->cpt_si_status;
721           to->si_utime = from->cpt_si_utime;
722           to->si_stime = from->cpt_si_stime;
723           break;
724         case SIGILL:
725         case SIGFPE:
726         case SIGSEGV:
727         case SIGBUS:
728           to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
729           break;
730         case SIGPOLL:
731           to->si_band = from->cpt_si_band;
732           to->si_fd = from->cpt_si_fd;
733           break;
734         default:
735           to->si_pid = from->cpt_si_pid;
736           to->si_uid = from->cpt_si_uid;
737           to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
738           break;
739         }
740     }
741 }
742
743 #endif /* __x86_64__ */
744
745 /* Convert a native/host siginfo object, into/from the siginfo in the
746    layout of the inferiors' architecture.  Returns true if any
747    conversion was done; false otherwise.  If DIRECTION is 1, then copy
748    from INF to NATIVE.  If DIRECTION is 0, copy from NATIVE to
749    INF.  */
750
751 static int
752 x86_siginfo_fixup (struct siginfo *native, void *inf, int direction)
753 {
754 #ifdef __x86_64__
755   /* Is the inferior 32-bit?  If so, then fixup the siginfo object.  */
756   if (register_size (0) == 4)
757     {
758       if (sizeof (struct siginfo) != sizeof (compat_siginfo_t))
759         fatal ("unexpected difference in siginfo");
760
761       if (direction == 0)
762         compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
763       else
764         siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
765
766       return 1;
767     }
768 #endif
769
770   return 0;
771 }
772 \f
773 /* Initialize gdbserver for the architecture of the inferior.  */
774
775 static void
776 x86_arch_setup (void)
777 {
778 #ifdef __x86_64__
779   int pid = pid_of (get_thread_lwp (current_inferior));
780   char *file = linux_child_pid_to_exec_file (pid);
781   int use_64bit = elf_64_file_p (file);
782
783   free (file);
784
785   if (use_64bit < 0)
786     {
787       /* This can only happen if /proc/<pid>/exe is unreadable,
788          but "that can't happen" if we've gotten this far.
789          Fall through and assume this is a 32-bit program.  */
790     }
791   else if (use_64bit)
792     {
793       init_registers_x86_64_linux ();
794
795       /* Amd64 doesn't have HAVE_LINUX_USRREGS.  */
796       the_low_target.num_regs = -1;
797       the_low_target.regmap = NULL;
798       the_low_target.cannot_fetch_register = NULL;
799       the_low_target.cannot_store_register = NULL;
800
801       /* Amd64 has 16 xmm regs.  */
802       num_xmm_registers = 16;
803
804       return;
805     }
806 #endif
807
808   /* Ok we have a 32-bit inferior.  */
809
810   init_registers_i386_linux ();
811
812   the_low_target.num_regs = I386_NUM_REGS;
813   the_low_target.regmap = i386_regmap;
814   the_low_target.cannot_fetch_register = i386_cannot_fetch_register;
815   the_low_target.cannot_store_register = i386_cannot_store_register;
816
817   /* I386 has 8 xmm regs.  */
818   num_xmm_registers = 8;
819 }
820
821 /* This is initialized assuming an amd64 target.
822    x86_arch_setup will correct it for i386 or amd64 targets.  */
823
824 struct linux_target_ops the_low_target =
825 {
826   x86_arch_setup,
827   -1,
828   NULL,
829   NULL,
830   NULL,
831   x86_get_pc,
832   x86_set_pc,
833   x86_breakpoint,
834   x86_breakpoint_len,
835   NULL,
836   1,
837   x86_breakpoint_at,
838   x86_insert_point,
839   x86_remove_point,
840   x86_stopped_by_watchpoint,
841   x86_stopped_data_address,
842   /* collect_ptrace_register/supply_ptrace_register are not needed in the
843      native i386 case (no registers smaller than an xfer unit), and are not
844      used in the biarch case (HAVE_LINUX_USRREGS is not defined).  */
845   NULL,
846   NULL,
847   /* need to fix up i386 siginfo if host is amd64 */
848   x86_siginfo_fixup,
849   x86_linux_new_process,
850   x86_linux_new_thread,
851   x86_linux_prepare_to_resume
852 };