]>
Commit | Line | Data |
---|---|---|
a80b95ba TG |
1 | /* Darwin support for GDB, the GNU debugger. |
2 | Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2008 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | Contributed by Apple Computer, Inc. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #include "defs.h" | |
23 | #include "frame.h" | |
24 | #include "inferior.h" | |
25 | #include "target.h" | |
26 | #include "symfile.h" | |
27 | #include "symtab.h" | |
28 | #include "objfiles.h" | |
29 | #include "gdbcmd.h" | |
30 | #include "regcache.h" | |
31 | #include "gdb_assert.h" | |
32 | #include "i386-tdep.h" | |
33 | #include "amd64-nat.h" | |
34 | #include "i387-tdep.h" | |
35 | #include "gdbarch.h" | |
36 | #include "arch-utils.h" | |
37 | ||
38 | #include "darwin-nat.h" | |
39 | #include "i386-darwin-tdep.h" | |
40 | ||
41 | /* Read register values from the inferior process. | |
42 | If REGNO is -1, do this for all registers. | |
43 | Otherwise, REGNO specifies which register (so we can save time). */ | |
44 | static void | |
45 | i386_darwin_fetch_inferior_registers (struct regcache *regcache, int regno) | |
46 | { | |
47 | thread_t current_thread = ptid_get_tid (inferior_ptid); | |
48 | int fetched = 0; | |
49 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
50 | ||
51 | if (gdbarch_ptr_bit (gdbarch) == 64) | |
52 | { | |
53 | if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno)) | |
54 | { | |
55 | x86_thread_state_t gp_regs; | |
56 | unsigned int gp_count = x86_THREAD_STATE_COUNT; | |
57 | kern_return_t ret; | |
58 | ||
59 | ret = thread_get_state | |
60 | (current_thread, x86_THREAD_STATE, (thread_state_t) & gp_regs, | |
61 | &gp_count); | |
62 | if (ret != KERN_SUCCESS) | |
63 | { | |
64 | printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread); | |
65 | MACH_CHECK_ERROR (ret); | |
66 | } | |
67 | amd64_supply_native_gregset (regcache, &gp_regs.uts, -1); | |
68 | fetched++; | |
69 | } | |
70 | ||
71 | if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno)) | |
72 | { | |
73 | x86_float_state_t fp_regs; | |
74 | unsigned int fp_count = x86_FLOAT_STATE_COUNT; | |
75 | kern_return_t ret; | |
76 | ||
77 | ret = thread_get_state | |
78 | (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs, | |
79 | &fp_count); | |
80 | if (ret != KERN_SUCCESS) | |
81 | { | |
82 | printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread); | |
83 | MACH_CHECK_ERROR (ret); | |
84 | } | |
85 | i387_supply_fxsave (regcache, -1, &fp_regs.ufs.fs64); | |
86 | fetched++; | |
87 | } | |
88 | } | |
89 | else | |
90 | { | |
91 | if (regno == -1 || regno < I386_NUM_GREGS) | |
92 | { | |
93 | i386_thread_state_t gp_regs; | |
94 | unsigned int gp_count = i386_THREAD_STATE_COUNT; | |
95 | kern_return_t ret; | |
96 | int i; | |
97 | ||
98 | ret = thread_get_state | |
99 | (current_thread, i386_THREAD_STATE, (thread_state_t) & gp_regs, | |
100 | &gp_count); | |
101 | if (ret != KERN_SUCCESS) | |
102 | { | |
103 | printf_unfiltered (_("Error calling thread_get_state for GP registers for thread 0x%ulx"), current_thread); | |
104 | MACH_CHECK_ERROR (ret); | |
105 | } | |
106 | for (i = 0; i < I386_NUM_GREGS; i++) | |
107 | regcache_raw_supply | |
108 | (regcache, i, | |
109 | (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]); | |
110 | ||
111 | fetched++; | |
112 | } | |
113 | ||
114 | if (regno == -1 | |
115 | || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS)) | |
116 | { | |
117 | i386_float_state_t fp_regs; | |
118 | unsigned int fp_count = i386_FLOAT_STATE_COUNT; | |
119 | kern_return_t ret; | |
120 | ||
121 | ret = thread_get_state | |
122 | (current_thread, i386_FLOAT_STATE, (thread_state_t) & fp_regs, | |
123 | &fp_count); | |
124 | if (ret != KERN_SUCCESS) | |
125 | { | |
126 | printf_unfiltered (_("Error calling thread_get_state for float registers for thread 0x%ulx"), current_thread); | |
127 | MACH_CHECK_ERROR (ret); | |
128 | } | |
129 | i387_supply_fxsave (regcache, -1, &fp_regs.__fpu_fcw); | |
130 | fetched++; | |
131 | } | |
132 | } | |
133 | ||
134 | if (! fetched) | |
135 | { | |
136 | warning (_("unknown register %d"), regno); | |
137 | regcache_raw_supply (regcache, regno, NULL); | |
138 | } | |
139 | } | |
140 | ||
141 | /* Store our register values back into the inferior. | |
142 | If REGNO is -1, do this for all registers. | |
143 | Otherwise, REGNO specifies which register (so we can save time). */ | |
144 | ||
145 | static void | |
146 | i386_darwin_store_inferior_registers (struct regcache *regcache, int regno) | |
147 | { | |
148 | thread_t current_thread = ptid_get_tid (inferior_ptid); | |
149 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
150 | ||
151 | if (gdbarch_ptr_bit (gdbarch) == 64) | |
152 | { | |
153 | if (regno == -1 || amd64_native_gregset_supplies_p (gdbarch, regno)) | |
154 | { | |
155 | x86_thread_state_t gp_regs; | |
156 | kern_return_t ret; | |
157 | unsigned int gp_count = x86_THREAD_STATE_COUNT; | |
158 | ||
159 | ret = thread_get_state | |
160 | (current_thread, x86_THREAD_STATE, (thread_state_t) &gp_regs, | |
161 | &gp_count); | |
162 | MACH_CHECK_ERROR (ret); | |
163 | gdb_assert (gp_regs.tsh.flavor == x86_THREAD_STATE64); | |
164 | gdb_assert (gp_regs.tsh.count == x86_THREAD_STATE64_COUNT); | |
165 | ||
166 | amd64_collect_native_gregset (regcache, &gp_regs.uts, regno); | |
167 | ||
168 | ret = thread_set_state (current_thread, x86_THREAD_STATE, | |
169 | (thread_state_t) &gp_regs, | |
170 | x86_THREAD_STATE_COUNT); | |
171 | MACH_CHECK_ERROR (ret); | |
172 | } | |
173 | ||
174 | if (regno == -1 || !amd64_native_gregset_supplies_p (gdbarch, regno)) | |
175 | { | |
176 | x86_float_state_t fp_regs; | |
177 | kern_return_t ret; | |
178 | unsigned int fp_count = x86_FLOAT_STATE_COUNT; | |
179 | ||
180 | ret = thread_get_state | |
181 | (current_thread, x86_FLOAT_STATE, (thread_state_t) & fp_regs, | |
182 | &fp_count); | |
183 | MACH_CHECK_ERROR (ret); | |
184 | gdb_assert (fp_regs.fsh.flavor == x86_FLOAT_STATE64); | |
185 | gdb_assert (fp_regs.fsh.count == x86_FLOAT_STATE64_COUNT); | |
186 | ||
187 | i387_collect_fxsave (regcache, regno, &fp_regs.ufs.fs64.__fpu_fcw); | |
188 | ||
189 | ret = thread_set_state (current_thread, x86_FLOAT_STATE, | |
190 | (thread_state_t) & fp_regs, | |
191 | x86_FLOAT_STATE_COUNT); | |
192 | MACH_CHECK_ERROR (ret); | |
193 | } | |
194 | } | |
195 | else | |
196 | { | |
197 | if (regno == -1 || regno < I386_NUM_GREGS) | |
198 | { | |
199 | i386_thread_state_t gp_regs; | |
200 | kern_return_t ret; | |
201 | unsigned int gp_count = i386_THREAD_STATE_COUNT; | |
202 | int i; | |
203 | ||
204 | ret = thread_get_state | |
205 | (current_thread, i386_THREAD_STATE, (thread_state_t) & gp_regs, | |
206 | &gp_count); | |
207 | MACH_CHECK_ERROR (ret); | |
208 | ||
209 | for (i = 0; i < I386_NUM_GREGS; i++) | |
210 | if (regno == -1 || regno == i) | |
211 | regcache_raw_collect | |
212 | (regcache, i, | |
213 | (char *)&gp_regs + i386_darwin_thread_state_reg_offset[i]); | |
214 | ||
215 | ret = thread_set_state (current_thread, i386_THREAD_STATE, | |
216 | (thread_state_t) & gp_regs, | |
217 | i386_THREAD_STATE_COUNT); | |
218 | MACH_CHECK_ERROR (ret); | |
219 | } | |
220 | ||
221 | if (regno == -1 | |
222 | || (regno >= I386_ST0_REGNUM && regno < I386_SSE_NUM_REGS)) | |
223 | { | |
224 | i386_float_state_t fp_regs; | |
225 | unsigned int fp_count = i386_FLOAT_STATE_COUNT; | |
226 | kern_return_t ret; | |
227 | ||
228 | ret = thread_get_state | |
229 | (current_thread, i386_FLOAT_STATE, (thread_state_t) & fp_regs, | |
230 | &fp_count); | |
231 | MACH_CHECK_ERROR (ret); | |
232 | ||
233 | i387_collect_fxsave (regcache, regno, &fp_regs.__fpu_fcw); | |
234 | ||
235 | ret = thread_set_state (current_thread, i386_FLOAT_STATE, | |
236 | (thread_state_t) & fp_regs, | |
237 | i386_FLOAT_STATE_COUNT); | |
238 | MACH_CHECK_ERROR (ret); | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
243 | ||
244 | /* Support for debug registers, boosted mostly from i386-linux-nat.c. */ | |
245 | ||
246 | #ifndef DR_FIRSTADDR | |
247 | #define DR_FIRSTADDR 0 | |
248 | #endif | |
249 | ||
250 | #ifndef DR_LASTADDR | |
251 | #define DR_LASTADDR 3 | |
252 | #endif | |
253 | ||
254 | #ifndef DR_STATUS | |
255 | #define DR_STATUS 6 | |
256 | #endif | |
257 | ||
258 | #ifndef DR_CONTROL | |
259 | #define DR_CONTROL 7 | |
260 | #endif | |
261 | ||
262 | ||
263 | static void | |
264 | i386_darwin_dr_set (int regnum, uint32_t value) | |
265 | { | |
266 | int current_pid; | |
267 | thread_t current_thread; | |
268 | x86_debug_state_t dr_regs; | |
269 | kern_return_t ret; | |
270 | unsigned int dr_count = x86_DEBUG_STATE_COUNT; | |
271 | ||
272 | gdb_assert (regnum >= 0 && regnum <= DR_CONTROL); | |
273 | ||
274 | current_thread = ptid_get_tid (inferior_ptid); | |
275 | ||
276 | dr_regs.dsh.flavor = x86_DEBUG_STATE32; | |
277 | dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT; | |
278 | dr_count = x86_DEBUG_STATE_COUNT; | |
279 | ret = thread_get_state (current_thread, x86_DEBUG_STATE, | |
280 | (thread_state_t) &dr_regs, &dr_count); | |
281 | ||
282 | if (ret != KERN_SUCCESS) | |
283 | { | |
284 | printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread); | |
285 | MACH_CHECK_ERROR (ret); | |
286 | } | |
287 | ||
288 | switch (regnum) | |
289 | { | |
290 | case 0: | |
291 | dr_regs.uds.ds32.__dr0 = value; | |
292 | break; | |
293 | case 1: | |
294 | dr_regs.uds.ds32.__dr1 = value; | |
295 | break; | |
296 | case 2: | |
297 | dr_regs.uds.ds32.__dr2 = value; | |
298 | break; | |
299 | case 3: | |
300 | dr_regs.uds.ds32.__dr3 = value; | |
301 | break; | |
302 | case 4: | |
303 | dr_regs.uds.ds32.__dr4 = value; | |
304 | break; | |
305 | case 5: | |
306 | dr_regs.uds.ds32.__dr5 = value; | |
307 | break; | |
308 | case 6: | |
309 | dr_regs.uds.ds32.__dr6 = value; | |
310 | break; | |
311 | case 7: | |
312 | dr_regs.uds.ds32.__dr7 = value; | |
313 | break; | |
314 | } | |
315 | ||
316 | ret = thread_set_state (current_thread, x86_DEBUG_STATE, | |
317 | (thread_state_t) &dr_regs, dr_count); | |
318 | ||
319 | if (ret != KERN_SUCCESS) | |
320 | { | |
321 | printf_unfiltered (_("Error writing debug registers thread 0x%x via thread_get_state\n"), (int) current_thread); | |
322 | MACH_CHECK_ERROR (ret); | |
323 | } | |
324 | } | |
325 | ||
326 | static uint32_t | |
327 | i386_darwin_dr_get (int regnum) | |
328 | { | |
329 | thread_t current_thread; | |
330 | x86_debug_state_t dr_regs; | |
331 | kern_return_t ret; | |
332 | unsigned int dr_count = x86_DEBUG_STATE_COUNT; | |
333 | ||
334 | gdb_assert (regnum >= 0 && regnum <= DR_CONTROL); | |
335 | ||
336 | current_thread = ptid_get_tid (inferior_ptid); | |
337 | ||
338 | dr_regs.dsh.flavor = x86_DEBUG_STATE32; | |
339 | dr_regs.dsh.count = x86_DEBUG_STATE32_COUNT; | |
340 | dr_count = x86_DEBUG_STATE_COUNT; | |
341 | ret = thread_get_state (current_thread, x86_DEBUG_STATE, | |
342 | (thread_state_t) &dr_regs, &dr_count); | |
343 | ||
344 | if (ret != KERN_SUCCESS) | |
345 | { | |
346 | printf_unfiltered (_("Error reading debug registers thread 0x%x via thread_get_state\n"), (int) current_thread); | |
347 | MACH_CHECK_ERROR (ret); | |
348 | } | |
349 | ||
350 | switch (regnum) | |
351 | { | |
352 | case 0: | |
353 | return dr_regs.uds.ds32.__dr0; | |
354 | case 1: | |
355 | return dr_regs.uds.ds32.__dr1; | |
356 | case 2: | |
357 | return dr_regs.uds.ds32.__dr2; | |
358 | case 3: | |
359 | return dr_regs.uds.ds32.__dr3; | |
360 | case 4: | |
361 | return dr_regs.uds.ds32.__dr4; | |
362 | case 5: | |
363 | return dr_regs.uds.ds32.__dr5; | |
364 | case 6: | |
365 | return dr_regs.uds.ds32.__dr6; | |
366 | case 7: | |
367 | return dr_regs.uds.ds32.__dr7; | |
368 | default: | |
369 | return -1; | |
370 | } | |
371 | } | |
372 | ||
373 | void | |
374 | i386_darwin_dr_set_control (unsigned long control) | |
375 | { | |
376 | i386_darwin_dr_set (DR_CONTROL, control); | |
377 | } | |
378 | ||
379 | void | |
380 | i386_darwin_dr_set_addr (int regnum, CORE_ADDR addr) | |
381 | { | |
382 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); | |
383 | ||
384 | i386_darwin_dr_set (DR_FIRSTADDR + regnum, addr); | |
385 | } | |
386 | ||
387 | void | |
388 | i386_darwin_dr_reset_addr (int regnum) | |
389 | { | |
390 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); | |
391 | ||
392 | i386_darwin_dr_set (DR_FIRSTADDR + regnum, 0L); | |
393 | } | |
394 | ||
395 | unsigned long | |
396 | i386_darwin_dr_get_status (void) | |
397 | { | |
398 | return i386_darwin_dr_get (DR_STATUS); | |
399 | } | |
400 | ||
401 | void | |
402 | darwin_check_osabi (darwin_inferior *inf, thread_t thread) | |
403 | { | |
404 | if (gdbarch_osabi (current_gdbarch) == GDB_OSABI_UNKNOWN) | |
405 | { | |
406 | /* Attaching to a process. Let's figure out what kind it is. */ | |
407 | x86_thread_state_t gp_regs; | |
408 | struct gdbarch_info info; | |
409 | unsigned int gp_count = x86_THREAD_STATE_COUNT; | |
410 | kern_return_t ret; | |
411 | ||
412 | ret = thread_get_state (thread, x86_THREAD_STATE, | |
413 | (thread_state_t) &gp_regs, &gp_count); | |
414 | if (ret != KERN_SUCCESS) | |
415 | { | |
416 | MACH_CHECK_ERROR (ret); | |
417 | return; | |
418 | } | |
419 | ||
420 | gdbarch_info_init (&info); | |
421 | gdbarch_info_fill (&info); | |
422 | info.byte_order = gdbarch_byte_order (current_gdbarch); | |
423 | info.osabi = GDB_OSABI_DARWIN; | |
424 | if (gp_regs.tsh.flavor == x86_THREAD_STATE64) | |
425 | info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386, | |
426 | bfd_mach_x86_64); | |
427 | else | |
428 | info.bfd_arch_info = bfd_lookup_arch (bfd_arch_i386, | |
429 | bfd_mach_i386_i386); | |
430 | gdbarch_update_p (info); | |
431 | } | |
432 | } | |
433 | ||
434 | #define X86_EFLAGS_T 0x100UL | |
435 | ||
436 | void | |
437 | darwin_set_sstep (thread_t thread, int enable) | |
438 | { | |
439 | x86_thread_state_t regs; | |
440 | unsigned int count = x86_THREAD_STATE_COUNT; | |
441 | kern_return_t kret; | |
442 | ||
443 | kret = thread_get_state (thread, x86_THREAD_STATE, | |
444 | (thread_state_t) ®s, &count); | |
445 | if (kret != KERN_SUCCESS) | |
446 | { | |
447 | printf_unfiltered (_("darwin_set_sstep: error %x, thread=%x\n"), | |
448 | kret, thread); | |
449 | return; | |
450 | } | |
451 | switch (regs.tsh.flavor) | |
452 | { | |
453 | case x86_THREAD_STATE32: | |
454 | { | |
455 | __uint32_t bit = enable ? X86_EFLAGS_T : 0; | |
456 | ||
457 | if ((regs.uts.ts32.__eflags & X86_EFLAGS_T) == bit) | |
458 | return; | |
459 | regs.uts.ts32.__eflags = (regs.uts.ts32.__eflags & ~X86_EFLAGS_T) | bit; | |
460 | kret = thread_set_state (thread, x86_THREAD_STATE, | |
461 | (thread_state_t) ®s, count); | |
462 | MACH_CHECK_ERROR (kret); | |
463 | } | |
464 | break; | |
465 | case x86_THREAD_STATE64: | |
466 | { | |
467 | __uint64_t bit = enable ? X86_EFLAGS_T : 0; | |
468 | ||
469 | if ((regs.uts.ts64.__rflags & X86_EFLAGS_T) == bit) | |
470 | return; | |
471 | regs.uts.ts64.__rflags = (regs.uts.ts64.__rflags & ~X86_EFLAGS_T) | bit; | |
472 | kret = thread_set_state (thread, x86_THREAD_STATE, | |
473 | (thread_state_t) ®s, count); | |
474 | MACH_CHECK_ERROR (kret); | |
475 | } | |
476 | break; | |
477 | default: | |
478 | error (_("darwin_set_sstep: unknown flavour: %d\n"), regs.tsh.flavor); | |
479 | } | |
480 | } | |
481 | ||
482 | void | |
483 | darwin_complete_target (struct target_ops *target) | |
484 | { | |
485 | amd64_native_gregset64_reg_offset = amd64_darwin_thread_state_reg_offset; | |
486 | amd64_native_gregset64_num_regs = amd64_darwin_thread_state_num_regs; | |
487 | amd64_native_gregset32_reg_offset = i386_darwin_thread_state_reg_offset; | |
488 | amd64_native_gregset32_num_regs = i386_darwin_thread_state_num_regs; | |
489 | ||
490 | target->to_fetch_registers = i386_darwin_fetch_inferior_registers; | |
491 | target->to_store_registers = i386_darwin_store_inferior_registers; | |
492 | } |