1 /* Darwin support for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2008, 2009
3 Free Software Foundation, Inc.
5 Contributed by Apple Computer, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* The name of the ppc_thread_state structure, and the names of its
23 members, have been changed for Unix conformance reasons. The easiest
24 way to have gdb build on systems with the older names and systems
25 with the newer names is to build this compilation unit with the
26 non-conformant define below. This doesn't seem to cause the resulting
27 binary any problems but it seems like it could cause us problems in
28 the future. It'd be good to remove this at some point when compiling on
29 Tiger is no longer important. */
39 #include <sys/param.h>
40 #include <sys/sysctl.h>
42 #include "darwin-nat.h"
44 #include <mach/thread_info.h>
45 #include <mach/thread_act.h>
46 #include <mach/task.h>
47 #include <mach/vm_map.h>
48 #include <mach/mach_port.h>
49 #include <mach/mach_init.h>
50 #include <mach/mach_vm.h>
52 #define CHECK_ARGS(what, args) do { \
53 if ((NULL == args) || ((args[0] != '0') && (args[1] != 'x'))) \
54 error("%s must be specified with 0x...", what); \
57 #define PRINT_FIELD(structure, field) \
58 printf_unfiltered(_(#field":\t%#lx\n"), (unsigned long) (structure)->field)
60 #define PRINT_TV_FIELD(structure, field) \
61 printf_unfiltered(_(#field":\t%u.%06u sec\n"), \
62 (unsigned) (structure)->field.seconds, \
63 (unsigned) (structure)->field.microseconds)
65 #define task_self mach_task_self
66 #define task_by_unix_pid task_for_pid
67 #define port_name_array_t mach_port_array_t
68 #define port_type_array_t mach_port_array_t
71 info_mach_tasks_command (char *args, int from_tty)
76 struct kinfo_proc *procInfo;
78 sysControl[0] = CTL_KERN;
79 sysControl[1] = KERN_PROC;
80 sysControl[2] = KERN_PROC_ALL;
82 sysctl (sysControl, 3, NULL, &length, NULL, 0);
83 procInfo = (struct kinfo_proc *) xmalloc (length);
84 sysctl (sysControl, 3, procInfo, &length, NULL, 0);
86 count = (length / sizeof (struct kinfo_proc));
87 printf_unfiltered (_("%d processes:\n"), count);
88 for (index = 0; index < count; ++index)
94 task_by_unix_pid (mach_task_self (), procInfo[index].kp_proc.p_pid,
96 if (KERN_SUCCESS == result)
98 printf_unfiltered (_(" %s is %d has task %#x\n"),
99 procInfo[index].kp_proc.p_comm,
100 procInfo[index].kp_proc.p_pid, taskPort);
104 printf_unfiltered (_(" %s is %d unknown task port\n"),
105 procInfo[index].kp_proc.p_comm,
106 procInfo[index].kp_proc.p_pid);
114 get_task_from_args (char *args)
119 if (args == NULL || *args == 0)
121 if (darwin_inf->task == TASK_NULL)
122 printf_unfiltered (_("No inferior running\n"));
123 return darwin_inf->task;
125 if (strcmp (args, "gdb") == 0)
126 return mach_task_self ();
127 task = strtoul (args, &eptr, 0);
130 printf_unfiltered (_("cannot parse task id '%s'\n"), args);
137 info_mach_task_command (char *args, int from_tty)
141 struct task_basic_info basic;
142 struct task_events_info events;
143 struct task_thread_times_info thread_times;
146 kern_return_t result;
147 unsigned int info_count;
150 task = get_task_from_args (args);
151 if (task == TASK_NULL)
154 printf_unfiltered (_("TASK_BASIC_INFO for 0x%x:\n"), task);
155 info_count = TASK_BASIC_INFO_COUNT;
156 result = task_info (task,
158 (task_info_t) & task_info_data.basic, &info_count);
159 MACH_CHECK_ERROR (result);
161 PRINT_FIELD (&task_info_data.basic, suspend_count);
162 PRINT_FIELD (&task_info_data.basic, virtual_size);
163 PRINT_FIELD (&task_info_data.basic, resident_size);
164 PRINT_TV_FIELD (&task_info_data.basic, user_time);
165 PRINT_TV_FIELD (&task_info_data.basic, system_time);
166 printf_unfiltered (_("\nTASK_EVENTS_INFO:\n"));
167 info_count = TASK_EVENTS_INFO_COUNT;
168 result = task_info (task,
170 (task_info_t) & task_info_data.events, &info_count);
171 MACH_CHECK_ERROR (result);
173 PRINT_FIELD (&task_info_data.events, faults);
175 PRINT_FIELD (&task_info_data.events, zero_fills);
176 PRINT_FIELD (&task_info_data.events, reactivations);
178 PRINT_FIELD (&task_info_data.events, pageins);
179 PRINT_FIELD (&task_info_data.events, cow_faults);
180 PRINT_FIELD (&task_info_data.events, messages_sent);
181 PRINT_FIELD (&task_info_data.events, messages_received);
182 printf_unfiltered (_("\nTASK_THREAD_TIMES_INFO:\n"));
183 info_count = TASK_THREAD_TIMES_INFO_COUNT;
184 result = task_info (task,
185 TASK_THREAD_TIMES_INFO,
186 (task_info_t) & task_info_data.thread_times,
188 MACH_CHECK_ERROR (result);
189 PRINT_TV_FIELD (&task_info_data.thread_times, user_time);
190 PRINT_TV_FIELD (&task_info_data.thread_times, system_time);
194 info_mach_ports_command (char *args, int from_tty)
196 port_name_array_t names;
197 port_type_array_t types;
198 unsigned int name_count, type_count;
199 kern_return_t result;
203 task = get_task_from_args (args);
204 if (task == TASK_NULL)
207 result = mach_port_names (task, &names, &name_count, &types, &type_count);
208 MACH_CHECK_ERROR (result);
210 gdb_assert (name_count == type_count);
212 printf_unfiltered (_("Ports for task 0x%x:\n"), task);
213 printf_unfiltered (_("port type\n"));
214 for (index = 0; index < name_count; ++index)
216 mach_port_t port = names[index];
220 mach_port_type_t type;
222 mach_port_right_t right;
224 static struct type_descr descrs[] =
226 {MACH_PORT_TYPE_SEND, "send", MACH_PORT_RIGHT_SEND},
227 {MACH_PORT_TYPE_SEND_ONCE, "send-once", MACH_PORT_RIGHT_SEND_ONCE},
228 {MACH_PORT_TYPE_RECEIVE, "receive", MACH_PORT_RIGHT_RECEIVE},
229 {MACH_PORT_TYPE_PORT_SET, "port-set", MACH_PORT_RIGHT_PORT_SET},
230 {MACH_PORT_TYPE_DEAD_NAME, "dead", MACH_PORT_RIGHT_DEAD_NAME}
233 printf_unfiltered (_("%04x: %08x "), port, types[index]);
234 for (j = 0; j < sizeof(descrs) / sizeof(*descrs); j++)
235 if (types[index] & descrs[j].type)
237 mach_port_urefs_t ref;
240 printf_unfiltered (_(" %s("), descrs[j].name);
241 ret = mach_port_get_refs (task, port, descrs[j].right, &ref);
242 if (ret != KERN_SUCCESS)
243 printf_unfiltered (_("??"));
245 printf_unfiltered (_("%u"), ref);
246 printf_unfiltered (_(" refs)"));
249 if (task == task_self ())
251 if (port == task_self())
252 printf_unfiltered (_(" gdb-task"));
253 else if (port == darwin_host_self)
254 printf_unfiltered (_(" host-self"));
255 else if (port == darwin_not_port)
256 printf_unfiltered (_(" gdb-notifier"));
257 else if (port == darwin_ex_port)
258 printf_unfiltered (_(" gdb-exception"));
259 else if (port == darwin_port_set)
260 printf_unfiltered (_(" gdb-port_set"));
261 else if (darwin_inf && port == darwin_inf->task)
262 printf_unfiltered (_(" inferior-task"));
263 else if (darwin_inf && darwin_inf->threads)
267 for (k = 0; VEC_iterate(thread_t, darwin_inf->threads, k, t); k++)
270 printf_unfiltered (_(" inferior-thread for 0x%x"),
276 printf_unfiltered (_("\n"));
279 vm_deallocate (task_self (), (vm_address_t) names,
280 (name_count * sizeof (mach_port_t)));
281 vm_deallocate (task_self (), (vm_address_t) types,
282 (type_count * sizeof (mach_port_type_t)));
287 darwin_debug_port_info (task_t task, mach_port_t port)
290 mach_port_status_t status;
291 mach_msg_type_number_t len = sizeof (status);
293 kret = mach_port_get_attributes
294 (task, port, MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &len);
295 MACH_CHECK_ERROR (kret);
297 printf_unfiltered (_("Port 0x%lx in task 0x%lx:\n"), (unsigned long) port,
298 (unsigned long) task);
299 printf_unfiltered (_(" port set: 0x%x\n"), status.mps_pset);
300 printf_unfiltered (_(" seqno: 0x%x\n"), status.mps_seqno);
301 printf_unfiltered (_(" mscount: 0x%x\n"), status.mps_mscount);
302 printf_unfiltered (_(" qlimit: 0x%x\n"), status.mps_qlimit);
303 printf_unfiltered (_(" msgcount: 0x%x\n"), status.mps_msgcount);
304 printf_unfiltered (_(" sorights: 0x%x\n"), status.mps_sorights);
305 printf_unfiltered (_(" srights: 0x%x\n"), status.mps_srights);
306 printf_unfiltered (_(" pdrequest: 0x%x\n"), status.mps_pdrequest);
307 printf_unfiltered (_(" nsrequest: 0x%x\n"), status.mps_nsrequest);
308 printf_unfiltered (_(" flags: 0x%x\n"), status.mps_flags);
312 info_mach_port_command (char *args, int from_tty)
317 CHECK_ARGS (_("Task and port"), args);
318 sscanf (args, "0x%x 0x%x", &task, &port);
320 darwin_debug_port_info (task, port);
324 info_mach_threads_command (char *args, int from_tty)
326 thread_array_t threads;
327 unsigned int thread_count;
328 kern_return_t result;
332 task = get_task_from_args (args);
333 if (task == TASK_NULL)
336 result = task_threads (task, &threads, &thread_count);
337 MACH_CHECK_ERROR (result);
339 printf_unfiltered (_("Threads in task %#x:\n"), task);
340 for (i = 0; i < thread_count; ++i)
342 printf_unfiltered (_(" %#x\n"), threads[i]);
343 mach_port_deallocate (task_self (), threads[i]);
346 vm_deallocate (task_self (), (vm_address_t) threads,
347 (thread_count * sizeof (thread_t)));
351 info_mach_thread_command (char *args, int from_tty)
355 struct thread_basic_info basic;
359 kern_return_t result;
360 unsigned int info_count;
362 CHECK_ARGS (_("Thread"), args);
363 sscanf (args, "0x%x", &thread);
365 printf_unfiltered (_("THREAD_BASIC_INFO\n"));
366 info_count = THREAD_BASIC_INFO_COUNT;
367 result = thread_info (thread,
369 (thread_info_t) & thread_info_data.basic,
371 MACH_CHECK_ERROR (result);
374 PRINT_FIELD (&thread_info_data.basic, user_time);
375 PRINT_FIELD (&thread_info_data.basic, system_time);
377 PRINT_FIELD (&thread_info_data.basic, cpu_usage);
378 PRINT_FIELD (&thread_info_data.basic, run_state);
379 PRINT_FIELD (&thread_info_data.basic, flags);
380 PRINT_FIELD (&thread_info_data.basic, suspend_count);
381 PRINT_FIELD (&thread_info_data.basic, sleep_time);
385 unparse_protection (vm_prot_t p)
395 case VM_PROT_READ | VM_PROT_WRITE:
397 case VM_PROT_EXECUTE:
399 case VM_PROT_EXECUTE | VM_PROT_READ:
401 case VM_PROT_EXECUTE | VM_PROT_WRITE:
403 case VM_PROT_EXECUTE | VM_PROT_WRITE | VM_PROT_READ:
411 unparse_inheritance (vm_inherit_t i)
415 case VM_INHERIT_SHARE:
417 case VM_INHERIT_COPY:
419 case VM_INHERIT_NONE:
427 unparse_share_mode (unsigned char p)
440 return _("true-shrd");
441 case SM_PRIVATE_ALIASED:
442 return _("prv-alias");
443 case SM_SHARED_ALIASED:
444 return _("shr-alias");
451 unparse_user_tag (unsigned int tag)
457 case VM_MEMORY_MALLOC:
459 case VM_MEMORY_MALLOC_SMALL:
460 return _("malloc_small");
461 case VM_MEMORY_MALLOC_LARGE:
462 return _("malloc_large");
463 case VM_MEMORY_MALLOC_HUGE:
464 return _("malloc_huge");
467 case VM_MEMORY_REALLOC:
469 case VM_MEMORY_MALLOC_TINY:
470 return _("malloc_tiny");
471 case VM_MEMORY_ANALYSIS_TOOL:
472 return _("analysis_tool");
473 case VM_MEMORY_MACH_MSG:
474 return _("mach_msg");
475 case VM_MEMORY_IOKIT:
477 case VM_MEMORY_STACK:
479 case VM_MEMORY_GUARD:
481 case VM_MEMORY_SHARED_PMAP:
482 return _("shared_pmap");
483 case VM_MEMORY_DYLIB:
485 case VM_MEMORY_APPKIT:
487 case VM_MEMORY_FOUNDATION:
488 return _("foundation");
495 darwin_debug_regions (task_t task, mach_vm_address_t address, int max)
498 vm_region_basic_info_data_64_t info, prev_info;
499 mach_vm_address_t prev_address;
500 mach_vm_size_t size, prev_size;
502 mach_port_t object_name;
503 mach_msg_type_number_t count;
508 count = VM_REGION_BASIC_INFO_COUNT_64;
509 kret = mach_vm_region (task, &address, &size, VM_REGION_BASIC_INFO_64,
510 (vm_region_info_t) &info, &count, &object_name);
511 if (kret != KERN_SUCCESS)
513 printf_filtered (_("No memory regions."));
516 memcpy (&prev_info, &info, sizeof (vm_region_basic_info_data_64_t));
517 prev_address = address;
526 address = prev_address + prev_size;
528 /* Check to see if address space has wrapped around. */
534 count = VM_REGION_BASIC_INFO_COUNT_64;
536 mach_vm_region (task, &address, &size, VM_REGION_BASIC_INFO_64,
537 (vm_region_info_t) &info, &count, &object_name);
538 if (kret != KERN_SUCCESS)
545 if (address != prev_address + prev_size)
548 if ((info.protection != prev_info.protection)
549 || (info.max_protection != prev_info.max_protection)
550 || (info.inheritance != prev_info.inheritance)
551 || (info.shared != prev_info.reserved)
552 || (info.reserved != prev_info.reserved))
557 printf_filtered (_("%s-%s %s/%s %s %s %s"),
559 paddr(prev_address + prev_size),
560 unparse_protection (prev_info.protection),
561 unparse_protection (prev_info.max_protection),
562 unparse_inheritance (prev_info.inheritance),
563 prev_info.shared ? _("shrd") : _("priv"),
564 prev_info.reserved ? _("reserved") : _("not-rsvd"));
567 printf_filtered (_(" (%d sub-rgn)"), nsubregions);
569 printf_filtered (_("\n"));
571 prev_address = address;
573 memcpy (&prev_info, &info, sizeof (vm_region_basic_info_data_64_t));
584 if ((max > 0) && (num_printed >= max))
593 darwin_debug_regions_recurse (task_t task)
595 mach_vm_address_t r_addr;
596 mach_vm_address_t r_start;
597 mach_vm_size_t r_size;
599 mach_msg_type_number_t r_info_size;
600 vm_region_submap_short_info_data_64_t r_info;
610 r_info_size = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
612 kret = mach_vm_region_recurse (task, &r_start, &r_size, &r_depth,
613 (vm_region_recurse_info_t) &r_info,
615 if (kret != KERN_SUCCESS)
617 printf_filtered (_("%s-%s %s/%s %-5s %-10s %2d %s"),
619 paddr(r_start + r_size),
620 unparse_protection (r_info.protection),
621 unparse_protection (r_info.max_protection),
622 unparse_inheritance (r_info.inheritance),
623 unparse_share_mode (r_info.share_mode),
625 r_info.is_submap ? _("sm ") : _("obj"));
626 tag = unparse_user_tag (r_info.user_tag);
628 printf_unfiltered (_(" %s\n"), tag);
630 printf_unfiltered (_(" %u\n"), r_info.user_tag);
631 if (r_info.is_submap)
640 darwin_debug_region (task_t task, mach_vm_address_t address)
642 darwin_debug_regions (task, address, 1);
646 info_mach_regions_command (char *args, int from_tty)
650 task = get_task_from_args (args);
651 if (task == TASK_NULL)
654 darwin_debug_regions (task, 0, -1);
658 info_mach_regions_recurse_command (char *args, int from_tty)
662 task = get_task_from_args (args);
663 if (task == TASK_NULL)
666 darwin_debug_regions_recurse (task);
670 info_mach_region_command (char *exp, int from_tty)
672 struct expression *expr;
674 mach_vm_address_t address;
676 expr = parse_expression (exp);
677 val = evaluate_expression (expr);
678 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
680 val = value_ind (val);
682 /* In rvalue contexts, such as this, functions are coerced into
683 pointers to functions. */
684 if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
685 && VALUE_LVAL (val) == lval_memory)
687 address = value_address (val);
691 address = value_as_address (val);
694 if ((!darwin_inf) || (darwin_inf->task == TASK_NULL))
695 error (_("Inferior not available"));
697 darwin_debug_region (darwin_inf->task, address);
701 disp_exception (const darwin_exception_info *info)
705 printf_filtered (_("%d exceptions:\n"), info->count);
706 for (i = 0; i < info->count; i++)
708 exception_mask_t mask = info->masks[i];
710 printf_filtered (_("port 0x%04x, behavior: "), info->ports[i]);
711 switch (info->behaviors[i])
713 case EXCEPTION_DEFAULT:
714 printf_unfiltered (_("default"));
716 case EXCEPTION_STATE:
717 printf_unfiltered (_("state"));
719 case EXCEPTION_STATE_IDENTITY:
720 printf_unfiltered (_("state-identity"));
723 printf_unfiltered (_("0x%x"), info->behaviors[i]);
725 printf_unfiltered (_(", masks:"));
726 if (mask & EXC_MASK_BAD_ACCESS)
727 printf_unfiltered (_(" BAD_ACCESS"));
728 if (mask & EXC_MASK_BAD_INSTRUCTION)
729 printf_unfiltered (_(" BAD_INSTRUCTION"));
730 if (mask & EXC_MASK_ARITHMETIC)
731 printf_unfiltered (_(" ARITHMETIC"));
732 if (mask & EXC_MASK_EMULATION)
733 printf_unfiltered (_(" EMULATION"));
734 if (mask & EXC_MASK_SOFTWARE)
735 printf_unfiltered (_(" SOFTWARE"));
736 if (mask & EXC_MASK_BREAKPOINT)
737 printf_unfiltered (_(" BREAKPOINT"));
738 if (mask & EXC_MASK_SYSCALL)
739 printf_unfiltered (_(" SYSCALL"));
740 if (mask & EXC_MASK_MACH_SYSCALL)
741 printf_unfiltered (_(" MACH_SYSCALL"));
742 if (mask & EXC_MASK_RPC_ALERT)
743 printf_unfiltered (_(" RPC_ALERT"));
744 if (mask & EXC_MASK_CRASH)
745 printf_unfiltered (_(" CRASH"));
746 printf_unfiltered (_("\n"));
751 info_mach_exceptions_command (char *args, int from_tty)
756 darwin_exception_info info;
758 info.count = sizeof (info.ports) / sizeof (info.ports[0]);
762 if (strcmp (args, "saved") == 0)
764 if (darwin_inf->task == TASK_NULL)
765 error (_("No inferior running\n"));
766 disp_exception (&darwin_inf->exception_info);
769 else if (strcmp (args, "host") == 0)
771 /* FIXME: This need a the privilegied host port! */
772 kret = host_get_exception_ports
773 (darwin_host_self, EXC_MASK_ALL, info.masks,
774 &info.count, info.ports, info.behaviors, info.flavors);
775 MACH_CHECK_ERROR (kret);
776 disp_exception (&info);
779 error (_("Parameter is saved, host or none"));
783 if (darwin_inf->task == TASK_NULL)
784 error (_("No inferior running\n"));
786 kret = task_get_exception_ports
787 (darwin_inf->task, EXC_MASK_ALL, info.masks,
788 &info.count, info.ports, info.behaviors, info.flavors);
789 MACH_CHECK_ERROR (kret);
790 disp_exception (&info);
795 darwin_list_gdb_ports (const char *msg)
797 mach_port_name_array_t names;
798 mach_port_type_array_t types;
799 unsigned int name_count, type_count;
800 kern_return_t result;
803 result = mach_port_names (mach_task_self (),
804 &names, &name_count, &types, &type_count);
805 MACH_CHECK_ERROR (result);
807 gdb_assert (name_count == type_count);
809 printf_unfiltered (_("Ports for %s:"), msg);
810 for (i = 0; i < name_count; ++i)
811 printf_unfiltered (_(" 0x%04x"), names[i]);
812 printf_unfiltered (_("\n"));
814 vm_deallocate (mach_task_self (), (vm_address_t) names,
815 (name_count * sizeof (mach_port_t)));
816 vm_deallocate (mach_task_self (), (vm_address_t) types,
817 (type_count * sizeof (mach_port_type_t)));
821 _initialize_darwin_info_commands (void)
823 add_info ("mach-tasks", info_mach_tasks_command,
824 _("Get list of tasks in system."));
825 add_info ("mach-ports", info_mach_ports_command,
826 _("Get list of ports in a task."));
827 add_info ("mach-port", info_mach_port_command,
828 _("Get info on a specific port."));
829 add_info ("mach-task", info_mach_task_command,
830 _("Get info on a specific task."));
831 add_info ("mach-threads", info_mach_threads_command,
832 _("Get list of threads in a task."));
833 add_info ("mach-thread", info_mach_thread_command,
834 _("Get info on a specific thread."));
836 add_info ("mach-regions", info_mach_regions_command,
837 _("Get information on all mach region for the task."));
838 add_info ("mach-regions-rec", info_mach_regions_recurse_command,
839 _("Get information on all mach sub region for the task."));
840 add_info ("mach-region", info_mach_region_command,
841 _("Get information on mach region at given address."));
843 add_info ("mach-exceptions", info_mach_exceptions_command,
844 _("Disp mach exceptions."));