gdb/darwin-nat-info.c - gdb

Functions defined

Macros defined

Source code

  1. /* Darwin support for GDB, the GNU debugger.
  2.    Copyright (C) 1997-2015 Free Software Foundation, Inc.

  3.    Contributed by Apple Computer, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. /* The name of the ppc_thread_state structure, and the names of its
  16.    members, have been changed for Unix conformance reasons.  The easiest
  17.    way to have gdb build on systems with the older names and systems
  18.    with the newer names is to build this compilation unit with the
  19.    non-conformant define below.  This doesn't seem to cause the resulting
  20.    binary any problems but it seems like it could cause us problems in
  21.    the future.  It'd be good to remove this at some point when compiling on
  22.    Tiger is no longer important.  */

  23. #include "defs.h"
  24. #include "symtab.h"
  25. #include "gdbtypes.h"
  26. #include "gdbcore.h"
  27. #include "value.h"
  28. #include "gdbcmd.h"
  29. #include "inferior.h"

  30. #include <sys/sysctl.h>

  31. #include "darwin-nat.h"

  32. #include <mach/thread_info.h>
  33. #include <mach/thread_act.h>
  34. #include <mach/task.h>
  35. #include <mach/vm_map.h>
  36. #include <mach/mach_port.h>
  37. #include <mach/mach_init.h>
  38. #include <mach/mach_vm.h>

  39. #define CHECK_ARGS(what, args) do { \
  40.   if ((NULL == args) || ((args[0] != '0') && (args[1] != 'x'))) \
  41.     error(_("%s must be specified with 0x..."), what);                \
  42. } while (0)

  43. #define PRINT_FIELD(structure, field) \
  44.   printf_unfiltered(_(#field":\t%#lx\n"), (unsigned long) (structure)->field)

  45. #define PRINT_TV_FIELD(structure, field) \
  46.   printf_unfiltered(_(#field":\t%u.%06u sec\n"),        \
  47.   (unsigned) (structure)->field.seconds, \
  48.   (unsigned) (structure)->field.microseconds)

  49. #define task_self mach_task_self
  50. #define task_by_unix_pid task_for_pid
  51. #define port_name_array_t mach_port_array_t
  52. #define port_type_array_t mach_port_array_t

  53. static void
  54. info_mach_tasks_command (char *args, int from_tty)
  55. {
  56.   int sysControl[4];
  57.   int count, index;
  58.   size_t length;
  59.   struct kinfo_proc *procInfo;

  60.   sysControl[0] = CTL_KERN;
  61.   sysControl[1] = KERN_PROC;
  62.   sysControl[2] = KERN_PROC_ALL;

  63.   sysctl (sysControl, 3, NULL, &length, NULL, 0);
  64.   procInfo = (struct kinfo_proc *) xmalloc (length);
  65.   sysctl (sysControl, 3, procInfo, &length, NULL, 0);

  66.   count = (length / sizeof (struct kinfo_proc));
  67.   printf_unfiltered (_("%d processes:\n"), count);
  68.   for (index = 0; index < count; ++index)
  69.     {
  70.       kern_return_t result;
  71.       mach_port_t taskPort;

  72.       result =
  73.         task_by_unix_pid (mach_task_self (), procInfo[index].kp_proc.p_pid,
  74.                           &taskPort);
  75.       if (KERN_SUCCESS == result)
  76.         {
  77.           printf_unfiltered (_("    %s is %d has task %#x\n"),
  78.                              procInfo[index].kp_proc.p_comm,
  79.                              procInfo[index].kp_proc.p_pid, taskPort);
  80.         }
  81.       else
  82.         {
  83.           printf_unfiltered (_("    %s is %d unknown task port\n"),
  84.                              procInfo[index].kp_proc.p_comm,
  85.                              procInfo[index].kp_proc.p_pid);
  86.         }
  87.     }

  88.   xfree (procInfo);
  89. }

  90. static task_t
  91. get_task_from_args (char *args)
  92. {
  93.   task_t task;
  94.   char *eptr;

  95.   if (args == NULL || *args == 0)
  96.     {
  97.       if (ptid_equal (inferior_ptid, null_ptid))
  98.         printf_unfiltered (_("No inferior running\n"));
  99.       return current_inferior ()->private->task;
  100.     }
  101.   if (strcmp (args, "gdb") == 0)
  102.     return mach_task_self ();
  103.   task = strtoul (args, &eptr, 0);
  104.   if (*eptr)
  105.     {
  106.       printf_unfiltered (_("cannot parse task id '%s'\n"), args);
  107.       return TASK_NULL;
  108.     }
  109.   return task;
  110. }

  111. static void
  112. info_mach_task_command (char *args, int from_tty)
  113. {
  114.   union
  115.   {
  116.     struct task_basic_info basic;
  117.     struct task_events_info events;
  118.     struct task_thread_times_info thread_times;
  119.   } task_info_data;

  120.   kern_return_t result;
  121.   unsigned int info_count;
  122.   task_t task;

  123.   task = get_task_from_args (args);
  124.   if (task == TASK_NULL)
  125.     return;

  126.   printf_unfiltered (_("TASK_BASIC_INFO for 0x%x:\n"), task);
  127.   info_count = TASK_BASIC_INFO_COUNT;
  128.   result = task_info (task,
  129.                       TASK_BASIC_INFO,
  130.                       (task_info_t) & task_info_data.basic, &info_count);
  131.   MACH_CHECK_ERROR (result);

  132.   PRINT_FIELD (&task_info_data.basic, suspend_count);
  133.   PRINT_FIELD (&task_info_data.basic, virtual_size);
  134.   PRINT_FIELD (&task_info_data.basic, resident_size);
  135.   PRINT_TV_FIELD (&task_info_data.basic, user_time);
  136.   PRINT_TV_FIELD (&task_info_data.basic, system_time);
  137.   printf_unfiltered (_("\nTASK_EVENTS_INFO:\n"));
  138.   info_count = TASK_EVENTS_INFO_COUNT;
  139.   result = task_info (task,
  140.                       TASK_EVENTS_INFO,
  141.                       (task_info_t) & task_info_data.events, &info_count);
  142.   MACH_CHECK_ERROR (result);

  143.   PRINT_FIELD (&task_info_data.events, faults);
  144. #if 0
  145.   PRINT_FIELD (&task_info_data.events, zero_fills);
  146.   PRINT_FIELD (&task_info_data.events, reactivations);
  147. #endif
  148.   PRINT_FIELD (&task_info_data.events, pageins);
  149.   PRINT_FIELD (&task_info_data.events, cow_faults);
  150.   PRINT_FIELD (&task_info_data.events, messages_sent);
  151.   PRINT_FIELD (&task_info_data.events, messages_received);
  152.   printf_unfiltered (_("\nTASK_THREAD_TIMES_INFO:\n"));
  153.   info_count = TASK_THREAD_TIMES_INFO_COUNT;
  154.   result = task_info (task,
  155.                       TASK_THREAD_TIMES_INFO,
  156.                       (task_info_t) & task_info_data.thread_times,
  157.                       &info_count);
  158.   MACH_CHECK_ERROR (result);
  159.   PRINT_TV_FIELD (&task_info_data.thread_times, user_time);
  160.   PRINT_TV_FIELD (&task_info_data.thread_times, system_time);
  161. }

  162. static void
  163. info_mach_ports_command (char *args, int from_tty)
  164. {
  165.   port_name_array_t names;
  166.   port_type_array_t types;
  167.   unsigned int name_count, type_count;
  168.   kern_return_t result;
  169.   int index;
  170.   task_t task;

  171.   task = get_task_from_args (args);
  172.   if (task == TASK_NULL)
  173.     return;

  174.   result = mach_port_names (task, &names, &name_count, &types, &type_count);
  175.   MACH_CHECK_ERROR (result);

  176.   gdb_assert (name_count == type_count);

  177.   printf_unfiltered (_("Ports for task 0x%x:\n"), task);
  178.   printf_unfiltered (_("port   type\n"));
  179.   for (index = 0; index < name_count; ++index)
  180.     {
  181.       mach_port_t port = names[index];
  182.       unsigned int j;
  183.       struct type_descr
  184.       {
  185.         mach_port_type_t type;
  186.         const char *name;
  187.         mach_port_right_t right;
  188.       };
  189.       static struct type_descr descrs[] =
  190.         {
  191.           {MACH_PORT_TYPE_SEND, "send", MACH_PORT_RIGHT_SEND},
  192.           {MACH_PORT_TYPE_SEND_ONCE, "send-once", MACH_PORT_RIGHT_SEND_ONCE},
  193.           {MACH_PORT_TYPE_RECEIVE, "receive", MACH_PORT_RIGHT_RECEIVE},
  194.           {MACH_PORT_TYPE_PORT_SET, "port-set", MACH_PORT_RIGHT_PORT_SET},
  195.           {MACH_PORT_TYPE_DEAD_NAME, "dead", MACH_PORT_RIGHT_DEAD_NAME}
  196.         };

  197.       printf_unfiltered (_("%04x: %08x "), port, types[index]);
  198.       for (j = 0; j < sizeof(descrs) / sizeof(*descrs); j++)
  199.         if (types[index] & descrs[j].type)
  200.           {
  201.             mach_port_urefs_t ref;
  202.             kern_return_t ret;

  203.             printf_unfiltered (_(" %s("), descrs[j].name);
  204.             ret = mach_port_get_refs (task, port, descrs[j].right, &ref);
  205.             if (ret != KERN_SUCCESS)
  206.               printf_unfiltered (_("??"));
  207.             else
  208.               printf_unfiltered (_("%u"), ref);
  209.             printf_unfiltered (_(" refs)"));
  210.           }

  211.       if (task == task_self ())
  212.         {
  213.           if (port == task_self())
  214.             printf_unfiltered (_(" gdb-task"));
  215.           else if (port == darwin_host_self)
  216.             printf_unfiltered (_(" host-self"));
  217.           else if (port == darwin_ex_port)
  218.             printf_unfiltered (_(" gdb-exception"));
  219.           else if (port == darwin_port_set)
  220.             printf_unfiltered (_(" gdb-port_set"));
  221.           else if (!ptid_equal (inferior_ptid, null_ptid))
  222.             {
  223.               struct inferior *inf = current_inferior ();

  224.               if (port == inf->private->task)
  225.                 printf_unfiltered (_(" inferior-task"));
  226.               else if (port == inf->private->notify_port)
  227.                 printf_unfiltered (_(" inferior-notify"));
  228.               else
  229.                 {
  230.                   int k;
  231.                   darwin_thread_t *t;

  232.                   for (k = 0; k < inf->private->exception_info.count; k++)
  233.                     if (port == inf->private->exception_info.ports[k])
  234.                       {
  235.                         printf_unfiltered (_(" inferior-excp-port"));
  236.                         break;
  237.                       }

  238.                   if (inf->private->threads)
  239.                     {
  240.                       for (k = 0;
  241.                            VEC_iterate(darwin_thread_t,
  242.                                        inf->private->threads, k, t);
  243.                            k++)
  244.                         if (port == t->gdb_port)
  245.                           {
  246.                             printf_unfiltered (_(" inferior-thread for 0x%x"),
  247.                                                inf->private->task);
  248.                             break;
  249.                           }
  250.                     }
  251.                 }
  252.             }
  253.         }
  254.       printf_unfiltered (_("\n"));
  255.     }

  256.   vm_deallocate (task_self (), (vm_address_t) names,
  257.                  (name_count * sizeof (mach_port_t)));
  258.   vm_deallocate (task_self (), (vm_address_t) types,
  259.                  (type_count * sizeof (mach_port_type_t)));
  260. }


  261. static void
  262. darwin_debug_port_info (task_t task, mach_port_t port)
  263. {
  264.   kern_return_t kret;
  265.   mach_port_status_t status;
  266.   mach_msg_type_number_t len = sizeof (status);

  267.   kret = mach_port_get_attributes
  268.     (task, port, MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &len);
  269.   MACH_CHECK_ERROR (kret);

  270.   printf_unfiltered (_("Port 0x%lx in task 0x%lx:\n"), (unsigned long) port,
  271.                      (unsigned long) task);
  272.   printf_unfiltered (_("  port set: 0x%x\n"), status.mps_pset);
  273.   printf_unfiltered (_("     seqno: 0x%x\n"), status.mps_seqno);
  274.   printf_unfiltered (_("   mscount: 0x%x\n"), status.mps_mscount);
  275.   printf_unfiltered (_("    qlimit: 0x%x\n"), status.mps_qlimit);
  276.   printf_unfiltered (_("  msgcount: 0x%x\n"), status.mps_msgcount);
  277.   printf_unfiltered (_("  sorights: 0x%x\n"), status.mps_sorights);
  278.   printf_unfiltered (_("   srights: 0x%x\n"), status.mps_srights);
  279.   printf_unfiltered (_(" pdrequest: 0x%x\n"), status.mps_pdrequest);
  280.   printf_unfiltered (_(" nsrequest: 0x%x\n"), status.mps_nsrequest);
  281.   printf_unfiltered (_("     flags: 0x%x\n"), status.mps_flags);
  282. }

  283. static void
  284. info_mach_port_command (char *args, int from_tty)
  285. {
  286.   task_t task;
  287.   mach_port_t port;

  288.   CHECK_ARGS (_("Task and port"), args);
  289.   sscanf (args, "0x%x 0x%x", &task, &port);

  290.   darwin_debug_port_info (task, port);
  291. }

  292. static void
  293. info_mach_threads_command (char *args, int from_tty)
  294. {
  295.   thread_array_t threads;
  296.   unsigned int thread_count;
  297.   kern_return_t result;
  298.   task_t task;
  299.   int i;

  300.   task = get_task_from_args (args);
  301.   if (task == TASK_NULL)
  302.     return;

  303.   result = task_threads (task, &threads, &thread_count);
  304.   MACH_CHECK_ERROR (result);

  305.   printf_unfiltered (_("Threads in task %#x:\n"), task);
  306.   for (i = 0; i < thread_count; ++i)
  307.     {
  308.       printf_unfiltered (_("    %#x\n"), threads[i]);
  309.       mach_port_deallocate (task_self (), threads[i]);
  310.     }

  311.   vm_deallocate (task_self (), (vm_address_t) threads,
  312.                  (thread_count * sizeof (thread_t)));
  313. }

  314. static void
  315. info_mach_thread_command (char *args, int from_tty)
  316. {
  317.   union
  318.   {
  319.     struct thread_basic_info basic;
  320.   } thread_info_data;

  321.   thread_t thread;
  322.   kern_return_t result;
  323.   unsigned int info_count;

  324.   CHECK_ARGS (_("Thread"), args);
  325.   sscanf (args, "0x%x", &thread);

  326.   printf_unfiltered (_("THREAD_BASIC_INFO\n"));
  327.   info_count = THREAD_BASIC_INFO_COUNT;
  328.   result = thread_info (thread,
  329.                         THREAD_BASIC_INFO,
  330.                         (thread_info_t) & thread_info_data.basic,
  331.                         &info_count);
  332.   MACH_CHECK_ERROR (result);

  333. #if 0
  334.   PRINT_FIELD (&thread_info_data.basic, user_time);
  335.   PRINT_FIELD (&thread_info_data.basic, system_time);
  336. #endif
  337.   PRINT_FIELD (&thread_info_data.basic, cpu_usage);
  338.   PRINT_FIELD (&thread_info_data.basic, run_state);
  339.   PRINT_FIELD (&thread_info_data.basic, flags);
  340.   PRINT_FIELD (&thread_info_data.basic, suspend_count);
  341.   PRINT_FIELD (&thread_info_data.basic, sleep_time);
  342. }

  343. static const char *
  344. unparse_protection (vm_prot_t p)
  345. {
  346.   switch (p)
  347.     {
  348.     case VM_PROT_NONE:
  349.       return "---";
  350.     case VM_PROT_READ:
  351.       return "r--";
  352.     case VM_PROT_WRITE:
  353.       return "-w-";
  354.     case VM_PROT_READ | VM_PROT_WRITE:
  355.       return "rw-";
  356.     case VM_PROT_EXECUTE:
  357.       return "--x";
  358.     case VM_PROT_EXECUTE | VM_PROT_READ:
  359.       return "r-x";
  360.     case VM_PROT_EXECUTE | VM_PROT_WRITE:
  361.       return "-wx";
  362.     case VM_PROT_EXECUTE | VM_PROT_WRITE | VM_PROT_READ:
  363.       return "rwx";
  364.     default:
  365.       return "???";
  366.     }
  367. }

  368. static const char *
  369. unparse_inheritance (vm_inherit_t i)
  370. {
  371.   switch (i)
  372.     {
  373.     case VM_INHERIT_SHARE:
  374.       return _("share");
  375.     case VM_INHERIT_COPY:
  376.       return _("copy ");
  377.     case VM_INHERIT_NONE:
  378.       return _("none ");
  379.     default:
  380.       return _("???  ");
  381.     }
  382. }

  383. static const char *
  384. unparse_share_mode (unsigned char p)
  385. {
  386.   switch (p)
  387.     {
  388.     case SM_COW:
  389.       return _("cow");
  390.     case SM_PRIVATE:
  391.       return _("private");
  392.     case SM_EMPTY:
  393.       return _("empty");
  394.     case SM_SHARED:
  395.       return _("shared");
  396.     case SM_TRUESHARED:
  397.       return _("true-shrd");
  398.     case SM_PRIVATE_ALIASED:
  399.       return _("prv-alias");
  400.     case SM_SHARED_ALIASED:
  401.       return _("shr-alias");
  402.     default:
  403.       return _("???");
  404.     }
  405. }

  406. static const char *
  407. unparse_user_tag (unsigned int tag)
  408. {
  409.   switch (tag)
  410.     {
  411.     case 0:
  412.       return _("default");
  413.     case VM_MEMORY_MALLOC:
  414.       return _("malloc");
  415.     case VM_MEMORY_MALLOC_SMALL:
  416.       return _("malloc_small");
  417.     case VM_MEMORY_MALLOC_LARGE:
  418.       return _("malloc_large");
  419.     case VM_MEMORY_MALLOC_HUGE:
  420.       return _("malloc_huge");
  421.     case VM_MEMORY_SBRK:
  422.       return _("sbrk");
  423.     case VM_MEMORY_REALLOC:
  424.       return _("realloc");
  425.     case VM_MEMORY_MALLOC_TINY:
  426.       return _("malloc_tiny");
  427.     case VM_MEMORY_ANALYSIS_TOOL:
  428.       return _("analysis_tool");
  429.     case VM_MEMORY_MACH_MSG:
  430.       return _("mach_msg");
  431.     case VM_MEMORY_IOKIT:
  432.       return _("iokit");
  433.     case VM_MEMORY_STACK:
  434.       return _("stack");
  435.     case VM_MEMORY_GUARD:
  436.       return _("guard");
  437.     case VM_MEMORY_SHARED_PMAP:
  438.       return _("shared_pmap");
  439.     case VM_MEMORY_DYLIB:
  440.       return _("dylib");
  441.     case VM_MEMORY_APPKIT:
  442.       return _("appkit");
  443.     case VM_MEMORY_FOUNDATION:
  444.       return _("foundation");
  445.     default:
  446.       return NULL;
  447.     }
  448. }

  449. static void
  450. darwin_debug_regions (task_t task, mach_vm_address_t address, int max)
  451. {
  452.   kern_return_t kret;
  453.   vm_region_basic_info_data_64_t info, prev_info;
  454.   mach_vm_address_t prev_address;
  455.   mach_vm_size_t size, prev_size;

  456.   mach_port_t object_name;
  457.   mach_msg_type_number_t count;

  458.   int nsubregions = 0;
  459.   int num_printed = 0;

  460.   count = VM_REGION_BASIC_INFO_COUNT_64;
  461.   kret = mach_vm_region (task, &address, &size, VM_REGION_BASIC_INFO_64,
  462.                          (vm_region_info_t) &info, &count, &object_name);
  463.   if (kret != KERN_SUCCESS)
  464.     {
  465.       printf_filtered (_("No memory regions."));
  466.       return;
  467.     }
  468.   memcpy (&prev_info, &info, sizeof (vm_region_basic_info_data_64_t));
  469.   prev_address = address;
  470.   prev_size = size;
  471.   nsubregions = 1;

  472.   for (;;)
  473.     {
  474.       int print = 0;
  475.       int done = 0;

  476.       address = prev_address + prev_size;

  477.       /* Check to see if address space has wrapped around.  */
  478.       if (address == 0)
  479.         print = done = 1;

  480.       if (!done)
  481.         {
  482.           count = VM_REGION_BASIC_INFO_COUNT_64;
  483.           kret =
  484.             mach_vm_region (task, &address, &size, VM_REGION_BASIC_INFO_64,
  485.                                (vm_region_info_t) &info, &count, &object_name);
  486.           if (kret != KERN_SUCCESS)
  487.             {
  488.               size = 0;
  489.               print = done = 1;
  490.             }
  491.         }

  492.       if (address != prev_address + prev_size)
  493.         print = 1;

  494.       if ((info.protection != prev_info.protection)
  495.           || (info.max_protection != prev_info.max_protection)
  496.           || (info.inheritance != prev_info.inheritance)
  497.           || (info.shared != prev_info.reserved)
  498.           || (info.reserved != prev_info.reserved))
  499.         print = 1;

  500.       if (print)
  501.         {
  502.           printf_filtered (_("%s-%s %s/%s  %s %s %s"),
  503.                            paddress (target_gdbarch (), prev_address),
  504.                            paddress (target_gdbarch (), prev_address + prev_size),
  505.                            unparse_protection (prev_info.protection),
  506.                            unparse_protection (prev_info.max_protection),
  507.                            unparse_inheritance (prev_info.inheritance),
  508.                            prev_info.shared ? _("shrd") : _("priv"),
  509.                            prev_info.reserved ? _("reserved") : _("not-rsvd"));

  510.           if (nsubregions > 1)
  511.             printf_filtered (_(" (%d sub-rgn)"), nsubregions);

  512.           printf_filtered (_("\n"));

  513.           prev_address = address;
  514.           prev_size = size;
  515.           memcpy (&prev_info, &info, sizeof (vm_region_basic_info_data_64_t));
  516.           nsubregions = 1;

  517.           num_printed++;
  518.         }
  519.       else
  520.         {
  521.           prev_size += size;
  522.           nsubregions++;
  523.         }

  524.       if ((max > 0) && (num_printed >= max))
  525.         done = 1;

  526.       if (done)
  527.         break;
  528.     }
  529. }

  530. static void
  531. darwin_debug_regions_recurse (task_t task)
  532. {
  533.   mach_vm_address_t r_addr;
  534.   mach_vm_address_t r_start;
  535.   mach_vm_size_t r_size;
  536.   natural_t r_depth;
  537.   mach_msg_type_number_t r_info_size;
  538.   vm_region_submap_short_info_data_64_t r_info;
  539.   kern_return_t kret;
  540.   int ret;
  541.   struct cleanup *table_chain;
  542.   struct ui_out *uiout = current_uiout;

  543.   table_chain = make_cleanup_ui_out_table_begin_end (uiout, 9, -1, "regions");

  544.   if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
  545.     {
  546.       ui_out_table_header (uiout, 10, ui_left, "start", "Start");
  547.       ui_out_table_header (uiout, 10, ui_left, "end", "End");
  548.     }
  549.   else
  550.     {
  551.       ui_out_table_header (uiout, 18, ui_left, "start", "Start");
  552.       ui_out_table_header (uiout, 18, ui_left, "end", "End");
  553.     }
  554.   ui_out_table_header (uiout, 3, ui_left, "min-prot", "Min");
  555.   ui_out_table_header (uiout, 3, ui_left, "max-prot", "Max");
  556.   ui_out_table_header (uiout, 5, ui_left, "inheritence", "Inh");
  557.   ui_out_table_header (uiout, 9, ui_left, "share-mode", "Shr");
  558.   ui_out_table_header (uiout, 1, ui_left, "depth", "D");
  559.   ui_out_table_header (uiout, 3, ui_left, "submap", "Sm");
  560.   ui_out_table_header (uiout, 0, ui_noalign, "tag", "Tag");

  561.   ui_out_table_body (uiout);

  562.   r_start = 0;
  563.   r_depth = 0;
  564.   while (1)
  565.     {
  566.       const char *tag;
  567.       struct cleanup *row_chain;

  568.       r_info_size = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
  569.       r_size = -1;
  570.       kret = mach_vm_region_recurse (task, &r_start, &r_size, &r_depth,
  571.                                      (vm_region_recurse_info_t) &r_info,
  572.                                      &r_info_size);
  573.       if (kret != KERN_SUCCESS)
  574.         break;
  575.       row_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "regions-row");

  576.       ui_out_field_core_addr (uiout, "start", target_gdbarch (), r_start);
  577.       ui_out_field_core_addr (uiout, "end", target_gdbarch (), r_start + r_size);
  578.       ui_out_field_string (uiout, "min-prot",
  579.                            unparse_protection (r_info.protection));
  580.       ui_out_field_string (uiout, "max-prot",
  581.                            unparse_protection (r_info.max_protection));
  582.       ui_out_field_string (uiout, "inheritence",
  583.                            unparse_inheritance (r_info.inheritance));
  584.       ui_out_field_string (uiout, "share-mode",
  585.                            unparse_share_mode (r_info.share_mode));
  586.       ui_out_field_int (uiout, "depth", r_depth);
  587.       ui_out_field_string (uiout, "submap",
  588.                            r_info.is_submap ? _("sm ") : _("obj"));
  589.       tag = unparse_user_tag (r_info.user_tag);
  590.       if (tag)
  591.         ui_out_field_string (uiout, "tag", tag);
  592.       else
  593.         ui_out_field_int (uiout, "tag", r_info.user_tag);

  594.       do_cleanups (row_chain);

  595.       if (!ui_out_is_mi_like_p (uiout))
  596.         ui_out_text (uiout, "\n");

  597.       if (r_info.is_submap)
  598.         r_depth++;
  599.       else
  600.         r_start += r_size;
  601.     }
  602.   do_cleanups (table_chain);

  603. }


  604. static void
  605. darwin_debug_region (task_t task, mach_vm_address_t address)
  606. {
  607.   darwin_debug_regions (task, address, 1);
  608. }

  609. static void
  610. info_mach_regions_command (char *args, int from_tty)
  611. {
  612.   task_t task;

  613.   task = get_task_from_args (args);
  614.   if (task == TASK_NULL)
  615.     return;

  616.   darwin_debug_regions (task, 0, -1);
  617. }

  618. static void
  619. info_mach_regions_recurse_command (char *args, int from_tty)
  620. {
  621.   task_t task;

  622.   task = get_task_from_args (args);
  623.   if (task == TASK_NULL)
  624.     return;

  625.   darwin_debug_regions_recurse (task);
  626. }

  627. static void
  628. info_mach_region_command (char *exp, int from_tty)
  629. {
  630.   struct expression *expr;
  631.   struct value *val;
  632.   mach_vm_address_t address;
  633.   struct inferior *inf;

  634.   expr = parse_expression (exp);
  635.   val = evaluate_expression (expr);
  636.   if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
  637.     {
  638.       val = value_ind (val);
  639.     }
  640.   address = value_as_address (val);

  641.   if (ptid_equal (inferior_ptid, null_ptid))
  642.     error (_("Inferior not available"));

  643.   inf = current_inferior ();
  644.   darwin_debug_region (inf->private->task, address);
  645. }

  646. static void
  647. disp_exception (const darwin_exception_info *info)
  648. {
  649.   int i;

  650.   printf_filtered (_("%d exceptions:\n"), info->count);
  651.   for (i = 0; i < info->count; i++)
  652.     {
  653.       exception_mask_t mask = info->masks[i];

  654.       printf_filtered (_("port 0x%04x, behavior: "), info->ports[i]);
  655.       switch (info->behaviors[i])
  656.         {
  657.         case EXCEPTION_DEFAULT:
  658.           printf_unfiltered (_("default"));
  659.           break;
  660.         case EXCEPTION_STATE:
  661.           printf_unfiltered (_("state"));
  662.           break;
  663.         case EXCEPTION_STATE_IDENTITY:
  664.           printf_unfiltered (_("state-identity"));
  665.           break;
  666.         default:
  667.           printf_unfiltered (_("0x%x"), info->behaviors[i]);
  668.         }
  669.       printf_unfiltered (_(", masks:"));
  670.       if (mask & EXC_MASK_BAD_ACCESS)
  671.         printf_unfiltered (_(" BAD_ACCESS"));
  672.       if (mask & EXC_MASK_BAD_INSTRUCTION)
  673.         printf_unfiltered (_(" BAD_INSTRUCTION"));
  674.       if (mask & EXC_MASK_ARITHMETIC)
  675.         printf_unfiltered (_(" ARITHMETIC"));
  676.       if (mask & EXC_MASK_EMULATION)
  677.         printf_unfiltered (_(" EMULATION"));
  678.       if (mask & EXC_MASK_SOFTWARE)
  679.         printf_unfiltered (_(" SOFTWARE"));
  680.       if (mask & EXC_MASK_BREAKPOINT)
  681.         printf_unfiltered (_(" BREAKPOINT"));
  682.       if (mask & EXC_MASK_SYSCALL)
  683.         printf_unfiltered (_(" SYSCALL"));
  684.       if (mask & EXC_MASK_MACH_SYSCALL)
  685.         printf_unfiltered (_(" MACH_SYSCALL"));
  686.       if (mask & EXC_MASK_RPC_ALERT)
  687.         printf_unfiltered (_(" RPC_ALERT"));
  688.       if (mask & EXC_MASK_CRASH)
  689.         printf_unfiltered (_(" CRASH"));
  690.       printf_unfiltered (_("\n"));
  691.     }
  692. }

  693. static void
  694. info_mach_exceptions_command (char *args, int from_tty)
  695. {
  696.   int i;
  697.   task_t task;
  698.   kern_return_t kret;
  699.   darwin_exception_info info;

  700.   info.count = sizeof (info.ports) / sizeof (info.ports[0]);

  701.   if (args != NULL)
  702.     {
  703.       if (strcmp (args, "saved") == 0)
  704.         {
  705.           if (ptid_equal (inferior_ptid, null_ptid))
  706.             printf_unfiltered (_("No inferior running\n"));
  707.           disp_exception (&current_inferior ()->private->exception_info);
  708.           return;
  709.         }
  710.       else if (strcmp (args, "host") == 0)
  711.         {
  712.           /* FIXME: This need a privilegied host port!  */
  713.           kret = host_get_exception_ports
  714.             (darwin_host_self, EXC_MASK_ALL, info.masks,
  715.              &info.count, info.ports, info.behaviors, info.flavors);
  716.           MACH_CHECK_ERROR (kret);
  717.           disp_exception (&info);
  718.         }
  719.       else
  720.         error (_("Parameter is saved, host or none"));
  721.     }
  722.   else
  723.     {
  724.       struct inferior *inf;

  725.       if (ptid_equal (inferior_ptid, null_ptid))
  726.         printf_unfiltered (_("No inferior running\n"));
  727.       inf = current_inferior ();

  728.       kret = task_get_exception_ports
  729.         (inf->private->task, EXC_MASK_ALL, info.masks,
  730.          &info.count, info.ports, info.behaviors, info.flavors);
  731.       MACH_CHECK_ERROR (kret);
  732.       disp_exception (&info);
  733.     }
  734. }

  735. /* -Wmissing-prototypes */
  736. extern initialize_file_ftype _initialize_darwin_info_commands;

  737. void
  738. _initialize_darwin_info_commands (void)
  739. {
  740.   add_info ("mach-tasks", info_mach_tasks_command,
  741.             _("Get list of tasks in system."));
  742.   add_info ("mach-ports", info_mach_ports_command,
  743.             _("Get list of ports in a task."));
  744.   add_info ("mach-port", info_mach_port_command,
  745.             _("Get info on a specific port."));
  746.   add_info ("mach-task", info_mach_task_command,
  747.             _("Get info on a specific task."));
  748.   add_info ("mach-threads", info_mach_threads_command,
  749.             _("Get list of threads in a task."));
  750.   add_info ("mach-thread", info_mach_thread_command,
  751.             _("Get info on a specific thread."));

  752.   add_info ("mach-regions", info_mach_regions_command,
  753.             _("Get information on all mach region for the task."));
  754.   add_info ("mach-regions-rec", info_mach_regions_recurse_command,
  755.             _("Get information on all mach sub region for the task."));
  756.   add_info ("mach-region", info_mach_region_command,
  757.             _("Get information on mach region at given address."));

  758.   add_info ("mach-exceptions", info_mach_exceptions_command,
  759.             _("Disp mach exceptions."));
  760. }