1 /* GNU/Linux on ARM native support.
2 Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
25 #include "gdb_string.h"
28 #include "linux-nat.h"
31 #include "arm-linux-tdep.h"
34 #include <sys/ptrace.h>
35 #include <sys/utsname.h>
36 #include <sys/procfs.h>
38 /* Prototypes for supply_gregset etc. */
41 /* Defines ps_err_e, struct ps_prochandle. */
42 #include "gdb_proc_service.h"
44 #ifndef PTRACE_GET_THREAD_AREA
45 #define PTRACE_GET_THREAD_AREA 22
48 extern int arm_apcs_32
;
50 /* The following variables are used to determine the version of the
51 underlying GNU/Linux operating system. Examples:
53 GNU/Linux 2.0.35 GNU/Linux 2.2.12
54 os_version = 0x00020023 os_version = 0x0002020c
55 os_major = 2 os_major = 2
56 os_minor = 0 os_minor = 2
57 os_release = 35 os_release = 12
59 Note: os_version = (os_major << 16) | (os_minor << 8) | os_release
61 These are initialized using get_linux_version() from
62 _initialize_arm_linux_nat(). */
64 static unsigned int os_version
, os_major
, os_minor
, os_release
;
66 /* On GNU/Linux, threads are implemented as pseudo-processes, in which
67 case we may be tracing more than one process at a time. In that
68 case, inferior_ptid will contain the main process ID and the
69 individual thread (process) ID. get_thread_id () is used to get
70 the thread id if it's available, and the process id otherwise. */
73 get_thread_id (ptid_t ptid
)
75 int tid
= TIDGET (ptid
);
80 #define GET_THREAD_ID(PTID) get_thread_id ((PTID));
82 /* Get the value of a particular register from the floating point
83 state of the process and store it into regcache. */
86 fetch_fpregister (int regno
)
89 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
91 /* Get the thread id for the ptrace call. */
92 tid
= GET_THREAD_ID (inferior_ptid
);
94 /* Read the floating point state. */
95 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
98 warning (_("Unable to fetch floating point register."));
103 if (ARM_FPS_REGNUM
== regno
)
104 regcache_raw_supply (current_regcache
, ARM_FPS_REGNUM
,
105 fp
+ NWFPE_FPSR_OFFSET
);
107 /* Fetch the floating point register. */
108 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
109 supply_nwfpe_register (current_regcache
, regno
, fp
);
112 /* Get the whole floating point state of the process and store it
119 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
121 /* Get the thread id for the ptrace call. */
122 tid
= GET_THREAD_ID (inferior_ptid
);
124 /* Read the floating point state. */
125 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
128 warning (_("Unable to fetch the floating point registers."));
133 regcache_raw_supply (current_regcache
, ARM_FPS_REGNUM
,
134 fp
+ NWFPE_FPSR_OFFSET
);
136 /* Fetch the floating point registers. */
137 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
138 supply_nwfpe_register (current_regcache
, regno
, fp
);
141 /* Save a particular register into the floating point state of the
142 process using the contents from regcache. */
145 store_fpregister (int regno
)
148 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
150 /* Get the thread id for the ptrace call. */
151 tid
= GET_THREAD_ID (inferior_ptid
);
153 /* Read the floating point state. */
154 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
157 warning (_("Unable to fetch the floating point registers."));
162 if (ARM_FPS_REGNUM
== regno
&& register_cached (ARM_FPS_REGNUM
))
163 regcache_raw_collect (current_regcache
, ARM_FPS_REGNUM
,
164 fp
+ NWFPE_FPSR_OFFSET
);
166 /* Store the floating point register. */
167 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_F7_REGNUM
)
168 collect_nwfpe_register (current_regcache
, regno
, fp
);
170 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
173 warning (_("Unable to store floating point register."));
178 /* Save the whole floating point state of the process using
179 the contents from regcache. */
185 gdb_byte fp
[ARM_LINUX_SIZEOF_NWFPE
];
187 /* Get the thread id for the ptrace call. */
188 tid
= GET_THREAD_ID (inferior_ptid
);
190 /* Read the floating point state. */
191 ret
= ptrace (PT_GETFPREGS
, tid
, 0, fp
);
194 warning (_("Unable to fetch the floating point registers."));
199 if (register_cached (ARM_FPS_REGNUM
))
200 regcache_raw_collect (current_regcache
, ARM_FPS_REGNUM
,
201 fp
+ NWFPE_FPSR_OFFSET
);
203 /* Store the floating point registers. */
204 for (regno
= ARM_F0_REGNUM
; regno
<= ARM_F7_REGNUM
; regno
++)
205 if (register_cached (regno
))
206 collect_nwfpe_register (current_regcache
, regno
, fp
);
208 ret
= ptrace (PTRACE_SETFPREGS
, tid
, 0, fp
);
211 warning (_("Unable to store floating point registers."));
216 /* Fetch a general register of the process and store into
220 fetch_register (int regno
)
225 /* Get the thread id for the ptrace call. */
226 tid
= GET_THREAD_ID (inferior_ptid
);
228 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
231 warning (_("Unable to fetch general register."));
235 if (regno
>= ARM_A1_REGNUM
&& regno
< ARM_PC_REGNUM
)
236 regcache_raw_supply (current_regcache
, regno
, (char *) ®s
[regno
]);
238 if (ARM_PS_REGNUM
== regno
)
241 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
242 (char *) ®s
[ARM_CPSR_REGNUM
]);
244 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
245 (char *) ®s
[ARM_PC_REGNUM
]);
248 if (ARM_PC_REGNUM
== regno
)
250 regs
[ARM_PC_REGNUM
] = ADDR_BITS_REMOVE (regs
[ARM_PC_REGNUM
]);
251 regcache_raw_supply (current_regcache
, ARM_PC_REGNUM
,
252 (char *) ®s
[ARM_PC_REGNUM
]);
256 /* Fetch all general registers of the process and store into
265 /* Get the thread id for the ptrace call. */
266 tid
= GET_THREAD_ID (inferior_ptid
);
268 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
271 warning (_("Unable to fetch general registers."));
275 for (regno
= ARM_A1_REGNUM
; regno
< ARM_PC_REGNUM
; regno
++)
276 regcache_raw_supply (current_regcache
, regno
, (char *) ®s
[regno
]);
279 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
280 (char *) ®s
[ARM_CPSR_REGNUM
]);
282 regcache_raw_supply (current_regcache
, ARM_PS_REGNUM
,
283 (char *) ®s
[ARM_PC_REGNUM
]);
285 regs
[ARM_PC_REGNUM
] = ADDR_BITS_REMOVE (regs
[ARM_PC_REGNUM
]);
286 regcache_raw_supply (current_regcache
, ARM_PC_REGNUM
,
287 (char *) ®s
[ARM_PC_REGNUM
]);
290 /* Store all general registers of the process from the values in
294 store_register (int regno
)
299 if (!register_cached (regno
))
302 /* Get the thread id for the ptrace call. */
303 tid
= GET_THREAD_ID (inferior_ptid
);
305 /* Get the general registers from the process. */
306 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
309 warning (_("Unable to fetch general registers."));
313 if (regno
>= ARM_A1_REGNUM
&& regno
<= ARM_PC_REGNUM
)
314 regcache_raw_collect (current_regcache
, regno
, (char *) ®s
[regno
]);
315 else if (arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
316 regcache_raw_collect (current_regcache
, regno
,
317 (char *) ®s
[ARM_CPSR_REGNUM
]);
318 else if (!arm_apcs_32
&& regno
== ARM_PS_REGNUM
)
319 regcache_raw_collect (current_regcache
, ARM_PC_REGNUM
,
320 (char *) ®s
[ARM_PC_REGNUM
]);
322 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
325 warning (_("Unable to store general register."));
336 /* Get the thread id for the ptrace call. */
337 tid
= GET_THREAD_ID (inferior_ptid
);
339 /* Fetch the general registers. */
340 ret
= ptrace (PTRACE_GETREGS
, tid
, 0, ®s
);
343 warning (_("Unable to fetch general registers."));
347 for (regno
= ARM_A1_REGNUM
; regno
<= ARM_PC_REGNUM
; regno
++)
349 if (register_cached (regno
))
350 regcache_raw_collect (current_regcache
, regno
, (char *) ®s
[regno
]);
353 if (arm_apcs_32
&& register_cached (ARM_PS_REGNUM
))
354 regcache_raw_collect (current_regcache
, ARM_PS_REGNUM
,
355 (char *) ®s
[ARM_CPSR_REGNUM
]);
357 ret
= ptrace (PTRACE_SETREGS
, tid
, 0, ®s
);
361 warning (_("Unable to store general registers."));
366 /* Fetch registers from the child process. Fetch all registers if
367 regno == -1, otherwise fetch all general registers or all floating
368 point registers depending upon the value of regno. */
371 arm_linux_fetch_inferior_registers (int regno
)
380 if (regno
< ARM_F0_REGNUM
|| regno
> ARM_FPS_REGNUM
)
381 fetch_register (regno
);
383 if (regno
>= ARM_F0_REGNUM
&& regno
<= ARM_FPS_REGNUM
)
384 fetch_fpregister (regno
);
388 /* Store registers back into the inferior. Store all registers if
389 regno == -1, otherwise store all general registers or all floating
390 point registers depending upon the value of regno. */
393 arm_linux_store_inferior_registers (int regno
)
402 if ((regno
< ARM_F0_REGNUM
) || (regno
> ARM_FPS_REGNUM
))
403 store_register (regno
);
405 if ((regno
>= ARM_F0_REGNUM
) && (regno
<= ARM_FPS_REGNUM
))
406 store_fpregister (regno
);
410 /* Wrapper functions for the standard regset handling, used by
414 fill_gregset (gdb_gregset_t
*gregsetp
, int regno
)
416 arm_linux_collect_gregset (NULL
, current_regcache
, regno
, gregsetp
, 0);
420 supply_gregset (gdb_gregset_t
*gregsetp
)
422 arm_linux_supply_gregset (NULL
, current_regcache
, -1, gregsetp
, 0);
426 fill_fpregset (gdb_fpregset_t
*fpregsetp
, int regno
)
428 arm_linux_collect_nwfpe (NULL
, current_regcache
, regno
, fpregsetp
, 0);
431 /* Fill GDB's register array with the floating-point register values
435 supply_fpregset (gdb_fpregset_t
*fpregsetp
)
437 arm_linux_supply_nwfpe (NULL
, current_regcache
, -1, fpregsetp
, 0);
441 arm_linux_kernel_u_size (void)
443 return (sizeof (struct user
));
446 /* Fetch the thread-local storage pointer for libthread_db. */
449 ps_get_thread_area (const struct ps_prochandle
*ph
,
450 lwpid_t lwpid
, int idx
, void **base
)
452 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
455 /* IDX is the bias from the thread pointer to the beginning of the
456 thread descriptor. It has to be subtracted due to implementation
457 quirks in libthread_db. */
458 *base
= (void *) ((char *)*base
- idx
);
464 get_linux_version (unsigned int *vmajor
,
465 unsigned int *vminor
,
466 unsigned int *vrelease
)
469 char *pmajor
, *pminor
, *prelease
, *tail
;
471 if (-1 == uname (&info
))
473 warning (_("Unable to determine GNU/Linux version."));
477 pmajor
= strtok (info
.release
, ".");
478 pminor
= strtok (NULL
, ".");
479 prelease
= strtok (NULL
, ".");
481 *vmajor
= (unsigned int) strtoul (pmajor
, &tail
, 0);
482 *vminor
= (unsigned int) strtoul (pminor
, &tail
, 0);
483 *vrelease
= (unsigned int) strtoul (prelease
, &tail
, 0);
485 return ((*vmajor
<< 16) | (*vminor
<< 8) | *vrelease
);
488 void _initialize_arm_linux_nat (void);
491 _initialize_arm_linux_nat (void)
493 struct target_ops
*t
;
495 os_version
= get_linux_version (&os_major
, &os_minor
, &os_release
);
497 /* Fill in the generic GNU/Linux methods. */
500 /* Add our register access methods. */
501 t
->to_fetch_registers
= arm_linux_fetch_inferior_registers
;
502 t
->to_store_registers
= arm_linux_store_inferior_registers
;
504 /* Register the target. */
505 linux_nat_add_target (t
);