1 /* Interface GDB to Mach 3.0 operating systems.
2 (Most) Mach 3.0 related routines live in this file.
4 Copyright (C) 1992, 1996 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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, Boston, MA 02111-1307, USA. */
25 * Helsinki University of Technology
28 * Thanks to my friends who helped with ideas and testing:
30 * Johannes Helander, Antti Louko, Tero Mononen,
33 * Tero Kivinen and Eamonn McManus
41 #include <servers/netname.h>
42 #include <servers/machid.h>
43 #include <mach/message.h>
44 #include <mach/notify.h>
45 #include <mach_error.h>
46 #include <mach/exception.h>
47 #include <mach/vm_attributes.h>
60 #include <servers/machid_lib.h>
62 #define MACH_TYPE_TASK 1
63 #define MACH_TYPE_THREAD 2
66 /* Included only for signal names and NSIG
68 * note: There are many problems in signal handling with
69 * gdb in Mach 3.0 in general.
72 #define SIG_UNKNOWN 0 /* Exception that has no matching unix signal */
76 /* This is what a cproc looks like. This is here partly because
77 cthread_internals.h is not a header we can just #include, partly with
78 an eye towards perhaps getting this to work with cross-debugging
79 someday. Best solution is if CMU publishes a real interface to this
81 #define CPROC_NEXT_OFFSET 0
82 #define CPROC_NEXT_SIZE (TARGET_PTR_BIT / HOST_CHAR_BIT)
83 #define CPROC_INCARNATION_OFFSET (CPROC_NEXT_OFFSET + CPROC_NEXT_SIZE)
84 #define CPROC_INCARNATION_SIZE (sizeof (cthread_t))
85 #define CPROC_LIST_OFFSET (CPROC_INCARNATION_OFFSET + CPROC_INCARNATION_SIZE)
86 #define CPROC_LIST_SIZE (TARGET_PTR_BIT / HOST_CHAR_BIT)
87 #define CPROC_WAIT_OFFSET (CPROC_LIST_OFFSET + CPROC_LIST_SIZE)
88 #define CPROC_WAIT_SIZE (TARGET_PTR_BIT / HOST_CHAR_BIT)
89 #define CPROC_REPLY_OFFSET (CPROC_WAIT_OFFSET + CPROC_WAIT_SIZE)
90 #define CPROC_REPLY_SIZE (sizeof (mach_port_t))
91 #define CPROC_CONTEXT_OFFSET (CPROC_REPLY_OFFSET + CPROC_REPLY_SIZE)
92 #define CPROC_CONTEXT_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT)
93 #define CPROC_LOCK_OFFSET (CPROC_CONTEXT_OFFSET + CPROC_CONTEXT_SIZE)
94 #define CPROC_LOCK_SIZE (sizeof (spin_lock_t))
95 #define CPROC_STATE_OFFSET (CPROC_LOCK_OFFSET + CPROC_LOCK_SIZE)
96 #define CPROC_STATE_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT)
97 #define CPROC_WIRED_OFFSET (CPROC_STATE_OFFSET + CPROC_STATE_SIZE)
98 #define CPROC_WIRED_SIZE (sizeof (mach_port_t))
99 #define CPROC_BUSY_OFFSET (CPROC_WIRED_OFFSET + CPROC_WIRED_SIZE)
100 #define CPROC_BUSY_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT)
101 #define CPROC_MSG_OFFSET (CPROC_BUSY_OFFSET + CPROC_BUSY_SIZE)
102 #define CPROC_MSG_SIZE (sizeof (mach_msg_header_t))
103 #define CPROC_BASE_OFFSET (CPROC_MSG_OFFSET + CPROC_MSG_SIZE)
104 #define CPROC_BASE_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT)
105 #define CPROC_SIZE_OFFSET (CPROC_BASE_OFFSET + CPROC_BASE_SIZE)
106 #define CPROC_SIZE_SIZE (TARGET_INT_BIT / HOST_CHAR_BIT)
107 #define CPROC_SIZE (CPROC_SIZE_OFFSET + CPROC_SIZE_SIZE)
109 /* Values for the state field in the cproc. */
110 #define CPROC_RUNNING 0
111 #define CPROC_SWITCHING 1
112 #define CPROC_BLOCKED 2
113 #define CPROC_CONDWAIT 4
115 /* For cproc and kernel thread mapping */
116 typedef struct gdb_thread {
121 boolean_t in_emulator;
124 /* This is for the mthreads list. It points to the cproc list.
125 Perhaps the two lists should be merged (or perhaps it was a mistake
126 to make them both use a struct gdb_thread). */
127 struct gdb_thread *cproc;
129 /* These are for the cproc list, which is linked through the next field
130 of the struct gdb_thread. */
131 char raw_cproc[CPROC_SIZE];
132 /* The cthread which is pointed to by the incarnation field from the
133 cproc. This points to the copy we've read into GDB. */
135 /* Point back to the mthreads list. */
137 struct gdb_thread *next;
141 * Actions for Mach exceptions.
143 * sigmap field maps the exception to corresponding Unix signal.
145 * I do not know how to map the exception to unix signal
146 * if SIG_UNKNOWN is specified.
149 struct exception_list {
154 } exception_map[] = {
155 {"not_mach3_exception", FALSE, TRUE, SIG_UNKNOWN},
156 {"EXC_BAD_ACCESS", FALSE, TRUE, SIGSEGV},
157 {"EXC_BAD_INSTRUCTION", FALSE, TRUE, SIGILL},
158 {"EXC_ARITHMETIC", FALSE, TRUE, SIGFPE},
159 {"EXC_EMULATION", FALSE, TRUE, SIGEMT}, /* ??? */
160 {"EXC_SOFTWARE", FALSE, TRUE, SIG_UNKNOWN},
161 {"EXC_BREAKPOINT", FALSE, FALSE, SIGTRAP}
164 /* Mach exception table size */
165 int max_exception = sizeof(exception_map)/sizeof(struct exception_list) - 1;
167 #define MAX_EXCEPTION max_exception
169 WAITTYPE wait_status;
171 /* If you define this, intercepted bsd server calls will be
172 * dumped while waiting the inferior to EXEC the correct
175 /* #define DUMP_SYSCALL /* debugging interceptor */
177 /* xx_debug() outputs messages if this is nonzero.
178 * If > 1, DUMP_SYSCALL will dump message contents.
182 /* "Temporary" debug stuff */
184 xx_debug (fmt, a,b,c)
189 warning (fmt, a, b, c);
192 /* This is in libmach.a */
193 extern mach_port_t name_server_port;
195 /* Set in catch_exception_raise */
196 int stop_exception, stop_code, stop_subcode;
197 int stopped_in_exception;
199 /* Thread that was the active thread when we stopped */
200 thread_t stop_thread = MACH_PORT_NULL;
204 /* Set when task is attached or created */
205 boolean_t emulator_present = FALSE;
207 task_t inferior_task;
208 thread_t current_thread;
210 /* Exception ports for inferior task */
211 mach_port_t inferior_exception_port = MACH_PORT_NULL;
212 mach_port_t inferior_old_exception_port = MACH_PORT_NULL;
214 /* task exceptions and notifications */
215 mach_port_t inferior_wait_port_set = MACH_PORT_NULL;
216 mach_port_t our_notify_port = MACH_PORT_NULL;
218 /* This is "inferior_wait_port_set" when not single stepping, and
219 * "singlestepped_thread_port" when we are single stepping.
221 * This is protected by a cleanup function: discard_single_step()
223 mach_port_t currently_waiting_for = MACH_PORT_NULL;
225 /* A port for external messages to gdb.
226 * External in the meaning that they do not come
227 * from the inferior_task, but rather from external
230 * As a debugging feature:
231 * A debugger debugging another debugger can stop the
232 * inferior debugger by the following command sequence
233 * (without running external programs)
235 * (top-gdb) set stop_inferior_gdb ()
238 mach_port_t our_message_port = MACH_PORT_NULL;
240 /* For single stepping */
241 mach_port_t thread_exception_port = MACH_PORT_NULL;
242 mach_port_t thread_saved_exception_port = MACH_PORT_NULL;
243 mach_port_t singlestepped_thread_port = MACH_PORT_NULL;
245 /* For machid calls */
246 mach_port_t mid_server = MACH_PORT_NULL;
247 mach_port_t mid_auth = MACH_PORT_NULL;
249 /* If gdb thinks the inferior task is not suspended, it
250 * must take suspend/abort the threads when it reads the state.
252 int must_suspend_thread = 0;
254 /* When single stepping, we switch the port that mach_really_wait() listens to.
255 * This cleanup is a guard to prevent the port set from being left to
256 * the singlestepped_thread_port when error() is called.
257 * This is nonzero only when we are single stepping.
259 #define NULL_CLEANUP (struct cleanup *)0
260 struct cleanup *cleanup_step = NULL_CLEANUP;
263 extern struct target_ops m3_ops;
264 static void m3_kill_inferior ();
267 #define MACH_TYPE_EXCEPTION_PORT -1
270 /* Chain of ports to remember requested notifications. */
273 struct port_chain *next;
276 int mid; /* Now only valid with MACH_TYPE_THREAD and */
277 /* MACH_TYPE_THREAD */
279 typedef struct port_chain *port_chain_t;
281 /* Room for chain nodes comes from pchain_obstack */
282 struct obstack pchain_obstack;
283 struct obstack *port_chain_obstack = &pchain_obstack;
285 /* For thread handling */
286 struct obstack Cproc_obstack;
287 struct obstack *cproc_obstack = &Cproc_obstack;
289 /* the list of notified ports */
290 port_chain_t notify_chain = (port_chain_t) NULL;
293 port_chain_insert (list, name, type)
302 if (! MACH_PORT_VALID (name))
305 if (type == MACH_TYPE_TASK || type == MACH_TYPE_THREAD)
307 if (! MACH_PORT_VALID (mid_server))
309 warning ("Machid server port invalid, can not map port 0x%x to MID",
315 ret = machid_mach_register (mid_server, mid_auth, name, type, &mid);
317 if (ret != KERN_SUCCESS)
319 warning ("Can not map name (0x%x) to MID with machid", name);
327 new = (port_chain_t) obstack_alloc (port_chain_obstack,
328 sizeof (struct port_chain));
338 port_chain_delete (list, elem)
343 if (list->port == elem)
348 if (list->next->port == elem)
349 list->next = list->next->next; /* GCd with obstack_free() */
357 port_chain_destroy (ostack)
358 struct obstack *ostack;
360 obstack_free (ostack, 0);
361 obstack_init (ostack);
365 port_chain_member (list, elem)
371 if (list->port == elem)
375 return (port_chain_t) NULL;
379 map_port_name_to_mid (name, type)
385 if (!MACH_PORT_VALID (name))
388 elem = port_chain_member (notify_chain, name);
390 if (elem && (elem->type == type))
396 if (! MACH_PORT_VALID (mid_server))
398 warning ("Machid server port invalid, can not map port 0x%x to mid",
407 ret = machid_mach_register (mid_server, mid_auth, name, type, &mid);
409 if (ret != KERN_SUCCESS)
411 warning ("Can not map name (0x%x) to mid with machid", name);
418 /* Guard for currently_waiting_for and singlestepped_thread_port */
420 discard_single_step (thread)
423 currently_waiting_for = inferior_wait_port_set;
425 cleanup_step = NULL_CLEANUP;
426 if (MACH_PORT_VALID (thread) && MACH_PORT_VALID (singlestepped_thread_port))
427 setup_single_step (thread, FALSE);
430 setup_single_step (thread, start_step)
432 boolean_t start_step;
436 if (! MACH_PORT_VALID (thread))
437 error ("Invalid thread supplied to setup_single_step");
442 /* Get the current thread exception port */
443 ret = thread_get_exception_port (thread, &teport);
444 CHK ("Getting thread's exception port", ret);
448 if (MACH_PORT_VALID (singlestepped_thread_port))
450 warning ("Singlestepped_thread_port (0x%x) is still valid?",
451 singlestepped_thread_port);
452 singlestepped_thread_port = MACH_PORT_NULL;
455 /* If we are already stepping this thread */
456 if (MACH_PORT_VALID (teport) && teport == thread_exception_port)
458 ret = mach_port_deallocate (mach_task_self (), teport);
459 CHK ("Could not deallocate thread exception port", ret);
463 ret = thread_set_exception_port (thread, thread_exception_port);
464 CHK ("Setting exception port for thread", ret);
466 /* Insert thread exception port to wait port set */
467 ret = mach_port_move_member (mach_task_self(),
468 thread_exception_port,
469 inferior_wait_port_set);
470 CHK ("Moving thread exception port to inferior_wait_port_set",
473 thread_saved_exception_port = teport;
476 thread_trace (thread, TRUE);
478 singlestepped_thread_port = thread_exception_port;
479 currently_waiting_for = singlestepped_thread_port;
480 cleanup_step = make_cleanup (discard_single_step, thread);
484 if (! MACH_PORT_VALID (teport))
485 error ("Single stepped thread had an invalid exception port?");
487 if (teport != thread_exception_port)
488 error ("Single stepped thread had an unknown exception port?");
490 ret = mach_port_deallocate (mach_task_self (), teport);
491 CHK ("Couldn't deallocate thread exception port", ret);
493 /* Remove thread exception port from wait port set */
494 ret = mach_port_move_member (mach_task_self(),
495 thread_exception_port,
497 CHK ("Removing thread exception port from inferior_wait_port_set",
500 /* Restore thread's old exception port */
501 ret = thread_set_exception_port (thread,
502 thread_saved_exception_port);
503 CHK ("Restoring stepped thread's exception port", ret);
505 if (MACH_PORT_VALID (thread_saved_exception_port))
506 (void) mach_port_deallocate (mach_task_self (),
507 thread_saved_exception_port);
509 thread_trace (thread, FALSE);
511 singlestepped_thread_port = MACH_PORT_NULL;
512 currently_waiting_for = inferior_wait_port_set;
514 discard_cleanups (cleanup_step);
520 request_notify (name, variant, type)
522 mach_msg_id_t variant;
526 mach_port_t previous_port_dummy = MACH_PORT_NULL;
528 if (! MACH_PORT_VALID (name))
531 if (port_chain_member (notify_chain, name))
534 ret = mach_port_request_notification (mach_task_self(),
539 MACH_MSG_TYPE_MAKE_SEND_ONCE,
540 &previous_port_dummy);
541 CHK ("Serious: request_notify failed", ret);
543 (void) mach_port_deallocate (mach_task_self (),
544 previous_port_dummy);
546 notify_chain = port_chain_insert (notify_chain, name, type);
549 reverse_msg_bits(msgp, type)
550 mach_msg_header_t *msgp;
554 rbits = MACH_MSGH_BITS_REMOTE(msgp->msgh_bits);
557 (msgp->msgh_bits & ~MACH_MSGH_BITS_PORTS_MASK) |
558 MACH_MSGH_BITS(lbits,rbits);
561 /* On the third day He said:
564 and then it was global.
566 When creating the inferior fork, the
567 child code in inflow.c sets the name of the
568 bootstrap_port in its address space to this
571 The name is transferred to our address space
572 with mach3_read_inferior().
574 Thou shalt not do this with
575 task_get_bootstrap_port() in this task, since
576 the name in the inferior task is different than
579 For blessed are the meek, as they shall inherit
582 mach_port_t original_server_port_name = MACH_PORT_NULL;
585 /* Called from inferior after FORK but before EXEC */
591 /* Get the NAME of the bootstrap port in this task
592 so that GDB can read it */
593 ret = task_get_bootstrap_port (mach_task_self (),
594 &original_server_port_name);
595 if (ret != KERN_SUCCESS)
597 ret = mach_port_deallocate (mach_task_self (),
598 original_server_port_name);
599 if (ret != KERN_SUCCESS)
602 /* Suspend this task to let the parent change my ports.
603 Resumed by the debugger */
604 ret = task_suspend (mach_task_self ());
605 if (ret != KERN_SUCCESS)
610 * Intercept system calls to Unix server.
611 * After EXEC_COUNTER calls to exec(), return.
613 * Pre-assertion: Child is suspended. (Not verified)
614 * Post-condition: Child is suspended after EXEC_COUNTER exec() calls.
618 intercept_exec_calls (exec_counter)
621 int terminal_initted = 0;
623 struct syscall_msg_t {
624 mach_msg_header_t header;
625 mach_msg_type_t type;
626 char room[ 2000 ]; /* Enuff space */
629 struct syscall_msg_t syscall_in, syscall_out;
631 mach_port_t fake_server;
632 mach_port_t original_server_send;
633 mach_port_t original_exec_reply;
634 mach_port_t exec_reply;
635 mach_port_t exec_reply_send;
636 mach_msg_type_name_t acquired;
637 mach_port_t emulator_server_port_name;
638 struct task_basic_info info;
639 mach_msg_type_number_t info_count;
643 if (exec_counter <= 0)
644 return; /* We are already set up in the correct program */
646 ret = mach_port_allocate(mach_task_self(),
647 MACH_PORT_RIGHT_RECEIVE,
649 CHK("create inferior_fake_server port failed", ret);
651 /* Wait for inferior_task to suspend itself */
654 info_count = sizeof (info);
655 ret = task_info (inferior_task,
659 CHK ("Task info", ret);
661 if (info.suspend_count)
664 /* Note that the definition of the parameter was undefined
665 * at the time of this writing, so I just use an `ad hoc' value.
667 (void) swtch_pri (42); /* Universal Priority Value */
670 /* Read the inferior's bootstrap port name */
671 if (!mach3_read_inferior (&original_server_port_name,
672 &original_server_port_name,
673 sizeof (original_server_port_name)))
674 error ("Can't read inferior task bootstrap port name");
676 /* @@ BUG: If more than 1 send right GDB will FAIL!!! */
677 /* Should get refs, and set them back when restoring */
678 /* Steal the original bsd server send right from inferior */
679 ret = mach_port_extract_right (inferior_task,
680 original_server_port_name,
681 MACH_MSG_TYPE_MOVE_SEND,
682 &original_server_send,
684 CHK("mach_port_extract_right (bsd server send)",ret);
686 if (acquired != MACH_MSG_TYPE_PORT_SEND)
687 error("Incorrect right extracted, send right to bsd server excpected");
689 ret = mach_port_insert_right (inferior_task,
690 original_server_port_name,
692 MACH_MSG_TYPE_MAKE_SEND);
693 CHK("mach_port_insert_right (fake server send)",ret);
695 xx_debug ("inferior task bsd server ports set up \nfs %x, ospn %x, oss %x\n",
697 original_server_port_name, original_server_send);
699 /* A receive right to the reply generated by unix server exec() request */
700 ret = mach_port_allocate(mach_task_self(),
701 MACH_PORT_RIGHT_RECEIVE,
703 CHK("create intercepted_reply_port port failed", ret);
705 /* Pass this send right to Unix server so it replies to us after exec() */
706 ret = mach_port_extract_right (mach_task_self (),
708 MACH_MSG_TYPE_MAKE_SEND_ONCE,
711 CHK("mach_port_extract_right (exec_reply)",ret);
713 if (acquired != MACH_MSG_TYPE_PORT_SEND_ONCE)
714 error("Incorrect right extracted, send once excpected for exec reply");
716 ret = mach_port_move_member(mach_task_self(),
718 inferior_wait_port_set);
719 CHK ("Moving fake syscall port to inferior_wait_port_set", ret);
721 xx_debug ("syscall fake server set up, resuming inferior\n");
723 ret = task_resume (inferior_task);
724 CHK("task_resume (startup)", ret);
726 /* Read requests from the inferior.
727 Pass directly through everything else except exec() calls.
729 while(exec_counter > 0)
731 ret = mach_msg (&syscall_in.header, /* header */
732 MACH_RCV_MSG, /* options */
734 sizeof (struct syscall_msg_t), /* receive size */
735 inferior_wait_port_set, /* receive_name */
736 MACH_MSG_TIMEOUT_NONE,
738 CHK("mach_msg (intercepted sycall)", ret);
741 print_msg (&syscall_in.header);
744 /* ASSERT : msgh_local_port == fake_server */
746 if (notify_server (&syscall_in.header, &syscall_out.header))
747 error ("received a notify while intercepting syscalls");
749 if (syscall_in.header.msgh_id == MIG_EXEC_SYSCALL_ID)
751 xx_debug ("Received EXEC SYSCALL, counter = %d\n", exec_counter);
752 if (exec_counter == 1)
754 original_exec_reply = syscall_in.header.msgh_remote_port;
755 syscall_in.header.msgh_remote_port = exec_reply_send;
758 if (!terminal_initted)
760 /* Now that the child has exec'd we know it has already set its
761 process group. On POSIX systems, tcsetpgrp will fail with
762 EPERM if we try it before the child's setpgid. */
764 /* Set up the "saved terminal modes" of the inferior
765 based on what modes we are starting it with. */
766 target_terminal_init ();
768 /* Install inferior's terminal modes. */
769 target_terminal_inferior ();
771 terminal_initted = 1;
777 syscall_in.header.msgh_local_port = syscall_in.header.msgh_remote_port;
778 syscall_in.header.msgh_remote_port = original_server_send;
780 reverse_msg_bits(&syscall_in.header, MACH_MSG_TYPE_COPY_SEND);
782 ret = mach_msg_send (&syscall_in.header);
783 CHK ("Forwarded syscall", ret);
786 ret = mach_port_move_member(mach_task_self(),
789 CHK ("Moving fake syscall out of inferior_wait_port_set", ret);
791 ret = mach_port_move_member(mach_task_self(),
793 inferior_wait_port_set);
794 CHK ("Moving exec_reply to inferior_wait_port_set", ret);
796 ret = mach_msg (&syscall_in.header, /* header */
797 MACH_RCV_MSG, /* options */
799 sizeof (struct syscall_msg_t), /* receive size */
800 inferior_wait_port_set, /* receive_name */
801 MACH_MSG_TIMEOUT_NONE,
803 CHK("mach_msg (exec reply)", ret);
805 ret = task_suspend (inferior_task);
806 CHK ("Suspending inferior after last exec", ret);
808 must_suspend_thread = 0;
810 xx_debug ("Received exec reply from bsd server, suspended inferior task\n");
813 print_msg (&syscall_in.header);
816 /* Message should appear as if it came from the unix server */
817 syscall_in.header.msgh_local_port = MACH_PORT_NULL;
819 /* and go to the inferior task original reply port */
820 syscall_in.header.msgh_remote_port = original_exec_reply;
822 reverse_msg_bits(&syscall_in.header, MACH_MSG_TYPE_MOVE_SEND_ONCE);
824 ret = mach_msg_send (&syscall_in.header);
825 CHK ("Forwarding exec reply to inferior", ret);
827 /* Garbage collect */
828 ret = mach_port_deallocate (inferior_task,
829 original_server_port_name);
830 CHK ("deallocating fake server send right", ret);
832 ret = mach_port_insert_right (inferior_task,
833 original_server_port_name,
834 original_server_send,
835 MACH_MSG_TYPE_MOVE_SEND);
836 CHK ("Restoring the original bsd server send right", ret);
838 ret = mach_port_destroy (mach_task_self (),
840 fake_server = MACH_PORT_DEAD;
841 CHK("mach_port_destroy (fake_server)", ret);
843 ret = mach_port_destroy (mach_task_self (),
845 exec_reply = MACH_PORT_DEAD;
846 CHK("mach_port_destroy (exec_reply)", ret);
848 xx_debug ("Done with exec call interception\n");
852 consume_send_rights (thread_list, thread_count)
853 thread_array_t thread_list;
861 for (index = 0; index < thread_count; index++)
863 /* Since thread kill command kills threads, don't check ret */
864 (void) mach_port_deallocate (mach_task_self (),
865 thread_list [ index ]);
869 /* suspend/abort/resume a thread. */
870 setup_thread (thread, what)
878 ret = thread_suspend (thread);
879 CHK ("setup_thread thread_suspend", ret);
881 ret = thread_abort (thread);
882 CHK ("setup_thread thread_abort", ret);
886 ret = thread_resume (thread);
887 CHK ("setup_thread thread_resume", ret);
892 map_slot_to_mid (slot, threads, thread_count)
894 thread_array_t threads;
905 ret = task_threads (inferior_task, &threads, &thread_count);
906 CHK ("Can not select a thread from a dead task", ret);
909 if (slot < 0 || slot >= thread_count)
913 consume_send_rights (threads, thread_count);
914 (void) vm_deallocate (mach_task_self(), (vm_address_t)threads,
915 (thread_count * sizeof(mach_port_t)));
918 error ("invalid slot number");
923 mid = map_port_name_to_mid (threads [slot], MACH_TYPE_THREAD);
927 consume_send_rights (threads, thread_count);
928 (void) vm_deallocate (mach_task_self(), (vm_address_t)threads,
929 (thread_count * sizeof(mach_port_t)));
936 parse_thread_id (arg, thread_count, slots)
949 while (*arg && (*arg == ' ' || *arg == '\t'))
955 /* Currently parse MID and @SLOTNUMBER */
960 error ("valid thread mid expected");
968 error ("invalid slot number");
970 /* If you want slot numbers to remain slot numbers, set slots.
972 * Well, since 0 is reserved, return the ordinal number
973 * of the thread rather than the slot number. Awk, this
974 * counts as a kludge.
979 if (thread_count && slot >= thread_count)
982 mid = map_slot_to_mid (slot);
987 /* THREAD_ID 0 is special; it selects the first kernel
988 * thread from the list (i.e. SLOTNUMBER 0)
989 * This is used when starting the program with 'run' or when attaching.
991 * If FLAG is 0 the context is not changed, and the registers, frame, etc
992 * will continue to describe the old thread.
994 * If FLAG is nonzero, really select the thread.
995 * If FLAG is 2, the THREAD_ID is a slotnumber instead of a mid.
999 select_thread (task, thread_id, flag)
1004 thread_array_t thread_list;
1008 thread_t new_thread = MACH_PORT_NULL;
1011 error ("Can't select cprocs without kernel thread");
1013 ret = task_threads (task, &thread_list, &thread_count);
1014 if (ret != KERN_SUCCESS)
1016 warning ("Can not select a thread from a dead task");
1017 m3_kill_inferior ();
1018 return KERN_FAILURE;
1021 if (thread_count == 0)
1023 /* The task can not do anything anymore, but it still
1024 * exists as a container for memory and ports.
1026 registers_changed ();
1027 warning ("Task %d has no threads",
1028 map_port_name_to_mid (task, MACH_TYPE_TASK));
1029 current_thread = MACH_PORT_NULL;
1030 (void) vm_deallocate(mach_task_self(),
1031 (vm_address_t) thread_list,
1032 (thread_count * sizeof(mach_port_t)));
1033 return KERN_FAILURE;
1036 if (! thread_id || flag == 2)
1038 /* First thread or a slotnumber */
1040 new_thread = thread_list[0];
1043 if (thread_id < thread_count)
1044 new_thread = thread_list[ thread_id ];
1047 (void) vm_deallocate(mach_task_self(),
1048 (vm_address_t) thread_list,
1049 (thread_count * sizeof(mach_port_t)));
1050 error ("No such thread slot number : %d", thread_id);
1056 for (index = 0; index < thread_count; index++)
1057 if (thread_id == map_port_name_to_mid (thread_list [index],
1060 new_thread = thread_list [index];
1066 error ("No thread with mid %d", thread_id);
1069 /* Notify when the selected thread dies */
1070 request_notify (new_thread, MACH_NOTIFY_DEAD_NAME, MACH_TYPE_THREAD);
1072 ret = vm_deallocate(mach_task_self(),
1073 (vm_address_t) thread_list,
1074 (thread_count * sizeof(mach_port_t)));
1075 CHK ("vm_deallocate", ret);
1078 current_thread = new_thread;
1082 if (MACH_PORT_VALID (current_thread))
1084 /* Store the gdb's view of the thread we are deselecting
1086 * @@ I think gdb updates registers immediately when they are
1087 * changed, so don't do this.
1089 ret = thread_abort (current_thread);
1090 CHK ("Could not abort system calls when saving state of old thread",
1092 target_prepare_to_store ();
1093 target_store_registers (-1);
1097 registers_changed ();
1099 current_thread = new_thread;
1101 ret = thread_abort (current_thread);
1102 CHK ("Could not abort system calls when selecting a thread", ret);
1104 stop_pc = read_pc();
1105 flush_cached_frames ();
1107 select_frame (get_current_frame (), 0);
1110 return KERN_SUCCESS;
1114 * Switch to use thread named NEW_THREAD.
1118 switch_to_thread (new_thread)
1119 thread_t new_thread;
1121 thread_t saved_thread = current_thread;
1124 mid = map_port_name_to_mid (new_thread,
1127 warning ("Can't map thread name 0x%x to mid", new_thread);
1128 else if (select_thread (inferior_task, mid, 1) != KERN_SUCCESS)
1131 current_thread = saved_thread;
1132 error ("Could not select thread %d", mid);
1138 /* Do this in gdb after doing FORK but before STARTUP_INFERIOR.
1139 * Note that the registers are not yet valid in the inferior task.
1147 push_target (&m3_ops);
1149 inferior_task = task_by_pid (pid);
1151 if (! MACH_PORT_VALID (inferior_task))
1152 error ("Can not map Unix pid %d to Mach task", pid);
1154 /* Clean up previous notifications and create new ones */
1155 setup_notify_port (1);
1157 /* When notification appears, the inferior task has died */
1158 request_notify (inferior_task, MACH_NOTIFY_DEAD_NAME, MACH_TYPE_TASK);
1160 emulator_present = have_emulator_p (inferior_task);
1162 /* By default, select the first thread,
1163 * If task has no threads, gives a warning
1164 * Does not fetch registers, since they are not yet valid.
1166 select_thread (inferior_task, 0, 0);
1168 inferior_exception_port = MACH_PORT_NULL;
1170 setup_exception_port ();
1172 xx_debug ("Now the debugged task is created\n");
1174 /* One trap to exec the shell, one to exec the program being debugged. */
1175 intercept_exec_calls (2);
1180 setup_exception_port ()
1184 ret = mach_port_allocate (mach_task_self(),
1185 MACH_PORT_RIGHT_RECEIVE,
1186 &inferior_exception_port);
1187 CHK("mach_port_allocate",ret);
1189 /* add send right */
1190 ret = mach_port_insert_right (mach_task_self (),
1191 inferior_exception_port,
1192 inferior_exception_port,
1193 MACH_MSG_TYPE_MAKE_SEND);
1194 CHK("mach_port_insert_right",ret);
1196 ret = mach_port_move_member (mach_task_self(),
1197 inferior_exception_port,
1198 inferior_wait_port_set);
1199 CHK("mach_port_move_member",ret);
1201 ret = task_get_special_port (inferior_task,
1202 TASK_EXCEPTION_PORT,
1203 &inferior_old_exception_port);
1204 CHK ("task_get_special_port(old exc)",ret);
1206 ret = task_set_special_port (inferior_task,
1207 TASK_EXCEPTION_PORT,
1208 inferior_exception_port);
1209 CHK("task_set_special_port",ret);
1211 ret = mach_port_deallocate (mach_task_self (),
1212 inferior_exception_port);
1213 CHK("mack_port_deallocate",ret);
1216 /* When notify appears, the inferior_task's exception
1217 * port has been destroyed.
1219 * Not used, since the dead_name_notification already
1220 * appears when task dies.
1223 request_notify (inferior_exception_port,
1224 MACH_NOTIFY_NO_SENDERS,
1225 MACH_TYPE_EXCEPTION_PORT);
1229 /* Nonzero if gdb is waiting for a message */
1230 int mach_really_waiting;
1232 /* Wait for the inferior to stop for some reason.
1233 - Loop on notifications until inferior_task dies.
1234 - Loop on exceptions until stopped_in_exception comes true.
1235 (e.g. we receive a single step trace trap)
1236 - a message arrives to gdb's message port
1238 There is no other way to exit this loop.
1240 Returns the inferior_pid for rest of gdb.
1241 Side effects: Set *OURSTATUS. */
1243 mach_really_wait (pid, ourstatus)
1245 struct target_waitstatus *ourstatus;
1251 mach_msg_header_t header;
1252 mach_msg_type_t foo;
1256 /* Either notify (death), exception or message can stop the inferior */
1257 stopped_in_exception = FALSE;
1263 stop_exception = stop_code = stop_subcode = -1;
1264 stop_thread = MACH_PORT_NULL;
1266 mach_really_waiting = 1;
1267 ret = mach_msg (&in_msg.header, /* header */
1268 MACH_RCV_MSG, /* options */
1270 sizeof (struct msg), /* receive size */
1271 currently_waiting_for, /* receive name */
1272 MACH_MSG_TIMEOUT_NONE,
1274 mach_really_waiting = 0;
1275 CHK("mach_msg (receive)", ret);
1277 /* Check if we received a notify of the childs' death */
1278 if (notify_server (&in_msg.header, &out_msg.header))
1280 /* If inferior_task is null then the inferior has
1281 gone away and we want to return to command level.
1282 Otherwise it was just an informative message and we
1283 need to look to see if there are any more. */
1284 if (inferior_task != MACH_PORT_NULL)
1288 /* Collect Unix exit status for gdb */
1290 wait3(&w, WNOHANG, 0);
1292 /* This mess is here to check that the rest of
1293 * gdb knows that the inferior died. It also
1294 * tries to hack around the fact that Mach 3.0 (mk69)
1295 * unix server (ux28) does not always know what
1296 * has happened to it's children when mach-magic
1297 * is applied on them.
1299 if ((!WIFEXITED(w) && WIFSTOPPED(w)) ||
1300 (WIFEXITED(w) && WEXITSTATUS(w) > 0377))
1303 warning ("Using exit value 0 for terminated task");
1305 else if (!WIFEXITED(w))
1307 int sig = WTERMSIG(w);
1309 /* Signals cause problems. Warn the user. */
1310 if (sig != SIGKILL) /* Bad luck if garbage matches this */
1311 warning ("The terminating signal stuff may be nonsense");
1312 else if (sig > NSIG)
1315 warning ("Using exit value 0 for terminated task");
1318 store_waitstatus (ourstatus, w);
1319 return inferior_pid;
1323 /* Hmm. Check for exception, as it was not a notification.
1324 exc_server() does an upcall to catch_exception_raise()
1325 if this rpc is an exception. Further actions are decided
1328 if (! exc_server (&in_msg.header, &out_msg.header))
1331 /* Not an exception, check for message.
1333 * Messages don't come from the inferior, or if they
1334 * do they better be asynchronous or it will hang.
1336 if (gdb_message_server (&in_msg.header))
1339 error ("Unrecognized message received in mach_really_wait");
1342 /* Send the reply of the exception rpc to the suspended task */
1343 ret = mach_msg_send (&out_msg.header);
1344 CHK ("mach_msg_send (exc reply)", ret);
1346 if (stopped_in_exception)
1348 /* Get unix state. May be changed in mach3_exception_actions() */
1349 wait3(&w, WNOHANG, 0);
1351 mach3_exception_actions (&w, FALSE, "Task");
1353 store_waitstatus (ourstatus, w);
1354 return inferior_pid;
1359 /* Called by macro DO_QUIT() in utils.c(quit).
1360 * This is called just before calling error() to return to command level
1368 if (mach_really_waiting)
1370 ret = task_suspend (inferior_task);
1372 if (ret != KERN_SUCCESS)
1374 warning ("Could not suspend task for interrupt: %s",
1375 mach_error_string (ret));
1376 mach_really_waiting = 0;
1381 must_suspend_thread = 0;
1382 mach_really_waiting = 0;
1384 mid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD);
1387 warning ("Selecting first existing kernel thread");
1391 current_thread = MACH_PORT_NULL; /* Force setup */
1392 select_thread (inferior_task, mid, 1);
1398 /* bogus bogus bogus. It is NOT OK to quit out of target_wait. */
1400 /* If ^C is typed when we are waiting for a message
1401 * and your Unix server is able to notice that we
1404 * Called by REQUEST_QUIT() from utils.c(request_quit)
1407 mach3_request_quit ()
1409 if (mach_really_waiting)
1415 * Gdb message server.
1416 * Currently implemented is the STOP message, that causes
1417 * gdb to return to the command level like ^C had been typed from terminal.
1420 gdb_message_server (InP)
1421 mach_msg_header_t *InP;
1426 if (InP->msgh_local_port == our_message_port)
1428 /* A message coming to our_message_port. Check validity */
1429 switch (InP->msgh_id) {
1431 case GDB_MESSAGE_ID_STOP:
1432 ret = task_suspend (inferior_task);
1433 if (ret != KERN_SUCCESS)
1434 warning ("Could not suspend task for stop message: %s",
1435 mach_error_string (ret));
1437 /* QUIT in mach_really_wait() loop. */
1442 warning ("Invalid message id %d received, ignored.",
1450 /* Message not handled by this server */
1454 /* NOTE: This is not an RPC call. It is a simpleroutine.
1456 * This is not called from this gdb code.
1458 * It may be called by another debugger to cause this
1459 * debugger to enter command level:
1461 * (gdb) set stop_inferior_gdb ()
1464 * External program "stop-gdb" implements this also.
1467 stop_inferior_gdb ()
1471 /* Code generated by mig, with minor cleanups :-)
1473 * simpleroutine stop_inferior_gdb (our_message_port : mach_port_t);
1477 mach_msg_header_t Head;
1482 register Request *InP = &Mess;
1484 InP->Head.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
1486 /* msgh_size passed as argument */
1487 InP->Head.msgh_remote_port = our_message_port;
1488 InP->Head.msgh_local_port = MACH_PORT_NULL;
1489 InP->Head.msgh_seqno = 0;
1490 InP->Head.msgh_id = GDB_MESSAGE_ID_STOP;
1492 ret = mach_msg (&InP->Head,
1493 MACH_SEND_MSG|MACH_MSG_OPTION_NONE,
1497 MACH_MSG_TIMEOUT_NONE,
1501 #ifdef THREAD_ALLOWED_TO_BREAK
1503 * Return 1 if the MID specifies the thread that caused the
1505 * Since catch_exception_raise() selects the thread causing
1506 * the last exception to current_thread, we just check that
1507 * it is selected and the last exception was a breakpoint.
1510 mach_thread_for_breakpoint (mid)
1513 int cmid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD);
1517 mid = map_slot_to_mid (-(mid+1), 0, 0);
1519 return 0; /* Don't stop, no such slot */
1522 if (! mid || cmid == -1)
1523 return 1; /* stop */
1525 return cmid == mid && stop_exception == EXC_BREAKPOINT;
1527 #endif /* THREAD_ALLOWED_TO_BREAK */
1529 #ifdef THREAD_PARSE_ID
1531 * Map a thread id string (MID or a @SLOTNUMBER)
1534 * 0 matches all threads.
1535 * Otherwise the meaning is defined only in this file.
1536 * (mach_thread_for_breakpoint uses it)
1538 * @@ This allows non-existent MIDs to be specified.
1539 * It now also allows non-existent slots to be
1540 * specified. (Slot numbers stored are negative,
1541 * and the magnitude is one greater than the actual
1542 * slot index. (Since 0 is reserved))
1545 mach_thread_parse_id (arg)
1550 error ("thread id excpected");
1551 mid = parse_thread_id (arg, 0, 1);
1555 #endif /* THREAD_PARSE_ID */
1557 #ifdef THREAD_OUTPUT_ID
1559 mach_thread_output_id (mid)
1562 static char foobar [20];
1565 sprintf (foobar, "mid %d", mid);
1567 sprintf (foobar, "@%d", -(mid+1));
1569 sprintf (foobar, "*any thread*");
1573 #endif /* THREAD_OUTPUT_ID */
1575 /* Called with hook PREPARE_TO_PROCEED() from infrun.c.
1577 * If we have switched threads and stopped at breakpoint return 1 otherwise 0.
1579 * if SELECT_IT is nonzero, reselect the thread that was active when
1580 * we stopped at a breakpoint.
1584 mach3_prepare_to_proceed (select_it)
1588 stop_thread != current_thread &&
1589 stop_exception == EXC_BREAKPOINT)
1596 mid = switch_to_thread (stop_thread);
1604 /* this stuff here is an upcall via libmach/excServer.c
1605 and mach_really_wait which does the actual upcall.
1607 The code will pass the exception to the inferior if:
1609 - The task that signaled is not the inferior task
1610 (e.g. when debugging another debugger)
1612 - The user has explicitely requested to pass on the exceptions.
1613 (e.g to the default unix exception handler, which maps
1614 exceptions to signals, or the user has her own exception handler)
1616 - If the thread that signaled is being single-stepped and it
1617 has set it's own exception port and the exception is not
1618 EXC_BREAKPOINT. (Maybe this is not desirable?)
1622 catch_exception_raise (port, thread, task, exception, code, subcode)
1626 int exception, code, subcode;
1629 boolean_t signal_thread;
1630 int mid = map_port_name_to_mid (thread, MACH_TYPE_THREAD);
1632 if (! MACH_PORT_VALID (thread))
1634 /* If the exception was sent and thread dies before we
1635 receive it, THREAD will be MACH_PORT_DEAD
1638 current_thread = thread = MACH_PORT_NULL;
1639 error ("Received exception from nonexistent thread");
1642 /* Check if the task died in transit.
1643 * @@ Isn't the thread also invalid in such case?
1645 if (! MACH_PORT_VALID (task))
1647 current_thread = thread = MACH_PORT_NULL;
1648 error ("Received exception from nonexistent task");
1651 if (exception < 0 || exception > MAX_EXCEPTION)
1652 fatal ("catch_exception_raise: unknown exception code %d thread %d",
1656 if (! MACH_PORT_VALID (inferior_task))
1657 error ("got an exception, but inferior_task is null or dead");
1659 stop_exception = exception;
1661 stop_subcode = subcode;
1662 stop_thread = thread;
1664 signal_thread = exception != EXC_BREAKPOINT &&
1665 port == singlestepped_thread_port &&
1666 MACH_PORT_VALID (thread_saved_exception_port);
1668 /* If it was not our inferior or if we want to forward
1669 * the exception to the inferior's handler, do it here
1671 * Note: If you have forwarded EXC_BREAKPOINT I trust you know why.
1673 if (task != inferior_task ||
1675 exception_map [exception].forward)
1677 mach_port_t eport = inferior_old_exception_port;
1682 GDB now forwards the exeption to thread's original handler,
1683 since the user propably knows what he is doing.
1684 Give a message, though.
1687 mach3_exception_actions ((WAITTYPE *)NULL, TRUE, "Thread");
1688 eport = thread_saved_exception_port;
1691 /* Send the exception to the original handler */
1692 ret = exception_raise (eport,
1699 (void) mach_port_deallocate (mach_task_self (), task);
1700 (void) mach_port_deallocate (mach_task_self (), thread);
1702 /* If we come here, we don't want to trace any more, since we
1703 * will never stop for tracing anyway.
1705 discard_single_step (thread);
1707 /* Do not stop the inferior */
1711 /* Now gdb handles the exception */
1712 stopped_in_exception = TRUE;
1714 ret = task_suspend (task);
1715 CHK ("Error suspending inferior after exception", ret);
1717 must_suspend_thread = 0;
1719 if (current_thread != thread)
1721 if (MACH_PORT_VALID (singlestepped_thread_port))
1722 /* Cleanup discards single stepping */
1723 error ("Exception from thread %d while singlestepping thread %d",
1725 map_port_name_to_mid (current_thread, MACH_TYPE_THREAD));
1727 /* Then select the thread that caused the exception */
1728 if (select_thread (inferior_task, mid, 0) != KERN_SUCCESS)
1729 error ("Could not select thread %d causing exception", mid);
1731 warning ("Gdb selected thread %d", mid);
1734 /* If we receive an exception that is not breakpoint
1735 * exception, we interrupt the single step and return to
1736 * debugger. Trace condition is cleared.
1738 if (MACH_PORT_VALID (singlestepped_thread_port))
1740 if (stop_exception != EXC_BREAKPOINT)
1741 warning ("Single step interrupted by exception");
1742 else if (port == singlestepped_thread_port)
1744 /* Single step exception occurred, remove trace bit
1745 * and return to gdb.
1747 if (! MACH_PORT_VALID (current_thread))
1748 error ("Single stepped thread is not valid");
1750 /* Resume threads, but leave the task suspended */
1751 resume_all_threads (0);
1754 warning ("Breakpoint while single stepping?");
1756 discard_single_step (current_thread);
1759 (void) mach_port_deallocate (mach_task_self (), task);
1760 (void) mach_port_deallocate (mach_task_self (), thread);
1762 return KERN_SUCCESS;
1766 port_valid (port, mask)
1771 mach_port_type_t type;
1773 ret = mach_port_type (mach_task_self (),
1776 if (ret != KERN_SUCCESS || (type & mask) != mask)
1781 /* @@ No vm read cache implemented yet */
1782 boolean_t vm_read_cache_valid = FALSE;
1785 * Read inferior task's LEN bytes from ADDR and copy it to MYADDR
1786 * in gdb's address space.
1788 * Return 0 on failure; number of bytes read otherwise.
1791 mach3_read_inferior (addr, myaddr, length)
1797 vm_address_t low_address = (vm_address_t) trunc_page (addr);
1798 vm_size_t aligned_length =
1799 (vm_size_t) round_page (addr+length) - low_address;
1800 pointer_t copied_memory;
1803 /* Get memory from inferior with page aligned addresses */
1804 ret = vm_read (inferior_task,
1809 if (ret != KERN_SUCCESS)
1811 /* the problem is that the inferior might be killed for whatever reason
1812 * before we go to mach_really_wait. This is one place that ought to
1813 * catch many of those errors.
1814 * @@ A better fix would be to make all external events to GDB
1815 * to arrive via a SINGLE port set. (Including user input!)
1818 if (! port_valid (inferior_task, MACH_PORT_TYPE_SEND))
1820 m3_kill_inferior ();
1821 error ("Inferior killed (task port invalid)");
1827 /* valprint.c gives nicer format if this does not
1828 screw it. Eamonn seems to like this, so I enable
1829 it if OSF is defined...
1831 warning ("[read inferior %x failed: %s]",
1832 addr, mach_error_string (ret));
1839 memcpy (myaddr, (char *)addr - low_address + copied_memory, length);
1841 ret = vm_deallocate (mach_task_self (),
1844 CHK("mach3_read_inferior vm_deallocate failed", ret);
1850 #define CHK_GOTO_OUT(str,ret) \
1851 do if (ret != KERN_SUCCESS) { errstr = #str; goto out; } while(0)
1853 #define CHK_GOTO_OUT(str,ret) \
1854 do if (ret != KERN_SUCCESS) { errstr = str; goto out; } while(0)
1857 struct vm_region_list {
1858 struct vm_region_list *next;
1859 vm_prot_t protection;
1864 struct obstack region_obstack;
1867 * Write inferior task's LEN bytes from ADDR and copy it to MYADDR
1868 * in gdb's address space.
1871 mach3_write_inferior (addr, myaddr, length)
1877 vm_address_t low_address = (vm_address_t) trunc_page (addr);
1878 vm_size_t aligned_length =
1879 (vm_size_t) round_page (addr+length) - low_address;
1880 pointer_t copied_memory;
1884 char *errstr = "Bug in mach3_write_inferior";
1886 struct vm_region_list *region_element;
1887 struct vm_region_list *region_head = (struct vm_region_list *)NULL;
1889 /* Get memory from inferior with page aligned addresses */
1890 ret = vm_read (inferior_task,
1895 CHK_GOTO_OUT ("mach3_write_inferior vm_read failed", ret);
1899 memcpy ((char *)addr - low_address + copied_memory, myaddr, length);
1901 obstack_init (®ion_obstack);
1903 /* Do writes atomically.
1904 * First check for holes and unwritable memory.
1907 vm_size_t remaining_length = aligned_length;
1908 vm_address_t region_address = low_address;
1910 struct vm_region_list *scan;
1912 while(region_address < low_address + aligned_length)
1914 vm_prot_t protection;
1915 vm_prot_t max_protection;
1916 vm_inherit_t inheritance;
1918 mach_port_t object_name;
1920 vm_size_t region_length = remaining_length;
1921 vm_address_t old_address = region_address;
1923 ret = vm_region (inferior_task,
1932 CHK_GOTO_OUT ("vm_region failed", ret);
1934 /* Check for holes in memory */
1935 if (old_address != region_address)
1937 warning ("No memory at 0x%x. Nothing written",
1944 if (!(max_protection & VM_PROT_WRITE))
1946 warning ("Memory at address 0x%x is unwritable. Nothing written",
1953 /* Chain the regions for later use */
1955 (struct vm_region_list *)
1956 obstack_alloc (®ion_obstack, sizeof (struct vm_region_list));
1958 region_element->protection = protection;
1959 region_element->start = region_address;
1960 region_element->length = region_length;
1962 /* Chain the regions along with protections */
1963 region_element->next = region_head;
1964 region_head = region_element;
1966 region_address += region_length;
1967 remaining_length = remaining_length - region_length;
1970 /* If things fail after this, we give up.
1971 * Somebody is messing up inferior_task's mappings.
1974 /* Enable writes to the chained vm regions */
1975 for (scan = region_head; scan; scan = scan->next)
1977 boolean_t protection_changed = FALSE;
1979 if (!(scan->protection & VM_PROT_WRITE))
1981 ret = vm_protect (inferior_task,
1985 scan->protection | VM_PROT_WRITE);
1986 CHK_GOTO_OUT ("vm_protect: enable write failed", ret);
1990 ret = vm_write (inferior_task,
1994 CHK_GOTO_OUT ("vm_write failed", ret);
1996 /* Set up the original region protections, if they were changed */
1997 for (scan = region_head; scan; scan = scan->next)
1999 boolean_t protection_changed = FALSE;
2001 if (!(scan->protection & VM_PROT_WRITE))
2003 ret = vm_protect (inferior_task,
2008 CHK_GOTO_OUT ("vm_protect: enable write failed", ret);
2016 obstack_free (®ion_obstack, 0);
2018 (void) vm_deallocate (mach_task_self (),
2023 if (ret != KERN_SUCCESS)
2025 warning ("%s %s", errstr, mach_error_string (ret));
2032 /* Return 0 on failure, number of bytes handled otherwise. */
2034 m3_xfer_memory (memaddr, myaddr, len, write, target)
2039 struct target_ops *target; /* IGNORED */
2044 result = mach3_write_inferior (memaddr, myaddr, len);
2046 result = mach3_read_inferior (memaddr, myaddr, len);
2053 translate_state(state)
2057 case TH_STATE_RUNNING: return("R");
2058 case TH_STATE_STOPPED: return("S");
2059 case TH_STATE_WAITING: return("W");
2060 case TH_STATE_UNINTERRUPTIBLE: return("U");
2061 case TH_STATE_HALTED: return("H");
2062 default: return("?");
2067 translate_cstate (state)
2072 case CPROC_RUNNING: return "R";
2073 case CPROC_SWITCHING: return "S";
2074 case CPROC_BLOCKED: return "B";
2075 case CPROC_CONDWAIT: return "C";
2076 case CPROC_CONDWAIT|CPROC_SWITCHING: return "CS";
2077 default: return "?";
2081 /* type == MACH_MSG_TYPE_COPY_SEND || type == MACH_MSG_TYPE_MAKE_SEND */
2083 mach_port_t /* no mach_port_name_t found in include files. */
2084 map_inferior_port_name (inferior_name, type)
2085 mach_port_t inferior_name;
2086 mach_msg_type_name_t type;
2089 mach_msg_type_name_t acquired;
2092 ret = mach_port_extract_right (inferior_task,
2097 CHK("mach_port_extract_right (map_inferior_port_name)", ret);
2099 if (acquired != MACH_MSG_TYPE_PORT_SEND)
2100 error("Incorrect right extracted, (map_inferior_port_name)");
2102 ret = mach_port_deallocate (mach_task_self (),
2104 CHK ("Deallocating mapped port (map_inferior_port_name)", ret);
2110 * Naming convention:
2111 * Always return user defined name if found.
2112 * _K == A kernel thread with no matching CPROC
2113 * _C == A cproc with no current cthread
2114 * _t == A cthread with no user defined name
2116 * The digits that follow the _names are the SLOT number of the
2117 * kernel thread if there is such a thing, otherwise just a negation
2118 * of the sequential number of such cprocs.
2124 get_thread_name (one_cproc, id)
2125 gdb_thread_t one_cproc;
2129 if (one_cproc->cthread == NULL)
2131 /* cproc not mapped to any cthread */
2132 sprintf(buf, "_C%d", id);
2134 else if (! one_cproc->cthread->name)
2136 /* cproc and cthread, but no name */
2137 sprintf(buf, "_t%d", id);
2140 return (char *)(one_cproc->cthread->name);
2144 warning ("Inconsistency in thread name id %d", id);
2146 /* Kernel thread without cproc */
2147 sprintf(buf, "_K%d", id);
2154 fetch_thread_info (task, mthreads_out)
2156 gdb_thread_t *mthreads_out; /* out */
2159 thread_array_t th_table;
2161 gdb_thread_t mthreads = NULL;
2164 ret = task_threads (task, &th_table, &th_count);
2165 if (ret != KERN_SUCCESS)
2167 warning ("Error getting inferior's thread list:%s",
2168 mach_error_string(ret));
2169 m3_kill_inferior ();
2173 mthreads = (gdb_thread_t)
2176 th_count * sizeof (struct gdb_thread));
2178 for (index = 0; index < th_count; index++)
2180 thread_t saved_thread = MACH_PORT_NULL;
2183 if (must_suspend_thread)
2184 setup_thread (th_table[ index ], 1);
2186 if (th_table[index] != current_thread)
2188 saved_thread = current_thread;
2190 mid = switch_to_thread (th_table[ index ]);
2193 mthreads[index].name = th_table[index];
2194 mthreads[index].cproc = NULL; /* map_cprocs_to_kernel_threads() */
2195 mthreads[index].in_emulator = FALSE;
2196 mthreads[index].slotid = index;
2198 mthreads[index].sp = read_register (SP_REGNUM);
2199 mthreads[index].fp = read_register (FP_REGNUM);
2200 mthreads[index].pc = read_pc ();
2202 if (MACH_PORT_VALID (saved_thread))
2203 mid = switch_to_thread (saved_thread);
2205 if (must_suspend_thread)
2206 setup_thread (th_table[ index ], 0);
2209 consume_send_rights (th_table, th_count);
2210 ret = vm_deallocate (mach_task_self(), (vm_address_t)th_table,
2211 (th_count * sizeof(mach_port_t)));
2212 if (ret != KERN_SUCCESS)
2214 warning ("Error trying to deallocate thread list : %s",
2215 mach_error_string (ret));
2218 *mthreads_out = mthreads;
2225 * Current emulator always saves the USP on top of
2226 * emulator stack below struct emul_stack_top stuff.
2229 fetch_usp_from_emulator_stack (sp)
2232 CORE_ADDR stack_pointer;
2234 sp = (sp & ~(EMULATOR_STACK_SIZE-1)) +
2235 EMULATOR_STACK_SIZE - sizeof (struct emul_stack_top);
2237 if (mach3_read_inferior (sp,
2239 sizeof (CORE_ADDR)) != sizeof (CORE_ADDR))
2241 warning ("Can't read user sp from emulator stack address 0x%x", sp);
2245 return stack_pointer;
2250 /* get_emulation_vector() interface was changed after mk67 */
2251 #define EMUL_VECTOR_COUNT 400 /* Value does not matter too much */
2255 /* Check if the emulator exists at task's address space.
2258 have_emulator_p (task)
2262 #ifndef EMUL_VECTOR_COUNT
2263 vm_offset_t *emulation_vector;
2266 vm_offset_t emulation_vector[ EMUL_VECTOR_COUNT ];
2267 int n = EMUL_VECTOR_COUNT;
2272 ret = task_get_emulation_vector (task,
2274 #ifndef EMUL_VECTOR_COUNT
2280 CHK("task_get_emulation_vector", ret);
2281 xx_debug ("%d vectors from %d at 0x%08x\n",
2282 n, vector_start, emulation_vector);
2284 for(i = 0; i < n; i++)
2286 vm_offset_t entry = emulation_vector [i];
2288 if (EMULATOR_BASE <= entry && entry <= EMULATOR_END)
2292 static boolean_t informed = FALSE;
2295 warning("Emulation vector address 0x08%x outside emulator space",
2304 /* Map cprocs to kernel threads and vice versa. */
2307 map_cprocs_to_kernel_threads (cprocs, mthreads, thread_count)
2308 gdb_thread_t cprocs;
2309 gdb_thread_t mthreads;
2314 boolean_t all_mapped = TRUE;
2318 for (scan = cprocs; scan; scan = scan->next)
2320 /* Default to: no kernel thread for this cproc */
2321 scan->reverse_map = -1;
2323 /* Check if the cproc is found by its stack */
2324 for (index = 0; index < thread_count; index++)
2327 extract_signed_integer (scan->raw_cproc + CPROC_BASE_OFFSET,
2330 extract_signed_integer (scan->raw_cproc + CPROC_SIZE_OFFSET,
2332 if ((mthreads + index)->sp > stack_base &&
2333 (mthreads + index)->sp <= stack_base + stack_size)
2335 (mthreads + index)->cproc = scan;
2336 scan->reverse_map = index;
2340 all_mapped &= (scan->reverse_map != -1);
2343 /* Check for threads that are currently in the emulator.
2344 * If so, they have a different stack, and the still unmapped
2345 * cprocs may well get mapped to these threads.
2348 * - cproc stack does not match any kernel thread stack pointer
2349 * - there is at least one extra kernel thread
2350 * that has no cproc mapped above.
2351 * - some kernel thread stack pointer points to emulator space
2352 * then we find the user stack pointer saved in the emulator
2353 * stack, and try to map that to the cprocs.
2355 * Also set in_emulator for kernel threads.
2358 if (emulator_present)
2360 for (index = 0; index < thread_count; index++)
2365 gdb_thread_t mthread = (mthreads+index);
2366 emul_sp = mthread->sp;
2368 if (mthread->cproc == NULL &&
2369 EMULATOR_BASE <= emul_sp && emul_sp <= EMULATOR_END)
2371 mthread->in_emulator = emulator_present;
2373 if (!all_mapped && cprocs)
2375 usp = fetch_usp_from_emulator_stack (emul_sp);
2377 /* @@ Could be more accurate */
2379 error ("Zero stack pointer read from emulator?");
2381 /* Try to match this stack pointer to the cprocs that
2382 * don't yet have a kernel thread.
2384 for (scan = cprocs; scan; scan = scan->next)
2387 /* Check is this unmapped CPROC stack contains
2388 * the user stack pointer saved in the
2391 if (scan->reverse_map == -1)
2394 extract_signed_integer
2395 (scan->raw_cproc + CPROC_BASE_OFFSET,
2398 extract_signed_integer
2399 (scan->raw_cproc + CPROC_SIZE_OFFSET,
2401 if (usp > stack_base &&
2402 usp <= stack_base + stack_size)
2404 mthread->cproc = scan;
2405 scan->reverse_map = index;
2417 * Format of the thread_list command
2419 * slot mid sel name emul ks susp cstate wired address
2421 #define TL_FORMAT "%-2.2s %5d%c %-10.10s %1.1s%s%-5.5s %-2.2s %-5.5s "
2423 #define TL_HEADER "\n@ MID Name KState CState Where\n"
2426 print_tl_address (stream, pc)
2430 if (! lookup_minimal_symbol_by_pc (pc))
2431 fprintf_filtered (stream, local_hex_format(), pc);
2434 extern int addressprint;
2435 extern int asm_demangle;
2437 int store = addressprint;
2439 print_address_symbolic (pc, stream, asm_demangle, "");
2440 addressprint = store;
2444 /* For thread names, but also for gdb_message_port external name */
2445 #define MAX_NAME_LEN 50
2447 /* Returns the address of variable NAME or 0 if not found */
2449 lookup_address_of_variable (name)
2453 CORE_ADDR symaddr = 0;
2454 struct minimal_symbol *msymbol;
2456 sym = lookup_symbol (name,
2457 (struct block *)NULL,
2460 (struct symtab **)NULL);
2463 symaddr = SYMBOL_VALUE (sym);
2467 msymbol = lookup_minimal_symbol (name, NULL, NULL);
2469 if (msymbol && msymbol->type == mst_data)
2470 symaddr = SYMBOL_VALUE_ADDRESS (msymbol);
2479 gdb_thread_t cproc_head;
2480 gdb_thread_t cproc_copy;
2481 CORE_ADDR their_cprocs;
2482 char *buf[TARGET_PTR_BIT / HOST_CHAR_BIT];
2487 symaddr = lookup_address_of_variable ("cproc_list");
2491 /* cproc_list is not in a file compiled with debugging
2492 symbols, but don't give up yet */
2494 symaddr = lookup_address_of_variable ("cprocs");
2498 static int informed = 0;
2502 warning ("Your program is loaded with an old threads library.");
2503 warning ("GDB does not know the old form of threads");
2504 warning ("so things may not work.");
2509 /* Stripped or no -lthreads loaded or "cproc_list" is in wrong segment. */
2513 /* Get the address of the first cproc in the task */
2514 if (!mach3_read_inferior (symaddr,
2516 TARGET_PTR_BIT / HOST_CHAR_BIT))
2517 error ("Can't read cproc master list at address (0x%x).", symaddr);
2518 their_cprocs = extract_address (buf, TARGET_PTR_BIT / HOST_CHAR_BIT);
2520 /* Scan the CPROCs in the task.
2521 CPROCs are chained with LIST field, not NEXT field, which
2522 chains mutexes, condition variables and queues */
2526 while (their_cprocs != (CORE_ADDR)0)
2528 CORE_ADDR cproc_copy_incarnation;
2529 cproc_copy = (gdb_thread_t) obstack_alloc (cproc_obstack,
2530 sizeof (struct gdb_thread));
2532 if (!mach3_read_inferior (their_cprocs,
2533 &cproc_copy->raw_cproc[0],
2535 error("Can't read next cproc at 0x%x.", their_cprocs);
2538 extract_address (cproc_copy->raw_cproc + CPROC_LIST_OFFSET,
2540 cproc_copy_incarnation =
2541 extract_address (cproc_copy->raw_cproc + CPROC_INCARNATION_OFFSET,
2542 CPROC_INCARNATION_SIZE);
2544 if (cproc_copy_incarnation == (CORE_ADDR)0)
2545 cproc_copy->cthread = NULL;
2548 /* This CPROC has an attached CTHREAD. Get its name */
2549 cthread = (cthread_t)obstack_alloc (cproc_obstack,
2550 sizeof(struct cthread));
2552 if (!mach3_read_inferior (cproc_copy_incarnation,
2554 sizeof(struct cthread)))
2555 error("Can't read next thread at 0x%x.",
2556 cproc_copy_incarnation);
2558 cproc_copy->cthread = cthread;
2562 name = (char *) obstack_alloc (cproc_obstack, MAX_NAME_LEN);
2564 if (!mach3_read_inferior(cthread->name, name, MAX_NAME_LEN))
2565 error("Can't read next thread's name at 0x%x.", cthread->name);
2567 cthread->name = name;
2571 /* insert in front */
2572 cproc_copy->next = cproc_head;
2573 cproc_head = cproc_copy;
2578 #ifndef FETCH_CPROC_STATE
2580 * Check if your machine does not grok the way this routine
2581 * fetches the FP,PC and SP of a cproc that is not
2582 * currently attached to any kernel thread (e.g. its cproc.context
2583 * field points to the place in stack where the context
2586 * If it doesn't, define your own routine.
2588 #define FETCH_CPROC_STATE(mth) mach3_cproc_state (mth)
2591 mach3_cproc_state (mthread)
2592 gdb_thread_t mthread;
2596 if (! mthread || !mthread->cproc)
2599 context = extract_signed_integer
2600 (mthread->cproc->raw_cproc + CPROC_CONTEXT_OFFSET,
2601 CPROC_CONTEXT_SIZE);
2605 mthread->sp = context + MACHINE_CPROC_SP_OFFSET;
2607 if (mach3_read_inferior (context + MACHINE_CPROC_PC_OFFSET,
2609 sizeof (CORE_ADDR)) != sizeof (CORE_ADDR))
2611 warning ("Can't read cproc pc from inferior");
2615 if (mach3_read_inferior (context + MACHINE_CPROC_FP_OFFSET,
2617 sizeof (CORE_ADDR)) != sizeof (CORE_ADDR))
2619 warning ("Can't read cproc fp from inferior");
2625 #endif /* FETCH_CPROC_STATE */
2629 thread_list_command()
2631 thread_basic_info_data_t ths;
2633 gdb_thread_t cprocs;
2641 mach_port_t mid_or_port;
2642 gdb_thread_t their_threads;
2643 gdb_thread_t kthread;
2647 char *fmt = "There are %d kernel threads in task %d.\n";
2649 int tmid = map_port_name_to_mid (inferior_task, MACH_TYPE_TASK);
2651 MACH_ERROR_NO_INFERIOR;
2653 thread_count = fetch_thread_info (inferior_task,
2655 if (thread_count == -1)
2658 if (thread_count == 1)
2659 fmt = "There is %d kernel thread in task %d.\n";
2661 printf_filtered (fmt, thread_count, tmid);
2663 puts_filtered (TL_HEADER);
2665 cprocs = get_cprocs();
2667 map_cprocs_to_kernel_threads (cprocs, their_threads, thread_count);
2669 for (scan = cprocs; scan; scan = scan->next)
2675 extract_signed_integer
2676 (scan->raw_cproc + CPROC_STATE_OFFSET, CPROC_STATE_SIZE);
2680 /* a wired cproc? */
2681 wired = (extract_address (scan->raw_cproc + CPROC_WIRED_OFFSET,
2685 if (scan->reverse_map != -1)
2686 kthread = (their_threads + scan->reverse_map);
2692 /* These cprocs have a kernel thread */
2694 mid = map_port_name_to_mid (kthread->name, MACH_TYPE_THREAD);
2696 infoCnt = THREAD_BASIC_INFO_COUNT;
2698 ret = thread_info (kthread->name,
2700 (thread_info_t)&ths,
2703 if (ret != KERN_SUCCESS)
2705 warning ("Unable to get basic info on thread %d : %s",
2707 mach_error_string (ret));
2711 /* Who is the first to have more than 100 threads */
2712 sprintf (slot, "%d", kthread->slotid%100);
2714 if (kthread->name == current_thread)
2717 if (ths.suspend_count)
2718 sprintf (buf, "%d", ths.suspend_count);
2723 if (ths.flags & TH_FLAGS_SWAPPED)
2727 if (ths.flags & TH_FLAGS_IDLE)
2730 printf_filtered (TL_FORMAT,
2734 get_thread_name (scan, kthread->slotid),
2735 kthread->in_emulator ? "E" : "",
2736 translate_state (ths.run_state),
2738 translate_cstate (cproc_state),
2740 print_tl_address (gdb_stdout, kthread->pc);
2744 /* These cprocs don't have a kernel thread.
2745 * find out the calling frame with
2746 * FETCH_CPROC_STATE.
2749 struct gdb_thread state;
2752 /* jtv -> emcmanus: why do you want this here? */
2753 if (scan->incarnation == NULL)
2754 continue; /* EMcM */
2757 printf_filtered (TL_FORMAT,
2759 -neworder, /* Pseudo MID */
2761 get_thread_name (scan, -neworder),
2763 "-", /* kernel state */
2765 translate_cstate (cproc_state),
2769 if (FETCH_CPROC_STATE (&state) == -1)
2770 puts_filtered ("???");
2772 print_tl_address (gdb_stdout, state.pc);
2776 puts_filtered ("\n");
2779 /* Scan for kernel threads without cprocs */
2780 for (index = 0; index < thread_count; index++)
2782 if (! their_threads[index].cproc)
2789 mach_port_t name = their_threads[index].name;
2791 mid = map_port_name_to_mid (name, MACH_TYPE_THREAD);
2793 infoCnt = THREAD_BASIC_INFO_COUNT;
2795 ret = thread_info(name,
2797 (thread_info_t)&ths,
2800 if (ret != KERN_SUCCESS)
2802 warning ("Unable to get basic info on thread %d : %s",
2804 mach_error_string (ret));
2808 sprintf (slot, "%d", index%100);
2810 if (name == current_thread)
2815 if (ths.suspend_count)
2816 sprintf (buf, "%d", ths.suspend_count);
2821 if (ths.flags & TH_FLAGS_SWAPPED)
2825 if (ths.flags & TH_FLAGS_IDLE)
2828 printf_filtered (TL_FORMAT,
2832 get_thread_name (NULL, index),
2833 their_threads[index].in_emulator ? "E" : "",
2834 translate_state (ths.run_state),
2836 "", /* No cproc state */
2837 ""); /* Can't be wired */
2838 print_tl_address (gdb_stdout, their_threads[index].pc);
2839 puts_filtered ("\n");
2843 obstack_free (cproc_obstack, 0);
2844 obstack_init (cproc_obstack);
2848 thread_select_command(args, from_tty)
2853 thread_array_t thread_list;
2858 MACH_ERROR_NO_INFERIOR;
2861 error_no_arg ("MID or @SLOTNUMBER to specify a thread to select");
2863 while (*args == ' ' || *args == '\t')
2875 if (!is_slot || *args != '0') /* Rudimentary checks */
2876 error ("You must select threads by MID or @SLOTNUMBER");
2878 if (select_thread (inferior_task, mid, is_slot?2:1) != KERN_SUCCESS)
2882 printf_filtered ("Thread %d selected\n",
2883 is_slot ? map_port_name_to_mid (current_thread,
2884 MACH_TYPE_THREAD) : mid);
2887 thread_trace (thread, set)
2891 int flavor = TRACE_FLAVOR;
2892 unsigned int stateCnt = TRACE_FLAVOR_SIZE;
2894 thread_state_data_t state;
2896 if (! MACH_PORT_VALID (thread))
2898 warning ("thread_trace: invalid thread");
2902 if (must_suspend_thread)
2903 setup_thread (thread, 1);
2905 ret = thread_get_state(thread, flavor, state, &stateCnt);
2906 CHK ("thread_trace: error reading thread state", ret);
2910 TRACE_SET (thread, state);
2914 if (! TRACE_CLEAR (thread, state))
2916 if (must_suspend_thread)
2917 setup_thread (thread, 0);
2922 ret = thread_set_state(thread, flavor, state, stateCnt);
2923 CHK ("thread_trace: error writing thread state", ret);
2924 if (must_suspend_thread)
2925 setup_thread (thread, 0);
2928 #ifdef FLUSH_INFERIOR_CACHE
2930 /* When over-writing code on some machines the I-Cache must be flushed
2931 explicitly, because it is not kept coherent by the lazy hardware.
2932 This definitely includes breakpoints, for instance, or else we
2933 end up looping in mysterious Bpt traps */
2935 flush_inferior_icache(pc, amount)
2938 vm_machine_attribute_val_t flush = MATTR_VAL_ICACHE_FLUSH;
2941 ret = vm_machine_attribute (inferior_task,
2946 if (ret != KERN_SUCCESS)
2947 warning ("Error flushing inferior's cache : %s",
2948 mach_error_string (ret));
2950 #endif FLUSH_INFERIOR_CACHE
2954 suspend_all_threads (from_tty)
2958 thread_array_t thread_list;
2959 int thread_count, index;
2961 thread_basic_info_data_t th_info;
2964 ret = task_threads (inferior_task, &thread_list, &thread_count);
2965 if (ret != KERN_SUCCESS)
2967 warning ("Could not suspend inferior threads.");
2968 m3_kill_inferior ();
2969 return_to_top_level (RETURN_ERROR);
2972 for (index = 0; index < thread_count; index++)
2976 mid = map_port_name_to_mid (thread_list[ index ],
2979 ret = thread_suspend(thread_list[ index ]);
2981 if (ret != KERN_SUCCESS)
2982 warning ("Error trying to suspend thread %d : %s",
2983 mid, mach_error_string (ret));
2987 infoCnt = THREAD_BASIC_INFO_COUNT;
2988 ret = thread_info (thread_list[ index ],
2990 (thread_info_t) &th_info,
2992 CHK ("suspend can't get thread info", ret);
2994 warning ("Thread %d suspend count is %d",
2995 mid, th_info.suspend_count);
2999 consume_send_rights (thread_list, thread_count);
3000 ret = vm_deallocate(mach_task_self(),
3001 (vm_address_t)thread_list,
3002 (thread_count * sizeof(int)));
3003 CHK ("Error trying to deallocate thread list", ret);
3007 thread_suspend_command (args, from_tty)
3013 mach_port_t saved_thread;
3015 thread_basic_info_data_t th_info;
3017 MACH_ERROR_NO_INFERIOR;
3019 if (!strcasecmp (args, "all")) {
3020 suspend_all_threads (from_tty);
3024 saved_thread = current_thread;
3026 mid = parse_thread_id (args, 0, 0);
3029 error ("You can suspend only existing kernel threads with MID or @SLOTNUMBER");
3032 mid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD);
3034 if (select_thread (inferior_task, mid, 0) != KERN_SUCCESS)
3037 current_thread = saved_thread;
3038 error ("Could not select thread %d", mid);
3041 ret = thread_suspend (current_thread);
3042 if (ret != KERN_SUCCESS)
3043 warning ("thread_suspend failed : %s",
3044 mach_error_string (ret));
3046 infoCnt = THREAD_BASIC_INFO_COUNT;
3047 ret = thread_info (current_thread,
3049 (thread_info_t) &th_info,
3051 CHK ("suspend can't get thread info", ret);
3053 warning ("Thread %d suspend count is %d", mid, th_info.suspend_count);
3055 current_thread = saved_thread;
3058 resume_all_threads (from_tty)
3062 thread_array_t thread_list;
3063 int thread_count, index;
3066 thread_basic_info_data_t th_info;
3068 ret = task_threads (inferior_task, &thread_list, &thread_count);
3069 if (ret != KERN_SUCCESS)
3071 m3_kill_inferior ();
3072 error("task_threads", mach_error_string( ret));
3075 for (index = 0; index < thread_count; index++)
3077 infoCnt = THREAD_BASIC_INFO_COUNT;
3078 ret = thread_info (thread_list [ index ],
3080 (thread_info_t) &th_info,
3082 CHK ("resume_all can't get thread info", ret);
3084 mid = map_port_name_to_mid (thread_list[ index ],
3087 if (! th_info.suspend_count)
3089 if (mid != -1 && from_tty)
3090 warning ("Thread %d is not suspended", mid);
3094 ret = thread_resume (thread_list[ index ]);
3096 if (ret != KERN_SUCCESS)
3097 warning ("Error trying to resume thread %d : %s",
3098 mid, mach_error_string (ret));
3099 else if (mid != -1 && from_tty)
3100 warning ("Thread %d suspend count is %d",
3101 mid, --th_info.suspend_count);
3104 consume_send_rights (thread_list, thread_count);
3105 ret = vm_deallocate(mach_task_self(),
3106 (vm_address_t)thread_list,
3107 (thread_count * sizeof(int)));
3108 CHK("Error trying to deallocate thread list", ret);
3112 thread_resume_command (args, from_tty)
3117 mach_port_t saved_thread;
3119 thread_basic_info_data_t th_info;
3120 int infoCnt = THREAD_BASIC_INFO_COUNT;
3122 MACH_ERROR_NO_INFERIOR;
3124 if (!strcasecmp (args, "all")) {
3125 resume_all_threads (from_tty);
3129 saved_thread = current_thread;
3131 mid = parse_thread_id (args, 0, 0);
3134 error ("You can resume only existing kernel threads with MID or @SLOTNUMBER");
3137 mid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD);
3139 if (select_thread (inferior_task, mid, 0) != KERN_SUCCESS)
3142 current_thread = saved_thread;
3143 return_to_top_level (RETURN_ERROR);
3146 ret = thread_info (current_thread,
3148 (thread_info_t) &th_info,
3150 CHK ("resume can't get thread info", ret);
3152 if (! th_info.suspend_count)
3154 warning ("Thread %d is not suspended", mid);
3158 ret = thread_resume (current_thread);
3159 if (ret != KERN_SUCCESS)
3160 warning ("thread_resume failed : %s",
3161 mach_error_string (ret));
3164 th_info.suspend_count--;
3165 warning ("Thread %d suspend count is %d", mid, th_info.suspend_count);
3169 current_thread = saved_thread;
3173 thread_kill_command (args, from_tty)
3180 thread_array_t thread_table;
3182 mach_port_t thread_to_kill = MACH_PORT_NULL;
3185 MACH_ERROR_NO_INFERIOR;
3188 error_no_arg ("thread mid to kill from the inferior task");
3190 mid = parse_thread_id (args, 0, 0);
3193 error ("You can kill only existing kernel threads with MID or @SLOTNUMBER");
3197 ret = machid_mach_port (mid_server, mid_auth, mid, &thread_to_kill);
3198 CHK ("thread_kill_command: machid_mach_port map failed", ret);
3201 mid = map_port_name_to_mid (current_thread, MACH_TYPE_THREAD);
3203 /* Don't allow gdb to kill *any* thread in the system. Use mkill program for that */
3204 ret = task_threads (inferior_task, &thread_table, &thread_count);
3205 CHK ("Error getting inferior's thread list", ret);
3207 if (thread_to_kill == current_thread)
3209 ret = thread_terminate (thread_to_kill);
3210 CHK ("Thread could not be terminated", ret);
3212 if (select_thread (inferior_task, 0, 1) != KERN_SUCCESS)
3213 warning ("Last thread was killed, use \"kill\" command to kill task");
3216 for (index = 0; index < thread_count; index++)
3217 if (thread_table [ index ] == thread_to_kill)
3219 ret = thread_terminate (thread_to_kill);
3220 CHK ("Thread could not be terminated", ret);
3223 if (thread_count > 1)
3224 consume_send_rights (thread_table, thread_count);
3226 ret = vm_deallocate (mach_task_self(), (vm_address_t)thread_table,
3227 (thread_count * sizeof(mach_port_t)));
3228 CHK ("Error trying to deallocate thread list", ret);
3230 warning ("Thread %d killed", mid);
3234 /* Task specific commands; add more if you like */
3237 task_resume_command (args, from_tty)
3242 task_basic_info_data_t ta_info;
3243 int infoCnt = TASK_BASIC_INFO_COUNT;
3244 int mid = map_port_name_to_mid (inferior_task, MACH_TYPE_TASK);
3246 MACH_ERROR_NO_INFERIOR;
3248 /* Would be trivial to change, but is it desirable? */
3250 error ("Currently gdb can resume only it's inferior task");
3252 ret = task_info (inferior_task,
3254 (task_info_t) &ta_info,
3256 CHK ("task_resume_command: task_info failed", ret);
3258 if (ta_info.suspend_count == 0)
3259 error ("Inferior task %d is not suspended", mid);
3260 else if (ta_info.suspend_count == 1 &&
3262 !query ("Suspend count is now 1. Do you know what you are doing? "))
3263 error ("Task not resumed");
3265 ret = task_resume (inferior_task);
3266 CHK ("task_resume_command: task_resume", ret);
3268 if (ta_info.suspend_count == 1)
3270 warning ("Inferior task %d is no longer suspended", mid);
3271 must_suspend_thread = 1;
3272 /* @@ This is not complete: Registers change all the time when not
3274 registers_changed ();
3277 warning ("Inferior task %d suspend count is now %d",
3278 mid, ta_info.suspend_count-1);
3283 task_suspend_command (args, from_tty)
3288 task_basic_info_data_t ta_info;
3289 int infoCnt = TASK_BASIC_INFO_COUNT;
3290 int mid = map_port_name_to_mid (inferior_task, MACH_TYPE_TASK);
3292 MACH_ERROR_NO_INFERIOR;
3294 /* Would be trivial to change, but is it desirable? */
3296 error ("Currently gdb can suspend only it's inferior task");
3298 ret = task_suspend (inferior_task);
3299 CHK ("task_suspend_command: task_suspend", ret);
3301 must_suspend_thread = 0;
3303 ret = task_info (inferior_task,
3305 (task_info_t) &ta_info,
3307 CHK ("task_suspend_command: task_info failed", ret);
3309 warning ("Inferior task %d suspend count is now %d",
3310 mid, ta_info.suspend_count);
3317 static char size [ 30 ];
3318 int zz = bytes/1024;
3321 sprintf (size, "%-2.1f M", ((float)bytes)/(1024.0*1024.0));
3323 sprintf (size, "%d K", zz);
3328 /* Does this require the target task to be suspended?? I don't think so. */
3330 task_info_command (args, from_tty)
3337 task_basic_info_data_t ta_info;
3338 int infoCnt = TASK_BASIC_INFO_COUNT;
3339 int page_size = round_page(1);
3340 int thread_count = 0;
3342 if (MACH_PORT_VALID (inferior_task))
3343 mid = map_port_name_to_mid (inferior_task,
3346 task = inferior_task;
3350 int tmid = atoi (args);
3353 error ("Invalid mid %d for task info", tmid);
3358 ret = machid_mach_port (mid_server, mid_auth, tmid, &task);
3359 CHK ("task_info_command: machid_mach_port map failed", ret);
3364 error ("You have to give the task MID as an argument");
3366 ret = task_info (task,
3368 (task_info_t) &ta_info,
3370 CHK ("task_info_command: task_info failed", ret);
3372 printf_filtered ("\nTask info for task %d:\n\n", mid);
3373 printf_filtered (" Suspend count : %d\n", ta_info.suspend_count);
3374 printf_filtered (" Base priority : %d\n", ta_info.base_priority);
3375 printf_filtered (" Virtual size : %s\n", get_size (ta_info.virtual_size));
3376 printf_filtered (" Resident size : %s\n", get_size (ta_info.resident_size));
3379 thread_array_t thread_list;
3381 ret = task_threads (task, &thread_list, &thread_count);
3382 CHK ("task_info_command: task_threads", ret);
3384 printf_filtered (" Thread count : %d\n", thread_count);
3386 consume_send_rights (thread_list, thread_count);
3387 ret = vm_deallocate(mach_task_self(),
3388 (vm_address_t)thread_list,
3389 (thread_count * sizeof(int)));
3390 CHK("Error trying to deallocate thread list", ret);
3392 if (have_emulator_p (task))
3393 printf_filtered (" Emulator at : 0x%x..0x%x\n",
3394 EMULATOR_BASE, EMULATOR_END);
3396 printf_filtered (" No emulator.\n");
3398 if (thread_count && task == inferior_task)
3399 printf_filtered ("\nUse the \"thread list\" command to see the threads\n");
3402 /* You may either FORWARD the exception to the inferior, or KEEP
3403 * it and return to GDB command level.
3405 * exception mid [ forward | keep ]
3409 exception_command (args, from_tty)
3418 error_no_arg ("exception number action");
3420 while (*scan == ' ' || *scan == '\t') scan++;
3422 if ('0' <= *scan && *scan <= '9')
3423 while ('0' <= *scan && *scan <= '9')
3426 error ("exception number action");
3428 exception = atoi (args);
3429 if (exception <= 0 || exception > MAX_EXCEPTION)
3430 error ("Allowed exception numbers are in range 1..%d",
3433 if (*scan != ' ' && *scan != '\t')
3434 error ("exception number must be followed by a space");
3436 while (*scan == ' ' || *scan == '\t') scan++;
3447 error("exception number action");
3449 if (!strncasecmp (args, "forward", len))
3450 exception_map[ exception ].forward = TRUE;
3451 else if (!strncasecmp (args, "keep", len))
3452 exception_map[ exception ].forward = FALSE;
3454 error ("exception action is either \"keep\" or \"forward\"");
3458 print_exception_info (exception)
3461 boolean_t forward = exception_map[ exception ].forward;
3463 printf_filtered ("%s\t(%d): ", exception_map[ exception ].name,
3466 if (exception_map[ exception ].sigmap != SIG_UNKNOWN)
3467 printf_filtered ("keep and handle as signal %d\n",
3468 exception_map[ exception ].sigmap);
3470 printf_filtered ("keep and handle as unknown signal %d\n",
3471 exception_map[ exception ].sigmap);
3473 printf_filtered ("forward exception to inferior\n");
3477 exception_info (args, from_tty)
3484 for (exception = 1; exception <= MAX_EXCEPTION; exception++)
3485 print_exception_info (exception);
3488 exception = atoi (args);
3490 if (exception <= 0 || exception > MAX_EXCEPTION)
3491 error ("Invalid exception number, values from 1 to %d allowed",
3493 print_exception_info (exception);
3497 /* Check for actions for mach exceptions.
3499 mach3_exception_actions (w, force_print_only, who)
3501 boolean_t force_print_only;
3504 boolean_t force_print = FALSE;
3507 if (force_print_only ||
3508 exception_map[stop_exception].sigmap == SIG_UNKNOWN)
3511 WSETSTOP (*w, exception_map[stop_exception].sigmap);
3513 if (exception_map[stop_exception].print || force_print)
3515 target_terminal_ours ();
3517 printf_filtered ("\n%s received %s exception : ",
3519 exception_map[stop_exception].name);
3523 switch(stop_exception) {
3524 case EXC_BAD_ACCESS:
3525 printf_filtered ("referencing address 0x%x : %s\n",
3527 mach_error_string (stop_code));
3529 case EXC_BAD_INSTRUCTION:
3531 ("illegal or undefined instruction. code %d subcode %d\n",
3532 stop_code, stop_subcode);
3534 case EXC_ARITHMETIC:
3535 printf_filtered ("code %d\n", stop_code);
3538 printf_filtered ("code %d subcode %d\n", stop_code, stop_subcode);
3541 printf_filtered ("%s specific, code 0x%x\n",
3542 stop_code < 0xffff ? "hardware" : "os emulation",
3545 case EXC_BREAKPOINT:
3546 printf_filtered ("type %d (machine dependent)\n",
3550 fatal ("Unknown exception");
3555 setup_notify_port (create_new)
3560 if (MACH_PORT_VALID (our_notify_port))
3562 ret = mach_port_destroy (mach_task_self (), our_notify_port);
3563 CHK ("Could not destroy our_notify_port", ret);
3566 our_notify_port = MACH_PORT_NULL;
3567 notify_chain = (port_chain_t) NULL;
3568 port_chain_destroy (port_chain_obstack);
3572 ret = mach_port_allocate (mach_task_self(),
3573 MACH_PORT_RIGHT_RECEIVE,
3575 if (ret != KERN_SUCCESS)
3576 fatal("Creating notify port %s", mach_error_string(ret));
3578 ret = mach_port_move_member(mach_task_self(),
3580 inferior_wait_port_set);
3581 if (ret != KERN_SUCCESS)
3582 fatal("initial move member %s",mach_error_string(ret));
3587 * Register our message port to the net name server
3589 * Currently used only by the external stop-gdb program
3590 * since ^C does not work if you would like to enter
3591 * gdb command level while debugging your program.
3593 * NOTE: If the message port is sometimes used for other
3594 * purposes also, the NAME must not be a guessable one.
3595 * Then, there should be a way to change it.
3598 char registered_name[ MAX_NAME_LEN ];
3601 message_port_info (args, from_tty)
3605 if (registered_name[0])
3606 printf_filtered ("gdb's message port name: '%s'\n",
3609 printf_filtered ("gdb's message port is not currently registered\n");
3613 gdb_register_port (name, port)
3618 static int already_signed = 0;
3621 if (! MACH_PORT_VALID (port) || !name || !*name)
3623 warning ("Invalid registration request");
3627 if (! already_signed)
3629 ret = mach_port_insert_right (mach_task_self (),
3632 MACH_MSG_TYPE_MAKE_SEND);
3633 CHK ("Failed to create a signature to our_message_port", ret);
3636 else if (already_signed > 1)
3638 ret = netname_check_out (name_server_port,
3641 CHK ("Failed to check out gdb's message port", ret);
3642 registered_name[0] = '\000';
3646 ret = netname_check_in (name_server_port, /* Name server port */
3647 name, /* Name of service */
3648 our_message_port, /* Signature */
3649 port); /* Creates a new send right */
3650 CHK("Failed to check in the port", ret);
3653 while(len < MAX_NAME_LEN && *(name+len))
3655 registered_name[len] = *(name+len);
3658 registered_name[len] = '\000';
3662 struct cmd_list_element *cmd_thread_list;
3663 struct cmd_list_element *cmd_task_list;
3667 thread_command (arg, from_tty)
3671 printf_unfiltered ("\"thread\" must be followed by the name of a thread command.\n");
3672 help_list (cmd_thread_list, "thread ", -1, gdb_stdout);
3677 task_command (arg, from_tty)
3681 printf_unfiltered ("\"task\" must be followed by the name of a task command.\n");
3682 help_list (cmd_task_list, "task ", -1, gdb_stdout);
3685 add_mach_specific_commands ()
3687 /* Thread handling commands */
3689 /* FIXME: Move our thread support into the generic thread.c stuff so we
3690 can share that code. */
3691 add_prefix_cmd ("mthread", class_stack, thread_command,
3692 "Generic command for handling Mach threads in the debugged task.",
3693 &cmd_thread_list, "thread ", 0, &cmdlist);
3695 add_com_alias ("th", "mthread", class_stack, 1);
3697 add_cmd ("select", class_stack, thread_select_command,
3698 "Select and print MID of the selected thread",
3700 add_cmd ("list", class_stack, thread_list_command,
3701 "List info of task's threads. Selected thread is marked with '*'",
3703 add_cmd ("suspend", class_run, thread_suspend_command,
3704 "Suspend one or all of the threads in the selected task.",
3706 add_cmd ("resume", class_run, thread_resume_command,
3707 "Resume one or all of the threads in the selected task.",
3709 add_cmd ("kill", class_run, thread_kill_command,
3710 "Kill the specified thread MID from inferior task.",
3713 /* The rest of this support (condition_thread) was not merged. It probably
3714 should not be merged in this form, but instead added to the generic GDB
3716 add_cmd ("break", class_breakpoint, condition_thread,
3717 "Breakpoint N will only be effective for thread MID or @SLOT\n\
3718 If MID/@SLOT is omitted allow all threads to break at breakpoint",
3721 /* Thread command shorthands (for backward compatibility) */
3722 add_alias_cmd ("ts", "mthread select", 0, 0, &cmdlist);
3723 add_alias_cmd ("tl", "mthread list", 0, 0, &cmdlist);
3725 /* task handling commands */
3727 add_prefix_cmd ("task", class_stack, task_command,
3728 "Generic command for handling debugged task.",
3729 &cmd_task_list, "task ", 0, &cmdlist);
3731 add_com_alias ("ta", "task", class_stack, 1);
3733 add_cmd ("suspend", class_run, task_suspend_command,
3734 "Suspend the inferior task.",
3736 add_cmd ("resume", class_run, task_resume_command,
3737 "Resume the inferior task.",
3739 add_cmd ("info", no_class, task_info_command,
3740 "Print information about the specified task.",
3743 /* Print my message port name */
3745 add_info ("message-port", message_port_info,
3746 "Returns the name of gdb's message port in the netnameserver");
3748 /* Exception commands */
3750 add_info ("exceptions", exception_info,
3751 "What debugger does when program gets various exceptions.\n\
3752 Specify an exception number as argument to print info on that\n\
3755 add_com ("exception", class_run, exception_command,
3756 "Specify how to handle an exception.\n\
3757 Args are exception number followed by \"forward\" or \"keep\".\n\
3758 `Forward' means forward the exception to the program's normal exception\n\
3760 `Keep' means reenter debugger if this exception happens, and GDB maps\n\
3761 the exception to some signal (see info exception)\n\
3762 Normally \"keep\" is used to return to GDB on exception.");
3766 do_mach_notify_dead_name (notify, name)
3770 kern_return_t kr = KERN_SUCCESS;
3772 /* Find the thing that notified */
3773 port_chain_t element = port_chain_member (notify_chain, name);
3775 /* Take name of from unreceived dead name notification list */
3776 notify_chain = port_chain_delete (notify_chain, name);
3779 error ("Received a dead name notify from unchained port (0x%x)", name);
3781 switch (element->type) {
3783 case MACH_TYPE_THREAD:
3784 target_terminal_ours_for_output ();
3785 if (name == current_thread)
3787 printf_filtered ("\nCurrent thread %d died", element->mid);
3788 current_thread = MACH_PORT_NULL;
3791 printf_filtered ("\nThread %d died", element->mid);
3795 case MACH_TYPE_TASK:
3796 target_terminal_ours_for_output ();
3797 if (name != inferior_task)
3798 printf_filtered ("Task %d died, but it was not the selected task",
3802 printf_filtered ("Current task %d died", element->mid);
3804 mach_port_destroy (mach_task_self(), name);
3805 inferior_task = MACH_PORT_NULL;
3808 warning ("There were still unreceived dead_name_notifications???");
3810 /* Destroy the old notifications */
3811 setup_notify_port (0);
3817 error ("Unregistered dead_name 0x%x notification received. Type is %d, mid is 0x%x",
3818 name, element->type, element->mid);
3822 return KERN_SUCCESS;
3826 do_mach_notify_msg_accepted (notify, name)
3830 warning ("do_mach_notify_msg_accepted : notify %x, name %x",
3832 return KERN_SUCCESS;
3836 do_mach_notify_no_senders (notify, mscount)
3838 mach_port_mscount_t mscount;
3840 warning ("do_mach_notify_no_senders : notify %x, mscount %x",
3842 return KERN_SUCCESS;
3846 do_mach_notify_port_deleted (notify, name)
3850 warning ("do_mach_notify_port_deleted : notify %x, name %x",
3852 return KERN_SUCCESS;
3856 do_mach_notify_port_destroyed (notify, rights)
3860 warning ("do_mach_notify_port_destroyed : notify %x, rights %x",
3862 return KERN_SUCCESS;
3866 do_mach_notify_send_once (notify)
3870 /* MANY of these are generated. */
3871 warning ("do_mach_notify_send_once : notify %x",
3874 return KERN_SUCCESS;
3877 /* Kills the inferior. It's gone when you call this */
3879 kill_inferior_fast ()
3883 if (inferior_pid == 0 || inferior_pid == 1)
3886 /* kill() it, since the Unix server does not otherwise notice when
3887 * killed with task_terminate().
3889 if (inferior_pid > 0)
3890 kill (inferior_pid, SIGKILL);
3892 /* It's propably terminate already */
3893 (void) task_terminate (inferior_task);
3895 inferior_task = MACH_PORT_NULL;
3896 current_thread = MACH_PORT_NULL;
3898 wait3 (&w, WNOHANG, 0);
3900 setup_notify_port (0);
3906 kill_inferior_fast ();
3907 target_mourn_inferior ();
3910 /* Clean up after the inferior dies. */
3913 m3_mourn_inferior ()
3915 unpush_target (&m3_ops);
3916 generic_mourn_inferior ();
3920 /* Fork an inferior process, and start debugging it. */
3923 m3_create_inferior (exec_file, allargs, env)
3928 fork_inferior (exec_file, allargs, env, m3_trace_me, m3_trace_him, NULL);
3929 /* We are at the first instruction we care about. */
3930 /* Pedal to the metal... */
3931 proceed ((CORE_ADDR) -1, 0, 0);
3934 /* Mark our target-struct as eligible for stray "run" and "attach"
3942 /* Mach 3.0 does not need ptrace for anything
3943 * Make sure nobody uses it on mach.
3948 error ("Lose, Lose! Somebody called ptrace\n");
3951 /* Resume execution of the inferior process.
3952 If STEP is nonzero, single-step it.
3953 If SIGNAL is nonzero, give it that signal. */
3956 m3_resume (pid, step, signal)
3959 enum target_signal signal;
3965 thread_basic_info_data_t th_info;
3966 unsigned int infoCnt = THREAD_BASIC_INFO_COUNT;
3968 /* There is no point in single stepping when current_thread
3971 if (! MACH_PORT_VALID (current_thread))
3972 error ("No thread selected; can not single step");
3974 /* If current_thread is suspended, tracing it would never return.
3976 ret = thread_info (current_thread,
3978 (thread_info_t) &th_info,
3980 CHK ("child_resume: can't get thread info", ret);
3982 if (th_info.suspend_count)
3983 error ("Can't trace a suspended thread. Use \"thread resume\" command to resume it");
3986 vm_read_cache_valid = FALSE;
3988 if (signal && inferior_pid > 0) /* Do not signal, if attached by MID */
3989 kill (inferior_pid, target_signal_to_host (signal));
3993 suspend_all_threads (0);
3995 setup_single_step (current_thread, TRUE);
3997 ret = thread_resume (current_thread);
3998 CHK ("thread_resume", ret);
4001 ret = task_resume (inferior_task);
4002 if (ret == KERN_FAILURE)
4003 warning ("Task was not suspended");
4005 CHK ("Resuming task", ret);
4007 /* HACK HACK This is needed by the multiserver system HACK HACK */
4008 while ((ret = task_resume(inferior_task)) == KERN_SUCCESS)
4009 /* make sure it really runs */;
4010 /* HACK HACK This is needed by the multiserver system HACK HACK */
4013 #ifdef ATTACH_DETACH
4015 /* Start debugging the process with the given task */
4021 inferior_task = tid;
4023 ret = task_suspend (inferior_task);
4024 CHK("task_attach: task_suspend", ret);
4026 must_suspend_thread = 0;
4028 setup_notify_port (1);
4030 request_notify (inferior_task, MACH_NOTIFY_DEAD_NAME, MACH_TYPE_TASK);
4032 setup_exception_port ();
4034 emulator_present = have_emulator_p (inferior_task);
4039 /* Well, we can call error also here and leave the
4040 * target stack inconsistent. Sigh.
4041 * Fix this sometime (the only way to fail here is that
4042 * the task has no threads at all, which is rare, but
4043 * possible; or if the target task has died, which is also
4044 * possible, but unlikely, since it has been suspended.
4045 * (Someone must have killed it))
4050 if (select_thread (inferior_task, 0, 1) != KERN_SUCCESS)
4051 error ("Could not select any threads to attach to");
4059 ret = machid_mach_port (mid_server, mid_auth, mid, &inferior_task);
4060 CHK("mid_attach: machid_mach_port", ret);
4062 task_attach (inferior_task);
4068 * Start debugging the process whose unix process-id is PID.
4069 * A negative "pid" value is legal and signifies a mach_id not a unix pid.
4071 * Prevent (possible unwanted) dangerous operations by enabled users
4072 * like "atta 0" or "atta foo" (equal to the previous :-) and
4073 * "atta pidself". Anyway, the latter is allowed by specifying a MID.
4082 error("MID=0, Debugging the master unix server does not compute");
4084 /* Foo. This assumes gdb has a unix pid */
4085 if (pid == getpid())
4086 error ("I will debug myself only by mid. (Gdb would suspend itself!)");
4090 mid_attach (-(pid));
4092 /* inferior_pid will be NEGATIVE! */
4095 return inferior_pid;
4098 inferior_task = task_by_pid (pid);
4099 if (! MACH_PORT_VALID (inferior_task))
4100 error("Cannot map Unix pid %d to Mach task port", pid);
4102 task_attach (inferior_task);
4106 return inferior_pid;
4109 /* Attach to process PID, then initialize for debugging it
4110 and wait for the trace-trap that results from attaching. */
4113 m3_attach (args, from_tty)
4121 error_no_arg ("process-id to attach");
4125 if (pid == getpid()) /* Trying to masturbate? */
4126 error ("I refuse to debug myself!");
4130 exec_file = (char *) get_exec_file (0);
4133 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid));
4135 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid));
4137 gdb_flush (gdb_stdout);
4142 push_target (&m3_ops);
4146 deallocate_inferior_ports ()
4149 thread_array_t thread_list;
4150 int thread_count, index;
4152 if (!MACH_PORT_VALID (inferior_task))
4155 ret = task_threads (inferior_task, &thread_list, &thread_count);
4156 if (ret != KERN_SUCCESS)
4158 warning ("deallocate_inferior_ports: task_threads",
4159 mach_error_string(ret));
4163 /* Get rid of send rights to task threads */
4164 for (index = 0; index < thread_count; index++)
4167 ret = mach_port_get_refs (mach_task_self (),
4169 MACH_PORT_RIGHT_SEND,
4171 CHK("deallocate_inferior_ports: get refs", ret);
4175 ret = mach_port_mod_refs (mach_task_self (),
4177 MACH_PORT_RIGHT_SEND,
4179 CHK("deallocate_inferior_ports: mod refs", ret);
4183 ret = mach_port_mod_refs (mach_task_self (),
4184 inferior_exception_port,
4185 MACH_PORT_RIGHT_RECEIVE,
4187 CHK ("deallocate_inferior_ports: cannot get rid of exception port", ret);
4189 ret = mach_port_deallocate (mach_task_self (),
4191 CHK ("deallocate_task_port: deallocating inferior_task", ret);
4193 current_thread = MACH_PORT_NULL;
4194 inferior_task = MACH_PORT_NULL;
4197 /* Stop debugging the process whose number is PID
4198 and continue it with signal number SIGNAL.
4199 SIGNAL = 0 means just continue it. */
4202 m3_do_detach (signal)
4207 MACH_ERROR_NO_INFERIOR;
4209 if (current_thread != MACH_PORT_NULL)
4211 /* Store the gdb's view of the thread we are deselecting
4213 * @@ I am really not sure if this is ever needeed.
4215 target_prepare_to_store ();
4216 target_store_registers (-1);
4219 ret = task_set_special_port (inferior_task,
4220 TASK_EXCEPTION_PORT,
4221 inferior_old_exception_port);
4222 CHK ("task_set_special_port", ret);
4224 /* Discard all requested notifications */
4225 setup_notify_port (0);
4227 if (remove_breakpoints ())
4228 warning ("Could not remove breakpoints when detaching");
4230 if (signal && inferior_pid > 0)
4231 kill (inferior_pid, signal);
4233 /* the task might be dead by now */
4234 (void) task_resume (inferior_task);
4236 deallocate_inferior_ports ();
4241 /* Take a program previously attached to and detaches it.
4242 The program resumes execution and will no longer stop
4243 on signals, etc. We'd better not have left any breakpoints
4244 in the program or it'll die when it hits one. For this
4245 to work, it may be necessary for the process to have been
4246 previously attached. It *might* work if the program was
4247 started via fork. */
4250 m3_detach (args, from_tty)
4258 char *exec_file = get_exec_file (0);
4261 printf_unfiltered ("Detaching from program: %s %s\n",
4262 exec_file, target_pid_to_str (inferior_pid));
4263 gdb_flush (gdb_stdout);
4266 siggnal = atoi (args);
4268 m3_do_detach (siggnal);
4270 unpush_target (&m3_ops); /* Pop out of handling an inferior */
4272 #endif /* ATTACH_DETACH */
4274 /* Get ready to modify the registers array. On machines which store
4275 individual registers, this doesn't need to do anything. On machines
4276 which store all the registers in one fell swoop, this makes sure
4277 that registers contains all the registers from the program being
4281 m3_prepare_to_store ()
4283 #ifdef CHILD_PREPARE_TO_STORE
4284 CHILD_PREPARE_TO_STORE ();
4288 /* Print status information about what we're accessing. */
4291 m3_files_info (ignore)
4292 struct target_ops *ignore;
4294 /* FIXME: should print MID and all that crap. */
4295 printf_unfiltered ("\tUsing the running image of %s %s.\n",
4296 attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
4300 m3_open (arg, from_tty)
4304 error ("Use the \"run\" command to start a Unix child process.");
4314 char *bsd1_names[] = {
4382 int bsd1_nnames = sizeof(bsd1_names)/sizeof(bsd1_names[0]);
4392 case MACH_MSG_TYPE_BOOLEAN:
4394 case MACH_MSG_TYPE_INTEGER_16:
4396 case MACH_MSG_TYPE_INTEGER_32:
4398 case MACH_MSG_TYPE_CHAR:
4400 case MACH_MSG_TYPE_BYTE:
4402 case MACH_MSG_TYPE_REAL:
4404 case MACH_MSG_TYPE_STRING:
4407 sprintf(buf,"%d",name);
4420 if (id >= 101000 && id < 101000+bsd1_nnames) {
4421 if (p = bsd1_names[id-101000])
4425 return "psignal_retry";
4428 sprintf(buf,"%d",id);
4433 mach_msg_header_t *mp;
4435 char *fmt_x = "%20s : 0x%08x\n";
4436 char *fmt_d = "%20s : %10d\n";
4437 char *fmt_s = "%20s : %s\n";
4440 puts_filtered ("\n");
4441 #define pr(fmt,h,x) printf_filtered(fmt,STR(x),(h).x)
4442 pr(fmt_x,(*mp),msgh_bits);
4443 pr(fmt_d,(*mp),msgh_size);
4444 pr(fmt_x,(*mp),msgh_remote_port);
4445 pr(fmt_x,(*mp),msgh_local_port);
4446 pr(fmt_d,(*mp),msgh_kind);
4447 printf_filtered(fmt_s,STR(msgh_id),id_str(mp->msgh_id,buf));
4449 if (debug_level > 1)
4454 ep = p+mp->msgh_size;
4456 for(; p < ep; p += plen) {
4457 mach_msg_type_t *tp;
4458 mach_msg_type_long_t *tlp;
4459 int name,size,number;
4460 tp = (mach_msg_type_t*)p;
4461 if (tp->msgt_longform) {
4462 tlp = (mach_msg_type_long_t*)tp;
4463 name = tlp->msgtl_name;
4464 size = tlp->msgtl_size;
4465 number = tlp->msgtl_number;
4466 plen = sizeof(*tlp);
4468 name = tp->msgt_name;
4469 size = tp->msgt_size;
4470 number = tp->msgt_number;
4473 printf_filtered("name=%-16s size=%2d number=%7d inline=%d long=%d deal=%d\n",
4474 name_str(name,buf),size,number,tp->msgt_inline,
4475 tp->msgt_longform, tp->msgt_deallocate);
4477 if (tp->msgt_inline) {
4480 l = (l+sizeof(long)-1)&~((sizeof(long))-1);
4482 print_data(dp,size,number);
4484 plen += sizeof(int*);
4486 printf_filtered("plen=%d\n",plen);
4491 print_data(p,size,number)
4502 for(i = 0; i < number; i++) {
4503 printf_filtered(" %02x",p[i]);
4508 for(i = 0; i < number; i++) {
4509 printf_filtered(" %04x",sp[i]);
4514 for(i = 0; i < number; i++) {
4515 printf_filtered(" %08x",ip[i]);
4519 puts_filtered("\n");
4526 error ("to_stop target function not implemented");
4529 struct target_ops m3_ops = {
4530 "mach", /* to_shortname */
4531 "Mach child process", /* to_longname */
4532 "Mach child process (started by the \"run\" command).", /* to_doc */
4533 m3_open, /* to_open */
4535 m3_attach, /* to_attach */
4536 m3_detach, /* to_detach */
4537 m3_resume, /* to_resume */
4538 mach_really_wait, /* to_wait */
4539 fetch_inferior_registers, /* to_fetch_registers */
4540 store_inferior_registers, /* to_store_registers */
4541 m3_prepare_to_store, /* to_prepare_to_store */
4542 m3_xfer_memory, /* to_xfer_memory */
4543 m3_files_info, /* to_files_info */
4544 memory_insert_breakpoint, /* to_insert_breakpoint */
4545 memory_remove_breakpoint, /* to_remove_breakpoint */
4546 terminal_init_inferior, /* to_terminal_init */
4547 terminal_inferior, /* to_terminal_inferior */
4548 terminal_ours_for_output, /* to_terminal_ours_for_output */
4549 terminal_ours, /* to_terminal_ours */
4550 child_terminal_info, /* to_terminal_info */
4551 m3_kill_inferior, /* to_kill */
4553 0, /* to_lookup_symbol */
4555 m3_create_inferior, /* to_create_inferior */
4556 m3_mourn_inferior, /* to_mourn_inferior */
4557 m3_can_run, /* to_can_run */
4558 0, /* to_notice_signals */
4559 0, /* to_thread_alive */
4560 m3_stop, /* to_stop */
4561 process_stratum, /* to_stratum */
4563 1, /* to_has_all_memory */
4564 1, /* to_has_memory */
4565 1, /* to_has_stack */
4566 1, /* to_has_registers */
4567 1, /* to_has_execution */
4569 0, /* sections_end */
4570 OPS_MAGIC /* to_magic */
4574 _initialize_m3_nat ()
4578 add_target (&m3_ops);
4580 ret = mach_port_allocate(mach_task_self(),
4581 MACH_PORT_RIGHT_PORT_SET,
4582 &inferior_wait_port_set);
4583 if (ret != KERN_SUCCESS)
4584 fatal("initial port set %s",mach_error_string(ret));
4586 /* mach_really_wait now waits for this */
4587 currently_waiting_for = inferior_wait_port_set;
4589 ret = netname_look_up(name_server_port, hostname, "MachID", &mid_server);
4590 if (ret != KERN_SUCCESS)
4592 mid_server = MACH_PORT_NULL;
4594 warning ("initialize machid: netname_lookup_up(MachID) : %s",
4595 mach_error_string(ret));
4596 warning ("Some (most?) features disabled...");
4599 mid_auth = mach_privileged_host_port();
4600 if (mid_auth == MACH_PORT_NULL)
4601 mid_auth = mach_task_self();
4603 obstack_init (port_chain_obstack);
4605 ret = mach_port_allocate (mach_task_self (),
4606 MACH_PORT_RIGHT_RECEIVE,
4607 &thread_exception_port);
4608 CHK ("Creating thread_exception_port for single stepping", ret);
4610 ret = mach_port_insert_right (mach_task_self (),
4611 thread_exception_port,
4612 thread_exception_port,
4613 MACH_MSG_TYPE_MAKE_SEND);
4614 CHK ("Inserting send right to thread_exception_port", ret);
4616 /* Allocate message port */
4617 ret = mach_port_allocate (mach_task_self (),
4618 MACH_PORT_RIGHT_RECEIVE,
4620 if (ret != KERN_SUCCESS)
4621 warning ("Creating message port %s", mach_error_string (ret));
4624 char buf[ MAX_NAME_LEN ];
4625 ret = mach_port_move_member(mach_task_self (),
4627 inferior_wait_port_set);
4628 if (ret != KERN_SUCCESS)
4629 warning ("message move member %s", mach_error_string (ret));
4632 /* @@@@ No way to change message port name currently */
4633 /* Foo. This assumes gdb has a unix pid */
4634 sprintf (buf, "gdb-%d", getpid ());
4635 gdb_register_port (buf, our_message_port);
4638 /* Heap for thread commands */
4639 obstack_init (cproc_obstack);
4641 add_mach_specific_commands ();