Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Interface GDB to Mach 3.0 operating systems. |
2 | (Most) Mach 3.0 related routines live in this file. | |
3 | ||
d9fcf2fb | 4 | Copyright (C) 1992, 1996, 1999-2000 Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | /* | |
24 | * Author: Jukka Virtanen <jtv@hut.fi> | |
c5aa993b | 25 | * Computing Centre |
c906108c SS |
26 | * Helsinki University of Technology |
27 | * Finland | |
28 | * | |
29 | * Thanks to my friends who helped with ideas and testing: | |
30 | * | |
c5aa993b JM |
31 | * Johannes Helander, Antti Louko, Tero Mononen, |
32 | * jvh@cs.hut.fi alo@hut.fi tmo@cs.hut.fi | |
c906108c SS |
33 | * |
34 | * Tero Kivinen and Eamonn McManus | |
c5aa993b JM |
35 | * kivinen@cs.hut.fi emcmanus@gr.osf.org |
36 | * | |
c906108c SS |
37 | */ |
38 | ||
39 | #include <stdio.h> | |
40 | ||
41 | #include <mach.h> | |
42 | #include <servers/netname.h> | |
43 | #include <servers/machid.h> | |
44 | #include <mach/message.h> | |
45 | #include <mach/notify.h> | |
46 | #include <mach_error.h> | |
47 | #include <mach/exception.h> | |
48 | #include <mach/vm_attributes.h> | |
49 | ||
50 | #include "defs.h" | |
51 | #include "inferior.h" | |
52 | #include "symtab.h" | |
53 | #include "value.h" | |
54 | #include "language.h" | |
55 | #include "target.h" | |
03f2053f | 56 | #include "gdb_wait.h" |
c906108c SS |
57 | #include "gdbcmd.h" |
58 | #include "gdbcore.h" | |
59 | ||
60 | #if 0 | |
61 | #include <servers/machid_lib.h> | |
62 | #else | |
63 | #define MACH_TYPE_TASK 1 | |
64 | #define MACH_TYPE_THREAD 2 | |
65 | #endif | |
66 | ||
67 | /* Included only for signal names and NSIG | |
c5aa993b | 68 | |
c906108c SS |
69 | * note: There are many problems in signal handling with |
70 | * gdb in Mach 3.0 in general. | |
71 | */ | |
72 | #include <signal.h> | |
c5aa993b | 73 | #define SIG_UNKNOWN 0 /* Exception that has no matching unix signal */ |
c906108c SS |
74 | |
75 | #include <cthreads.h> | |
76 | ||
77 | /* This is what a cproc looks like. This is here partly because | |
78 | cthread_internals.h is not a header we can just #include, partly with | |
79 | an eye towards perhaps getting this to work with cross-debugging | |
80 | someday. Best solution is if CMU publishes a real interface to this | |
81 | stuff. */ | |
82 | #define CPROC_NEXT_OFFSET 0 | |
83 | #define CPROC_NEXT_SIZE (TARGET_PTR_BIT / HOST_CHAR_BIT) | |
84 | #define CPROC_INCARNATION_OFFSET (CPROC_NEXT_OFFSET + CPROC_NEXT_SIZE) | |
85 | #define CPROC_INCARNATION_SIZE (sizeof (cthread_t)) | |
86 | #define CPROC_LIST_OFFSET (CPROC_INCARNATION_OFFSET + CPROC_INCARNATION_SIZE) | |
87 | #define CPROC_LIST_SIZE (TARGET_PTR_BIT / HOST_CHAR_BIT) | |
88 | #define CPROC_WAIT_OFFSET (CPROC_LIST_OFFSET + CPROC_LIST_SIZE) | |
89 | #define CPROC_WAIT_SIZE (TARGET_PTR_BIT / HOST_CHAR_BIT) | |
90 | #define CPROC_REPLY_OFFSET (CPROC_WAIT_OFFSET + CPROC_WAIT_SIZE) | |
91 | #define CPROC_REPLY_SIZE (sizeof (mach_port_t)) | |
92 | #define CPROC_CONTEXT_OFFSET (CPROC_REPLY_OFFSET + CPROC_REPLY_SIZE) | |
93 | #define CPROC_CONTEXT_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT) | |
94 | #define CPROC_LOCK_OFFSET (CPROC_CONTEXT_OFFSET + CPROC_CONTEXT_SIZE) | |
95 | #define CPROC_LOCK_SIZE (sizeof (spin_lock_t)) | |
96 | #define CPROC_STATE_OFFSET (CPROC_LOCK_OFFSET + CPROC_LOCK_SIZE) | |
97 | #define CPROC_STATE_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT) | |
98 | #define CPROC_WIRED_OFFSET (CPROC_STATE_OFFSET + CPROC_STATE_SIZE) | |
99 | #define CPROC_WIRED_SIZE (sizeof (mach_port_t)) | |
100 | #define CPROC_BUSY_OFFSET (CPROC_WIRED_OFFSET + CPROC_WIRED_SIZE) | |
101 | #define CPROC_BUSY_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT) | |
102 | #define CPROC_MSG_OFFSET (CPROC_BUSY_OFFSET + CPROC_BUSY_SIZE) | |
103 | #define CPROC_MSG_SIZE (sizeof (mach_msg_header_t)) | |
104 | #define CPROC_BASE_OFFSET (CPROC_MSG_OFFSET + CPROC_MSG_SIZE) | |
105 | #define CPROC_BASE_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT) | |
106 | #define CPROC_SIZE_OFFSET (CPROC_BASE_OFFSET + CPROC_BASE_SIZE) | |
107 | #define CPROC_SIZE_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT) | |
108 | #define CPROC_SIZE (CPROC_SIZE_OFFSET + CPROC_SIZE_SIZE) | |
109 | ||
110 | /* Values for the state field in the cproc. */ | |
111 | #define CPROC_RUNNING 0 | |
112 | #define CPROC_SWITCHING 1 | |
113 | #define CPROC_BLOCKED 2 | |
114 | #define CPROC_CONDWAIT 4 | |
115 | ||
116 | /* For cproc and kernel thread mapping */ | |
c5aa993b JM |
117 | typedef struct gdb_thread |
118 | { | |
119 | mach_port_t name; | |
120 | CORE_ADDR sp; | |
121 | CORE_ADDR pc; | |
122 | CORE_ADDR fp; | |
123 | boolean_t in_emulator; | |
124 | int slotid; | |
125 | ||
126 | /* This is for the mthreads list. It points to the cproc list. | |
127 | Perhaps the two lists should be merged (or perhaps it was a mistake | |
128 | to make them both use a struct gdb_thread). */ | |
129 | struct gdb_thread *cproc; | |
130 | ||
131 | /* These are for the cproc list, which is linked through the next field | |
132 | of the struct gdb_thread. */ | |
133 | char raw_cproc[CPROC_SIZE]; | |
134 | /* The cthread which is pointed to by the incarnation field from the | |
135 | cproc. This points to the copy we've read into GDB. */ | |
136 | cthread_t cthread; | |
137 | /* Point back to the mthreads list. */ | |
138 | int reverse_map; | |
139 | struct gdb_thread *next; | |
140 | } | |
141 | *gdb_thread_t; | |
c906108c SS |
142 | |
143 | /* | |
144 | * Actions for Mach exceptions. | |
145 | * | |
146 | * sigmap field maps the exception to corresponding Unix signal. | |
147 | * | |
148 | * I do not know how to map the exception to unix signal | |
149 | * if SIG_UNKNOWN is specified. | |
150 | */ | |
151 | ||
c5aa993b JM |
152 | struct exception_list |
153 | { | |
154 | char *name; | |
155 | boolean_t forward; | |
156 | boolean_t print; | |
157 | int sigmap; | |
158 | } | |
159 | exception_map[] = | |
160 | { | |
161 | { | |
162 | "not_mach3_exception", FALSE, TRUE, SIG_UNKNOWN | |
163 | } | |
164 | , | |
165 | { | |
166 | "EXC_BAD_ACCESS", FALSE, TRUE, SIGSEGV | |
167 | } | |
168 | , | |
169 | { | |
170 | "EXC_BAD_INSTRUCTION", FALSE, TRUE, SIGILL | |
171 | } | |
172 | , | |
173 | { | |
174 | "EXC_ARITHMETIC", FALSE, TRUE, SIGFPE | |
175 | } | |
176 | , | |
177 | { | |
178 | "EXC_EMULATION", FALSE, TRUE, SIGEMT | |
179 | } | |
180 | , /* ??? */ | |
181 | { | |
182 | "EXC_SOFTWARE", FALSE, TRUE, SIG_UNKNOWN | |
183 | } | |
184 | , | |
185 | { | |
186 | "EXC_BREAKPOINT", FALSE, FALSE, SIGTRAP | |
187 | } | |
c906108c SS |
188 | }; |
189 | ||
190 | /* Mach exception table size */ | |
c5aa993b | 191 | int max_exception = sizeof (exception_map) / sizeof (struct exception_list) - 1; |
c906108c SS |
192 | |
193 | #define MAX_EXCEPTION max_exception | |
194 | ||
195 | WAITTYPE wait_status; | |
196 | ||
197 | /* If you define this, intercepted bsd server calls will be | |
198 | * dumped while waiting the inferior to EXEC the correct | |
199 | * program | |
200 | */ | |
c5aa993b | 201 | /* #define DUMP_SYSCALL /* debugging interceptor */ |
c906108c SS |
202 | |
203 | /* xx_debug() outputs messages if this is nonzero. | |
204 | * If > 1, DUMP_SYSCALL will dump message contents. | |
205 | */ | |
206 | int debug_level = 0; | |
207 | ||
208 | /* "Temporary" debug stuff */ | |
209 | void | |
fba45db2 | 210 | xx_debug (char *fmt, int a, int b, int c) |
c906108c SS |
211 | { |
212 | if (debug_level) | |
213 | warning (fmt, a, b, c); | |
214 | } | |
215 | ||
216 | /* This is in libmach.a */ | |
c5aa993b | 217 | extern mach_port_t name_server_port; |
c906108c SS |
218 | |
219 | /* Set in catch_exception_raise */ | |
220 | int stop_exception, stop_code, stop_subcode; | |
221 | int stopped_in_exception; | |
222 | ||
223 | /* Thread that was the active thread when we stopped */ | |
224 | thread_t stop_thread = MACH_PORT_NULL; | |
225 | ||
226 | char *hostname = ""; | |
227 | ||
228 | /* Set when task is attached or created */ | |
229 | boolean_t emulator_present = FALSE; | |
230 | ||
c5aa993b | 231 | task_t inferior_task; |
c906108c SS |
232 | thread_t current_thread; |
233 | ||
234 | /* Exception ports for inferior task */ | |
c5aa993b | 235 | mach_port_t inferior_exception_port = MACH_PORT_NULL; |
c906108c SS |
236 | mach_port_t inferior_old_exception_port = MACH_PORT_NULL; |
237 | ||
238 | /* task exceptions and notifications */ | |
c5aa993b JM |
239 | mach_port_t inferior_wait_port_set = MACH_PORT_NULL; |
240 | mach_port_t our_notify_port = MACH_PORT_NULL; | |
c906108c SS |
241 | |
242 | /* This is "inferior_wait_port_set" when not single stepping, and | |
243 | * "singlestepped_thread_port" when we are single stepping. | |
244 | * | |
245 | * This is protected by a cleanup function: discard_single_step() | |
246 | */ | |
c5aa993b | 247 | mach_port_t currently_waiting_for = MACH_PORT_NULL; |
c906108c SS |
248 | |
249 | /* A port for external messages to gdb. | |
250 | * External in the meaning that they do not come | |
251 | * from the inferior_task, but rather from external | |
252 | * tasks. | |
253 | * | |
254 | * As a debugging feature: | |
255 | * A debugger debugging another debugger can stop the | |
256 | * inferior debugger by the following command sequence | |
257 | * (without running external programs) | |
258 | * | |
259 | * (top-gdb) set stop_inferior_gdb () | |
260 | * (top-gdb) continue | |
261 | */ | |
c5aa993b | 262 | mach_port_t our_message_port = MACH_PORT_NULL; |
c906108c SS |
263 | |
264 | /* For single stepping */ | |
c5aa993b | 265 | mach_port_t thread_exception_port = MACH_PORT_NULL; |
c906108c | 266 | mach_port_t thread_saved_exception_port = MACH_PORT_NULL; |
c5aa993b | 267 | mach_port_t singlestepped_thread_port = MACH_PORT_NULL; |
c906108c SS |
268 | |
269 | /* For machid calls */ | |
270 | mach_port_t mid_server = MACH_PORT_NULL; | |
c5aa993b | 271 | mach_port_t mid_auth = MACH_PORT_NULL; |
c906108c SS |
272 | |
273 | /* If gdb thinks the inferior task is not suspended, it | |
274 | * must take suspend/abort the threads when it reads the state. | |
275 | */ | |
276 | int must_suspend_thread = 0; | |
277 | ||
278 | /* When single stepping, we switch the port that mach_really_wait() listens to. | |
279 | * This cleanup is a guard to prevent the port set from being left to | |
280 | * the singlestepped_thread_port when error() is called. | |
281 | * This is nonzero only when we are single stepping. | |
282 | */ | |
283 | #define NULL_CLEANUP (struct cleanup *)0 | |
284 | struct cleanup *cleanup_step = NULL_CLEANUP; | |
c906108c | 285 | \f |
c5aa993b | 286 | |
c906108c SS |
287 | static struct target_ops m3_ops; |
288 | ||
289 | static void m3_kill_inferior (); | |
290 | \f | |
291 | #if 0 | |
292 | #define MACH_TYPE_EXCEPTION_PORT -1 | |
293 | #endif | |
294 | ||
295 | /* Chain of ports to remember requested notifications. */ | |
296 | ||
c5aa993b JM |
297 | struct port_chain |
298 | { | |
299 | struct port_chain *next; | |
300 | mach_port_t port; | |
301 | int type; | |
302 | int mid; /* Now only valid with MACH_TYPE_THREAD and */ | |
303 | /* MACH_TYPE_THREAD */ | |
304 | }; | |
c906108c SS |
305 | typedef struct port_chain *port_chain_t; |
306 | ||
307 | /* Room for chain nodes comes from pchain_obstack */ | |
308 | struct obstack pchain_obstack; | |
309 | struct obstack *port_chain_obstack = &pchain_obstack; | |
310 | ||
311 | /* For thread handling */ | |
312 | struct obstack Cproc_obstack; | |
313 | struct obstack *cproc_obstack = &Cproc_obstack; | |
314 | ||
315 | /* the list of notified ports */ | |
316 | port_chain_t notify_chain = (port_chain_t) NULL; | |
317 | ||
318 | port_chain_t | |
fba45db2 | 319 | port_chain_insert (port_chain_t list, mach_port_t name, int type) |
c906108c SS |
320 | { |
321 | kern_return_t ret; | |
322 | port_chain_t new; | |
323 | int mid; | |
324 | ||
c5aa993b | 325 | if (!MACH_PORT_VALID (name)) |
c906108c | 326 | return list; |
c5aa993b | 327 | |
c906108c SS |
328 | if (type == MACH_TYPE_TASK || type == MACH_TYPE_THREAD) |
329 | { | |
c5aa993b | 330 | if (!MACH_PORT_VALID (mid_server)) |
c906108c SS |
331 | { |
332 | warning ("Machid server port invalid, can not map port 0x%x to MID", | |
333 | name); | |
334 | mid = name; | |
335 | } | |
336 | else | |
337 | { | |
338 | ret = machid_mach_register (mid_server, mid_auth, name, type, &mid); | |
c5aa993b | 339 | |
c906108c SS |
340 | if (ret != KERN_SUCCESS) |
341 | { | |
342 | warning ("Can not map name (0x%x) to MID with machid", name); | |
343 | mid = name; | |
344 | } | |
345 | } | |
346 | } | |
347 | else | |
348 | abort (); | |
349 | ||
350 | new = (port_chain_t) obstack_alloc (port_chain_obstack, | |
351 | sizeof (struct port_chain)); | |
c5aa993b JM |
352 | new->next = list; |
353 | new->port = name; | |
354 | new->type = type; | |
355 | new->mid = mid; | |
c906108c SS |
356 | |
357 | return new; | |
358 | } | |
359 | ||
360 | port_chain_t | |
fba45db2 | 361 | port_chain_delete (port_chain_t list, mach_port_t elem) |
c906108c SS |
362 | { |
363 | if (list) | |
364 | if (list->port == elem) | |
365 | list = list->next; | |
366 | else | |
367 | while (list->next) | |
368 | { | |
369 | if (list->next->port == elem) | |
c5aa993b | 370 | list->next = list->next->next; /* GCd with obstack_free() */ |
c906108c SS |
371 | else |
372 | list = list->next; | |
373 | } | |
374 | return list; | |
375 | } | |
376 | ||
377 | void | |
fba45db2 | 378 | port_chain_destroy (struct obstack *ostack) |
c906108c SS |
379 | { |
380 | obstack_free (ostack, 0); | |
381 | obstack_init (ostack); | |
382 | } | |
383 | ||
384 | port_chain_t | |
fba45db2 | 385 | port_chain_member (port_chain_t list, mach_port_t elem) |
c906108c SS |
386 | { |
387 | while (list) | |
388 | { | |
389 | if (list->port == elem) | |
390 | return list; | |
391 | list = list->next; | |
392 | } | |
393 | return (port_chain_t) NULL; | |
394 | } | |
395 | \f | |
396 | int | |
fba45db2 | 397 | map_port_name_to_mid (mach_port_t name, int type) |
c906108c SS |
398 | { |
399 | port_chain_t elem; | |
400 | ||
401 | if (!MACH_PORT_VALID (name)) | |
402 | return -1; | |
403 | ||
404 | elem = port_chain_member (notify_chain, name); | |
405 | ||
406 | if (elem && (elem->type == type)) | |
407 | return elem->mid; | |
c5aa993b | 408 | |
c906108c SS |
409 | if (elem) |
410 | return -1; | |
c5aa993b JM |
411 | |
412 | if (!MACH_PORT_VALID (mid_server)) | |
c906108c SS |
413 | { |
414 | warning ("Machid server port invalid, can not map port 0x%x to mid", | |
415 | name); | |
416 | return -1; | |
417 | } | |
418 | else | |
419 | { | |
420 | int mid; | |
421 | kern_return_t ret; | |
422 | ||
423 | ret = machid_mach_register (mid_server, mid_auth, name, type, &mid); | |
c5aa993b | 424 | |
c906108c SS |
425 | if (ret != KERN_SUCCESS) |
426 | { | |
427 | warning ("Can not map name (0x%x) to mid with machid", name); | |
428 | return -1; | |
429 | } | |
430 | return mid; | |
431 | } | |
432 | } | |
433 | \f | |
434 | /* Guard for currently_waiting_for and singlestepped_thread_port */ | |
435 | static void | |
fba45db2 | 436 | discard_single_step (thread_t thread) |
c906108c SS |
437 | { |
438 | currently_waiting_for = inferior_wait_port_set; | |
439 | ||
440 | cleanup_step = NULL_CLEANUP; | |
441 | if (MACH_PORT_VALID (thread) && MACH_PORT_VALID (singlestepped_thread_port)) | |
442 | setup_single_step (thread, FALSE); | |
443 | } | |
444 | ||
fba45db2 | 445 | setup_single_step (thread_t thread, boolean_t start_step) |
c906108c SS |
446 | { |
447 | kern_return_t ret; | |
448 | ||
c5aa993b | 449 | if (!MACH_PORT_VALID (thread)) |
c906108c SS |
450 | error ("Invalid thread supplied to setup_single_step"); |
451 | else | |
452 | { | |
453 | mach_port_t teport; | |
454 | ||
455 | /* Get the current thread exception port */ | |
456 | ret = thread_get_exception_port (thread, &teport); | |
457 | CHK ("Getting thread's exception port", ret); | |
c5aa993b | 458 | |
c906108c SS |
459 | if (start_step) |
460 | { | |
461 | if (MACH_PORT_VALID (singlestepped_thread_port)) | |
462 | { | |
463 | warning ("Singlestepped_thread_port (0x%x) is still valid?", | |
464 | singlestepped_thread_port); | |
465 | singlestepped_thread_port = MACH_PORT_NULL; | |
466 | } | |
c5aa993b | 467 | |
c906108c SS |
468 | /* If we are already stepping this thread */ |
469 | if (MACH_PORT_VALID (teport) && teport == thread_exception_port) | |
470 | { | |
471 | ret = mach_port_deallocate (mach_task_self (), teport); | |
472 | CHK ("Could not deallocate thread exception port", ret); | |
473 | } | |
474 | else | |
475 | { | |
476 | ret = thread_set_exception_port (thread, thread_exception_port); | |
477 | CHK ("Setting exception port for thread", ret); | |
478 | #if 0 | |
479 | /* Insert thread exception port to wait port set */ | |
c5aa993b | 480 | ret = mach_port_move_member (mach_task_self (), |
c906108c SS |
481 | thread_exception_port, |
482 | inferior_wait_port_set); | |
483 | CHK ("Moving thread exception port to inferior_wait_port_set", | |
484 | ret); | |
485 | #endif | |
486 | thread_saved_exception_port = teport; | |
487 | } | |
c5aa993b | 488 | |
c906108c | 489 | thread_trace (thread, TRUE); |
c5aa993b JM |
490 | |
491 | singlestepped_thread_port = thread_exception_port; | |
492 | currently_waiting_for = singlestepped_thread_port; | |
c906108c SS |
493 | cleanup_step = make_cleanup (discard_single_step, thread); |
494 | } | |
495 | else | |
496 | { | |
c5aa993b | 497 | if (!MACH_PORT_VALID (teport)) |
c906108c SS |
498 | error ("Single stepped thread had an invalid exception port?"); |
499 | ||
500 | if (teport != thread_exception_port) | |
501 | error ("Single stepped thread had an unknown exception port?"); | |
c5aa993b | 502 | |
c906108c SS |
503 | ret = mach_port_deallocate (mach_task_self (), teport); |
504 | CHK ("Couldn't deallocate thread exception port", ret); | |
505 | #if 0 | |
506 | /* Remove thread exception port from wait port set */ | |
c5aa993b | 507 | ret = mach_port_move_member (mach_task_self (), |
c906108c SS |
508 | thread_exception_port, |
509 | MACH_PORT_NULL); | |
510 | CHK ("Removing thread exception port from inferior_wait_port_set", | |
511 | ret); | |
c5aa993b | 512 | #endif |
c906108c SS |
513 | /* Restore thread's old exception port */ |
514 | ret = thread_set_exception_port (thread, | |
515 | thread_saved_exception_port); | |
516 | CHK ("Restoring stepped thread's exception port", ret); | |
c5aa993b | 517 | |
c906108c SS |
518 | if (MACH_PORT_VALID (thread_saved_exception_port)) |
519 | (void) mach_port_deallocate (mach_task_self (), | |
520 | thread_saved_exception_port); | |
c5aa993b | 521 | |
c906108c | 522 | thread_trace (thread, FALSE); |
c5aa993b | 523 | |
c906108c SS |
524 | singlestepped_thread_port = MACH_PORT_NULL; |
525 | currently_waiting_for = inferior_wait_port_set; | |
526 | if (cleanup_step) | |
527 | discard_cleanups (cleanup_step); | |
528 | } | |
529 | } | |
530 | } | |
531 | \f | |
532 | static | |
fba45db2 | 533 | request_notify (mach_port_t name, mach_msg_id_t variant, int type) |
c906108c SS |
534 | { |
535 | kern_return_t ret; | |
c5aa993b JM |
536 | mach_port_t previous_port_dummy = MACH_PORT_NULL; |
537 | ||
538 | if (!MACH_PORT_VALID (name)) | |
c906108c | 539 | return; |
c5aa993b | 540 | |
c906108c SS |
541 | if (port_chain_member (notify_chain, name)) |
542 | return; | |
543 | ||
c5aa993b | 544 | ret = mach_port_request_notification (mach_task_self (), |
c906108c SS |
545 | name, |
546 | variant, | |
547 | 1, | |
548 | our_notify_port, | |
549 | MACH_MSG_TYPE_MAKE_SEND_ONCE, | |
550 | &previous_port_dummy); | |
551 | CHK ("Serious: request_notify failed", ret); | |
552 | ||
553 | (void) mach_port_deallocate (mach_task_self (), | |
554 | previous_port_dummy); | |
555 | ||
556 | notify_chain = port_chain_insert (notify_chain, name, type); | |
557 | } | |
558 | ||
fba45db2 | 559 | reverse_msg_bits (mach_msg_header_t *msgp, int type) |
c906108c | 560 | { |
c5aa993b JM |
561 | int rbits, lbits; |
562 | rbits = MACH_MSGH_BITS_REMOTE (msgp->msgh_bits); | |
c906108c SS |
563 | lbits = type; |
564 | msgp->msgh_bits = | |
565 | (msgp->msgh_bits & ~MACH_MSGH_BITS_PORTS_MASK) | | |
c5aa993b | 566 | MACH_MSGH_BITS (lbits, rbits); |
c906108c SS |
567 | } |
568 | \f | |
569 | /* On the third day He said: | |
570 | ||
c5aa993b JM |
571 | Let this be global |
572 | and then it was global. | |
c906108c SS |
573 | |
574 | When creating the inferior fork, the | |
575 | child code in inflow.c sets the name of the | |
576 | bootstrap_port in its address space to this | |
577 | variable. | |
578 | ||
579 | The name is transferred to our address space | |
580 | with mach3_read_inferior(). | |
581 | ||
582 | Thou shalt not do this with | |
583 | task_get_bootstrap_port() in this task, since | |
584 | the name in the inferior task is different than | |
585 | the one we get. | |
586 | ||
587 | For blessed are the meek, as they shall inherit | |
588 | the address space. | |
589 | */ | |
590 | mach_port_t original_server_port_name = MACH_PORT_NULL; | |
591 | ||
592 | ||
593 | /* Called from inferior after FORK but before EXEC */ | |
594 | static void | |
fba45db2 | 595 | m3_trace_me (void) |
c906108c SS |
596 | { |
597 | kern_return_t ret; | |
c5aa993b | 598 | |
c906108c SS |
599 | /* Get the NAME of the bootstrap port in this task |
600 | so that GDB can read it */ | |
601 | ret = task_get_bootstrap_port (mach_task_self (), | |
602 | &original_server_port_name); | |
603 | if (ret != KERN_SUCCESS) | |
604 | abort (); | |
605 | ret = mach_port_deallocate (mach_task_self (), | |
606 | original_server_port_name); | |
607 | if (ret != KERN_SUCCESS) | |
608 | abort (); | |
c5aa993b | 609 | |
c906108c SS |
610 | /* Suspend this task to let the parent change my ports. |
611 | Resumed by the debugger */ | |
612 | ret = task_suspend (mach_task_self ()); | |
613 | if (ret != KERN_SUCCESS) | |
614 | abort (); | |
615 | } | |
616 | \f | |
617 | /* | |
618 | * Intercept system calls to Unix server. | |
619 | * After EXEC_COUNTER calls to exec(), return. | |
620 | * | |
621 | * Pre-assertion: Child is suspended. (Not verified) | |
622 | * Post-condition: Child is suspended after EXEC_COUNTER exec() calls. | |
623 | */ | |
624 | ||
625 | void | |
fba45db2 | 626 | intercept_exec_calls (int exec_counter) |
c906108c SS |
627 | { |
628 | int terminal_initted = 0; | |
629 | ||
c5aa993b JM |
630 | struct syscall_msg_t |
631 | { | |
632 | mach_msg_header_t header; | |
633 | mach_msg_type_t type; | |
634 | char room[2000]; /* Enuff space */ | |
635 | }; | |
c906108c SS |
636 | |
637 | struct syscall_msg_t syscall_in, syscall_out; | |
638 | ||
639 | mach_port_t fake_server; | |
640 | mach_port_t original_server_send; | |
641 | mach_port_t original_exec_reply; | |
642 | mach_port_t exec_reply; | |
643 | mach_port_t exec_reply_send; | |
644 | mach_msg_type_name_t acquired; | |
645 | mach_port_t emulator_server_port_name; | |
646 | struct task_basic_info info; | |
647 | mach_msg_type_number_t info_count; | |
648 | ||
649 | kern_return_t ret; | |
650 | ||
651 | if (exec_counter <= 0) | |
c5aa993b JM |
652 | return; /* We are already set up in the correct program */ |
653 | ||
654 | ret = mach_port_allocate (mach_task_self (), | |
655 | MACH_PORT_RIGHT_RECEIVE, | |
656 | &fake_server); | |
657 | CHK ("create inferior_fake_server port failed", ret); | |
c906108c | 658 | |
c906108c | 659 | /* Wait for inferior_task to suspend itself */ |
c5aa993b | 660 | while (1) |
c906108c SS |
661 | { |
662 | info_count = sizeof (info); | |
663 | ret = task_info (inferior_task, | |
664 | TASK_BASIC_INFO, | |
c5aa993b | 665 | (task_info_t) & info, |
c906108c SS |
666 | &info_count); |
667 | CHK ("Task info", ret); | |
668 | ||
669 | if (info.suspend_count) | |
670 | break; | |
671 | ||
672 | /* Note that the definition of the parameter was undefined | |
673 | * at the time of this writing, so I just use an `ad hoc' value. | |
674 | */ | |
c5aa993b | 675 | (void) swtch_pri (42); /* Universal Priority Value */ |
c906108c SS |
676 | } |
677 | ||
678 | /* Read the inferior's bootstrap port name */ | |
679 | if (!mach3_read_inferior (&original_server_port_name, | |
680 | &original_server_port_name, | |
681 | sizeof (original_server_port_name))) | |
682 | error ("Can't read inferior task bootstrap port name"); | |
683 | ||
684 | /* @@ BUG: If more than 1 send right GDB will FAIL!!! */ | |
685 | /* Should get refs, and set them back when restoring */ | |
686 | /* Steal the original bsd server send right from inferior */ | |
687 | ret = mach_port_extract_right (inferior_task, | |
688 | original_server_port_name, | |
689 | MACH_MSG_TYPE_MOVE_SEND, | |
690 | &original_server_send, | |
691 | &acquired); | |
c5aa993b JM |
692 | CHK ("mach_port_extract_right (bsd server send)", ret); |
693 | ||
c906108c | 694 | if (acquired != MACH_MSG_TYPE_PORT_SEND) |
c5aa993b | 695 | error ("Incorrect right extracted, send right to bsd server excpected"); |
c906108c SS |
696 | |
697 | ret = mach_port_insert_right (inferior_task, | |
698 | original_server_port_name, | |
699 | fake_server, | |
700 | MACH_MSG_TYPE_MAKE_SEND); | |
c5aa993b | 701 | CHK ("mach_port_insert_right (fake server send)", ret); |
c906108c SS |
702 | |
703 | xx_debug ("inferior task bsd server ports set up \nfs %x, ospn %x, oss %x\n", | |
704 | fake_server, | |
705 | original_server_port_name, original_server_send); | |
706 | ||
707 | /* A receive right to the reply generated by unix server exec() request */ | |
c5aa993b JM |
708 | ret = mach_port_allocate (mach_task_self (), |
709 | MACH_PORT_RIGHT_RECEIVE, | |
710 | &exec_reply); | |
711 | CHK ("create intercepted_reply_port port failed", ret); | |
712 | ||
c906108c SS |
713 | /* Pass this send right to Unix server so it replies to us after exec() */ |
714 | ret = mach_port_extract_right (mach_task_self (), | |
715 | exec_reply, | |
716 | MACH_MSG_TYPE_MAKE_SEND_ONCE, | |
717 | &exec_reply_send, | |
718 | &acquired); | |
c5aa993b | 719 | CHK ("mach_port_extract_right (exec_reply)", ret); |
c906108c SS |
720 | |
721 | if (acquired != MACH_MSG_TYPE_PORT_SEND_ONCE) | |
c5aa993b | 722 | error ("Incorrect right extracted, send once excpected for exec reply"); |
c906108c | 723 | |
c5aa993b JM |
724 | ret = mach_port_move_member (mach_task_self (), |
725 | fake_server, | |
726 | inferior_wait_port_set); | |
c906108c SS |
727 | CHK ("Moving fake syscall port to inferior_wait_port_set", ret); |
728 | ||
729 | xx_debug ("syscall fake server set up, resuming inferior\n"); | |
c5aa993b | 730 | |
c906108c | 731 | ret = task_resume (inferior_task); |
c5aa993b JM |
732 | CHK ("task_resume (startup)", ret); |
733 | ||
c906108c SS |
734 | /* Read requests from the inferior. |
735 | Pass directly through everything else except exec() calls. | |
736 | */ | |
c5aa993b | 737 | while (exec_counter > 0) |
c906108c SS |
738 | { |
739 | ret = mach_msg (&syscall_in.header, /* header */ | |
c5aa993b JM |
740 | MACH_RCV_MSG, /* options */ |
741 | 0, /* send size */ | |
742 | sizeof (struct syscall_msg_t), /* receive size */ | |
743 | inferior_wait_port_set, /* receive_name */ | |
c906108c SS |
744 | MACH_MSG_TIMEOUT_NONE, |
745 | MACH_PORT_NULL); | |
c5aa993b JM |
746 | CHK ("mach_msg (intercepted sycall)", ret); |
747 | ||
c906108c SS |
748 | #ifdef DUMP_SYSCALL |
749 | print_msg (&syscall_in.header); | |
750 | #endif | |
751 | ||
752 | /* ASSERT : msgh_local_port == fake_server */ | |
753 | ||
754 | if (notify_server (&syscall_in.header, &syscall_out.header)) | |
755 | error ("received a notify while intercepting syscalls"); | |
756 | ||
757 | if (syscall_in.header.msgh_id == MIG_EXEC_SYSCALL_ID) | |
758 | { | |
759 | xx_debug ("Received EXEC SYSCALL, counter = %d\n", exec_counter); | |
760 | if (exec_counter == 1) | |
761 | { | |
762 | original_exec_reply = syscall_in.header.msgh_remote_port; | |
763 | syscall_in.header.msgh_remote_port = exec_reply_send; | |
764 | } | |
765 | ||
766 | if (!terminal_initted) | |
767 | { | |
768 | /* Now that the child has exec'd we know it has already set its | |
c5aa993b JM |
769 | process group. On POSIX systems, tcsetpgrp will fail with |
770 | EPERM if we try it before the child's setpgid. */ | |
c906108c SS |
771 | |
772 | /* Set up the "saved terminal modes" of the inferior | |
c5aa993b | 773 | based on what modes we are starting it with. */ |
c906108c SS |
774 | target_terminal_init (); |
775 | ||
776 | /* Install inferior's terminal modes. */ | |
777 | target_terminal_inferior (); | |
778 | ||
779 | terminal_initted = 1; | |
780 | } | |
781 | ||
782 | exec_counter--; | |
783 | } | |
c5aa993b JM |
784 | |
785 | syscall_in.header.msgh_local_port = syscall_in.header.msgh_remote_port; | |
c906108c SS |
786 | syscall_in.header.msgh_remote_port = original_server_send; |
787 | ||
c5aa993b | 788 | reverse_msg_bits (&syscall_in.header, MACH_MSG_TYPE_COPY_SEND); |
c906108c SS |
789 | |
790 | ret = mach_msg_send (&syscall_in.header); | |
791 | CHK ("Forwarded syscall", ret); | |
792 | } | |
c5aa993b JM |
793 | |
794 | ret = mach_port_move_member (mach_task_self (), | |
795 | fake_server, | |
796 | MACH_PORT_NULL); | |
c906108c SS |
797 | CHK ("Moving fake syscall out of inferior_wait_port_set", ret); |
798 | ||
c5aa993b JM |
799 | ret = mach_port_move_member (mach_task_self (), |
800 | exec_reply, | |
801 | inferior_wait_port_set); | |
c906108c SS |
802 | CHK ("Moving exec_reply to inferior_wait_port_set", ret); |
803 | ||
804 | ret = mach_msg (&syscall_in.header, /* header */ | |
c5aa993b JM |
805 | MACH_RCV_MSG, /* options */ |
806 | 0, /* send size */ | |
c906108c | 807 | sizeof (struct syscall_msg_t), /* receive size */ |
c5aa993b | 808 | inferior_wait_port_set, /* receive_name */ |
c906108c SS |
809 | MACH_MSG_TIMEOUT_NONE, |
810 | MACH_PORT_NULL); | |
c5aa993b | 811 | CHK ("mach_msg (exec reply)", ret); |
c906108c SS |
812 | |
813 | ret = task_suspend (inferior_task); | |
814 | CHK ("Suspending inferior after last exec", ret); | |
815 | ||
816 | must_suspend_thread = 0; | |
817 | ||
818 | xx_debug ("Received exec reply from bsd server, suspended inferior task\n"); | |
819 | ||
820 | #ifdef DUMP_SYSCALL | |
c5aa993b | 821 | print_msg (&syscall_in.header); |
c906108c SS |
822 | #endif |
823 | ||
824 | /* Message should appear as if it came from the unix server */ | |
825 | syscall_in.header.msgh_local_port = MACH_PORT_NULL; | |
826 | ||
827 | /* and go to the inferior task original reply port */ | |
828 | syscall_in.header.msgh_remote_port = original_exec_reply; | |
829 | ||
c5aa993b | 830 | reverse_msg_bits (&syscall_in.header, MACH_MSG_TYPE_MOVE_SEND_ONCE); |
c906108c SS |
831 | |
832 | ret = mach_msg_send (&syscall_in.header); | |
833 | CHK ("Forwarding exec reply to inferior", ret); | |
834 | ||
835 | /* Garbage collect */ | |
836 | ret = mach_port_deallocate (inferior_task, | |
837 | original_server_port_name); | |
838 | CHK ("deallocating fake server send right", ret); | |
839 | ||
840 | ret = mach_port_insert_right (inferior_task, | |
841 | original_server_port_name, | |
842 | original_server_send, | |
843 | MACH_MSG_TYPE_MOVE_SEND); | |
844 | CHK ("Restoring the original bsd server send right", ret); | |
845 | ||
846 | ret = mach_port_destroy (mach_task_self (), | |
847 | fake_server); | |
848 | fake_server = MACH_PORT_DEAD; | |
c5aa993b | 849 | CHK ("mach_port_destroy (fake_server)", ret); |
c906108c SS |
850 | |
851 | ret = mach_port_destroy (mach_task_self (), | |
852 | exec_reply); | |
853 | exec_reply = MACH_PORT_DEAD; | |
c5aa993b | 854 | CHK ("mach_port_destroy (exec_reply)", ret); |
c906108c SS |
855 | |
856 | xx_debug ("Done with exec call interception\n"); | |
857 | } | |
858 | ||
859 | void | |
fba45db2 | 860 | consume_send_rights (thread_array_t thread_list, int thread_count) |
c906108c SS |
861 | { |
862 | int index; | |
863 | ||
864 | if (!thread_count) | |
865 | return; | |
866 | ||
867 | for (index = 0; index < thread_count; index++) | |
868 | { | |
869 | /* Since thread kill command kills threads, don't check ret */ | |
870 | (void) mach_port_deallocate (mach_task_self (), | |
c5aa993b | 871 | thread_list[index]); |
c906108c SS |
872 | } |
873 | } | |
874 | ||
875 | /* suspend/abort/resume a thread. */ | |
fba45db2 | 876 | setup_thread (mach_port_t thread, int what) |
c906108c SS |
877 | { |
878 | kern_return_t ret; | |
879 | ||
880 | if (what) | |
881 | { | |
882 | ret = thread_suspend (thread); | |
883 | CHK ("setup_thread thread_suspend", ret); | |
c5aa993b | 884 | |
c906108c SS |
885 | ret = thread_abort (thread); |
886 | CHK ("setup_thread thread_abort", ret); | |
887 | } | |
888 | else | |
889 | { | |
890 | ret = thread_resume (thread); | |
891 | CHK ("setup_thread thread_resume", ret); | |
892 | } | |
893 | } | |
894 | ||
895 | int | |
fba45db2 | 896 | map_slot_to_mid (int slot, thread_array_t threads, int thread_count) |
c906108c SS |
897 | { |
898 | kern_return_t ret; | |
899 | int deallocate = 0; | |
900 | int index; | |
901 | int mid; | |
902 | ||
c5aa993b | 903 | if (!threads) |
c906108c SS |
904 | { |
905 | deallocate++; | |
906 | ret = task_threads (inferior_task, &threads, &thread_count); | |
907 | CHK ("Can not select a thread from a dead task", ret); | |
908 | } | |
c5aa993b | 909 | |
c906108c SS |
910 | if (slot < 0 || slot >= thread_count) |
911 | { | |
912 | if (deallocate) | |
913 | { | |
914 | consume_send_rights (threads, thread_count); | |
c5aa993b JM |
915 | (void) vm_deallocate (mach_task_self (), (vm_address_t) threads, |
916 | (thread_count * sizeof (mach_port_t))); | |
c906108c SS |
917 | } |
918 | if (slot < 0) | |
919 | error ("invalid slot number"); | |
920 | else | |
c5aa993b | 921 | return -(slot + 1); |
c906108c SS |
922 | } |
923 | ||
c5aa993b | 924 | mid = map_port_name_to_mid (threads[slot], MACH_TYPE_THREAD); |
c906108c SS |
925 | |
926 | if (deallocate) | |
927 | { | |
928 | consume_send_rights (threads, thread_count); | |
c5aa993b JM |
929 | (void) vm_deallocate (mach_task_self (), (vm_address_t) threads, |
930 | (thread_count * sizeof (mach_port_t))); | |
c906108c SS |
931 | } |
932 | ||
933 | return mid; | |
934 | } | |
935 | ||
936 | static int | |
fba45db2 | 937 | parse_thread_id (char *arg, int thread_count, int slots) |
c906108c SS |
938 | { |
939 | kern_return_t ret; | |
940 | int mid; | |
941 | int slot; | |
942 | int index; | |
c5aa993b | 943 | |
c906108c SS |
944 | if (arg == 0) |
945 | return 0; | |
c5aa993b | 946 | |
c906108c SS |
947 | while (*arg && (*arg == ' ' || *arg == '\t')) |
948 | arg++; | |
c5aa993b JM |
949 | |
950 | if (!*arg) | |
c906108c | 951 | return 0; |
c5aa993b | 952 | |
c906108c SS |
953 | /* Currently parse MID and @SLOTNUMBER */ |
954 | if (*arg != '@') | |
955 | { | |
956 | mid = atoi (arg); | |
957 | if (mid <= 0) | |
958 | error ("valid thread mid expected"); | |
959 | return mid; | |
960 | } | |
c5aa993b | 961 | |
c906108c SS |
962 | arg++; |
963 | slot = atoi (arg); | |
964 | ||
965 | if (slot < 0) | |
966 | error ("invalid slot number"); | |
967 | ||
968 | /* If you want slot numbers to remain slot numbers, set slots. | |
c5aa993b | 969 | |
c906108c SS |
970 | * Well, since 0 is reserved, return the ordinal number |
971 | * of the thread rather than the slot number. Awk, this | |
972 | * counts as a kludge. | |
973 | */ | |
974 | if (slots) | |
c5aa993b | 975 | return -(slot + 1); |
c906108c SS |
976 | |
977 | if (thread_count && slot >= thread_count) | |
c5aa993b | 978 | return -(slot + 1); |
c906108c SS |
979 | |
980 | mid = map_slot_to_mid (slot); | |
c5aa993b | 981 | |
c906108c SS |
982 | return mid; |
983 | } | |
984 | ||
985 | /* THREAD_ID 0 is special; it selects the first kernel | |
986 | * thread from the list (i.e. SLOTNUMBER 0) | |
987 | * This is used when starting the program with 'run' or when attaching. | |
988 | * | |
989 | * If FLAG is 0 the context is not changed, and the registers, frame, etc | |
990 | * will continue to describe the old thread. | |
991 | * | |
992 | * If FLAG is nonzero, really select the thread. | |
993 | * If FLAG is 2, the THREAD_ID is a slotnumber instead of a mid. | |
994 | * | |
995 | */ | |
996 | kern_return_t | |
fba45db2 | 997 | select_thread (mach_port_t task, int thread_id, int flag) |
c906108c SS |
998 | { |
999 | thread_array_t thread_list; | |
1000 | int thread_count; | |
1001 | kern_return_t ret; | |
1002 | int index; | |
1003 | thread_t new_thread = MACH_PORT_NULL; | |
1004 | ||
1005 | if (thread_id < 0) | |
1006 | error ("Can't select cprocs without kernel thread"); | |
1007 | ||
1008 | ret = task_threads (task, &thread_list, &thread_count); | |
1009 | if (ret != KERN_SUCCESS) | |
1010 | { | |
1011 | warning ("Can not select a thread from a dead task"); | |
1012 | m3_kill_inferior (); | |
1013 | return KERN_FAILURE; | |
1014 | } | |
1015 | ||
1016 | if (thread_count == 0) | |
1017 | { | |
1018 | /* The task can not do anything anymore, but it still | |
1019 | * exists as a container for memory and ports. | |
1020 | */ | |
1021 | registers_changed (); | |
1022 | warning ("Task %d has no threads", | |
1023 | map_port_name_to_mid (task, MACH_TYPE_TASK)); | |
1024 | current_thread = MACH_PORT_NULL; | |
c5aa993b JM |
1025 | (void) vm_deallocate (mach_task_self (), |
1026 | (vm_address_t) thread_list, | |
1027 | (thread_count * sizeof (mach_port_t))); | |
c906108c SS |
1028 | return KERN_FAILURE; |
1029 | } | |
1030 | ||
c5aa993b | 1031 | if (!thread_id || flag == 2) |
c906108c SS |
1032 | { |
1033 | /* First thread or a slotnumber */ | |
c5aa993b | 1034 | if (!thread_id) |
c906108c SS |
1035 | new_thread = thread_list[0]; |
1036 | else | |
1037 | { | |
1038 | if (thread_id < thread_count) | |
c5aa993b | 1039 | new_thread = thread_list[thread_id]; |
c906108c SS |
1040 | else |
1041 | { | |
c5aa993b JM |
1042 | (void) vm_deallocate (mach_task_self (), |
1043 | (vm_address_t) thread_list, | |
1044 | (thread_count * sizeof (mach_port_t))); | |
c906108c SS |
1045 | error ("No such thread slot number : %d", thread_id); |
1046 | } | |
1047 | } | |
1048 | } | |
1049 | else | |
1050 | { | |
1051 | for (index = 0; index < thread_count; index++) | |
c5aa993b | 1052 | if (thread_id == map_port_name_to_mid (thread_list[index], |
c906108c SS |
1053 | MACH_TYPE_THREAD)) |
1054 | { | |
c5aa993b | 1055 | new_thread = thread_list[index]; |
c906108c SS |
1056 | index = -1; |
1057 | break; | |
1058 | } | |
c5aa993b | 1059 | |
c906108c SS |
1060 | if (index != -1) |
1061 | error ("No thread with mid %d", thread_id); | |
1062 | } | |
c5aa993b | 1063 | |
c906108c SS |
1064 | /* Notify when the selected thread dies */ |
1065 | request_notify (new_thread, MACH_NOTIFY_DEAD_NAME, MACH_TYPE_THREAD); | |
c5aa993b JM |
1066 | |
1067 | ret = vm_deallocate (mach_task_self (), | |
1068 | (vm_address_t) thread_list, | |
1069 | (thread_count * sizeof (mach_port_t))); | |
c906108c | 1070 | CHK ("vm_deallocate", ret); |
c5aa993b JM |
1071 | |
1072 | if (!flag) | |
c906108c SS |
1073 | current_thread = new_thread; |
1074 | else | |
1075 | { | |
1076 | #if 0 | |
1077 | if (MACH_PORT_VALID (current_thread)) | |
1078 | { | |
1079 | /* Store the gdb's view of the thread we are deselecting | |
c5aa993b | 1080 | |
c906108c SS |
1081 | * @@ I think gdb updates registers immediately when they are |
1082 | * changed, so don't do this. | |
1083 | */ | |
1084 | ret = thread_abort (current_thread); | |
1085 | CHK ("Could not abort system calls when saving state of old thread", | |
1086 | ret); | |
1087 | target_prepare_to_store (); | |
1088 | target_store_registers (-1); | |
1089 | } | |
1090 | #endif | |
1091 | ||
1092 | registers_changed (); | |
1093 | ||
1094 | current_thread = new_thread; | |
1095 | ||
1096 | ret = thread_abort (current_thread); | |
1097 | CHK ("Could not abort system calls when selecting a thread", ret); | |
1098 | ||
c5aa993b | 1099 | stop_pc = read_pc (); |
c906108c SS |
1100 | flush_cached_frames (); |
1101 | ||
1102 | select_frame (get_current_frame (), 0); | |
1103 | } | |
1104 | ||
1105 | return KERN_SUCCESS; | |
1106 | } | |
1107 | ||
1108 | /* | |
1109 | * Switch to use thread named NEW_THREAD. | |
1110 | * Return it's MID | |
1111 | */ | |
1112 | int | |
fba45db2 | 1113 | switch_to_thread (thread_t new_thread) |
c906108c SS |
1114 | { |
1115 | thread_t saved_thread = current_thread; | |
1116 | int mid; | |
1117 | ||
1118 | mid = map_port_name_to_mid (new_thread, | |
1119 | MACH_TYPE_THREAD); | |
1120 | if (mid == -1) | |
1121 | warning ("Can't map thread name 0x%x to mid", new_thread); | |
1122 | else if (select_thread (inferior_task, mid, 1) != KERN_SUCCESS) | |
1123 | { | |
1124 | if (current_thread) | |
1125 | current_thread = saved_thread; | |
1126 | error ("Could not select thread %d", mid); | |
1127 | } | |
c5aa993b | 1128 | |
c906108c SS |
1129 | return mid; |
1130 | } | |
1131 | ||
1132 | /* Do this in gdb after doing FORK but before STARTUP_INFERIOR. | |
1133 | * Note that the registers are not yet valid in the inferior task. | |
1134 | */ | |
1135 | static int | |
fba45db2 | 1136 | m3_trace_him (int pid) |
c906108c SS |
1137 | { |
1138 | kern_return_t ret; | |
1139 | ||
1140 | push_target (&m3_ops); | |
1141 | ||
1142 | inferior_task = task_by_pid (pid); | |
1143 | ||
c5aa993b | 1144 | if (!MACH_PORT_VALID (inferior_task)) |
c906108c SS |
1145 | error ("Can not map Unix pid %d to Mach task", pid); |
1146 | ||
1147 | /* Clean up previous notifications and create new ones */ | |
1148 | setup_notify_port (1); | |
1149 | ||
1150 | /* When notification appears, the inferior task has died */ | |
1151 | request_notify (inferior_task, MACH_NOTIFY_DEAD_NAME, MACH_TYPE_TASK); | |
1152 | ||
1153 | emulator_present = have_emulator_p (inferior_task); | |
1154 | ||
1155 | /* By default, select the first thread, | |
1156 | * If task has no threads, gives a warning | |
1157 | * Does not fetch registers, since they are not yet valid. | |
1158 | */ | |
1159 | select_thread (inferior_task, 0, 0); | |
1160 | ||
1161 | inferior_exception_port = MACH_PORT_NULL; | |
1162 | ||
1163 | setup_exception_port (); | |
1164 | ||
1165 | xx_debug ("Now the debugged task is created\n"); | |
1166 | ||
1167 | /* One trap to exec the shell, one to exec the program being debugged. */ | |
1168 | intercept_exec_calls (2); | |
1169 | ||
1170 | return pid; | |
1171 | } | |
1172 | ||
fba45db2 | 1173 | setup_exception_port (void) |
c906108c SS |
1174 | { |
1175 | kern_return_t ret; | |
1176 | ||
c5aa993b | 1177 | ret = mach_port_allocate (mach_task_self (), |
c906108c SS |
1178 | MACH_PORT_RIGHT_RECEIVE, |
1179 | &inferior_exception_port); | |
c5aa993b | 1180 | CHK ("mach_port_allocate", ret); |
c906108c SS |
1181 | |
1182 | /* add send right */ | |
1183 | ret = mach_port_insert_right (mach_task_self (), | |
1184 | inferior_exception_port, | |
1185 | inferior_exception_port, | |
1186 | MACH_MSG_TYPE_MAKE_SEND); | |
c5aa993b | 1187 | CHK ("mach_port_insert_right", ret); |
c906108c | 1188 | |
c5aa993b | 1189 | ret = mach_port_move_member (mach_task_self (), |
c906108c SS |
1190 | inferior_exception_port, |
1191 | inferior_wait_port_set); | |
c5aa993b | 1192 | CHK ("mach_port_move_member", ret); |
c906108c | 1193 | |
c5aa993b | 1194 | ret = task_get_special_port (inferior_task, |
c906108c SS |
1195 | TASK_EXCEPTION_PORT, |
1196 | &inferior_old_exception_port); | |
c5aa993b | 1197 | CHK ("task_get_special_port(old exc)", ret); |
c906108c SS |
1198 | |
1199 | ret = task_set_special_port (inferior_task, | |
c5aa993b | 1200 | TASK_EXCEPTION_PORT, |
c906108c | 1201 | inferior_exception_port); |
c5aa993b | 1202 | CHK ("task_set_special_port", ret); |
c906108c SS |
1203 | |
1204 | ret = mach_port_deallocate (mach_task_self (), | |
1205 | inferior_exception_port); | |
c5aa993b | 1206 | CHK ("mack_port_deallocate", ret); |
c906108c SS |
1207 | |
1208 | #if 0 | |
1209 | /* When notify appears, the inferior_task's exception | |
1210 | * port has been destroyed. | |
1211 | * | |
1212 | * Not used, since the dead_name_notification already | |
1213 | * appears when task dies. | |
1214 | * | |
1215 | */ | |
1216 | request_notify (inferior_exception_port, | |
1217 | MACH_NOTIFY_NO_SENDERS, | |
1218 | MACH_TYPE_EXCEPTION_PORT); | |
1219 | #endif | |
1220 | } | |
1221 | ||
1222 | /* Nonzero if gdb is waiting for a message */ | |
1223 | int mach_really_waiting; | |
1224 | ||
1225 | /* Wait for the inferior to stop for some reason. | |
1226 | - Loop on notifications until inferior_task dies. | |
1227 | - Loop on exceptions until stopped_in_exception comes true. | |
c5aa993b | 1228 | (e.g. we receive a single step trace trap) |
c906108c SS |
1229 | - a message arrives to gdb's message port |
1230 | ||
1231 | There is no other way to exit this loop. | |
1232 | ||
1233 | Returns the inferior_pid for rest of gdb. | |
1234 | Side effects: Set *OURSTATUS. */ | |
1235 | int | |
fba45db2 | 1236 | mach_really_wait (int pid, struct target_waitstatus *ourstatus) |
c906108c SS |
1237 | { |
1238 | kern_return_t ret; | |
1239 | int w; | |
1240 | ||
c5aa993b JM |
1241 | struct msg |
1242 | { | |
1243 | mach_msg_header_t header; | |
1244 | mach_msg_type_t foo; | |
1245 | int data[8000]; | |
1246 | } | |
1247 | in_msg, out_msg; | |
c906108c SS |
1248 | |
1249 | /* Either notify (death), exception or message can stop the inferior */ | |
1250 | stopped_in_exception = FALSE; | |
1251 | ||
1252 | while (1) | |
1253 | { | |
1254 | QUIT; | |
1255 | ||
1256 | stop_exception = stop_code = stop_subcode = -1; | |
1257 | stop_thread = MACH_PORT_NULL; | |
1258 | ||
1259 | mach_really_waiting = 1; | |
c5aa993b JM |
1260 | ret = mach_msg (&in_msg.header, /* header */ |
1261 | MACH_RCV_MSG, /* options */ | |
1262 | 0, /* send size */ | |
c906108c SS |
1263 | sizeof (struct msg), /* receive size */ |
1264 | currently_waiting_for, /* receive name */ | |
1265 | MACH_MSG_TIMEOUT_NONE, | |
1266 | MACH_PORT_NULL); | |
1267 | mach_really_waiting = 0; | |
c5aa993b | 1268 | CHK ("mach_msg (receive)", ret); |
c906108c SS |
1269 | |
1270 | /* Check if we received a notify of the childs' death */ | |
1271 | if (notify_server (&in_msg.header, &out_msg.header)) | |
1272 | { | |
1273 | /* If inferior_task is null then the inferior has | |
1274 | gone away and we want to return to command level. | |
1275 | Otherwise it was just an informative message and we | |
1276 | need to look to see if there are any more. */ | |
1277 | if (inferior_task != MACH_PORT_NULL) | |
1278 | continue; | |
1279 | else | |
1280 | { | |
1281 | /* Collect Unix exit status for gdb */ | |
1282 | ||
c5aa993b | 1283 | wait3 (&w, WNOHANG, 0); |
c906108c SS |
1284 | |
1285 | /* This mess is here to check that the rest of | |
1286 | * gdb knows that the inferior died. It also | |
1287 | * tries to hack around the fact that Mach 3.0 (mk69) | |
1288 | * unix server (ux28) does not always know what | |
1289 | * has happened to it's children when mach-magic | |
1290 | * is applied on them. | |
1291 | */ | |
c5aa993b JM |
1292 | if ((!WIFEXITED (w) && WIFSTOPPED (w)) || |
1293 | (WIFEXITED (w) && WEXITSTATUS (w) > 0377)) | |
c906108c | 1294 | { |
c5aa993b | 1295 | WSETEXIT (w, 0); |
c906108c SS |
1296 | warning ("Using exit value 0 for terminated task"); |
1297 | } | |
c5aa993b | 1298 | else if (!WIFEXITED (w)) |
c906108c | 1299 | { |
c5aa993b | 1300 | int sig = WTERMSIG (w); |
c906108c SS |
1301 | |
1302 | /* Signals cause problems. Warn the user. */ | |
c5aa993b | 1303 | if (sig != SIGKILL) /* Bad luck if garbage matches this */ |
c906108c SS |
1304 | warning ("The terminating signal stuff may be nonsense"); |
1305 | else if (sig > NSIG) | |
1306 | { | |
c5aa993b | 1307 | WSETEXIT (w, 0); |
c906108c SS |
1308 | warning ("Using exit value 0 for terminated task"); |
1309 | } | |
1310 | } | |
1311 | store_waitstatus (ourstatus, w); | |
1312 | return inferior_pid; | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | /* Hmm. Check for exception, as it was not a notification. | |
c5aa993b JM |
1317 | exc_server() does an upcall to catch_exception_raise() |
1318 | if this rpc is an exception. Further actions are decided | |
1319 | there. | |
c906108c | 1320 | */ |
c5aa993b | 1321 | if (!exc_server (&in_msg.header, &out_msg.header)) |
c906108c SS |
1322 | { |
1323 | ||
1324 | /* Not an exception, check for message. | |
c5aa993b | 1325 | |
c906108c SS |
1326 | * Messages don't come from the inferior, or if they |
1327 | * do they better be asynchronous or it will hang. | |
1328 | */ | |
1329 | if (gdb_message_server (&in_msg.header)) | |
1330 | continue; | |
1331 | ||
1332 | error ("Unrecognized message received in mach_really_wait"); | |
1333 | } | |
1334 | ||
1335 | /* Send the reply of the exception rpc to the suspended task */ | |
1336 | ret = mach_msg_send (&out_msg.header); | |
1337 | CHK ("mach_msg_send (exc reply)", ret); | |
c5aa993b | 1338 | |
c906108c SS |
1339 | if (stopped_in_exception) |
1340 | { | |
1341 | /* Get unix state. May be changed in mach3_exception_actions() */ | |
c5aa993b | 1342 | wait3 (&w, WNOHANG, 0); |
c906108c SS |
1343 | |
1344 | mach3_exception_actions (&w, FALSE, "Task"); | |
1345 | ||
1346 | store_waitstatus (ourstatus, w); | |
1347 | return inferior_pid; | |
1348 | } | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | /* Called by macro DO_QUIT() in utils.c(quit). | |
1353 | * This is called just before calling error() to return to command level | |
1354 | */ | |
1355 | void | |
fba45db2 | 1356 | mach3_quit (void) |
c906108c SS |
1357 | { |
1358 | int mid; | |
1359 | kern_return_t ret; | |
c5aa993b | 1360 | |
c906108c SS |
1361 | if (mach_really_waiting) |
1362 | { | |
1363 | ret = task_suspend (inferior_task); | |
c5aa993b | 1364 | |
c906108c SS |
1365 | if (ret != KERN_SUCCESS) |
1366 | { | |
1367 | warning ("Could not suspend task for interrupt: %s", | |
1368 | mach_error_string (ret)); | |
1369 | mach_really_waiting = 0; | |
1370 | return; | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | must_suspend_thread = 0; | |
1375 | mach_really_waiting = 0; | |
1376 | ||
1377 | mid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD); | |
1378 | if (mid == -1) | |
1379 | { | |
1380 | warning ("Selecting first existing kernel thread"); | |
1381 | mid = 0; | |
1382 | } | |
1383 | ||
c5aa993b | 1384 | current_thread = MACH_PORT_NULL; /* Force setup */ |
c906108c SS |
1385 | select_thread (inferior_task, mid, 1); |
1386 | ||
1387 | return; | |
1388 | } | |
1389 | ||
1390 | #if 0 | |
1391 | /* bogus bogus bogus. It is NOT OK to quit out of target_wait. */ | |
1392 | ||
1393 | /* If ^C is typed when we are waiting for a message | |
1394 | * and your Unix server is able to notice that we | |
1395 | * should quit now. | |
1396 | * | |
1397 | * Called by REQUEST_QUIT() from utils.c(request_quit) | |
1398 | */ | |
1399 | void | |
fba45db2 | 1400 | mach3_request_quit (void) |
c906108c SS |
1401 | { |
1402 | if (mach_really_waiting) | |
1403 | immediate_quit = 1; | |
c5aa993b | 1404 | } |
c906108c SS |
1405 | #endif |
1406 | ||
1407 | /* | |
1408 | * Gdb message server. | |
1409 | * Currently implemented is the STOP message, that causes | |
1410 | * gdb to return to the command level like ^C had been typed from terminal. | |
1411 | */ | |
1412 | int | |
fba45db2 | 1413 | gdb_message_server (mach_msg_header_t *InP) |
c906108c SS |
1414 | { |
1415 | kern_return_t ret; | |
1416 | int mid; | |
1417 | ||
1418 | if (InP->msgh_local_port == our_message_port) | |
1419 | { | |
1420 | /* A message coming to our_message_port. Check validity */ | |
c5aa993b JM |
1421 | switch (InP->msgh_id) |
1422 | { | |
c906108c | 1423 | |
c5aa993b JM |
1424 | case GDB_MESSAGE_ID_STOP: |
1425 | ret = task_suspend (inferior_task); | |
1426 | if (ret != KERN_SUCCESS) | |
1427 | warning ("Could not suspend task for stop message: %s", | |
1428 | mach_error_string (ret)); | |
c906108c | 1429 | |
c5aa993b JM |
1430 | /* QUIT in mach_really_wait() loop. */ |
1431 | request_quit (0); | |
1432 | break; | |
c906108c | 1433 | |
c5aa993b JM |
1434 | default: |
1435 | warning ("Invalid message id %d received, ignored.", | |
1436 | InP->msgh_id); | |
1437 | break; | |
1438 | } | |
c906108c SS |
1439 | |
1440 | return 1; | |
1441 | } | |
1442 | ||
1443 | /* Message not handled by this server */ | |
1444 | return 0; | |
1445 | } | |
1446 | ||
1447 | /* NOTE: This is not an RPC call. It is a simpleroutine. | |
c5aa993b | 1448 | |
c906108c SS |
1449 | * This is not called from this gdb code. |
1450 | * | |
1451 | * It may be called by another debugger to cause this | |
1452 | * debugger to enter command level: | |
1453 | * | |
1454 | * (gdb) set stop_inferior_gdb () | |
1455 | * (gdb) continue | |
1456 | * | |
1457 | * External program "stop-gdb" implements this also. | |
1458 | */ | |
1459 | void | |
fba45db2 | 1460 | stop_inferior_gdb (void) |
c906108c SS |
1461 | { |
1462 | kern_return_t ret; | |
1463 | ||
1464 | /* Code generated by mig, with minor cleanups :-) | |
c5aa993b | 1465 | |
c906108c SS |
1466 | * simpleroutine stop_inferior_gdb (our_message_port : mach_port_t); |
1467 | */ | |
1468 | ||
c5aa993b JM |
1469 | typedef struct |
1470 | { | |
1471 | mach_msg_header_t Head; | |
1472 | } | |
1473 | Request; | |
c906108c SS |
1474 | |
1475 | Request Mess; | |
1476 | ||
1477 | register Request *InP = &Mess; | |
1478 | ||
c5aa993b | 1479 | InP->Head.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0); |
c906108c SS |
1480 | |
1481 | /* msgh_size passed as argument */ | |
1482 | InP->Head.msgh_remote_port = our_message_port; | |
c5aa993b JM |
1483 | InP->Head.msgh_local_port = MACH_PORT_NULL; |
1484 | InP->Head.msgh_seqno = 0; | |
1485 | InP->Head.msgh_id = GDB_MESSAGE_ID_STOP; | |
c906108c SS |
1486 | |
1487 | ret = mach_msg (&InP->Head, | |
c5aa993b JM |
1488 | MACH_SEND_MSG | MACH_MSG_OPTION_NONE, |
1489 | sizeof (Request), | |
c906108c SS |
1490 | 0, |
1491 | MACH_PORT_NULL, | |
1492 | MACH_MSG_TIMEOUT_NONE, | |
1493 | MACH_PORT_NULL); | |
1494 | } | |
1495 | ||
1496 | #ifdef THREAD_ALLOWED_TO_BREAK | |
1497 | /* | |
1498 | * Return 1 if the MID specifies the thread that caused the | |
1499 | * last exception. | |
1500 | * Since catch_exception_raise() selects the thread causing | |
1501 | * the last exception to current_thread, we just check that | |
1502 | * it is selected and the last exception was a breakpoint. | |
1503 | */ | |
1504 | int | |
fba45db2 | 1505 | mach_thread_for_breakpoint (int mid) |
c906108c SS |
1506 | { |
1507 | int cmid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD); | |
1508 | ||
1509 | if (mid < 0) | |
1510 | { | |
c5aa993b | 1511 | mid = map_slot_to_mid (-(mid + 1), 0, 0); |
c906108c SS |
1512 | if (mid < 0) |
1513 | return 0; /* Don't stop, no such slot */ | |
1514 | } | |
1515 | ||
c5aa993b JM |
1516 | if (!mid || cmid == -1) |
1517 | return 1; /* stop */ | |
c906108c SS |
1518 | |
1519 | return cmid == mid && stop_exception == EXC_BREAKPOINT; | |
1520 | } | |
1521 | #endif /* THREAD_ALLOWED_TO_BREAK */ | |
1522 | ||
1523 | #ifdef THREAD_PARSE_ID | |
1524 | /* | |
1525 | * Map a thread id string (MID or a @SLOTNUMBER) | |
1526 | * to a thread-id. | |
1527 | * | |
1528 | * 0 matches all threads. | |
1529 | * Otherwise the meaning is defined only in this file. | |
1530 | * (mach_thread_for_breakpoint uses it) | |
1531 | * | |
1532 | * @@ This allows non-existent MIDs to be specified. | |
1533 | * It now also allows non-existent slots to be | |
1534 | * specified. (Slot numbers stored are negative, | |
1535 | * and the magnitude is one greater than the actual | |
1536 | * slot index. (Since 0 is reserved)) | |
1537 | */ | |
1538 | int | |
fba45db2 | 1539 | mach_thread_parse_id (char *arg) |
c906108c SS |
1540 | { |
1541 | int mid; | |
1542 | if (arg == 0) | |
1543 | error ("thread id excpected"); | |
1544 | mid = parse_thread_id (arg, 0, 1); | |
1545 | ||
1546 | return mid; | |
1547 | } | |
1548 | #endif /* THREAD_PARSE_ID */ | |
1549 | ||
1550 | #ifdef THREAD_OUTPUT_ID | |
1551 | char * | |
fba45db2 | 1552 | mach_thread_output_id (int mid) |
c906108c | 1553 | { |
c5aa993b | 1554 | static char foobar[20]; |
c906108c SS |
1555 | |
1556 | if (mid > 0) | |
1557 | sprintf (foobar, "mid %d", mid); | |
1558 | else if (mid < 0) | |
c5aa993b | 1559 | sprintf (foobar, "@%d", -(mid + 1)); |
c906108c SS |
1560 | else |
1561 | sprintf (foobar, "*any thread*"); | |
1562 | ||
1563 | return foobar; | |
1564 | } | |
1565 | #endif /* THREAD_OUTPUT_ID */ | |
1566 | ||
1567 | /* Called with hook PREPARE_TO_PROCEED() from infrun.c. | |
c5aa993b | 1568 | |
c906108c SS |
1569 | * If we have switched threads and stopped at breakpoint return 1 otherwise 0. |
1570 | * | |
1571 | * if SELECT_IT is nonzero, reselect the thread that was active when | |
1572 | * we stopped at a breakpoint. | |
1573 | * | |
1574 | */ | |
1575 | ||
fba45db2 | 1576 | mach3_prepare_to_proceed (int select_it) |
c906108c SS |
1577 | { |
1578 | if (stop_thread && | |
1579 | stop_thread != current_thread && | |
1580 | stop_exception == EXC_BREAKPOINT) | |
1581 | { | |
1582 | int mid; | |
1583 | ||
c5aa993b | 1584 | if (!select_it) |
c906108c SS |
1585 | return 1; |
1586 | ||
1587 | mid = switch_to_thread (stop_thread); | |
1588 | ||
1589 | return 1; | |
1590 | } | |
1591 | ||
1592 | return 0; | |
1593 | } | |
1594 | ||
1595 | /* this stuff here is an upcall via libmach/excServer.c | |
1596 | and mach_really_wait which does the actual upcall. | |
1597 | ||
1598 | The code will pass the exception to the inferior if: | |
1599 | ||
c5aa993b JM |
1600 | - The task that signaled is not the inferior task |
1601 | (e.g. when debugging another debugger) | |
c906108c | 1602 | |
c5aa993b JM |
1603 | - The user has explicitely requested to pass on the exceptions. |
1604 | (e.g to the default unix exception handler, which maps | |
1605 | exceptions to signals, or the user has her own exception handler) | |
c906108c | 1606 | |
c5aa993b JM |
1607 | - If the thread that signaled is being single-stepped and it |
1608 | has set it's own exception port and the exception is not | |
1609 | EXC_BREAKPOINT. (Maybe this is not desirable?) | |
c906108c SS |
1610 | */ |
1611 | ||
1612 | kern_return_t | |
fba45db2 KB |
1613 | catch_exception_raise (mach_port_t port, thread_t thread, task_t task, |
1614 | int exception, int code, int subcode) | |
c906108c SS |
1615 | { |
1616 | kern_return_t ret; | |
1617 | boolean_t signal_thread; | |
1618 | int mid = map_port_name_to_mid (thread, MACH_TYPE_THREAD); | |
1619 | ||
c5aa993b | 1620 | if (!MACH_PORT_VALID (thread)) |
c906108c SS |
1621 | { |
1622 | /* If the exception was sent and thread dies before we | |
c5aa993b | 1623 | receive it, THREAD will be MACH_PORT_DEAD |
c906108c SS |
1624 | */ |
1625 | ||
1626 | current_thread = thread = MACH_PORT_NULL; | |
1627 | error ("Received exception from nonexistent thread"); | |
1628 | } | |
1629 | ||
1630 | /* Check if the task died in transit. | |
1631 | * @@ Isn't the thread also invalid in such case? | |
1632 | */ | |
c5aa993b | 1633 | if (!MACH_PORT_VALID (task)) |
c906108c SS |
1634 | { |
1635 | current_thread = thread = MACH_PORT_NULL; | |
1636 | error ("Received exception from nonexistent task"); | |
1637 | } | |
1638 | ||
1639 | if (exception < 0 || exception > MAX_EXCEPTION) | |
96baa820 JM |
1640 | internal_error ("catch_exception_raise: unknown exception code %d thread %d", |
1641 | exception, | |
1642 | mid); | |
c906108c | 1643 | |
c5aa993b | 1644 | if (!MACH_PORT_VALID (inferior_task)) |
c906108c | 1645 | error ("got an exception, but inferior_task is null or dead"); |
c5aa993b | 1646 | |
c906108c | 1647 | stop_exception = exception; |
c5aa993b JM |
1648 | stop_code = code; |
1649 | stop_subcode = subcode; | |
1650 | stop_thread = thread; | |
1651 | ||
1652 | signal_thread = exception != EXC_BREAKPOINT && | |
1653 | port == singlestepped_thread_port && | |
1654 | MACH_PORT_VALID (thread_saved_exception_port); | |
c906108c SS |
1655 | |
1656 | /* If it was not our inferior or if we want to forward | |
1657 | * the exception to the inferior's handler, do it here | |
1658 | * | |
1659 | * Note: If you have forwarded EXC_BREAKPOINT I trust you know why. | |
1660 | */ | |
1661 | if (task != inferior_task || | |
c5aa993b JM |
1662 | signal_thread || |
1663 | exception_map[exception].forward) | |
c906108c SS |
1664 | { |
1665 | mach_port_t eport = inferior_old_exception_port; | |
1666 | ||
1667 | if (signal_thread) | |
1668 | { | |
1669 | /* | |
c5aa993b JM |
1670 | GDB now forwards the exeption to thread's original handler, |
1671 | since the user propably knows what he is doing. | |
1672 | Give a message, though. | |
c906108c SS |
1673 | */ |
1674 | ||
c5aa993b | 1675 | mach3_exception_actions ((WAITTYPE *) NULL, TRUE, "Thread"); |
c906108c SS |
1676 | eport = thread_saved_exception_port; |
1677 | } | |
1678 | ||
1679 | /* Send the exception to the original handler */ | |
1680 | ret = exception_raise (eport, | |
c5aa993b | 1681 | thread, |
c906108c SS |
1682 | task, |
1683 | exception, | |
1684 | code, | |
1685 | subcode); | |
1686 | ||
1687 | (void) mach_port_deallocate (mach_task_self (), task); | |
1688 | (void) mach_port_deallocate (mach_task_self (), thread); | |
1689 | ||
1690 | /* If we come here, we don't want to trace any more, since we | |
1691 | * will never stop for tracing anyway. | |
1692 | */ | |
1693 | discard_single_step (thread); | |
1694 | ||
1695 | /* Do not stop the inferior */ | |
1696 | return ret; | |
1697 | } | |
c5aa993b | 1698 | |
c906108c SS |
1699 | /* Now gdb handles the exception */ |
1700 | stopped_in_exception = TRUE; | |
1701 | ||
1702 | ret = task_suspend (task); | |
1703 | CHK ("Error suspending inferior after exception", ret); | |
1704 | ||
1705 | must_suspend_thread = 0; | |
1706 | ||
1707 | if (current_thread != thread) | |
1708 | { | |
1709 | if (MACH_PORT_VALID (singlestepped_thread_port)) | |
1710 | /* Cleanup discards single stepping */ | |
1711 | error ("Exception from thread %d while singlestepping thread %d", | |
1712 | mid, | |
1713 | map_port_name_to_mid (current_thread, MACH_TYPE_THREAD)); | |
c5aa993b | 1714 | |
c906108c SS |
1715 | /* Then select the thread that caused the exception */ |
1716 | if (select_thread (inferior_task, mid, 0) != KERN_SUCCESS) | |
1717 | error ("Could not select thread %d causing exception", mid); | |
1718 | else | |
1719 | warning ("Gdb selected thread %d", mid); | |
1720 | } | |
1721 | ||
1722 | /* If we receive an exception that is not breakpoint | |
1723 | * exception, we interrupt the single step and return to | |
1724 | * debugger. Trace condition is cleared. | |
1725 | */ | |
1726 | if (MACH_PORT_VALID (singlestepped_thread_port)) | |
1727 | { | |
1728 | if (stop_exception != EXC_BREAKPOINT) | |
1729 | warning ("Single step interrupted by exception"); | |
1730 | else if (port == singlestepped_thread_port) | |
1731 | { | |
1732 | /* Single step exception occurred, remove trace bit | |
1733 | * and return to gdb. | |
1734 | */ | |
c5aa993b | 1735 | if (!MACH_PORT_VALID (current_thread)) |
c906108c | 1736 | error ("Single stepped thread is not valid"); |
c5aa993b | 1737 | |
c906108c SS |
1738 | /* Resume threads, but leave the task suspended */ |
1739 | resume_all_threads (0); | |
1740 | } | |
1741 | else | |
1742 | warning ("Breakpoint while single stepping?"); | |
1743 | ||
1744 | discard_single_step (current_thread); | |
1745 | } | |
c5aa993b | 1746 | |
c906108c SS |
1747 | (void) mach_port_deallocate (mach_task_self (), task); |
1748 | (void) mach_port_deallocate (mach_task_self (), thread); | |
1749 | ||
1750 | return KERN_SUCCESS; | |
1751 | } | |
1752 | \f | |
1753 | int | |
fba45db2 | 1754 | port_valid (mach_port_t port, int mask) |
c906108c SS |
1755 | { |
1756 | kern_return_t ret; | |
1757 | mach_port_type_t type; | |
1758 | ||
1759 | ret = mach_port_type (mach_task_self (), | |
1760 | port, | |
1761 | &type); | |
1762 | if (ret != KERN_SUCCESS || (type & mask) != mask) | |
1763 | return 0; | |
1764 | return 1; | |
1765 | } | |
1766 | \f | |
1767 | /* @@ No vm read cache implemented yet */ | |
1768 | boolean_t vm_read_cache_valid = FALSE; | |
1769 | ||
1770 | /* | |
1771 | * Read inferior task's LEN bytes from ADDR and copy it to MYADDR | |
1772 | * in gdb's address space. | |
1773 | * | |
1774 | * Return 0 on failure; number of bytes read otherwise. | |
1775 | */ | |
1776 | int | |
fba45db2 | 1777 | mach3_read_inferior (CORE_ADDR addr, char *myaddr, int length) |
c906108c SS |
1778 | { |
1779 | kern_return_t ret; | |
c5aa993b JM |
1780 | vm_address_t low_address = (vm_address_t) trunc_page (addr); |
1781 | vm_size_t aligned_length = | |
1782 | (vm_size_t) round_page (addr + length) - low_address; | |
1783 | pointer_t copied_memory; | |
1784 | int copy_count; | |
c906108c SS |
1785 | |
1786 | /* Get memory from inferior with page aligned addresses */ | |
1787 | ret = vm_read (inferior_task, | |
1788 | low_address, | |
1789 | aligned_length, | |
1790 | &copied_memory, | |
1791 | ©_count); | |
1792 | if (ret != KERN_SUCCESS) | |
1793 | { | |
1794 | /* the problem is that the inferior might be killed for whatever reason | |
1795 | * before we go to mach_really_wait. This is one place that ought to | |
1796 | * catch many of those errors. | |
1797 | * @@ A better fix would be to make all external events to GDB | |
1798 | * to arrive via a SINGLE port set. (Including user input!) | |
1799 | */ | |
1800 | ||
c5aa993b | 1801 | if (!port_valid (inferior_task, MACH_PORT_TYPE_SEND)) |
c906108c SS |
1802 | { |
1803 | m3_kill_inferior (); | |
1804 | error ("Inferior killed (task port invalid)"); | |
1805 | } | |
1806 | else | |
1807 | { | |
1808 | #ifdef OSF | |
1809 | extern int errno; | |
1810 | /* valprint.c gives nicer format if this does not | |
1811 | screw it. Eamonn seems to like this, so I enable | |
1812 | it if OSF is defined... | |
1813 | */ | |
1814 | warning ("[read inferior %x failed: %s]", | |
1815 | addr, mach_error_string (ret)); | |
1816 | errno = 0; | |
1817 | #endif | |
1818 | return 0; | |
1819 | } | |
1820 | } | |
1821 | ||
c5aa993b | 1822 | memcpy (myaddr, (char *) addr - low_address + copied_memory, length); |
c906108c SS |
1823 | |
1824 | ret = vm_deallocate (mach_task_self (), | |
1825 | copied_memory, | |
1826 | copy_count); | |
c5aa993b | 1827 | CHK ("mach3_read_inferior vm_deallocate failed", ret); |
c906108c SS |
1828 | |
1829 | return length; | |
1830 | } | |
1831 | ||
1832 | #ifdef __STDC__ | |
1833 | #define CHK_GOTO_OUT(str,ret) \ | |
1834 | do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0) | |
1835 | #else | |
1836 | #define CHK_GOTO_OUT(str,ret) \ | |
1837 | do if (ret != KERN_SUCCESS) { errstr = str; goto out; } while(0) | |
1838 | #endif | |
1839 | ||
c5aa993b JM |
1840 | struct vm_region_list |
1841 | { | |
c906108c | 1842 | struct vm_region_list *next; |
c5aa993b JM |
1843 | vm_prot_t protection; |
1844 | vm_address_t start; | |
1845 | vm_size_t length; | |
c906108c SS |
1846 | }; |
1847 | ||
c5aa993b | 1848 | struct obstack region_obstack; |
c906108c SS |
1849 | |
1850 | /* | |
1851 | * Write inferior task's LEN bytes from ADDR and copy it to MYADDR | |
1852 | * in gdb's address space. | |
1853 | */ | |
1854 | int | |
fba45db2 | 1855 | mach3_write_inferior (CORE_ADDR addr, char *myaddr, int length) |
c906108c SS |
1856 | { |
1857 | kern_return_t ret; | |
c5aa993b JM |
1858 | vm_address_t low_address = (vm_address_t) trunc_page (addr); |
1859 | vm_size_t aligned_length = | |
1860 | (vm_size_t) round_page (addr + length) - low_address; | |
1861 | pointer_t copied_memory; | |
1862 | int copy_count; | |
1863 | int deallocate = 0; | |
c906108c | 1864 | |
c5aa993b | 1865 | char *errstr = "Bug in mach3_write_inferior"; |
c906108c SS |
1866 | |
1867 | struct vm_region_list *region_element; | |
c5aa993b | 1868 | struct vm_region_list *region_head = (struct vm_region_list *) NULL; |
c906108c SS |
1869 | |
1870 | /* Get memory from inferior with page aligned addresses */ | |
1871 | ret = vm_read (inferior_task, | |
1872 | low_address, | |
1873 | aligned_length, | |
1874 | &copied_memory, | |
1875 | ©_count); | |
1876 | CHK_GOTO_OUT ("mach3_write_inferior vm_read failed", ret); | |
1877 | ||
1878 | deallocate++; | |
1879 | ||
c5aa993b | 1880 | memcpy ((char *) addr - low_address + copied_memory, myaddr, length); |
c906108c SS |
1881 | |
1882 | obstack_init (®ion_obstack); | |
1883 | ||
1884 | /* Do writes atomically. | |
1885 | * First check for holes and unwritable memory. | |
1886 | */ | |
1887 | { | |
c5aa993b JM |
1888 | vm_size_t remaining_length = aligned_length; |
1889 | vm_address_t region_address = low_address; | |
c906108c SS |
1890 | |
1891 | struct vm_region_list *scan; | |
1892 | ||
c5aa993b | 1893 | while (region_address < low_address + aligned_length) |
c906108c SS |
1894 | { |
1895 | vm_prot_t protection; | |
1896 | vm_prot_t max_protection; | |
1897 | vm_inherit_t inheritance; | |
1898 | boolean_t shared; | |
1899 | mach_port_t object_name; | |
1900 | vm_offset_t offset; | |
c5aa993b JM |
1901 | vm_size_t region_length = remaining_length; |
1902 | vm_address_t old_address = region_address; | |
1903 | ||
c906108c SS |
1904 | ret = vm_region (inferior_task, |
1905 | ®ion_address, | |
1906 | ®ion_length, | |
1907 | &protection, | |
1908 | &max_protection, | |
1909 | &inheritance, | |
1910 | &shared, | |
1911 | &object_name, | |
1912 | &offset); | |
1913 | CHK_GOTO_OUT ("vm_region failed", ret); | |
1914 | ||
1915 | /* Check for holes in memory */ | |
1916 | if (old_address != region_address) | |
1917 | { | |
1918 | warning ("No memory at 0x%x. Nothing written", | |
1919 | old_address); | |
1920 | ret = KERN_SUCCESS; | |
1921 | length = 0; | |
1922 | goto out; | |
1923 | } | |
1924 | ||
1925 | if (!(max_protection & VM_PROT_WRITE)) | |
1926 | { | |
1927 | warning ("Memory at address 0x%x is unwritable. Nothing written", | |
1928 | old_address); | |
1929 | ret = KERN_SUCCESS; | |
1930 | length = 0; | |
1931 | goto out; | |
1932 | } | |
1933 | ||
1934 | /* Chain the regions for later use */ | |
c5aa993b | 1935 | region_element = |
c906108c | 1936 | (struct vm_region_list *) |
c5aa993b JM |
1937 | obstack_alloc (®ion_obstack, sizeof (struct vm_region_list)); |
1938 | ||
c906108c | 1939 | region_element->protection = protection; |
c5aa993b JM |
1940 | region_element->start = region_address; |
1941 | region_element->length = region_length; | |
c906108c SS |
1942 | |
1943 | /* Chain the regions along with protections */ | |
1944 | region_element->next = region_head; | |
c5aa993b JM |
1945 | region_head = region_element; |
1946 | ||
c906108c SS |
1947 | region_address += region_length; |
1948 | remaining_length = remaining_length - region_length; | |
1949 | } | |
1950 | ||
1951 | /* If things fail after this, we give up. | |
1952 | * Somebody is messing up inferior_task's mappings. | |
1953 | */ | |
c5aa993b | 1954 | |
c906108c SS |
1955 | /* Enable writes to the chained vm regions */ |
1956 | for (scan = region_head; scan; scan = scan->next) | |
1957 | { | |
1958 | boolean_t protection_changed = FALSE; | |
c5aa993b | 1959 | |
c906108c SS |
1960 | if (!(scan->protection & VM_PROT_WRITE)) |
1961 | { | |
1962 | ret = vm_protect (inferior_task, | |
1963 | scan->start, | |
1964 | scan->length, | |
1965 | FALSE, | |
1966 | scan->protection | VM_PROT_WRITE); | |
1967 | CHK_GOTO_OUT ("vm_protect: enable write failed", ret); | |
1968 | } | |
1969 | } | |
1970 | ||
1971 | ret = vm_write (inferior_task, | |
1972 | low_address, | |
1973 | copied_memory, | |
1974 | aligned_length); | |
1975 | CHK_GOTO_OUT ("vm_write failed", ret); | |
c5aa993b | 1976 | |
c906108c SS |
1977 | /* Set up the original region protections, if they were changed */ |
1978 | for (scan = region_head; scan; scan = scan->next) | |
1979 | { | |
1980 | boolean_t protection_changed = FALSE; | |
c5aa993b | 1981 | |
c906108c SS |
1982 | if (!(scan->protection & VM_PROT_WRITE)) |
1983 | { | |
1984 | ret = vm_protect (inferior_task, | |
1985 | scan->start, | |
1986 | scan->length, | |
1987 | FALSE, | |
1988 | scan->protection); | |
1989 | CHK_GOTO_OUT ("vm_protect: enable write failed", ret); | |
1990 | } | |
1991 | } | |
1992 | } | |
1993 | ||
c5aa993b | 1994 | out: |
c906108c SS |
1995 | if (deallocate) |
1996 | { | |
1997 | obstack_free (®ion_obstack, 0); | |
c5aa993b | 1998 | |
c906108c SS |
1999 | (void) vm_deallocate (mach_task_self (), |
2000 | copied_memory, | |
2001 | copy_count); | |
2002 | } | |
2003 | ||
2004 | if (ret != KERN_SUCCESS) | |
2005 | { | |
2006 | warning ("%s %s", errstr, mach_error_string (ret)); | |
2007 | return 0; | |
2008 | } | |
2009 | ||
2010 | return length; | |
2011 | } | |
2012 | ||
2013 | /* Return 0 on failure, number of bytes handled otherwise. */ | |
2014 | static int | |
2015 | m3_xfer_memory (memaddr, myaddr, len, write, target) | |
2016 | CORE_ADDR memaddr; | |
2017 | char *myaddr; | |
2018 | int len; | |
2019 | int write; | |
2020 | struct target_ops *target; /* IGNORED */ | |
2021 | { | |
2022 | int result; | |
2023 | ||
2024 | if (write) | |
2025 | result = mach3_write_inferior (memaddr, myaddr, len); | |
2026 | else | |
c5aa993b | 2027 | result = mach3_read_inferior (memaddr, myaddr, len); |
c906108c SS |
2028 | |
2029 | return result; | |
2030 | } | |
c906108c | 2031 | \f |
c5aa993b | 2032 | |
c906108c | 2033 | static char * |
fba45db2 | 2034 | translate_state (int state) |
c906108c | 2035 | { |
c5aa993b JM |
2036 | switch (state) |
2037 | { | |
2038 | case TH_STATE_RUNNING: | |
2039 | return ("R"); | |
2040 | case TH_STATE_STOPPED: | |
2041 | return ("S"); | |
2042 | case TH_STATE_WAITING: | |
2043 | return ("W"); | |
2044 | case TH_STATE_UNINTERRUPTIBLE: | |
2045 | return ("U"); | |
2046 | case TH_STATE_HALTED: | |
2047 | return ("H"); | |
2048 | default: | |
2049 | return ("?"); | |
2050 | } | |
c906108c SS |
2051 | } |
2052 | ||
2053 | static char * | |
fba45db2 | 2054 | translate_cstate (int state) |
c906108c SS |
2055 | { |
2056 | switch (state) | |
2057 | { | |
c5aa993b JM |
2058 | case CPROC_RUNNING: |
2059 | return "R"; | |
2060 | case CPROC_SWITCHING: | |
2061 | return "S"; | |
2062 | case CPROC_BLOCKED: | |
2063 | return "B"; | |
2064 | case CPROC_CONDWAIT: | |
2065 | return "C"; | |
2066 | case CPROC_CONDWAIT | CPROC_SWITCHING: | |
2067 | return "CS"; | |
2068 | default: | |
2069 | return "?"; | |
c906108c SS |
2070 | } |
2071 | } | |
2072 | ||
2073 | /* type == MACH_MSG_TYPE_COPY_SEND || type == MACH_MSG_TYPE_MAKE_SEND */ | |
2074 | ||
c5aa993b | 2075 | mach_port_t /* no mach_port_name_t found in include files. */ |
fba45db2 | 2076 | map_inferior_port_name (mach_port_t inferior_name, mach_msg_type_name_t type) |
c906108c | 2077 | { |
c5aa993b | 2078 | kern_return_t ret; |
c906108c | 2079 | mach_msg_type_name_t acquired; |
c5aa993b JM |
2080 | mach_port_t iport; |
2081 | ||
c906108c SS |
2082 | ret = mach_port_extract_right (inferior_task, |
2083 | inferior_name, | |
2084 | type, | |
2085 | &iport, | |
2086 | &acquired); | |
c5aa993b | 2087 | CHK ("mach_port_extract_right (map_inferior_port_name)", ret); |
c906108c SS |
2088 | |
2089 | if (acquired != MACH_MSG_TYPE_PORT_SEND) | |
c5aa993b | 2090 | error ("Incorrect right extracted, (map_inferior_port_name)"); |
c906108c SS |
2091 | |
2092 | ret = mach_port_deallocate (mach_task_self (), | |
2093 | iport); | |
2094 | CHK ("Deallocating mapped port (map_inferior_port_name)", ret); | |
2095 | ||
2096 | return iport; | |
2097 | } | |
2098 | ||
2099 | /* | |
2100 | * Naming convention: | |
2101 | * Always return user defined name if found. | |
2102 | * _K == A kernel thread with no matching CPROC | |
2103 | * _C == A cproc with no current cthread | |
2104 | * _t == A cthread with no user defined name | |
2105 | * | |
2106 | * The digits that follow the _names are the SLOT number of the | |
2107 | * kernel thread if there is such a thing, otherwise just a negation | |
2108 | * of the sequential number of such cprocs. | |
2109 | */ | |
2110 | ||
2111 | static char buf[7]; | |
2112 | ||
2113 | static char * | |
fba45db2 | 2114 | get_thread_name (gdb_thread_t one_cproc, int id) |
c906108c SS |
2115 | { |
2116 | if (one_cproc) | |
2117 | if (one_cproc->cthread == NULL) | |
2118 | { | |
2119 | /* cproc not mapped to any cthread */ | |
c5aa993b | 2120 | sprintf (buf, "_C%d", id); |
c906108c | 2121 | } |
c5aa993b | 2122 | else if (!one_cproc->cthread->name) |
c906108c SS |
2123 | { |
2124 | /* cproc and cthread, but no name */ | |
c5aa993b | 2125 | sprintf (buf, "_t%d", id); |
c906108c SS |
2126 | } |
2127 | else | |
c5aa993b | 2128 | return (char *) (one_cproc->cthread->name); |
c906108c SS |
2129 | else |
2130 | { | |
2131 | if (id < 0) | |
2132 | warning ("Inconsistency in thread name id %d", id); | |
2133 | ||
2134 | /* Kernel thread without cproc */ | |
c5aa993b | 2135 | sprintf (buf, "_K%d", id); |
c906108c SS |
2136 | } |
2137 | ||
2138 | return buf; | |
2139 | } | |
2140 | ||
2141 | int | |
2142 | fetch_thread_info (task, mthreads_out) | |
c5aa993b JM |
2143 | mach_port_t task; |
2144 | gdb_thread_t *mthreads_out; /* out */ | |
c906108c | 2145 | { |
c5aa993b | 2146 | kern_return_t ret; |
c906108c | 2147 | thread_array_t th_table; |
c5aa993b | 2148 | int th_count; |
c906108c | 2149 | gdb_thread_t mthreads = NULL; |
c5aa993b | 2150 | int index; |
c906108c SS |
2151 | |
2152 | ret = task_threads (task, &th_table, &th_count); | |
2153 | if (ret != KERN_SUCCESS) | |
2154 | { | |
2155 | warning ("Error getting inferior's thread list:%s", | |
c5aa993b | 2156 | mach_error_string (ret)); |
c906108c SS |
2157 | m3_kill_inferior (); |
2158 | return -1; | |
2159 | } | |
c5aa993b | 2160 | |
c906108c | 2161 | mthreads = (gdb_thread_t) |
c5aa993b JM |
2162 | obstack_alloc |
2163 | (cproc_obstack, | |
2164 | th_count * sizeof (struct gdb_thread)); | |
c906108c SS |
2165 | |
2166 | for (index = 0; index < th_count; index++) | |
2167 | { | |
2168 | thread_t saved_thread = MACH_PORT_NULL; | |
2169 | int mid; | |
2170 | ||
2171 | if (must_suspend_thread) | |
c5aa993b | 2172 | setup_thread (th_table[index], 1); |
c906108c SS |
2173 | |
2174 | if (th_table[index] != current_thread) | |
2175 | { | |
2176 | saved_thread = current_thread; | |
c5aa993b JM |
2177 | |
2178 | mid = switch_to_thread (th_table[index]); | |
c906108c SS |
2179 | } |
2180 | ||
c5aa993b | 2181 | mthreads[index].name = th_table[index]; |
c906108c SS |
2182 | mthreads[index].cproc = NULL; /* map_cprocs_to_kernel_threads() */ |
2183 | mthreads[index].in_emulator = FALSE; | |
2184 | mthreads[index].slotid = index; | |
c5aa993b | 2185 | |
c906108c SS |
2186 | mthreads[index].sp = read_register (SP_REGNUM); |
2187 | mthreads[index].fp = read_register (FP_REGNUM); | |
2188 | mthreads[index].pc = read_pc (); | |
2189 | ||
2190 | if (MACH_PORT_VALID (saved_thread)) | |
2191 | mid = switch_to_thread (saved_thread); | |
2192 | ||
2193 | if (must_suspend_thread) | |
c5aa993b | 2194 | setup_thread (th_table[index], 0); |
c906108c | 2195 | } |
c5aa993b | 2196 | |
c906108c | 2197 | consume_send_rights (th_table, th_count); |
c5aa993b JM |
2198 | ret = vm_deallocate (mach_task_self (), (vm_address_t) th_table, |
2199 | (th_count * sizeof (mach_port_t))); | |
c906108c SS |
2200 | if (ret != KERN_SUCCESS) |
2201 | { | |
2202 | warning ("Error trying to deallocate thread list : %s", | |
2203 | mach_error_string (ret)); | |
2204 | } | |
2205 | ||
2206 | *mthreads_out = mthreads; | |
2207 | ||
2208 | return th_count; | |
2209 | } | |
2210 | ||
2211 | ||
2212 | /* | |
2213 | * Current emulator always saves the USP on top of | |
2214 | * emulator stack below struct emul_stack_top stuff. | |
2215 | */ | |
2216 | CORE_ADDR | |
fba45db2 | 2217 | fetch_usp_from_emulator_stack (CORE_ADDR sp) |
c906108c SS |
2218 | { |
2219 | CORE_ADDR stack_pointer; | |
2220 | ||
c5aa993b JM |
2221 | sp = (sp & ~(EMULATOR_STACK_SIZE - 1)) + |
2222 | EMULATOR_STACK_SIZE - sizeof (struct emul_stack_top); | |
2223 | ||
c906108c SS |
2224 | if (mach3_read_inferior (sp, |
2225 | &stack_pointer, | |
2226 | sizeof (CORE_ADDR)) != sizeof (CORE_ADDR)) | |
2227 | { | |
2228 | warning ("Can't read user sp from emulator stack address 0x%x", sp); | |
2229 | return 0; | |
2230 | } | |
2231 | ||
2232 | return stack_pointer; | |
2233 | } | |
2234 | ||
2235 | #ifdef MK67 | |
2236 | ||
2237 | /* get_emulation_vector() interface was changed after mk67 */ | |
2238 | #define EMUL_VECTOR_COUNT 400 /* Value does not matter too much */ | |
2239 | ||
2240 | #endif /* MK67 */ | |
2241 | ||
2242 | /* Check if the emulator exists at task's address space. | |
2243 | */ | |
2244 | boolean_t | |
fba45db2 | 2245 | have_emulator_p (task_t task) |
c906108c | 2246 | { |
c5aa993b | 2247 | kern_return_t ret; |
c906108c | 2248 | #ifndef EMUL_VECTOR_COUNT |
c5aa993b JM |
2249 | vm_offset_t *emulation_vector; |
2250 | int n; | |
c906108c | 2251 | #else |
c5aa993b JM |
2252 | vm_offset_t emulation_vector[EMUL_VECTOR_COUNT]; |
2253 | int n = EMUL_VECTOR_COUNT; | |
c906108c | 2254 | #endif |
c5aa993b JM |
2255 | int i; |
2256 | int vector_start; | |
2257 | ||
c906108c SS |
2258 | ret = task_get_emulation_vector (task, |
2259 | &vector_start, | |
2260 | #ifndef EMUL_VECTOR_COUNT | |
2261 | &emulation_vector, | |
2262 | #else | |
2263 | emulation_vector, | |
2264 | #endif | |
2265 | &n); | |
c5aa993b | 2266 | CHK ("task_get_emulation_vector", ret); |
c906108c SS |
2267 | xx_debug ("%d vectors from %d at 0x%08x\n", |
2268 | n, vector_start, emulation_vector); | |
c5aa993b JM |
2269 | |
2270 | for (i = 0; i < n; i++) | |
c906108c | 2271 | { |
c5aa993b | 2272 | vm_offset_t entry = emulation_vector[i]; |
c906108c SS |
2273 | |
2274 | if (EMULATOR_BASE <= entry && entry <= EMULATOR_END) | |
2275 | return TRUE; | |
2276 | else if (entry) | |
2277 | { | |
2278 | static boolean_t informed = FALSE; | |
2279 | if (!informed) | |
2280 | { | |
c5aa993b JM |
2281 | warning ("Emulation vector address 0x08%x outside emulator space", |
2282 | entry); | |
c906108c SS |
2283 | informed = TRUE; |
2284 | } | |
2285 | } | |
2286 | } | |
2287 | return FALSE; | |
2288 | } | |
2289 | ||
2290 | /* Map cprocs to kernel threads and vice versa. */ | |
2291 | ||
2292 | void | |
fba45db2 KB |
2293 | map_cprocs_to_kernel_threads (gdb_thread_t cprocs, gdb_thread_t mthreads, |
2294 | int thread_count) | |
c906108c SS |
2295 | { |
2296 | int index; | |
2297 | gdb_thread_t scan; | |
2298 | boolean_t all_mapped = TRUE; | |
2299 | LONGEST stack_base; | |
2300 | LONGEST stack_size; | |
2301 | ||
2302 | for (scan = cprocs; scan; scan = scan->next) | |
2303 | { | |
2304 | /* Default to: no kernel thread for this cproc */ | |
2305 | scan->reverse_map = -1; | |
2306 | ||
2307 | /* Check if the cproc is found by its stack */ | |
2308 | for (index = 0; index < thread_count; index++) | |
2309 | { | |
2310 | stack_base = | |
2311 | extract_signed_integer (scan->raw_cproc + CPROC_BASE_OFFSET, | |
2312 | CPROC_BASE_SIZE); | |
c5aa993b | 2313 | stack_size = |
c906108c SS |
2314 | extract_signed_integer (scan->raw_cproc + CPROC_SIZE_OFFSET, |
2315 | CPROC_SIZE_SIZE); | |
2316 | if ((mthreads + index)->sp > stack_base && | |
2317 | (mthreads + index)->sp <= stack_base + stack_size) | |
2318 | { | |
2319 | (mthreads + index)->cproc = scan; | |
2320 | scan->reverse_map = index; | |
2321 | break; | |
2322 | } | |
2323 | } | |
2324 | all_mapped &= (scan->reverse_map != -1); | |
2325 | } | |
2326 | ||
2327 | /* Check for threads that are currently in the emulator. | |
2328 | * If so, they have a different stack, and the still unmapped | |
2329 | * cprocs may well get mapped to these threads. | |
2330 | * | |
2331 | * If: | |
2332 | * - cproc stack does not match any kernel thread stack pointer | |
2333 | * - there is at least one extra kernel thread | |
2334 | * that has no cproc mapped above. | |
2335 | * - some kernel thread stack pointer points to emulator space | |
2336 | * then we find the user stack pointer saved in the emulator | |
2337 | * stack, and try to map that to the cprocs. | |
2338 | * | |
2339 | * Also set in_emulator for kernel threads. | |
c5aa993b | 2340 | */ |
c906108c SS |
2341 | |
2342 | if (emulator_present) | |
2343 | { | |
2344 | for (index = 0; index < thread_count; index++) | |
2345 | { | |
2346 | CORE_ADDR emul_sp; | |
2347 | CORE_ADDR usp; | |
2348 | ||
c5aa993b | 2349 | gdb_thread_t mthread = (mthreads + index); |
c906108c SS |
2350 | emul_sp = mthread->sp; |
2351 | ||
2352 | if (mthread->cproc == NULL && | |
2353 | EMULATOR_BASE <= emul_sp && emul_sp <= EMULATOR_END) | |
2354 | { | |
2355 | mthread->in_emulator = emulator_present; | |
c5aa993b | 2356 | |
c906108c SS |
2357 | if (!all_mapped && cprocs) |
2358 | { | |
2359 | usp = fetch_usp_from_emulator_stack (emul_sp); | |
c5aa993b | 2360 | |
c906108c | 2361 | /* @@ Could be more accurate */ |
c5aa993b | 2362 | if (!usp) |
c906108c | 2363 | error ("Zero stack pointer read from emulator?"); |
c5aa993b | 2364 | |
c906108c SS |
2365 | /* Try to match this stack pointer to the cprocs that |
2366 | * don't yet have a kernel thread. | |
2367 | */ | |
2368 | for (scan = cprocs; scan; scan = scan->next) | |
2369 | { | |
c5aa993b | 2370 | |
c906108c SS |
2371 | /* Check is this unmapped CPROC stack contains |
2372 | * the user stack pointer saved in the | |
2373 | * emulator. | |
2374 | */ | |
2375 | if (scan->reverse_map == -1) | |
2376 | { | |
2377 | stack_base = | |
2378 | extract_signed_integer | |
c5aa993b JM |
2379 | (scan->raw_cproc + CPROC_BASE_OFFSET, |
2380 | CPROC_BASE_SIZE); | |
2381 | stack_size = | |
c906108c | 2382 | extract_signed_integer |
c5aa993b JM |
2383 | (scan->raw_cproc + CPROC_SIZE_OFFSET, |
2384 | CPROC_SIZE_SIZE); | |
c906108c SS |
2385 | if (usp > stack_base && |
2386 | usp <= stack_base + stack_size) | |
2387 | { | |
2388 | mthread->cproc = scan; | |
2389 | scan->reverse_map = index; | |
2390 | break; | |
2391 | } | |
2392 | } | |
2393 | } | |
2394 | } | |
2395 | } | |
2396 | } | |
2397 | } | |
2398 | } | |
2399 | \f | |
2400 | /* | |
2401 | * Format of the thread_list command | |
2402 | * | |
c5aa993b | 2403 | * slot mid sel name emul ks susp cstate wired address |
c906108c SS |
2404 | */ |
2405 | #define TL_FORMAT "%-2.2s %5d%c %-10.10s %1.1s%s%-5.5s %-2.2s %-5.5s " | |
2406 | ||
2407 | #define TL_HEADER "\n@ MID Name KState CState Where\n" | |
2408 | ||
2409 | void | |
fba45db2 | 2410 | print_tl_address (struct ui_file *stream, CORE_ADDR pc) |
c906108c | 2411 | { |
c5aa993b JM |
2412 | if (!lookup_minimal_symbol_by_pc (pc)) |
2413 | fprintf_filtered (stream, local_hex_format (), pc); | |
c906108c SS |
2414 | else |
2415 | { | |
2416 | extern int addressprint; | |
2417 | extern int asm_demangle; | |
2418 | ||
c5aa993b | 2419 | int store = addressprint; |
c906108c SS |
2420 | addressprint = 0; |
2421 | print_address_symbolic (pc, stream, asm_demangle, ""); | |
2422 | addressprint = store; | |
2423 | } | |
2424 | } | |
2425 | \f | |
2426 | /* For thread names, but also for gdb_message_port external name */ | |
2427 | #define MAX_NAME_LEN 50 | |
2428 | ||
2429 | /* Returns the address of variable NAME or 0 if not found */ | |
2430 | CORE_ADDR | |
fba45db2 | 2431 | lookup_address_of_variable (char *name) |
c906108c SS |
2432 | { |
2433 | struct symbol *sym; | |
2434 | CORE_ADDR symaddr = 0; | |
2435 | struct minimal_symbol *msymbol; | |
2436 | ||
2437 | sym = lookup_symbol (name, | |
c5aa993b | 2438 | (struct block *) NULL, |
c906108c | 2439 | VAR_NAMESPACE, |
c5aa993b JM |
2440 | (int *) NULL, |
2441 | (struct symtab **) NULL); | |
c906108c SS |
2442 | |
2443 | if (sym) | |
2444 | symaddr = SYMBOL_VALUE (sym); | |
2445 | ||
c5aa993b | 2446 | if (!symaddr) |
c906108c SS |
2447 | { |
2448 | msymbol = lookup_minimal_symbol (name, NULL, NULL); | |
2449 | ||
2450 | if (msymbol && msymbol->type == mst_data) | |
2451 | symaddr = SYMBOL_VALUE_ADDRESS (msymbol); | |
2452 | } | |
2453 | ||
2454 | return symaddr; | |
2455 | } | |
2456 | ||
2457 | static gdb_thread_t | |
fba45db2 | 2458 | get_cprocs (void) |
c906108c SS |
2459 | { |
2460 | gdb_thread_t cproc_head; | |
2461 | gdb_thread_t cproc_copy; | |
2462 | CORE_ADDR their_cprocs; | |
2463 | char *buf[TARGET_PTR_BIT / HOST_CHAR_BIT]; | |
2464 | char *name; | |
2465 | cthread_t cthread; | |
2466 | CORE_ADDR symaddr; | |
c5aa993b | 2467 | |
c906108c SS |
2468 | symaddr = lookup_address_of_variable ("cproc_list"); |
2469 | ||
c5aa993b | 2470 | if (!symaddr) |
c906108c SS |
2471 | { |
2472 | /* cproc_list is not in a file compiled with debugging | |
c5aa993b | 2473 | symbols, but don't give up yet */ |
c906108c SS |
2474 | |
2475 | symaddr = lookup_address_of_variable ("cprocs"); | |
2476 | ||
2477 | if (symaddr) | |
2478 | { | |
2479 | static int informed = 0; | |
2480 | if (!informed) | |
2481 | { | |
2482 | informed++; | |
2483 | warning ("Your program is loaded with an old threads library."); | |
2484 | warning ("GDB does not know the old form of threads"); | |
2485 | warning ("so things may not work."); | |
2486 | } | |
2487 | } | |
2488 | } | |
2489 | ||
2490 | /* Stripped or no -lthreads loaded or "cproc_list" is in wrong segment. */ | |
c5aa993b | 2491 | if (!symaddr) |
c906108c SS |
2492 | return NULL; |
2493 | ||
2494 | /* Get the address of the first cproc in the task */ | |
2495 | if (!mach3_read_inferior (symaddr, | |
2496 | buf, | |
2497 | TARGET_PTR_BIT / HOST_CHAR_BIT)) | |
2498 | error ("Can't read cproc master list at address (0x%x).", symaddr); | |
2499 | their_cprocs = extract_address (buf, TARGET_PTR_BIT / HOST_CHAR_BIT); | |
2500 | ||
2501 | /* Scan the CPROCs in the task. | |
2502 | CPROCs are chained with LIST field, not NEXT field, which | |
2503 | chains mutexes, condition variables and queues */ | |
2504 | ||
2505 | cproc_head = NULL; | |
2506 | ||
c5aa993b | 2507 | while (their_cprocs != (CORE_ADDR) 0) |
c906108c SS |
2508 | { |
2509 | CORE_ADDR cproc_copy_incarnation; | |
2510 | cproc_copy = (gdb_thread_t) obstack_alloc (cproc_obstack, | |
2511 | sizeof (struct gdb_thread)); | |
2512 | ||
2513 | if (!mach3_read_inferior (their_cprocs, | |
2514 | &cproc_copy->raw_cproc[0], | |
2515 | CPROC_SIZE)) | |
c5aa993b | 2516 | error ("Can't read next cproc at 0x%x.", their_cprocs); |
c906108c SS |
2517 | |
2518 | their_cprocs = | |
2519 | extract_address (cproc_copy->raw_cproc + CPROC_LIST_OFFSET, | |
2520 | CPROC_LIST_SIZE); | |
2521 | cproc_copy_incarnation = | |
2522 | extract_address (cproc_copy->raw_cproc + CPROC_INCARNATION_OFFSET, | |
2523 | CPROC_INCARNATION_SIZE); | |
2524 | ||
c5aa993b | 2525 | if (cproc_copy_incarnation == (CORE_ADDR) 0) |
c906108c SS |
2526 | cproc_copy->cthread = NULL; |
2527 | else | |
2528 | { | |
2529 | /* This CPROC has an attached CTHREAD. Get its name */ | |
c5aa993b JM |
2530 | cthread = (cthread_t) obstack_alloc (cproc_obstack, |
2531 | sizeof (struct cthread)); | |
c906108c SS |
2532 | |
2533 | if (!mach3_read_inferior (cproc_copy_incarnation, | |
2534 | cthread, | |
c5aa993b JM |
2535 | sizeof (struct cthread))) |
2536 | error ("Can't read next thread at 0x%x.", | |
2537 | cproc_copy_incarnation); | |
c906108c SS |
2538 | |
2539 | cproc_copy->cthread = cthread; | |
2540 | ||
2541 | if (cthread->name) | |
2542 | { | |
2543 | name = (char *) obstack_alloc (cproc_obstack, MAX_NAME_LEN); | |
2544 | ||
c5aa993b JM |
2545 | if (!mach3_read_inferior (cthread->name, name, MAX_NAME_LEN)) |
2546 | error ("Can't read next thread's name at 0x%x.", cthread->name); | |
c906108c SS |
2547 | |
2548 | cthread->name = name; | |
2549 | } | |
2550 | } | |
2551 | ||
2552 | /* insert in front */ | |
2553 | cproc_copy->next = cproc_head; | |
2554 | cproc_head = cproc_copy; | |
2555 | } | |
2556 | return cproc_head; | |
2557 | } | |
2558 | ||
2559 | #ifndef FETCH_CPROC_STATE | |
2560 | /* | |
2561 | * Check if your machine does not grok the way this routine | |
2562 | * fetches the FP,PC and SP of a cproc that is not | |
2563 | * currently attached to any kernel thread (e.g. its cproc.context | |
2564 | * field points to the place in stack where the context | |
2565 | * is saved). | |
2566 | * | |
2567 | * If it doesn't, define your own routine. | |
2568 | */ | |
2569 | #define FETCH_CPROC_STATE(mth) mach3_cproc_state (mth) | |
2570 | ||
2571 | int | |
fba45db2 | 2572 | mach3_cproc_state (gdb_thread_t mthread) |
c906108c SS |
2573 | { |
2574 | int context; | |
2575 | ||
c5aa993b | 2576 | if (!mthread || !mthread->cproc) |
c906108c SS |
2577 | return -1; |
2578 | ||
2579 | context = extract_signed_integer | |
2580 | (mthread->cproc->raw_cproc + CPROC_CONTEXT_OFFSET, | |
2581 | CPROC_CONTEXT_SIZE); | |
2582 | if (context == 0) | |
2583 | return -1; | |
2584 | ||
2585 | mthread->sp = context + MACHINE_CPROC_SP_OFFSET; | |
2586 | ||
2587 | if (mach3_read_inferior (context + MACHINE_CPROC_PC_OFFSET, | |
2588 | &mthread->pc, | |
2589 | sizeof (CORE_ADDR)) != sizeof (CORE_ADDR)) | |
2590 | { | |
2591 | warning ("Can't read cproc pc from inferior"); | |
2592 | return -1; | |
2593 | } | |
2594 | ||
2595 | if (mach3_read_inferior (context + MACHINE_CPROC_FP_OFFSET, | |
2596 | &mthread->fp, | |
2597 | sizeof (CORE_ADDR)) != sizeof (CORE_ADDR)) | |
2598 | { | |
2599 | warning ("Can't read cproc fp from inferior"); | |
2600 | return -1; | |
2601 | } | |
2602 | ||
2603 | return 0; | |
2604 | } | |
2605 | #endif /* FETCH_CPROC_STATE */ | |
c906108c | 2606 | \f |
c5aa993b | 2607 | |
c906108c | 2608 | void |
fba45db2 | 2609 | thread_list_command (void) |
c906108c SS |
2610 | { |
2611 | thread_basic_info_data_t ths; | |
c5aa993b | 2612 | int thread_count; |
c906108c SS |
2613 | gdb_thread_t cprocs; |
2614 | gdb_thread_t scan; | |
c5aa993b JM |
2615 | int index; |
2616 | char *name; | |
2617 | char selected; | |
2618 | char *wired; | |
2619 | int infoCnt; | |
c906108c | 2620 | kern_return_t ret; |
c5aa993b JM |
2621 | mach_port_t mid_or_port; |
2622 | gdb_thread_t their_threads; | |
2623 | gdb_thread_t kthread; | |
c906108c SS |
2624 | |
2625 | int neworder = 1; | |
2626 | ||
2627 | char *fmt = "There are %d kernel threads in task %d.\n"; | |
c5aa993b | 2628 | |
c906108c | 2629 | int tmid = map_port_name_to_mid (inferior_task, MACH_TYPE_TASK); |
c5aa993b | 2630 | |
c906108c | 2631 | MACH_ERROR_NO_INFERIOR; |
c5aa993b | 2632 | |
c906108c SS |
2633 | thread_count = fetch_thread_info (inferior_task, |
2634 | &their_threads); | |
2635 | if (thread_count == -1) | |
2636 | return; | |
c5aa993b | 2637 | |
c906108c SS |
2638 | if (thread_count == 1) |
2639 | fmt = "There is %d kernel thread in task %d.\n"; | |
c5aa993b | 2640 | |
c906108c | 2641 | printf_filtered (fmt, thread_count, tmid); |
c5aa993b | 2642 | |
c906108c | 2643 | puts_filtered (TL_HEADER); |
c5aa993b JM |
2644 | |
2645 | cprocs = get_cprocs (); | |
2646 | ||
c906108c | 2647 | map_cprocs_to_kernel_threads (cprocs, their_threads, thread_count); |
c5aa993b | 2648 | |
c906108c SS |
2649 | for (scan = cprocs; scan; scan = scan->next) |
2650 | { | |
2651 | int mid; | |
2652 | char buf[10]; | |
2653 | char slot[3]; | |
2654 | int cproc_state = | |
c5aa993b JM |
2655 | extract_signed_integer |
2656 | (scan->raw_cproc + CPROC_STATE_OFFSET, CPROC_STATE_SIZE); | |
2657 | ||
c906108c | 2658 | selected = ' '; |
c5aa993b | 2659 | |
c906108c SS |
2660 | /* a wired cproc? */ |
2661 | wired = (extract_address (scan->raw_cproc + CPROC_WIRED_OFFSET, | |
2662 | CPROC_WIRED_SIZE) | |
2663 | ? "wired" : ""); | |
2664 | ||
2665 | if (scan->reverse_map != -1) | |
c5aa993b | 2666 | kthread = (their_threads + scan->reverse_map); |
c906108c | 2667 | else |
c5aa993b | 2668 | kthread = NULL; |
c906108c SS |
2669 | |
2670 | if (kthread) | |
2671 | { | |
2672 | /* These cprocs have a kernel thread */ | |
c5aa993b | 2673 | |
c906108c | 2674 | mid = map_port_name_to_mid (kthread->name, MACH_TYPE_THREAD); |
c5aa993b | 2675 | |
c906108c | 2676 | infoCnt = THREAD_BASIC_INFO_COUNT; |
c5aa993b | 2677 | |
c906108c SS |
2678 | ret = thread_info (kthread->name, |
2679 | THREAD_BASIC_INFO, | |
c5aa993b | 2680 | (thread_info_t) & ths, |
c906108c | 2681 | &infoCnt); |
c5aa993b | 2682 | |
c906108c SS |
2683 | if (ret != KERN_SUCCESS) |
2684 | { | |
2685 | warning ("Unable to get basic info on thread %d : %s", | |
2686 | mid, | |
2687 | mach_error_string (ret)); | |
2688 | continue; | |
2689 | } | |
2690 | ||
2691 | /* Who is the first to have more than 100 threads */ | |
c5aa993b | 2692 | sprintf (slot, "%d", kthread->slotid % 100); |
c906108c SS |
2693 | |
2694 | if (kthread->name == current_thread) | |
2695 | selected = '*'; | |
c5aa993b | 2696 | |
c906108c SS |
2697 | if (ths.suspend_count) |
2698 | sprintf (buf, "%d", ths.suspend_count); | |
2699 | else | |
2700 | buf[0] = '\000'; | |
2701 | ||
2702 | #if 0 | |
2703 | if (ths.flags & TH_FLAGS_SWAPPED) | |
2704 | strcat (buf, "S"); | |
2705 | #endif | |
2706 | ||
2707 | if (ths.flags & TH_FLAGS_IDLE) | |
2708 | strcat (buf, "I"); | |
2709 | ||
2710 | printf_filtered (TL_FORMAT, | |
2711 | slot, | |
2712 | mid, | |
2713 | selected, | |
2714 | get_thread_name (scan, kthread->slotid), | |
2715 | kthread->in_emulator ? "E" : "", | |
2716 | translate_state (ths.run_state), | |
2717 | buf, | |
2718 | translate_cstate (cproc_state), | |
2719 | wired); | |
2720 | print_tl_address (gdb_stdout, kthread->pc); | |
2721 | } | |
2722 | else | |
2723 | { | |
2724 | /* These cprocs don't have a kernel thread. | |
2725 | * find out the calling frame with | |
2726 | * FETCH_CPROC_STATE. | |
2727 | */ | |
2728 | ||
2729 | struct gdb_thread state; | |
2730 | ||
2731 | #if 0 | |
2732 | /* jtv -> emcmanus: why do you want this here? */ | |
2733 | if (scan->incarnation == NULL) | |
c5aa993b | 2734 | continue; /* EMcM */ |
c906108c SS |
2735 | #endif |
2736 | ||
2737 | printf_filtered (TL_FORMAT, | |
2738 | "-", | |
2739 | -neworder, /* Pseudo MID */ | |
2740 | selected, | |
2741 | get_thread_name (scan, -neworder), | |
2742 | "", | |
2743 | "-", /* kernel state */ | |
2744 | "", | |
2745 | translate_cstate (cproc_state), | |
2746 | ""); | |
2747 | state.cproc = scan; | |
2748 | ||
2749 | if (FETCH_CPROC_STATE (&state) == -1) | |
2750 | puts_filtered ("???"); | |
2751 | else | |
2752 | print_tl_address (gdb_stdout, state.pc); | |
2753 | ||
2754 | neworder++; | |
2755 | } | |
2756 | puts_filtered ("\n"); | |
2757 | } | |
c5aa993b | 2758 | |
c906108c SS |
2759 | /* Scan for kernel threads without cprocs */ |
2760 | for (index = 0; index < thread_count; index++) | |
2761 | { | |
c5aa993b | 2762 | if (!their_threads[index].cproc) |
c906108c SS |
2763 | { |
2764 | int mid; | |
c5aa993b | 2765 | |
c906108c SS |
2766 | char buf[10]; |
2767 | char slot[3]; | |
2768 | ||
2769 | mach_port_t name = their_threads[index].name; | |
c5aa993b | 2770 | |
c906108c | 2771 | mid = map_port_name_to_mid (name, MACH_TYPE_THREAD); |
c5aa993b | 2772 | |
c906108c | 2773 | infoCnt = THREAD_BASIC_INFO_COUNT; |
c5aa993b JM |
2774 | |
2775 | ret = thread_info (name, | |
2776 | THREAD_BASIC_INFO, | |
2777 | (thread_info_t) & ths, | |
2778 | &infoCnt); | |
2779 | ||
c906108c SS |
2780 | if (ret != KERN_SUCCESS) |
2781 | { | |
2782 | warning ("Unable to get basic info on thread %d : %s", | |
2783 | mid, | |
2784 | mach_error_string (ret)); | |
2785 | continue; | |
2786 | } | |
2787 | ||
c5aa993b | 2788 | sprintf (slot, "%d", index % 100); |
c906108c SS |
2789 | |
2790 | if (name == current_thread) | |
2791 | selected = '*'; | |
2792 | else | |
2793 | selected = ' '; | |
2794 | ||
2795 | if (ths.suspend_count) | |
2796 | sprintf (buf, "%d", ths.suspend_count); | |
2797 | else | |
2798 | buf[0] = '\000'; | |
2799 | ||
2800 | #if 0 | |
2801 | if (ths.flags & TH_FLAGS_SWAPPED) | |
2802 | strcat (buf, "S"); | |
2803 | #endif | |
2804 | ||
2805 | if (ths.flags & TH_FLAGS_IDLE) | |
2806 | strcat (buf, "I"); | |
2807 | ||
2808 | printf_filtered (TL_FORMAT, | |
2809 | slot, | |
2810 | mid, | |
2811 | selected, | |
2812 | get_thread_name (NULL, index), | |
2813 | their_threads[index].in_emulator ? "E" : "", | |
2814 | translate_state (ths.run_state), | |
2815 | buf, | |
c5aa993b | 2816 | "", /* No cproc state */ |
c906108c SS |
2817 | ""); /* Can't be wired */ |
2818 | print_tl_address (gdb_stdout, their_threads[index].pc); | |
2819 | puts_filtered ("\n"); | |
2820 | } | |
2821 | } | |
c5aa993b | 2822 | |
c906108c SS |
2823 | obstack_free (cproc_obstack, 0); |
2824 | obstack_init (cproc_obstack); | |
2825 | } | |
2826 | \f | |
2827 | void | |
fba45db2 | 2828 | thread_select_command (char *args, int from_tty) |
c906108c SS |
2829 | { |
2830 | int mid; | |
2831 | thread_array_t thread_list; | |
2832 | int thread_count; | |
2833 | kern_return_t ret; | |
2834 | int is_slot = 0; | |
2835 | ||
2836 | MACH_ERROR_NO_INFERIOR; | |
2837 | ||
2838 | if (!args) | |
2839 | error_no_arg ("MID or @SLOTNUMBER to specify a thread to select"); | |
2840 | ||
2841 | while (*args == ' ' || *args == '\t') | |
2842 | args++; | |
2843 | ||
2844 | if (*args == '@') | |
2845 | { | |
2846 | is_slot++; | |
2847 | args++; | |
2848 | } | |
2849 | ||
c5aa993b | 2850 | mid = atoi (args); |
c906108c SS |
2851 | |
2852 | if (mid == 0) | |
c5aa993b | 2853 | if (!is_slot || *args != '0') /* Rudimentary checks */ |
c906108c SS |
2854 | error ("You must select threads by MID or @SLOTNUMBER"); |
2855 | ||
c5aa993b | 2856 | if (select_thread (inferior_task, mid, is_slot ? 2 : 1) != KERN_SUCCESS) |
c906108c SS |
2857 | return; |
2858 | ||
2859 | if (from_tty) | |
2860 | printf_filtered ("Thread %d selected\n", | |
2861 | is_slot ? map_port_name_to_mid (current_thread, | |
c5aa993b | 2862 | MACH_TYPE_THREAD) : mid); |
c906108c SS |
2863 | } |
2864 | \f | |
fba45db2 | 2865 | thread_trace (mach_port_t thread, boolean_t set) |
c906108c | 2866 | { |
c5aa993b JM |
2867 | int flavor = TRACE_FLAVOR; |
2868 | unsigned int stateCnt = TRACE_FLAVOR_SIZE; | |
2869 | kern_return_t ret; | |
2870 | thread_state_data_t state; | |
c906108c | 2871 | |
c5aa993b | 2872 | if (!MACH_PORT_VALID (thread)) |
c906108c SS |
2873 | { |
2874 | warning ("thread_trace: invalid thread"); | |
2875 | return; | |
2876 | } | |
2877 | ||
2878 | if (must_suspend_thread) | |
2879 | setup_thread (thread, 1); | |
2880 | ||
c5aa993b | 2881 | ret = thread_get_state (thread, flavor, state, &stateCnt); |
c906108c | 2882 | CHK ("thread_trace: error reading thread state", ret); |
c5aa993b | 2883 | |
c906108c SS |
2884 | if (set) |
2885 | { | |
2886 | TRACE_SET (thread, state); | |
2887 | } | |
2888 | else | |
2889 | { | |
c5aa993b | 2890 | if (!TRACE_CLEAR (thread, state)) |
c906108c SS |
2891 | { |
2892 | if (must_suspend_thread) | |
2893 | setup_thread (thread, 0); | |
2894 | return; | |
2895 | } | |
2896 | } | |
2897 | ||
c5aa993b | 2898 | ret = thread_set_state (thread, flavor, state, stateCnt); |
c906108c SS |
2899 | CHK ("thread_trace: error writing thread state", ret); |
2900 | if (must_suspend_thread) | |
2901 | setup_thread (thread, 0); | |
c5aa993b | 2902 | } |
c906108c SS |
2903 | |
2904 | #ifdef FLUSH_INFERIOR_CACHE | |
2905 | ||
2906 | /* When over-writing code on some machines the I-Cache must be flushed | |
2907 | explicitly, because it is not kept coherent by the lazy hardware. | |
2908 | This definitely includes breakpoints, for instance, or else we | |
2909 | end up looping in mysterious Bpt traps */ | |
2910 | ||
fba45db2 | 2911 | flush_inferior_icache (CORE_ADDR pc, int amount) |
c906108c SS |
2912 | { |
2913 | vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH; | |
c5aa993b JM |
2914 | kern_return_t ret; |
2915 | ||
c906108c SS |
2916 | ret = vm_machine_attribute (inferior_task, |
2917 | pc, | |
2918 | amount, | |
2919 | MATTR_CACHE, | |
2920 | &flush); | |
2921 | if (ret != KERN_SUCCESS) | |
2922 | warning ("Error flushing inferior's cache : %s", | |
2923 | mach_error_string (ret)); | |
2924 | } | |
c5aa993b | 2925 | #endif /* FLUSH_INFERIOR_CACHE */ |
c906108c | 2926 | \f |
c5aa993b | 2927 | |
c906108c | 2928 | static |
fba45db2 | 2929 | suspend_all_threads (int from_tty) |
c906108c | 2930 | { |
c5aa993b JM |
2931 | kern_return_t ret; |
2932 | thread_array_t thread_list; | |
2933 | int thread_count, index; | |
2934 | int infoCnt; | |
c906108c SS |
2935 | thread_basic_info_data_t th_info; |
2936 | ||
c5aa993b | 2937 | |
c906108c SS |
2938 | ret = task_threads (inferior_task, &thread_list, &thread_count); |
2939 | if (ret != KERN_SUCCESS) | |
2940 | { | |
2941 | warning ("Could not suspend inferior threads."); | |
2942 | m3_kill_inferior (); | |
2943 | return_to_top_level (RETURN_ERROR); | |
2944 | } | |
c5aa993b | 2945 | |
c906108c SS |
2946 | for (index = 0; index < thread_count; index++) |
2947 | { | |
2948 | int mid; | |
2949 | ||
c5aa993b | 2950 | mid = map_port_name_to_mid (thread_list[index], |
c906108c | 2951 | MACH_TYPE_THREAD); |
c5aa993b JM |
2952 | |
2953 | ret = thread_suspend (thread_list[index]); | |
c906108c SS |
2954 | |
2955 | if (ret != KERN_SUCCESS) | |
2956 | warning ("Error trying to suspend thread %d : %s", | |
2957 | mid, mach_error_string (ret)); | |
2958 | ||
2959 | if (from_tty) | |
2960 | { | |
2961 | infoCnt = THREAD_BASIC_INFO_COUNT; | |
c5aa993b | 2962 | ret = thread_info (thread_list[index], |
c906108c | 2963 | THREAD_BASIC_INFO, |
c5aa993b | 2964 | (thread_info_t) & th_info, |
c906108c SS |
2965 | &infoCnt); |
2966 | CHK ("suspend can't get thread info", ret); | |
c5aa993b | 2967 | |
c906108c SS |
2968 | warning ("Thread %d suspend count is %d", |
2969 | mid, th_info.suspend_count); | |
2970 | } | |
2971 | } | |
2972 | ||
2973 | consume_send_rights (thread_list, thread_count); | |
c5aa993b JM |
2974 | ret = vm_deallocate (mach_task_self (), |
2975 | (vm_address_t) thread_list, | |
2976 | (thread_count * sizeof (int))); | |
c906108c SS |
2977 | CHK ("Error trying to deallocate thread list", ret); |
2978 | } | |
2979 | ||
2980 | void | |
fba45db2 | 2981 | thread_suspend_command (char *args, int from_tty) |
c906108c SS |
2982 | { |
2983 | kern_return_t ret; | |
c5aa993b JM |
2984 | int mid; |
2985 | mach_port_t saved_thread; | |
2986 | int infoCnt; | |
c906108c | 2987 | thread_basic_info_data_t th_info; |
c5aa993b | 2988 | |
c906108c SS |
2989 | MACH_ERROR_NO_INFERIOR; |
2990 | ||
c5aa993b JM |
2991 | if (!strcasecmp (args, "all")) |
2992 | { | |
2993 | suspend_all_threads (from_tty); | |
2994 | return; | |
2995 | } | |
c906108c SS |
2996 | |
2997 | saved_thread = current_thread; | |
2998 | ||
2999 | mid = parse_thread_id (args, 0, 0); | |
3000 | ||
3001 | if (mid < 0) | |
3002 | error ("You can suspend only existing kernel threads with MID or @SLOTNUMBER"); | |
3003 | ||
3004 | if (mid == 0) | |
3005 | mid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD); | |
c5aa993b JM |
3006 | else if (select_thread (inferior_task, mid, 0) != KERN_SUCCESS) |
3007 | { | |
3008 | if (current_thread) | |
3009 | current_thread = saved_thread; | |
3010 | error ("Could not select thread %d", mid); | |
3011 | } | |
c906108c SS |
3012 | |
3013 | ret = thread_suspend (current_thread); | |
3014 | if (ret != KERN_SUCCESS) | |
3015 | warning ("thread_suspend failed : %s", | |
3016 | mach_error_string (ret)); | |
3017 | ||
3018 | infoCnt = THREAD_BASIC_INFO_COUNT; | |
3019 | ret = thread_info (current_thread, | |
3020 | THREAD_BASIC_INFO, | |
c5aa993b | 3021 | (thread_info_t) & th_info, |
c906108c SS |
3022 | &infoCnt); |
3023 | CHK ("suspend can't get thread info", ret); | |
c5aa993b | 3024 | |
c906108c | 3025 | warning ("Thread %d suspend count is %d", mid, th_info.suspend_count); |
c5aa993b | 3026 | |
c906108c SS |
3027 | current_thread = saved_thread; |
3028 | } | |
3029 | ||
fba45db2 | 3030 | resume_all_threads (int from_tty) |
c906108c | 3031 | { |
c5aa993b JM |
3032 | kern_return_t ret; |
3033 | thread_array_t thread_list; | |
3034 | int thread_count, index; | |
3035 | int mid; | |
3036 | int infoCnt; | |
3037 | thread_basic_info_data_t th_info; | |
c906108c | 3038 | |
c5aa993b JM |
3039 | ret = task_threads (inferior_task, &thread_list, &thread_count); |
3040 | if (ret != KERN_SUCCESS) | |
3041 | { | |
3042 | m3_kill_inferior (); | |
3043 | error ("task_threads", mach_error_string (ret)); | |
3044 | } | |
c906108c | 3045 | |
c5aa993b JM |
3046 | for (index = 0; index < thread_count; index++) |
3047 | { | |
3048 | infoCnt = THREAD_BASIC_INFO_COUNT; | |
3049 | ret = thread_info (thread_list[index], | |
3050 | THREAD_BASIC_INFO, | |
3051 | (thread_info_t) & th_info, | |
3052 | &infoCnt); | |
3053 | CHK ("resume_all can't get thread info", ret); | |
c906108c | 3054 | |
c5aa993b JM |
3055 | mid = map_port_name_to_mid (thread_list[index], |
3056 | MACH_TYPE_THREAD); | |
c906108c | 3057 | |
c5aa993b JM |
3058 | if (!th_info.suspend_count) |
3059 | { | |
3060 | if (mid != -1 && from_tty) | |
3061 | warning ("Thread %d is not suspended", mid); | |
3062 | continue; | |
3063 | } | |
c906108c | 3064 | |
c5aa993b JM |
3065 | ret = thread_resume (thread_list[index]); |
3066 | ||
3067 | if (ret != KERN_SUCCESS) | |
3068 | warning ("Error trying to resume thread %d : %s", | |
3069 | mid, mach_error_string (ret)); | |
3070 | else if (mid != -1 && from_tty) | |
3071 | warning ("Thread %d suspend count is %d", | |
3072 | mid, --th_info.suspend_count); | |
3073 | } | |
3074 | ||
3075 | consume_send_rights (thread_list, thread_count); | |
3076 | ret = vm_deallocate (mach_task_self (), | |
3077 | (vm_address_t) thread_list, | |
3078 | (thread_count * sizeof (int))); | |
3079 | CHK ("Error trying to deallocate thread list", ret); | |
c906108c SS |
3080 | } |
3081 | ||
3082 | void | |
fba45db2 | 3083 | thread_resume_command (char *args, int from_tty) |
c906108c SS |
3084 | { |
3085 | int mid; | |
3086 | mach_port_t saved_thread; | |
3087 | kern_return_t ret; | |
3088 | thread_basic_info_data_t th_info; | |
3089 | int infoCnt = THREAD_BASIC_INFO_COUNT; | |
c5aa993b | 3090 | |
c906108c SS |
3091 | MACH_ERROR_NO_INFERIOR; |
3092 | ||
c5aa993b JM |
3093 | if (!strcasecmp (args, "all")) |
3094 | { | |
3095 | resume_all_threads (from_tty); | |
3096 | return; | |
3097 | } | |
c906108c SS |
3098 | |
3099 | saved_thread = current_thread; | |
3100 | ||
3101 | mid = parse_thread_id (args, 0, 0); | |
3102 | ||
3103 | if (mid < 0) | |
3104 | error ("You can resume only existing kernel threads with MID or @SLOTNUMBER"); | |
3105 | ||
3106 | if (mid == 0) | |
3107 | mid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD); | |
c5aa993b JM |
3108 | else if (select_thread (inferior_task, mid, 0) != KERN_SUCCESS) |
3109 | { | |
3110 | if (current_thread) | |
3111 | current_thread = saved_thread; | |
3112 | return_to_top_level (RETURN_ERROR); | |
3113 | } | |
c906108c SS |
3114 | |
3115 | ret = thread_info (current_thread, | |
3116 | THREAD_BASIC_INFO, | |
c5aa993b | 3117 | (thread_info_t) & th_info, |
c906108c SS |
3118 | &infoCnt); |
3119 | CHK ("resume can't get thread info", ret); | |
c5aa993b JM |
3120 | |
3121 | if (!th_info.suspend_count) | |
c906108c SS |
3122 | { |
3123 | warning ("Thread %d is not suspended", mid); | |
3124 | goto out; | |
3125 | } | |
3126 | ||
3127 | ret = thread_resume (current_thread); | |
3128 | if (ret != KERN_SUCCESS) | |
3129 | warning ("thread_resume failed : %s", | |
3130 | mach_error_string (ret)); | |
3131 | else | |
3132 | { | |
3133 | th_info.suspend_count--; | |
3134 | warning ("Thread %d suspend count is %d", mid, th_info.suspend_count); | |
3135 | } | |
c5aa993b JM |
3136 | |
3137 | out: | |
c906108c SS |
3138 | current_thread = saved_thread; |
3139 | } | |
3140 | ||
3141 | void | |
fba45db2 | 3142 | thread_kill_command (char *args, int from_tty) |
c906108c SS |
3143 | { |
3144 | int mid; | |
3145 | kern_return_t ret; | |
3146 | int thread_count; | |
3147 | thread_array_t thread_table; | |
c5aa993b | 3148 | int index; |
c906108c | 3149 | mach_port_t thread_to_kill = MACH_PORT_NULL; |
c5aa993b JM |
3150 | |
3151 | ||
c906108c SS |
3152 | MACH_ERROR_NO_INFERIOR; |
3153 | ||
3154 | if (!args) | |
3155 | error_no_arg ("thread mid to kill from the inferior task"); | |
3156 | ||
3157 | mid = parse_thread_id (args, 0, 0); | |
3158 | ||
3159 | if (mid < 0) | |
3160 | error ("You can kill only existing kernel threads with MID or @SLOTNUMBER"); | |
3161 | ||
3162 | if (mid) | |
3163 | { | |
3164 | ret = machid_mach_port (mid_server, mid_auth, mid, &thread_to_kill); | |
3165 | CHK ("thread_kill_command: machid_mach_port map failed", ret); | |
3166 | } | |
3167 | else | |
3168 | mid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD); | |
c5aa993b | 3169 | |
c906108c SS |
3170 | /* Don't allow gdb to kill *any* thread in the system. Use mkill program for that */ |
3171 | ret = task_threads (inferior_task, &thread_table, &thread_count); | |
3172 | CHK ("Error getting inferior's thread list", ret); | |
c5aa993b | 3173 | |
c906108c SS |
3174 | if (thread_to_kill == current_thread) |
3175 | { | |
3176 | ret = thread_terminate (thread_to_kill); | |
3177 | CHK ("Thread could not be terminated", ret); | |
3178 | ||
3179 | if (select_thread (inferior_task, 0, 1) != KERN_SUCCESS) | |
3180 | warning ("Last thread was killed, use \"kill\" command to kill task"); | |
3181 | } | |
3182 | else | |
3183 | for (index = 0; index < thread_count; index++) | |
c5aa993b | 3184 | if (thread_table[index] == thread_to_kill) |
c906108c SS |
3185 | { |
3186 | ret = thread_terminate (thread_to_kill); | |
3187 | CHK ("Thread could not be terminated", ret); | |
3188 | } | |
3189 | ||
3190 | if (thread_count > 1) | |
3191 | consume_send_rights (thread_table, thread_count); | |
c5aa993b JM |
3192 | |
3193 | ret = vm_deallocate (mach_task_self (), (vm_address_t) thread_table, | |
3194 | (thread_count * sizeof (mach_port_t))); | |
c906108c | 3195 | CHK ("Error trying to deallocate thread list", ret); |
c5aa993b | 3196 | |
c906108c SS |
3197 | warning ("Thread %d killed", mid); |
3198 | } | |
c906108c | 3199 | \f |
c5aa993b | 3200 | |
c906108c SS |
3201 | /* Task specific commands; add more if you like */ |
3202 | ||
3203 | void | |
fba45db2 | 3204 | task_resume_command (char *args, int from_tty) |
c906108c SS |
3205 | { |
3206 | kern_return_t ret; | |
3207 | task_basic_info_data_t ta_info; | |
3208 | int infoCnt = TASK_BASIC_INFO_COUNT; | |
3209 | int mid = map_port_name_to_mid (inferior_task, MACH_TYPE_TASK); | |
c5aa993b | 3210 | |
c906108c SS |
3211 | MACH_ERROR_NO_INFERIOR; |
3212 | ||
3213 | /* Would be trivial to change, but is it desirable? */ | |
3214 | if (args) | |
3215 | error ("Currently gdb can resume only it's inferior task"); | |
3216 | ||
3217 | ret = task_info (inferior_task, | |
3218 | TASK_BASIC_INFO, | |
c5aa993b | 3219 | (task_info_t) & ta_info, |
c906108c SS |
3220 | &infoCnt); |
3221 | CHK ("task_resume_command: task_info failed", ret); | |
c5aa993b | 3222 | |
c906108c SS |
3223 | if (ta_info.suspend_count == 0) |
3224 | error ("Inferior task %d is not suspended", mid); | |
3225 | else if (ta_info.suspend_count == 1 && | |
3226 | from_tty && | |
c5aa993b | 3227 | !query ("Suspend count is now 1. Do you know what you are doing? ")) |
c906108c SS |
3228 | error ("Task not resumed"); |
3229 | ||
3230 | ret = task_resume (inferior_task); | |
3231 | CHK ("task_resume_command: task_resume", ret); | |
3232 | ||
3233 | if (ta_info.suspend_count == 1) | |
3234 | { | |
3235 | warning ("Inferior task %d is no longer suspended", mid); | |
3236 | must_suspend_thread = 1; | |
3237 | /* @@ This is not complete: Registers change all the time when not | |
c5aa993b | 3238 | suspended! */ |
c906108c SS |
3239 | registers_changed (); |
3240 | } | |
3241 | else | |
3242 | warning ("Inferior task %d suspend count is now %d", | |
c5aa993b | 3243 | mid, ta_info.suspend_count - 1); |
c906108c SS |
3244 | } |
3245 | ||
3246 | ||
3247 | void | |
fba45db2 | 3248 | task_suspend_command (char *args, int from_tty) |
c906108c SS |
3249 | { |
3250 | kern_return_t ret; | |
3251 | task_basic_info_data_t ta_info; | |
3252 | int infoCnt = TASK_BASIC_INFO_COUNT; | |
3253 | int mid = map_port_name_to_mid (inferior_task, MACH_TYPE_TASK); | |
c5aa993b | 3254 | |
c906108c SS |
3255 | MACH_ERROR_NO_INFERIOR; |
3256 | ||
3257 | /* Would be trivial to change, but is it desirable? */ | |
3258 | if (args) | |
3259 | error ("Currently gdb can suspend only it's inferior task"); | |
3260 | ||
3261 | ret = task_suspend (inferior_task); | |
3262 | CHK ("task_suspend_command: task_suspend", ret); | |
3263 | ||
3264 | must_suspend_thread = 0; | |
3265 | ||
3266 | ret = task_info (inferior_task, | |
3267 | TASK_BASIC_INFO, | |
c5aa993b | 3268 | (task_info_t) & ta_info, |
c906108c SS |
3269 | &infoCnt); |
3270 | CHK ("task_suspend_command: task_info failed", ret); | |
c5aa993b | 3271 | |
c906108c SS |
3272 | warning ("Inferior task %d suspend count is now %d", |
3273 | mid, ta_info.suspend_count); | |
3274 | } | |
3275 | ||
3276 | static char * | |
fba45db2 | 3277 | get_size (int bytes) |
c906108c | 3278 | { |
c5aa993b JM |
3279 | static char size[30]; |
3280 | int zz = bytes / 1024; | |
c906108c SS |
3281 | |
3282 | if (zz / 1024) | |
c5aa993b | 3283 | sprintf (size, "%-2.1f M", ((float) bytes) / (1024.0 * 1024.0)); |
c906108c SS |
3284 | else |
3285 | sprintf (size, "%d K", zz); | |
3286 | ||
3287 | return size; | |
3288 | } | |
3289 | ||
3290 | /* Does this require the target task to be suspended?? I don't think so. */ | |
3291 | void | |
fba45db2 | 3292 | task_info_command (char *args, int from_tty) |
c906108c SS |
3293 | { |
3294 | int mid = -5; | |
3295 | mach_port_t task; | |
3296 | kern_return_t ret; | |
3297 | task_basic_info_data_t ta_info; | |
3298 | int infoCnt = TASK_BASIC_INFO_COUNT; | |
c5aa993b | 3299 | int page_size = round_page (1); |
c906108c | 3300 | int thread_count = 0; |
c5aa993b | 3301 | |
c906108c SS |
3302 | if (MACH_PORT_VALID (inferior_task)) |
3303 | mid = map_port_name_to_mid (inferior_task, | |
3304 | MACH_TYPE_TASK); | |
3305 | ||
3306 | task = inferior_task; | |
3307 | ||
3308 | if (args) | |
3309 | { | |
3310 | int tmid = atoi (args); | |
3311 | ||
3312 | if (tmid <= 0) | |
3313 | error ("Invalid mid %d for task info", tmid); | |
3314 | ||
3315 | if (tmid != mid) | |
3316 | { | |
3317 | mid = tmid; | |
3318 | ret = machid_mach_port (mid_server, mid_auth, tmid, &task); | |
3319 | CHK ("task_info_command: machid_mach_port map failed", ret); | |
3320 | } | |
3321 | } | |
3322 | ||
3323 | if (mid < 0) | |
3324 | error ("You have to give the task MID as an argument"); | |
3325 | ||
3326 | ret = task_info (task, | |
3327 | TASK_BASIC_INFO, | |
c5aa993b | 3328 | (task_info_t) & ta_info, |
c906108c SS |
3329 | &infoCnt); |
3330 | CHK ("task_info_command: task_info failed", ret); | |
3331 | ||
3332 | printf_filtered ("\nTask info for task %d:\n\n", mid); | |
3333 | printf_filtered (" Suspend count : %d\n", ta_info.suspend_count); | |
3334 | printf_filtered (" Base priority : %d\n", ta_info.base_priority); | |
3335 | printf_filtered (" Virtual size : %s\n", get_size (ta_info.virtual_size)); | |
3336 | printf_filtered (" Resident size : %s\n", get_size (ta_info.resident_size)); | |
3337 | ||
3338 | { | |
3339 | thread_array_t thread_list; | |
c5aa993b | 3340 | |
c906108c SS |
3341 | ret = task_threads (task, &thread_list, &thread_count); |
3342 | CHK ("task_info_command: task_threads", ret); | |
c5aa993b | 3343 | |
c906108c SS |
3344 | printf_filtered (" Thread count : %d\n", thread_count); |
3345 | ||
3346 | consume_send_rights (thread_list, thread_count); | |
c5aa993b JM |
3347 | ret = vm_deallocate (mach_task_self (), |
3348 | (vm_address_t) thread_list, | |
3349 | (thread_count * sizeof (int))); | |
3350 | CHK ("Error trying to deallocate thread list", ret); | |
c906108c SS |
3351 | } |
3352 | if (have_emulator_p (task)) | |
3353 | printf_filtered (" Emulator at : 0x%x..0x%x\n", | |
3354 | EMULATOR_BASE, EMULATOR_END); | |
3355 | else | |
3356 | printf_filtered (" No emulator.\n"); | |
3357 | ||
3358 | if (thread_count && task == inferior_task) | |
3359 | printf_filtered ("\nUse the \"thread list\" command to see the threads\n"); | |
3360 | } | |
3361 | \f | |
3362 | /* You may either FORWARD the exception to the inferior, or KEEP | |
3363 | * it and return to GDB command level. | |
3364 | * | |
3365 | * exception mid [ forward | keep ] | |
3366 | */ | |
3367 | ||
3368 | static void | |
fba45db2 | 3369 | exception_command (char *args, int from_tty) |
c906108c SS |
3370 | { |
3371 | char *scan = args; | |
3372 | int exception; | |
3373 | int len; | |
3374 | ||
3375 | if (!args) | |
3376 | error_no_arg ("exception number action"); | |
3377 | ||
c5aa993b JM |
3378 | while (*scan == ' ' || *scan == '\t') |
3379 | scan++; | |
3380 | ||
c906108c SS |
3381 | if ('0' <= *scan && *scan <= '9') |
3382 | while ('0' <= *scan && *scan <= '9') | |
3383 | scan++; | |
3384 | else | |
3385 | error ("exception number action"); | |
3386 | ||
3387 | exception = atoi (args); | |
3388 | if (exception <= 0 || exception > MAX_EXCEPTION) | |
3389 | error ("Allowed exception numbers are in range 1..%d", | |
3390 | MAX_EXCEPTION); | |
3391 | ||
3392 | if (*scan != ' ' && *scan != '\t') | |
3393 | error ("exception number must be followed by a space"); | |
3394 | else | |
c5aa993b JM |
3395 | while (*scan == ' ' || *scan == '\t') |
3396 | scan++; | |
c906108c SS |
3397 | |
3398 | args = scan; | |
3399 | len = 0; | |
3400 | while (*scan) | |
3401 | { | |
3402 | len++; | |
3403 | scan++; | |
3404 | } | |
3405 | ||
3406 | if (!len) | |
c5aa993b | 3407 | error ("exception number action"); |
c906108c SS |
3408 | |
3409 | if (!strncasecmp (args, "forward", len)) | |
c5aa993b | 3410 | exception_map[exception].forward = TRUE; |
c906108c | 3411 | else if (!strncasecmp (args, "keep", len)) |
c5aa993b | 3412 | exception_map[exception].forward = FALSE; |
c906108c SS |
3413 | else |
3414 | error ("exception action is either \"keep\" or \"forward\""); | |
3415 | } | |
3416 | ||
3417 | static void | |
fba45db2 | 3418 | print_exception_info (int exception) |
c906108c | 3419 | { |
c5aa993b | 3420 | boolean_t forward = exception_map[exception].forward; |
c906108c | 3421 | |
c5aa993b | 3422 | printf_filtered ("%s\t(%d): ", exception_map[exception].name, |
c906108c SS |
3423 | exception); |
3424 | if (!forward) | |
c5aa993b | 3425 | if (exception_map[exception].sigmap != SIG_UNKNOWN) |
c906108c | 3426 | printf_filtered ("keep and handle as signal %d\n", |
c5aa993b | 3427 | exception_map[exception].sigmap); |
c906108c SS |
3428 | else |
3429 | printf_filtered ("keep and handle as unknown signal %d\n", | |
c5aa993b | 3430 | exception_map[exception].sigmap); |
c906108c SS |
3431 | else |
3432 | printf_filtered ("forward exception to inferior\n"); | |
3433 | } | |
3434 | ||
3435 | void | |
fba45db2 | 3436 | exception_info (char *args, int from_tty) |
c906108c SS |
3437 | { |
3438 | int exception; | |
3439 | ||
3440 | if (!args) | |
3441 | for (exception = 1; exception <= MAX_EXCEPTION; exception++) | |
3442 | print_exception_info (exception); | |
3443 | else | |
3444 | { | |
3445 | exception = atoi (args); | |
3446 | ||
3447 | if (exception <= 0 || exception > MAX_EXCEPTION) | |
3448 | error ("Invalid exception number, values from 1 to %d allowed", | |
3449 | MAX_EXCEPTION); | |
3450 | print_exception_info (exception); | |
3451 | } | |
3452 | } | |
3453 | \f | |
3454 | /* Check for actions for mach exceptions. | |
3455 | */ | |
fba45db2 | 3456 | mach3_exception_actions (WAITTYPE *w, boolean_t force_print_only, char *who) |
c906108c SS |
3457 | { |
3458 | boolean_t force_print = FALSE; | |
3459 | ||
c5aa993b | 3460 | |
c906108c SS |
3461 | if (force_print_only || |
3462 | exception_map[stop_exception].sigmap == SIG_UNKNOWN) | |
3463 | force_print = TRUE; | |
3464 | else | |
3465 | WSETSTOP (*w, exception_map[stop_exception].sigmap); | |
3466 | ||
3467 | if (exception_map[stop_exception].print || force_print) | |
3468 | { | |
3469 | target_terminal_ours (); | |
c5aa993b | 3470 | |
c906108c SS |
3471 | printf_filtered ("\n%s received %s exception : ", |
3472 | who, | |
3473 | exception_map[stop_exception].name); | |
c5aa993b | 3474 | |
c906108c SS |
3475 | wrap_here (" "); |
3476 | ||
c5aa993b JM |
3477 | switch (stop_exception) |
3478 | { | |
3479 | case EXC_BAD_ACCESS: | |
3480 | printf_filtered ("referencing address 0x%x : %s\n", | |
3481 | stop_subcode, | |
3482 | mach_error_string (stop_code)); | |
3483 | break; | |
3484 | case EXC_BAD_INSTRUCTION: | |
3485 | printf_filtered | |
3486 | ("illegal or undefined instruction. code %d subcode %d\n", | |
3487 | stop_code, stop_subcode); | |
3488 | break; | |
3489 | case EXC_ARITHMETIC: | |
3490 | printf_filtered ("code %d\n", stop_code); | |
3491 | break; | |
3492 | case EXC_EMULATION: | |
3493 | printf_filtered ("code %d subcode %d\n", stop_code, stop_subcode); | |
3494 | break; | |
3495 | case EXC_SOFTWARE: | |
3496 | printf_filtered ("%s specific, code 0x%x\n", | |
3497 | stop_code < 0xffff ? "hardware" : "os emulation", | |
3498 | stop_code); | |
3499 | break; | |
3500 | case EXC_BREAKPOINT: | |
3501 | printf_filtered ("type %d (machine dependent)\n", | |
3502 | stop_code); | |
3503 | break; | |
3504 | default: | |
96baa820 | 3505 | internal_error ("Unknown exception"); |
c5aa993b | 3506 | } |
c906108c SS |
3507 | } |
3508 | } | |
3509 | \f | |
fba45db2 | 3510 | setup_notify_port (int create_new) |
c906108c SS |
3511 | { |
3512 | kern_return_t ret; | |
3513 | ||
3514 | if (MACH_PORT_VALID (our_notify_port)) | |
3515 | { | |
3516 | ret = mach_port_destroy (mach_task_self (), our_notify_port); | |
3517 | CHK ("Could not destroy our_notify_port", ret); | |
3518 | } | |
3519 | ||
3520 | our_notify_port = MACH_PORT_NULL; | |
c5aa993b | 3521 | notify_chain = (port_chain_t) NULL; |
c906108c SS |
3522 | port_chain_destroy (port_chain_obstack); |
3523 | ||
3524 | if (create_new) | |
3525 | { | |
c5aa993b | 3526 | ret = mach_port_allocate (mach_task_self (), |
c906108c SS |
3527 | MACH_PORT_RIGHT_RECEIVE, |
3528 | &our_notify_port); | |
3529 | if (ret != KERN_SUCCESS) | |
96baa820 | 3530 | internal_error ("Creating notify port %s", mach_error_string (ret)); |
c5aa993b JM |
3531 | |
3532 | ret = mach_port_move_member (mach_task_self (), | |
3533 | our_notify_port, | |
3534 | inferior_wait_port_set); | |
c906108c | 3535 | if (ret != KERN_SUCCESS) |
96baa820 | 3536 | internal_error ("initial move member %s", mach_error_string (ret)); |
c906108c SS |
3537 | } |
3538 | } | |
3539 | ||
3540 | /* | |
3541 | * Register our message port to the net name server | |
3542 | * | |
3543 | * Currently used only by the external stop-gdb program | |
3544 | * since ^C does not work if you would like to enter | |
3545 | * gdb command level while debugging your program. | |
3546 | * | |
3547 | * NOTE: If the message port is sometimes used for other | |
3548 | * purposes also, the NAME must not be a guessable one. | |
3549 | * Then, there should be a way to change it. | |
3550 | */ | |
3551 | ||
c5aa993b | 3552 | char registered_name[MAX_NAME_LEN]; |
c906108c SS |
3553 | |
3554 | void | |
fba45db2 | 3555 | message_port_info (char *args, int from_tty) |
c906108c SS |
3556 | { |
3557 | if (registered_name[0]) | |
3558 | printf_filtered ("gdb's message port name: '%s'\n", | |
3559 | registered_name); | |
3560 | else | |
3561 | printf_filtered ("gdb's message port is not currently registered\n"); | |
3562 | } | |
3563 | ||
3564 | void | |
fba45db2 | 3565 | gdb_register_port (char *name, mach_port_t port) |
c906108c SS |
3566 | { |
3567 | kern_return_t ret; | |
3568 | static int already_signed = 0; | |
3569 | int len; | |
3570 | ||
c5aa993b | 3571 | if (!MACH_PORT_VALID (port) || !name || !*name) |
c906108c SS |
3572 | { |
3573 | warning ("Invalid registration request"); | |
3574 | return; | |
3575 | } | |
3576 | ||
c5aa993b | 3577 | if (!already_signed) |
c906108c SS |
3578 | { |
3579 | ret = mach_port_insert_right (mach_task_self (), | |
3580 | our_message_port, | |
3581 | our_message_port, | |
3582 | MACH_MSG_TYPE_MAKE_SEND); | |
3583 | CHK ("Failed to create a signature to our_message_port", ret); | |
3584 | already_signed = 1; | |
3585 | } | |
3586 | else if (already_signed > 1) | |
3587 | { | |
3588 | ret = netname_check_out (name_server_port, | |
3589 | registered_name, | |
3590 | our_message_port); | |
3591 | CHK ("Failed to check out gdb's message port", ret); | |
3592 | registered_name[0] = '\000'; | |
3593 | already_signed = 1; | |
3594 | } | |
3595 | ||
3596 | ret = netname_check_in (name_server_port, /* Name server port */ | |
c5aa993b | 3597 | name, /* Name of service */ |
c906108c | 3598 | our_message_port, /* Signature */ |
c5aa993b JM |
3599 | port); /* Creates a new send right */ |
3600 | CHK ("Failed to check in the port", ret); | |
3601 | ||
c906108c | 3602 | len = 0; |
c5aa993b | 3603 | while (len < MAX_NAME_LEN && *(name + len)) |
c906108c | 3604 | { |
c5aa993b | 3605 | registered_name[len] = *(name + len); |
c906108c SS |
3606 | len++; |
3607 | } | |
3608 | registered_name[len] = '\000'; | |
3609 | already_signed = 2; | |
c5aa993b | 3610 | } |
c906108c SS |
3611 | |
3612 | struct cmd_list_element *cmd_thread_list; | |
3613 | struct cmd_list_element *cmd_task_list; | |
3614 | ||
c5aa993b | 3615 | /*ARGSUSED */ |
c906108c | 3616 | static void |
fba45db2 | 3617 | thread_command (char *arg, int from_tty) |
c906108c SS |
3618 | { |
3619 | printf_unfiltered ("\"thread\" must be followed by the name of a thread command.\n"); | |
3620 | help_list (cmd_thread_list, "thread ", -1, gdb_stdout); | |
3621 | } | |
3622 | ||
c5aa993b | 3623 | /*ARGSUSED */ |
c906108c | 3624 | static void |
fba45db2 | 3625 | task_command (char *arg, int from_tty) |
c906108c SS |
3626 | { |
3627 | printf_unfiltered ("\"task\" must be followed by the name of a task command.\n"); | |
3628 | help_list (cmd_task_list, "task ", -1, gdb_stdout); | |
3629 | } | |
3630 | ||
fba45db2 | 3631 | add_mach_specific_commands (void) |
c906108c SS |
3632 | { |
3633 | /* Thread handling commands */ | |
3634 | ||
3635 | /* FIXME: Move our thread support into the generic thread.c stuff so we | |
3636 | can share that code. */ | |
3637 | add_prefix_cmd ("mthread", class_stack, thread_command, | |
c5aa993b JM |
3638 | "Generic command for handling Mach threads in the debugged task.", |
3639 | &cmd_thread_list, "thread ", 0, &cmdlist); | |
c906108c SS |
3640 | |
3641 | add_com_alias ("th", "mthread", class_stack, 1); | |
3642 | ||
c5aa993b | 3643 | add_cmd ("select", class_stack, thread_select_command, |
c906108c SS |
3644 | "Select and print MID of the selected thread", |
3645 | &cmd_thread_list); | |
c5aa993b | 3646 | add_cmd ("list", class_stack, thread_list_command, |
c906108c SS |
3647 | "List info of task's threads. Selected thread is marked with '*'", |
3648 | &cmd_thread_list); | |
3649 | add_cmd ("suspend", class_run, thread_suspend_command, | |
3650 | "Suspend one or all of the threads in the selected task.", | |
3651 | &cmd_thread_list); | |
3652 | add_cmd ("resume", class_run, thread_resume_command, | |
3653 | "Resume one or all of the threads in the selected task.", | |
3654 | &cmd_thread_list); | |
3655 | add_cmd ("kill", class_run, thread_kill_command, | |
3656 | "Kill the specified thread MID from inferior task.", | |
3657 | &cmd_thread_list); | |
3658 | #if 0 | |
3659 | /* The rest of this support (condition_thread) was not merged. It probably | |
3660 | should not be merged in this form, but instead added to the generic GDB | |
3661 | thread support. */ | |
3662 | add_cmd ("break", class_breakpoint, condition_thread, | |
3663 | "Breakpoint N will only be effective for thread MID or @SLOT\n\ | |
3664 | If MID/@SLOT is omitted allow all threads to break at breakpoint", | |
3665 | &cmd_thread_list); | |
3666 | #endif | |
3667 | /* Thread command shorthands (for backward compatibility) */ | |
3668 | add_alias_cmd ("ts", "mthread select", 0, 0, &cmdlist); | |
c5aa993b | 3669 | add_alias_cmd ("tl", "mthread list", 0, 0, &cmdlist); |
c906108c SS |
3670 | |
3671 | /* task handling commands */ | |
3672 | ||
3673 | add_prefix_cmd ("task", class_stack, task_command, | |
c5aa993b JM |
3674 | "Generic command for handling debugged task.", |
3675 | &cmd_task_list, "task ", 0, &cmdlist); | |
c906108c SS |
3676 | |
3677 | add_com_alias ("ta", "task", class_stack, 1); | |
3678 | ||
3679 | add_cmd ("suspend", class_run, task_suspend_command, | |
3680 | "Suspend the inferior task.", | |
3681 | &cmd_task_list); | |
3682 | add_cmd ("resume", class_run, task_resume_command, | |
3683 | "Resume the inferior task.", | |
3684 | &cmd_task_list); | |
3685 | add_cmd ("info", no_class, task_info_command, | |
3686 | "Print information about the specified task.", | |
3687 | &cmd_task_list); | |
3688 | ||
3689 | /* Print my message port name */ | |
3690 | ||
3691 | add_info ("message-port", message_port_info, | |
3692 | "Returns the name of gdb's message port in the netnameserver"); | |
3693 | ||
3694 | /* Exception commands */ | |
3695 | ||
3696 | add_info ("exceptions", exception_info, | |
3697 | "What debugger does when program gets various exceptions.\n\ | |
3698 | Specify an exception number as argument to print info on that\n\ | |
3699 | exception only."); | |
3700 | ||
3701 | add_com ("exception", class_run, exception_command, | |
3702 | "Specify how to handle an exception.\n\ | |
3703 | Args are exception number followed by \"forward\" or \"keep\".\n\ | |
3704 | `Forward' means forward the exception to the program's normal exception\n\ | |
3705 | handler.\n\ | |
3706 | `Keep' means reenter debugger if this exception happens, and GDB maps\n\ | |
3707 | the exception to some signal (see info exception)\n\ | |
3708 | Normally \"keep\" is used to return to GDB on exception."); | |
3709 | } | |
3710 | ||
3711 | kern_return_t | |
fba45db2 | 3712 | do_mach_notify_dead_name (mach_port_t notify, mach_port_t name) |
c906108c SS |
3713 | { |
3714 | kern_return_t kr = KERN_SUCCESS; | |
3715 | ||
3716 | /* Find the thing that notified */ | |
3717 | port_chain_t element = port_chain_member (notify_chain, name); | |
3718 | ||
3719 | /* Take name of from unreceived dead name notification list */ | |
3720 | notify_chain = port_chain_delete (notify_chain, name); | |
3721 | ||
c5aa993b | 3722 | if (!element) |
c906108c | 3723 | error ("Received a dead name notify from unchained port (0x%x)", name); |
c906108c | 3724 | |
c5aa993b JM |
3725 | switch (element->type) |
3726 | { | |
c906108c | 3727 | |
c5aa993b JM |
3728 | case MACH_TYPE_THREAD: |
3729 | target_terminal_ours_for_output (); | |
3730 | if (name == current_thread) | |
3731 | { | |
3732 | printf_filtered ("\nCurrent thread %d died", element->mid); | |
3733 | current_thread = MACH_PORT_NULL; | |
3734 | } | |
3735 | else | |
3736 | printf_filtered ("\nThread %d died", element->mid); | |
c906108c | 3737 | |
c5aa993b | 3738 | break; |
c906108c | 3739 | |
c5aa993b JM |
3740 | case MACH_TYPE_TASK: |
3741 | target_terminal_ours_for_output (); | |
3742 | if (name != inferior_task) | |
3743 | printf_filtered ("Task %d died, but it was not the selected task", | |
3744 | element->mid); | |
3745 | else | |
3746 | { | |
3747 | printf_filtered ("Current task %d died", element->mid); | |
c906108c | 3748 | |
c5aa993b JM |
3749 | mach_port_destroy (mach_task_self (), name); |
3750 | inferior_task = MACH_PORT_NULL; | |
3751 | ||
3752 | if (notify_chain) | |
3753 | warning ("There were still unreceived dead_name_notifications???"); | |
3754 | ||
3755 | /* Destroy the old notifications */ | |
3756 | setup_notify_port (0); | |
3757 | ||
3758 | } | |
3759 | break; | |
3760 | ||
3761 | default: | |
3762 | error ("Unregistered dead_name 0x%x notification received. Type is %d, mid is 0x%x", | |
3763 | name, element->type, element->mid); | |
3764 | break; | |
3765 | } | |
c906108c SS |
3766 | |
3767 | return KERN_SUCCESS; | |
3768 | } | |
3769 | ||
3770 | kern_return_t | |
fba45db2 | 3771 | do_mach_notify_msg_accepted (mach_port_t notify, mach_port_t name) |
c906108c SS |
3772 | { |
3773 | warning ("do_mach_notify_msg_accepted : notify %x, name %x", | |
3774 | notify, name); | |
3775 | return KERN_SUCCESS; | |
3776 | } | |
3777 | ||
3778 | kern_return_t | |
fba45db2 | 3779 | do_mach_notify_no_senders (mach_port_t notify, mach_port_mscount_t mscount) |
c906108c SS |
3780 | { |
3781 | warning ("do_mach_notify_no_senders : notify %x, mscount %x", | |
3782 | notify, mscount); | |
3783 | return KERN_SUCCESS; | |
3784 | } | |
3785 | ||
3786 | kern_return_t | |
fba45db2 | 3787 | do_mach_notify_port_deleted (mach_port_t notify, mach_port_t name) |
c906108c SS |
3788 | { |
3789 | warning ("do_mach_notify_port_deleted : notify %x, name %x", | |
3790 | notify, name); | |
3791 | return KERN_SUCCESS; | |
3792 | } | |
3793 | ||
3794 | kern_return_t | |
fba45db2 | 3795 | do_mach_notify_port_destroyed (mach_port_t notify, mach_port_t rights) |
c906108c SS |
3796 | { |
3797 | warning ("do_mach_notify_port_destroyed : notify %x, rights %x", | |
3798 | notify, rights); | |
3799 | return KERN_SUCCESS; | |
3800 | } | |
3801 | ||
3802 | kern_return_t | |
fba45db2 | 3803 | do_mach_notify_send_once (mach_port_t notify) |
c906108c SS |
3804 | { |
3805 | #ifdef DUMP_SYSCALL | |
3806 | /* MANY of these are generated. */ | |
3807 | warning ("do_mach_notify_send_once : notify %x", | |
3808 | notify); | |
3809 | #endif | |
3810 | return KERN_SUCCESS; | |
3811 | } | |
3812 | ||
3813 | /* Kills the inferior. It's gone when you call this */ | |
3814 | static void | |
fba45db2 | 3815 | kill_inferior_fast (void) |
c906108c SS |
3816 | { |
3817 | WAITTYPE w; | |
3818 | ||
3819 | if (inferior_pid == 0 || inferior_pid == 1) | |
3820 | return; | |
3821 | ||
3822 | /* kill() it, since the Unix server does not otherwise notice when | |
3823 | * killed with task_terminate(). | |
3824 | */ | |
3825 | if (inferior_pid > 0) | |
3826 | kill (inferior_pid, SIGKILL); | |
3827 | ||
3828 | /* It's propably terminate already */ | |
3829 | (void) task_terminate (inferior_task); | |
3830 | ||
c5aa993b | 3831 | inferior_task = MACH_PORT_NULL; |
c906108c SS |
3832 | current_thread = MACH_PORT_NULL; |
3833 | ||
3834 | wait3 (&w, WNOHANG, 0); | |
3835 | ||
3836 | setup_notify_port (0); | |
3837 | } | |
3838 | ||
3839 | static void | |
fba45db2 | 3840 | m3_kill_inferior (void) |
c906108c SS |
3841 | { |
3842 | kill_inferior_fast (); | |
3843 | target_mourn_inferior (); | |
3844 | } | |
3845 | ||
3846 | /* Clean up after the inferior dies. */ | |
3847 | ||
3848 | static void | |
fba45db2 | 3849 | m3_mourn_inferior (void) |
c906108c SS |
3850 | { |
3851 | unpush_target (&m3_ops); | |
3852 | generic_mourn_inferior (); | |
3853 | } | |
c906108c | 3854 | \f |
c5aa993b | 3855 | |
c906108c SS |
3856 | /* Fork an inferior process, and start debugging it. */ |
3857 | ||
3858 | static void | |
fba45db2 | 3859 | m3_create_inferior (char *exec_file, char *allargs, char **env) |
c906108c SS |
3860 | { |
3861 | fork_inferior (exec_file, allargs, env, m3_trace_me, m3_trace_him, NULL, NULL); | |
3862 | /* We are at the first instruction we care about. */ | |
3863 | /* Pedal to the metal... */ | |
2acceee2 | 3864 | proceed ((CORE_ADDR) -1, 0, 0); |
c906108c SS |
3865 | } |
3866 | ||
3867 | /* Mark our target-struct as eligible for stray "run" and "attach" | |
3868 | commands. */ | |
3869 | static int | |
fba45db2 | 3870 | m3_can_run (void) |
c906108c SS |
3871 | { |
3872 | return 1; | |
3873 | } | |
3874 | \f | |
3875 | /* Mach 3.0 does not need ptrace for anything | |
3876 | * Make sure nobody uses it on mach. | |
3877 | */ | |
fba45db2 | 3878 | ptrace (int a, int b, int c, int d) |
c906108c SS |
3879 | { |
3880 | error ("Lose, Lose! Somebody called ptrace\n"); | |
3881 | } | |
3882 | ||
3883 | /* Resume execution of the inferior process. | |
3884 | If STEP is nonzero, single-step it. | |
3885 | If SIGNAL is nonzero, give it that signal. */ | |
3886 | ||
3887 | void | |
fba45db2 | 3888 | m3_resume (int pid, int step, enum target_signal signal) |
c906108c | 3889 | { |
c5aa993b | 3890 | kern_return_t ret; |
c906108c SS |
3891 | |
3892 | if (step) | |
3893 | { | |
3894 | thread_basic_info_data_t th_info; | |
c5aa993b JM |
3895 | unsigned int infoCnt = THREAD_BASIC_INFO_COUNT; |
3896 | ||
c906108c SS |
3897 | /* There is no point in single stepping when current_thread |
3898 | * is dead. | |
3899 | */ | |
c5aa993b | 3900 | if (!MACH_PORT_VALID (current_thread)) |
c906108c | 3901 | error ("No thread selected; can not single step"); |
c5aa993b | 3902 | |
c906108c SS |
3903 | /* If current_thread is suspended, tracing it would never return. |
3904 | */ | |
3905 | ret = thread_info (current_thread, | |
3906 | THREAD_BASIC_INFO, | |
c5aa993b | 3907 | (thread_info_t) & th_info, |
c906108c SS |
3908 | &infoCnt); |
3909 | CHK ("child_resume: can't get thread info", ret); | |
c5aa993b | 3910 | |
c906108c SS |
3911 | if (th_info.suspend_count) |
3912 | error ("Can't trace a suspended thread. Use \"thread resume\" command to resume it"); | |
3913 | } | |
3914 | ||
3915 | vm_read_cache_valid = FALSE; | |
3916 | ||
c5aa993b | 3917 | if (signal && inferior_pid > 0) /* Do not signal, if attached by MID */ |
c906108c SS |
3918 | kill (inferior_pid, target_signal_to_host (signal)); |
3919 | ||
3920 | if (step) | |
3921 | { | |
3922 | suspend_all_threads (0); | |
3923 | ||
3924 | setup_single_step (current_thread, TRUE); | |
c5aa993b | 3925 | |
c906108c SS |
3926 | ret = thread_resume (current_thread); |
3927 | CHK ("thread_resume", ret); | |
3928 | } | |
c5aa993b | 3929 | |
c906108c SS |
3930 | ret = task_resume (inferior_task); |
3931 | if (ret == KERN_FAILURE) | |
3932 | warning ("Task was not suspended"); | |
3933 | else | |
3934 | CHK ("Resuming task", ret); | |
c5aa993b | 3935 | |
c906108c | 3936 | /* HACK HACK This is needed by the multiserver system HACK HACK */ |
c5aa993b JM |
3937 | while ((ret = task_resume (inferior_task)) == KERN_SUCCESS) |
3938 | /* make sure it really runs */ ; | |
c906108c SS |
3939 | /* HACK HACK This is needed by the multiserver system HACK HACK */ |
3940 | } | |
3941 | \f | |
3942 | #ifdef ATTACH_DETACH | |
3943 | ||
3944 | /* Start debugging the process with the given task */ | |
3945 | void | |
fba45db2 | 3946 | task_attach (task_t tid) |
c906108c SS |
3947 | { |
3948 | kern_return_t ret; | |
3949 | inferior_task = tid; | |
3950 | ||
3951 | ret = task_suspend (inferior_task); | |
c5aa993b | 3952 | CHK ("task_attach: task_suspend", ret); |
c906108c SS |
3953 | |
3954 | must_suspend_thread = 0; | |
3955 | ||
3956 | setup_notify_port (1); | |
3957 | ||
3958 | request_notify (inferior_task, MACH_NOTIFY_DEAD_NAME, MACH_TYPE_TASK); | |
3959 | ||
3960 | setup_exception_port (); | |
c5aa993b | 3961 | |
c906108c SS |
3962 | emulator_present = have_emulator_p (inferior_task); |
3963 | ||
3964 | attach_flag = 1; | |
3965 | } | |
3966 | ||
3967 | /* Well, we can call error also here and leave the | |
3968 | * target stack inconsistent. Sigh. | |
3969 | * Fix this sometime (the only way to fail here is that | |
3970 | * the task has no threads at all, which is rare, but | |
3971 | * possible; or if the target task has died, which is also | |
3972 | * possible, but unlikely, since it has been suspended. | |
3973 | * (Someone must have killed it)) | |
3974 | */ | |
3975 | void | |
fba45db2 | 3976 | attach_to_thread (void) |
c906108c SS |
3977 | { |
3978 | if (select_thread (inferior_task, 0, 1) != KERN_SUCCESS) | |
3979 | error ("Could not select any threads to attach to"); | |
3980 | } | |
3981 | ||
fba45db2 | 3982 | mid_attach (int mid) |
c906108c | 3983 | { |
c5aa993b | 3984 | kern_return_t ret; |
c906108c | 3985 | |
c5aa993b JM |
3986 | ret = machid_mach_port (mid_server, mid_auth, mid, &inferior_task); |
3987 | CHK ("mid_attach: machid_mach_port", ret); | |
c906108c | 3988 | |
c5aa993b | 3989 | task_attach (inferior_task); |
c906108c | 3990 | |
c5aa993b | 3991 | return mid; |
c906108c SS |
3992 | } |
3993 | ||
3994 | /* | |
3995 | * Start debugging the process whose unix process-id is PID. | |
3996 | * A negative "pid" value is legal and signifies a mach_id not a unix pid. | |
3997 | * | |
3998 | * Prevent (possible unwanted) dangerous operations by enabled users | |
3999 | * like "atta 0" or "atta foo" (equal to the previous :-) and | |
4000 | * "atta pidself". Anyway, the latter is allowed by specifying a MID. | |
4001 | */ | |
4002 | static int | |
fba45db2 | 4003 | m3_do_attach (int pid) |
c906108c SS |
4004 | { |
4005 | kern_return_t ret; | |
4006 | ||
4007 | if (pid == 0) | |
c5aa993b | 4008 | error ("MID=0, Debugging the master unix server does not compute"); |
c906108c SS |
4009 | |
4010 | /* Foo. This assumes gdb has a unix pid */ | |
c5aa993b | 4011 | if (pid == getpid ()) |
c906108c SS |
4012 | error ("I will debug myself only by mid. (Gdb would suspend itself!)"); |
4013 | ||
4014 | if (pid < 0) | |
4015 | { | |
4016 | mid_attach (-(pid)); | |
4017 | ||
4018 | /* inferior_pid will be NEGATIVE! */ | |
4019 | inferior_pid = pid; | |
4020 | ||
4021 | return inferior_pid; | |
4022 | } | |
4023 | ||
4024 | inferior_task = task_by_pid (pid); | |
c5aa993b JM |
4025 | if (!MACH_PORT_VALID (inferior_task)) |
4026 | error ("Cannot map Unix pid %d to Mach task port", pid); | |
c906108c SS |
4027 | |
4028 | task_attach (inferior_task); | |
4029 | ||
4030 | inferior_pid = pid; | |
4031 | ||
4032 | return inferior_pid; | |
4033 | } | |
4034 | ||
4035 | /* Attach to process PID, then initialize for debugging it | |
4036 | and wait for the trace-trap that results from attaching. */ | |
4037 | ||
4038 | static void | |
fba45db2 | 4039 | m3_attach (char *args, int from_tty) |
c906108c SS |
4040 | { |
4041 | char *exec_file; | |
4042 | int pid; | |
4043 | ||
4044 | if (!args) | |
4045 | error_no_arg ("process-id to attach"); | |
4046 | ||
4047 | pid = atoi (args); | |
4048 | ||
c5aa993b | 4049 | if (pid == getpid ()) /* Trying to masturbate? */ |
c906108c SS |
4050 | error ("I refuse to debug myself!"); |
4051 | ||
4052 | if (from_tty) | |
4053 | { | |
4054 | exec_file = (char *) get_exec_file (0); | |
4055 | ||
4056 | if (exec_file) | |
4057 | printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid)); | |
4058 | else | |
4059 | printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid)); | |
4060 | ||
4061 | gdb_flush (gdb_stdout); | |
4062 | } | |
4063 | ||
4064 | m3_do_attach (pid); | |
4065 | inferior_pid = pid; | |
4066 | push_target (&m3_ops); | |
4067 | } | |
4068 | \f | |
4069 | void | |
fba45db2 | 4070 | deallocate_inferior_ports (void) |
c906108c | 4071 | { |
c5aa993b | 4072 | kern_return_t ret; |
c906108c | 4073 | thread_array_t thread_list; |
c5aa993b | 4074 | int thread_count, index; |
c906108c SS |
4075 | |
4076 | if (!MACH_PORT_VALID (inferior_task)) | |
4077 | return; | |
4078 | ||
4079 | ret = task_threads (inferior_task, &thread_list, &thread_count); | |
4080 | if (ret != KERN_SUCCESS) | |
4081 | { | |
4082 | warning ("deallocate_inferior_ports: task_threads", | |
c5aa993b | 4083 | mach_error_string (ret)); |
c906108c SS |
4084 | return; |
4085 | } | |
4086 | ||
4087 | /* Get rid of send rights to task threads */ | |
4088 | for (index = 0; index < thread_count; index++) | |
4089 | { | |
4090 | int rights; | |
4091 | ret = mach_port_get_refs (mach_task_self (), | |
4092 | thread_list[index], | |
4093 | MACH_PORT_RIGHT_SEND, | |
4094 | &rights); | |
c5aa993b | 4095 | CHK ("deallocate_inferior_ports: get refs", ret); |
c906108c SS |
4096 | |
4097 | if (rights > 0) | |
4098 | { | |
4099 | ret = mach_port_mod_refs (mach_task_self (), | |
4100 | thread_list[index], | |
4101 | MACH_PORT_RIGHT_SEND, | |
4102 | -rights); | |
c5aa993b | 4103 | CHK ("deallocate_inferior_ports: mod refs", ret); |
c906108c SS |
4104 | } |
4105 | } | |
4106 | ||
4107 | ret = mach_port_mod_refs (mach_task_self (), | |
4108 | inferior_exception_port, | |
4109 | MACH_PORT_RIGHT_RECEIVE, | |
4110 | -1); | |
4111 | CHK ("deallocate_inferior_ports: cannot get rid of exception port", ret); | |
4112 | ||
4113 | ret = mach_port_deallocate (mach_task_self (), | |
4114 | inferior_task); | |
4115 | CHK ("deallocate_task_port: deallocating inferior_task", ret); | |
4116 | ||
4117 | current_thread = MACH_PORT_NULL; | |
c5aa993b | 4118 | inferior_task = MACH_PORT_NULL; |
c906108c SS |
4119 | } |
4120 | ||
4121 | /* Stop debugging the process whose number is PID | |
4122 | and continue it with signal number SIGNAL. | |
4123 | SIGNAL = 0 means just continue it. */ | |
4124 | ||
4125 | static void | |
fba45db2 | 4126 | m3_do_detach (int signal) |
c906108c SS |
4127 | { |
4128 | kern_return_t ret; | |
4129 | ||
4130 | MACH_ERROR_NO_INFERIOR; | |
4131 | ||
4132 | if (current_thread != MACH_PORT_NULL) | |
4133 | { | |
4134 | /* Store the gdb's view of the thread we are deselecting | |
4135 | * before we detach. | |
4136 | * @@ I am really not sure if this is ever needeed. | |
4137 | */ | |
4138 | target_prepare_to_store (); | |
4139 | target_store_registers (-1); | |
4140 | } | |
4141 | ||
4142 | ret = task_set_special_port (inferior_task, | |
c5aa993b | 4143 | TASK_EXCEPTION_PORT, |
c906108c SS |
4144 | inferior_old_exception_port); |
4145 | CHK ("task_set_special_port", ret); | |
4146 | ||
4147 | /* Discard all requested notifications */ | |
4148 | setup_notify_port (0); | |
4149 | ||
4150 | if (remove_breakpoints ()) | |
4151 | warning ("Could not remove breakpoints when detaching"); | |
c5aa993b | 4152 | |
c906108c SS |
4153 | if (signal && inferior_pid > 0) |
4154 | kill (inferior_pid, signal); | |
c5aa993b | 4155 | |
c906108c SS |
4156 | /* the task might be dead by now */ |
4157 | (void) task_resume (inferior_task); | |
c5aa993b | 4158 | |
c906108c | 4159 | deallocate_inferior_ports (); |
c5aa993b | 4160 | |
c906108c SS |
4161 | attach_flag = 0; |
4162 | } | |
4163 | ||
4164 | /* Take a program previously attached to and detaches it. | |
4165 | The program resumes execution and will no longer stop | |
4166 | on signals, etc. We'd better not have left any breakpoints | |
4167 | in the program or it'll die when it hits one. For this | |
4168 | to work, it may be necessary for the process to have been | |
4169 | previously attached. It *might* work if the program was | |
4170 | started via fork. */ | |
4171 | ||
4172 | static void | |
fba45db2 | 4173 | m3_detach (char *args, int from_tty) |
c906108c SS |
4174 | { |
4175 | int siggnal = 0; | |
4176 | ||
4177 | if (from_tty) | |
4178 | { | |
4179 | char *exec_file = get_exec_file (0); | |
4180 | if (exec_file == 0) | |
4181 | exec_file = ""; | |
4182 | printf_unfiltered ("Detaching from program: %s %s\n", | |
c5aa993b | 4183 | exec_file, target_pid_to_str (inferior_pid)); |
c906108c SS |
4184 | gdb_flush (gdb_stdout); |
4185 | } | |
4186 | if (args) | |
4187 | siggnal = atoi (args); | |
c5aa993b | 4188 | |
c906108c SS |
4189 | m3_do_detach (siggnal); |
4190 | inferior_pid = 0; | |
c5aa993b | 4191 | unpush_target (&m3_ops); /* Pop out of handling an inferior */ |
c906108c SS |
4192 | } |
4193 | #endif /* ATTACH_DETACH */ | |
4194 | ||
4195 | /* Get ready to modify the registers array. On machines which store | |
4196 | individual registers, this doesn't need to do anything. On machines | |
4197 | which store all the registers in one fell swoop, this makes sure | |
4198 | that registers contains all the registers from the program being | |
4199 | debugged. */ | |
4200 | ||
4201 | static void | |
fba45db2 | 4202 | m3_prepare_to_store (void) |
c906108c SS |
4203 | { |
4204 | #ifdef CHILD_PREPARE_TO_STORE | |
4205 | CHILD_PREPARE_TO_STORE (); | |
4206 | #endif | |
4207 | } | |
4208 | ||
4209 | /* Print status information about what we're accessing. */ | |
4210 | ||
4211 | static void | |
fba45db2 | 4212 | m3_files_info (struct target_ops *ignore) |
c906108c SS |
4213 | { |
4214 | /* FIXME: should print MID and all that crap. */ | |
4215 | printf_unfiltered ("\tUsing the running image of %s %s.\n", | |
c5aa993b | 4216 | attach_flag ? "attached" : "child", target_pid_to_str (inferior_pid)); |
c906108c SS |
4217 | } |
4218 | ||
4219 | static void | |
fba45db2 | 4220 | m3_open (char *arg, int from_tty) |
c906108c SS |
4221 | { |
4222 | error ("Use the \"run\" command to start a Unix child process."); | |
4223 | } | |
4224 | ||
4225 | #ifdef DUMP_SYSCALL | |
4226 | #ifdef __STDC__ | |
4227 | #define STR(x) #x | |
4228 | #else | |
4229 | #define STR(x) "x" | |
4230 | #endif | |
4231 | ||
c5aa993b JM |
4232 | char *bsd1_names[] = |
4233 | { | |
c906108c SS |
4234 | "execve", |
4235 | "fork", | |
4236 | "take_signal", | |
4237 | "sigreturn", | |
4238 | "getrusage", | |
4239 | "chdir", | |
4240 | "chroot", | |
4241 | "open", | |
4242 | "creat", | |
4243 | "mknod", | |
4244 | "link", | |
4245 | "symlink", | |
4246 | "unlink", | |
4247 | "access", | |
4248 | "stat", | |
4249 | "readlink", | |
4250 | "chmod", | |
4251 | "chown", | |
4252 | "utimes", | |
4253 | "truncate", | |
4254 | "rename", | |
4255 | "mkdir", | |
4256 | "rmdir", | |
4257 | "xutimes", | |
4258 | "mount", | |
4259 | "umount", | |
4260 | "acct", | |
4261 | "setquota", | |
4262 | "write_short", | |
4263 | "write_long", | |
4264 | "send_short", | |
4265 | "send_long", | |
4266 | "sendto_short", | |
4267 | "sendto_long", | |
4268 | "select", | |
4269 | "task_by_pid", | |
4270 | "recvfrom_short", | |
4271 | "recvfrom_long", | |
4272 | "setgroups", | |
4273 | "setrlimit", | |
4274 | "sigvec", | |
4275 | "sigstack", | |
4276 | "settimeofday", | |
4277 | "adjtime", | |
4278 | "setitimer", | |
4279 | "sethostname", | |
4280 | "bind", | |
4281 | "accept", | |
4282 | "connect", | |
4283 | "setsockopt", | |
4284 | "getsockopt", | |
4285 | "getsockname", | |
4286 | "getpeername", | |
4287 | "init_process", | |
4288 | "table_set", | |
4289 | "table_get", | |
4290 | "pioctl", | |
4291 | "emulator_error", | |
4292 | "readwrite", | |
4293 | "share_wakeup", | |
4294 | 0, | |
4295 | "maprw_request_it", | |
4296 | "maprw_release_it", | |
4297 | "maprw_remap", | |
4298 | "pid_by_task", | |
4299 | }; | |
4300 | ||
c5aa993b | 4301 | int bsd1_nnames = sizeof (bsd1_names) / sizeof (bsd1_names[0]); |
c906108c | 4302 | |
c5aa993b | 4303 | char * |
fba45db2 | 4304 | name_str (int name, char *buf) |
c906108c | 4305 | { |
c5aa993b JM |
4306 | switch (name) |
4307 | { | |
4308 | case MACH_MSG_TYPE_BOOLEAN: | |
4309 | return "boolean"; | |
4310 | case MACH_MSG_TYPE_INTEGER_16: | |
4311 | return "short"; | |
4312 | case MACH_MSG_TYPE_INTEGER_32: | |
4313 | return "long"; | |
4314 | case MACH_MSG_TYPE_CHAR: | |
4315 | return "char"; | |
4316 | case MACH_MSG_TYPE_BYTE: | |
4317 | return "byte"; | |
4318 | case MACH_MSG_TYPE_REAL: | |
4319 | return "real"; | |
4320 | case MACH_MSG_TYPE_STRING: | |
4321 | return "string"; | |
4322 | default: | |
4323 | sprintf (buf, "%d", name); | |
4324 | return buf; | |
4325 | } | |
c906108c SS |
4326 | } |
4327 | ||
4328 | char * | |
fba45db2 | 4329 | id_str (int id, char *buf) |
c906108c | 4330 | { |
c5aa993b JM |
4331 | char *p; |
4332 | if (id >= 101000 && id < 101000 + bsd1_nnames) | |
4333 | { | |
4334 | if (p = bsd1_names[id - 101000]) | |
4335 | return p; | |
4336 | } | |
c906108c SS |
4337 | if (id == 102000) |
4338 | return "psignal_retry"; | |
4339 | if (id == 100000) | |
4340 | return "syscall"; | |
c5aa993b | 4341 | sprintf (buf, "%d", id); |
c906108c SS |
4342 | return buf; |
4343 | } | |
4344 | ||
fba45db2 | 4345 | print_msg (mach_msg_header_t *mp) |
c906108c | 4346 | { |
c5aa993b JM |
4347 | char *fmt_x = "%20s : 0x%08x\n"; |
4348 | char *fmt_d = "%20s : %10d\n"; | |
4349 | char *fmt_s = "%20s : %s\n"; | |
4350 | char buf[100]; | |
c906108c SS |
4351 | |
4352 | puts_filtered ("\n"); | |
4353 | #define pr(fmt,h,x) printf_filtered(fmt,STR(x),(h).x) | |
c5aa993b JM |
4354 | pr (fmt_x, (*mp), msgh_bits); |
4355 | pr (fmt_d, (*mp), msgh_size); | |
4356 | pr (fmt_x, (*mp), msgh_remote_port); | |
4357 | pr (fmt_x, (*mp), msgh_local_port); | |
4358 | pr (fmt_d, (*mp), msgh_kind); | |
4359 | printf_filtered (fmt_s, STR (msgh_id), id_str (mp->msgh_id, buf)); | |
4360 | ||
c906108c | 4361 | if (debug_level > 1) |
c5aa993b JM |
4362 | { |
4363 | char *p, *ep, *dp; | |
4364 | int plen; | |
4365 | p = (char *) mp; | |
4366 | ep = p + mp->msgh_size; | |
4367 | p += sizeof (*mp); | |
4368 | for (; p < ep; p += plen) | |
4369 | { | |
4370 | mach_msg_type_t *tp; | |
4371 | mach_msg_type_long_t *tlp; | |
4372 | int name, size, number; | |
4373 | tp = (mach_msg_type_t *) p; | |
4374 | if (tp->msgt_longform) | |
4375 | { | |
4376 | tlp = (mach_msg_type_long_t *) tp; | |
4377 | name = tlp->msgtl_name; | |
4378 | size = tlp->msgtl_size; | |
4379 | number = tlp->msgtl_number; | |
4380 | plen = sizeof (*tlp); | |
4381 | } | |
4382 | else | |
4383 | { | |
4384 | name = tp->msgt_name; | |
4385 | size = tp->msgt_size; | |
4386 | number = tp->msgt_number; | |
4387 | plen = sizeof (*tp); | |
4388 | } | |
4389 | printf_filtered ("name=%-16s size=%2d number=%7d inline=%d long=%d deal=%d\n", | |
4390 | name_str (name, buf), size, number, tp->msgt_inline, | |
4391 | tp->msgt_longform, tp->msgt_deallocate); | |
4392 | dp = p + plen; | |
4393 | if (tp->msgt_inline) | |
4394 | { | |
4395 | int l; | |
4396 | l = size * number / 8; | |
4397 | l = (l + sizeof (long) - 1) & ~((sizeof (long)) - 1); | |
4398 | plen += l; | |
4399 | print_data (dp, size, number); | |
4400 | } | |
4401 | else | |
4402 | { | |
4403 | plen += sizeof (int *); | |
4404 | } | |
4405 | printf_filtered ("plen=%d\n", plen); | |
4406 | } | |
c906108c | 4407 | } |
c906108c SS |
4408 | } |
4409 | ||
fba45db2 | 4410 | print_data (char *p, int size, int number) |
c906108c | 4411 | { |
c5aa993b JM |
4412 | int *ip; |
4413 | short *sp; | |
4414 | int i; | |
4415 | ||
4416 | switch (size) | |
4417 | { | |
4418 | case 8: | |
4419 | for (i = 0; i < number; i++) | |
4420 | { | |
4421 | printf_filtered (" %02x", p[i]); | |
4422 | } | |
4423 | break; | |
4424 | case 16: | |
4425 | sp = (short *) p; | |
4426 | for (i = 0; i < number; i++) | |
4427 | { | |
4428 | printf_filtered (" %04x", sp[i]); | |
4429 | } | |
4430 | break; | |
4431 | case 32: | |
4432 | ip = (int *) p; | |
4433 | for (i = 0; i < number; i++) | |
4434 | { | |
4435 | printf_filtered (" %08x", ip[i]); | |
4436 | } | |
4437 | break; | |
c906108c | 4438 | } |
c5aa993b | 4439 | puts_filtered ("\n"); |
c906108c | 4440 | } |
c5aa993b | 4441 | #endif /* DUMP_SYSCALL */ |
c906108c SS |
4442 | |
4443 | static void | |
fba45db2 | 4444 | m3_stop (void) |
c906108c SS |
4445 | { |
4446 | error ("to_stop target function not implemented"); | |
4447 | } | |
4448 | ||
4449 | static char * | |
fba45db2 | 4450 | m3_pid_to_exec_file (int pid) |
c906108c SS |
4451 | { |
4452 | error ("to_pid_to_exec_file target function not implemented"); | |
c5aa993b | 4453 | return NULL; /* To keep all compilers happy. */ |
c906108c SS |
4454 | } |
4455 | ||
4456 | static void | |
fba45db2 | 4457 | init_m3_ops (void) |
c906108c SS |
4458 | { |
4459 | m3_ops.to_shortname = "mach"; | |
4460 | m3_ops.to_longname = "Mach child process"; | |
4461 | m3_ops.to_doc = "Mach child process (started by the \"run\" command)."; | |
4462 | m3_ops.to_open = m3_open; | |
4463 | m3_ops.to_attach = m3_attach; | |
4464 | m3_ops.to_detach = m3_detach; | |
4465 | m3_ops.to_resume = m3_resume; | |
4466 | m3_ops.to_wait = mach_really__wait; | |
4467 | m3_ops.to_fetch_registers = fetch_inferior_registers; | |
4468 | m3_ops.to_store_registers = store_inferior_registers; | |
4469 | m3_ops.to_prepare_to_store = m3_prepare_to_store; | |
4470 | m3_ops.to_xfer_memory = m3_xfer_memory; | |
4471 | m3_ops.to_files_info = m3_files_info; | |
4472 | m3_ops.to_insert_breakpoint = memory_insert_breakpoint; | |
4473 | m3_ops.to_remove_breakpoint = memory_remove_breakpoint; | |
4474 | m3_ops.to_terminal_init = terminal_init_inferior; | |
4475 | m3_ops.to_terminal_inferior = terminal_inferior; | |
4476 | m3_ops.to_terminal_ours_for_output = terminal_ours_for_output; | |
4477 | m3_ops.to_terminal_ours = terminal_ours; | |
4478 | m3_ops.to_terminal_info = child_terminal_info; | |
4479 | m3_ops.to_kill = m3_kill_inferior; | |
4480 | m3_ops.to_create_inferior = m3_create_inferior; | |
4481 | m3_ops.to_mourn_inferior = m3_mourn_inferior; | |
4482 | m3_ops.to_can_run = m3_can_run; | |
4483 | m3_ops.to_stop = m3_stop; | |
4484 | m3_ops.to_pid_to_exec_file = m3_pid_to_exec_file; | |
4485 | m3_ops.to_stratum = process_stratum; | |
4486 | m3_ops.to_has_all_memory = 1; | |
4487 | m3_ops.to_has_memory = 1; | |
4488 | m3_ops.to_has_stack = 1; | |
4489 | m3_ops.to_has_registers = 1; | |
4490 | m3_ops.to_has_execution = 1; | |
4491 | m3_ops.to_magic = OPS_MAGIC; | |
4492 | } | |
4493 | ||
4494 | void | |
fba45db2 | 4495 | _initialize_m3_nat (void) |
c906108c SS |
4496 | { |
4497 | kern_return_t ret; | |
4498 | ||
4499 | init_m3_ops (); | |
4500 | add_target (&m3_ops); | |
4501 | ||
c5aa993b JM |
4502 | ret = mach_port_allocate (mach_task_self (), |
4503 | MACH_PORT_RIGHT_PORT_SET, | |
4504 | &inferior_wait_port_set); | |
c906108c | 4505 | if (ret != KERN_SUCCESS) |
96baa820 | 4506 | internal_error ("initial port set %s", mach_error_string (ret)); |
c906108c SS |
4507 | |
4508 | /* mach_really_wait now waits for this */ | |
4509 | currently_waiting_for = inferior_wait_port_set; | |
4510 | ||
c5aa993b | 4511 | ret = netname_look_up (name_server_port, hostname, "MachID", &mid_server); |
c906108c SS |
4512 | if (ret != KERN_SUCCESS) |
4513 | { | |
4514 | mid_server = MACH_PORT_NULL; | |
c5aa993b | 4515 | |
c906108c | 4516 | warning ("initialize machid: netname_lookup_up(MachID) : %s", |
c5aa993b | 4517 | mach_error_string (ret)); |
c906108c SS |
4518 | warning ("Some (most?) features disabled..."); |
4519 | } | |
c5aa993b JM |
4520 | |
4521 | mid_auth = mach_privileged_host_port (); | |
c906108c | 4522 | if (mid_auth == MACH_PORT_NULL) |
c5aa993b JM |
4523 | mid_auth = mach_task_self (); |
4524 | ||
c906108c SS |
4525 | obstack_init (port_chain_obstack); |
4526 | ||
c5aa993b | 4527 | ret = mach_port_allocate (mach_task_self (), |
c906108c SS |
4528 | MACH_PORT_RIGHT_RECEIVE, |
4529 | &thread_exception_port); | |
4530 | CHK ("Creating thread_exception_port for single stepping", ret); | |
c5aa993b | 4531 | |
c906108c SS |
4532 | ret = mach_port_insert_right (mach_task_self (), |
4533 | thread_exception_port, | |
4534 | thread_exception_port, | |
4535 | MACH_MSG_TYPE_MAKE_SEND); | |
4536 | CHK ("Inserting send right to thread_exception_port", ret); | |
4537 | ||
4538 | /* Allocate message port */ | |
4539 | ret = mach_port_allocate (mach_task_self (), | |
4540 | MACH_PORT_RIGHT_RECEIVE, | |
4541 | &our_message_port); | |
4542 | if (ret != KERN_SUCCESS) | |
4543 | warning ("Creating message port %s", mach_error_string (ret)); | |
4544 | else | |
4545 | { | |
c5aa993b JM |
4546 | char buf[MAX_NAME_LEN]; |
4547 | ret = mach_port_move_member (mach_task_self (), | |
4548 | our_message_port, | |
4549 | inferior_wait_port_set); | |
c906108c SS |
4550 | if (ret != KERN_SUCCESS) |
4551 | warning ("message move member %s", mach_error_string (ret)); | |
4552 | ||
4553 | ||
4554 | /* @@@@ No way to change message port name currently */ | |
4555 | /* Foo. This assumes gdb has a unix pid */ | |
4556 | sprintf (buf, "gdb-%d", getpid ()); | |
4557 | gdb_register_port (buf, our_message_port); | |
4558 | } | |
c5aa993b | 4559 | |
c906108c SS |
4560 | /* Heap for thread commands */ |
4561 | obstack_init (cproc_obstack); | |
4562 | ||
4563 | add_mach_specific_commands (); | |
4564 | } |