]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/darwin-nat-info.c
gdb, gdbserver, gdbsupport: remove includes of early headers
[thirdparty/binutils-gdb.git] / gdb / darwin-nat-info.c
CommitLineData
a80b95ba 1/* Darwin support for GDB, the GNU debugger.
1d506c26 2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
a80b95ba
TG
3
4 Contributed by Apple Computer, Inc.
5
6 This file is part of GDB.
7
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
dcf7800b 10 the Free Software Foundation; either version 3 of the License, or
a80b95ba
TG
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
dcf7800b 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
a80b95ba
TG
20
21/* The name of the ppc_thread_state structure, and the names of its
22 members, have been changed for Unix conformance reasons. The easiest
23 way to have gdb build on systems with the older names and systems
24 with the newer names is to build this compilation unit with the
25 non-conformant define below. This doesn't seem to cause the resulting
26 binary any problems but it seems like it could cause us problems in
27 the future. It'd be good to remove this at some point when compiling on
28 Tiger is no longer important. */
29
4de283e4
TT
30#include "symtab.h"
31#include "gdbtypes.h"
32#include "gdbcore.h"
33#include "value.h"
34#include "gdbcmd.h"
35#include "inferior.h"
b1c896b3 36#include "gdbarch.h"
a80b95ba 37
a80b95ba
TG
38#include <sys/sysctl.h>
39
40#include "darwin-nat.h"
4de283e4
TT
41
42#include <mach/thread_info.h>
43#include <mach/thread_act.h>
44#include <mach/task.h>
45#include <mach/vm_map.h>
46#include <mach/mach_port.h>
47#include <mach/mach_init.h>
48#include <mach/mach_vm.h>
a80b95ba
TG
49
50#define CHECK_ARGS(what, args) do { \
51 if ((NULL == args) || ((args[0] != '0') && (args[1] != 'x'))) \
7ea6d463 52 error(_("%s must be specified with 0x..."), what); \
a80b95ba
TG
53} while (0)
54
55#define PRINT_FIELD(structure, field) \
6cb06a8c 56 gdb_printf(_(#field":\t%#lx\n"), (unsigned long) (structure)->field)
a80b95ba
TG
57
58#define PRINT_TV_FIELD(structure, field) \
6cb06a8c
TT
59 gdb_printf(_(#field":\t%u.%06u sec\n"), \
60 (unsigned) (structure)->field.seconds, \
61 (unsigned) (structure)->field.microseconds)
a80b95ba
TG
62
63#define task_self mach_task_self
64#define task_by_unix_pid task_for_pid
65#define port_name_array_t mach_port_array_t
66#define port_type_array_t mach_port_array_t
67
68static void
1d12d88f 69info_mach_tasks_command (const char *args, int from_tty)
a80b95ba
TG
70{
71 int sysControl[4];
72 int count, index;
73 size_t length;
74 struct kinfo_proc *procInfo;
75
76 sysControl[0] = CTL_KERN;
77 sysControl[1] = KERN_PROC;
78 sysControl[2] = KERN_PROC_ALL;
79
80 sysctl (sysControl, 3, NULL, &length, NULL, 0);
81 procInfo = (struct kinfo_proc *) xmalloc (length);
82 sysctl (sysControl, 3, procInfo, &length, NULL, 0);
83
84 count = (length / sizeof (struct kinfo_proc));
6cb06a8c 85 gdb_printf (_("%d processes:\n"), count);
a80b95ba
TG
86 for (index = 0; index < count; ++index)
87 {
88 kern_return_t result;
89 mach_port_t taskPort;
90
91 result =
dda83cd7
SM
92 task_by_unix_pid (mach_task_self (), procInfo[index].kp_proc.p_pid,
93 &taskPort);
a80b95ba 94 if (KERN_SUCCESS == result)
dda83cd7 95 {
6cb06a8c
TT
96 gdb_printf (_(" %s is %d has task %#x\n"),
97 procInfo[index].kp_proc.p_comm,
98 procInfo[index].kp_proc.p_pid, taskPort);
dda83cd7 99 }
a80b95ba 100 else
dda83cd7 101 {
6cb06a8c
TT
102 gdb_printf (_(" %s is %d unknown task port\n"),
103 procInfo[index].kp_proc.p_comm,
104 procInfo[index].kp_proc.p_pid);
dda83cd7 105 }
a80b95ba
TG
106 }
107
108 xfree (procInfo);
109}
110
111static task_t
1d12d88f 112get_task_from_args (const char *args)
a80b95ba
TG
113{
114 task_t task;
115 char *eptr;
116
117 if (args == NULL || *args == 0)
118 {
d7e15655 119 if (inferior_ptid == null_ptid)
6cb06a8c 120 gdb_printf (_("No inferior running\n"));
089354bb
SM
121
122 darwin_inferior *priv = get_darwin_inferior (current_inferior ());
123
124 return priv->task;
a80b95ba
TG
125 }
126 if (strcmp (args, "gdb") == 0)
127 return mach_task_self ();
128 task = strtoul (args, &eptr, 0);
129 if (*eptr)
130 {
6cb06a8c 131 gdb_printf (_("cannot parse task id '%s'\n"), args);
a80b95ba
TG
132 return TASK_NULL;
133 }
134 return task;
135}
136
137static void
1d12d88f 138info_mach_task_command (const char *args, int from_tty)
a80b95ba
TG
139{
140 union
141 {
142 struct task_basic_info basic;
143 struct task_events_info events;
144 struct task_thread_times_info thread_times;
145 } task_info_data;
146
147 kern_return_t result;
148 unsigned int info_count;
149 task_t task;
150
151 task = get_task_from_args (args);
152 if (task == TASK_NULL)
153 return;
154
6cb06a8c 155 gdb_printf (_("TASK_BASIC_INFO for 0x%x:\n"), task);
a80b95ba
TG
156 info_count = TASK_BASIC_INFO_COUNT;
157 result = task_info (task,
dda83cd7
SM
158 TASK_BASIC_INFO,
159 (task_info_t) & task_info_data.basic, &info_count);
a80b95ba
TG
160 MACH_CHECK_ERROR (result);
161
162 PRINT_FIELD (&task_info_data.basic, suspend_count);
163 PRINT_FIELD (&task_info_data.basic, virtual_size);
164 PRINT_FIELD (&task_info_data.basic, resident_size);
165 PRINT_TV_FIELD (&task_info_data.basic, user_time);
166 PRINT_TV_FIELD (&task_info_data.basic, system_time);
6cb06a8c 167 gdb_printf (_("\nTASK_EVENTS_INFO:\n"));
a80b95ba
TG
168 info_count = TASK_EVENTS_INFO_COUNT;
169 result = task_info (task,
dda83cd7
SM
170 TASK_EVENTS_INFO,
171 (task_info_t) & task_info_data.events, &info_count);
a80b95ba
TG
172 MACH_CHECK_ERROR (result);
173
174 PRINT_FIELD (&task_info_data.events, faults);
175#if 0
176 PRINT_FIELD (&task_info_data.events, zero_fills);
177 PRINT_FIELD (&task_info_data.events, reactivations);
178#endif
179 PRINT_FIELD (&task_info_data.events, pageins);
180 PRINT_FIELD (&task_info_data.events, cow_faults);
181 PRINT_FIELD (&task_info_data.events, messages_sent);
182 PRINT_FIELD (&task_info_data.events, messages_received);
6cb06a8c 183 gdb_printf (_("\nTASK_THREAD_TIMES_INFO:\n"));
a80b95ba
TG
184 info_count = TASK_THREAD_TIMES_INFO_COUNT;
185 result = task_info (task,
dda83cd7
SM
186 TASK_THREAD_TIMES_INFO,
187 (task_info_t) & task_info_data.thread_times,
188 &info_count);
a80b95ba
TG
189 MACH_CHECK_ERROR (result);
190 PRINT_TV_FIELD (&task_info_data.thread_times, user_time);
191 PRINT_TV_FIELD (&task_info_data.thread_times, system_time);
192}
193
194static void
1d12d88f 195info_mach_ports_command (const char *args, int from_tty)
a80b95ba
TG
196{
197 port_name_array_t names;
198 port_type_array_t types;
199 unsigned int name_count, type_count;
200 kern_return_t result;
201 int index;
202 task_t task;
203
204 task = get_task_from_args (args);
205 if (task == TASK_NULL)
206 return;
207
208 result = mach_port_names (task, &names, &name_count, &types, &type_count);
209 MACH_CHECK_ERROR (result);
210
211 gdb_assert (name_count == type_count);
212
6cb06a8c
TT
213 gdb_printf (_("Ports for task 0x%x:\n"), task);
214 gdb_printf (_("port type\n"));
a80b95ba
TG
215 for (index = 0; index < name_count; ++index)
216 {
217 mach_port_t port = names[index];
218 unsigned int j;
219 struct type_descr
220 {
221 mach_port_type_t type;
222 const char *name;
223 mach_port_right_t right;
224 };
225 static struct type_descr descrs[] =
226 {
227 {MACH_PORT_TYPE_SEND, "send", MACH_PORT_RIGHT_SEND},
228 {MACH_PORT_TYPE_SEND_ONCE, "send-once", MACH_PORT_RIGHT_SEND_ONCE},
229 {MACH_PORT_TYPE_RECEIVE, "receive", MACH_PORT_RIGHT_RECEIVE},
230 {MACH_PORT_TYPE_PORT_SET, "port-set", MACH_PORT_RIGHT_PORT_SET},
231 {MACH_PORT_TYPE_DEAD_NAME, "dead", MACH_PORT_RIGHT_DEAD_NAME}
232 };
233
6cb06a8c 234 gdb_printf (_("%04x: %08x "), port, types[index]);
a80b95ba
TG
235 for (j = 0; j < sizeof(descrs) / sizeof(*descrs); j++)
236 if (types[index] & descrs[j].type)
237 {
238 mach_port_urefs_t ref;
239 kern_return_t ret;
240
6cb06a8c 241 gdb_printf (_(" %s("), descrs[j].name);
a80b95ba
TG
242 ret = mach_port_get_refs (task, port, descrs[j].right, &ref);
243 if (ret != KERN_SUCCESS)
6cb06a8c 244 gdb_printf (_("??"));
a80b95ba 245 else
6cb06a8c
TT
246 gdb_printf (_("%u"), ref);
247 gdb_printf (_(" refs)"));
a80b95ba
TG
248 }
249
250 if (task == task_self ())
251 {
252 if (port == task_self())
6cb06a8c 253 gdb_printf (_(" gdb-task"));
a80b95ba 254 else if (port == darwin_host_self)
6cb06a8c 255 gdb_printf (_(" host-self"));
a80b95ba 256 else if (port == darwin_ex_port)
6cb06a8c 257 gdb_printf (_(" gdb-exception"));
a80b95ba 258 else if (port == darwin_port_set)
6cb06a8c 259 gdb_printf (_(" gdb-port_set"));
d7e15655 260 else if (inferior_ptid != null_ptid)
a80b95ba 261 {
bb00b29d 262 struct inferior *inf = current_inferior ();
089354bb 263 darwin_inferior *priv = get_darwin_inferior (inf);
bb00b29d 264
089354bb 265 if (port == priv->task)
6cb06a8c 266 gdb_printf (_(" inferior-task"));
089354bb 267 else if (port == priv->notify_port)
6cb06a8c 268 gdb_printf (_(" inferior-notify"));
bb00b29d
TG
269 else
270 {
089354bb
SM
271 for (int k = 0; k < priv->exception_info.count; k++)
272 if (port == priv->exception_info.ports[k])
bb00b29d 273 {
6cb06a8c 274 gdb_printf (_(" inferior-excp-port"));
bb00b29d
TG
275 break;
276 }
277
089354bb 278 for (darwin_thread_t *t : priv->threads)
bb00b29d 279 {
089354bb
SM
280 if (port == t->gdb_port)
281 {
6cb06a8c
TT
282 gdb_printf (_(" inferior-thread for 0x%x"),
283 priv->task);
089354bb
SM
284 break;
285 }
bb00b29d 286 }
089354bb 287
bb00b29d 288 }
a80b95ba
TG
289 }
290 }
6cb06a8c 291 gdb_printf (_("\n"));
a80b95ba
TG
292 }
293
294 vm_deallocate (task_self (), (vm_address_t) names,
dda83cd7 295 (name_count * sizeof (mach_port_t)));
a80b95ba 296 vm_deallocate (task_self (), (vm_address_t) types,
dda83cd7 297 (type_count * sizeof (mach_port_type_t)));
a80b95ba
TG
298}
299
300
c381a3f6 301static void
a80b95ba
TG
302darwin_debug_port_info (task_t task, mach_port_t port)
303{
304 kern_return_t kret;
305 mach_port_status_t status;
306 mach_msg_type_number_t len = sizeof (status);
307
308 kret = mach_port_get_attributes
309 (task, port, MACH_PORT_RECEIVE_STATUS, (mach_port_info_t)&status, &len);
310 MACH_CHECK_ERROR (kret);
311
6cb06a8c
TT
312 gdb_printf (_("Port 0x%lx in task 0x%lx:\n"), (unsigned long) port,
313 (unsigned long) task);
314 gdb_printf (_(" port set: 0x%x\n"), status.mps_pset);
315 gdb_printf (_(" seqno: 0x%x\n"), status.mps_seqno);
316 gdb_printf (_(" mscount: 0x%x\n"), status.mps_mscount);
317 gdb_printf (_(" qlimit: 0x%x\n"), status.mps_qlimit);
318 gdb_printf (_(" msgcount: 0x%x\n"), status.mps_msgcount);
319 gdb_printf (_(" sorights: 0x%x\n"), status.mps_sorights);
320 gdb_printf (_(" srights: 0x%x\n"), status.mps_srights);
321 gdb_printf (_(" pdrequest: 0x%x\n"), status.mps_pdrequest);
322 gdb_printf (_(" nsrequest: 0x%x\n"), status.mps_nsrequest);
323 gdb_printf (_(" flags: 0x%x\n"), status.mps_flags);
a80b95ba
TG
324}
325
326static void
1d12d88f 327info_mach_port_command (const char *args, int from_tty)
a80b95ba
TG
328{
329 task_t task;
330 mach_port_t port;
331
332 CHECK_ARGS (_("Task and port"), args);
333 sscanf (args, "0x%x 0x%x", &task, &port);
334
335 darwin_debug_port_info (task, port);
336}
337
338static void
1d12d88f 339info_mach_threads_command (const char *args, int from_tty)
a80b95ba
TG
340{
341 thread_array_t threads;
342 unsigned int thread_count;
343 kern_return_t result;
344 task_t task;
345 int i;
346
347 task = get_task_from_args (args);
348 if (task == TASK_NULL)
349 return;
350
351 result = task_threads (task, &threads, &thread_count);
352 MACH_CHECK_ERROR (result);
353
6cb06a8c 354 gdb_printf (_("Threads in task %#x:\n"), task);
a80b95ba
TG
355 for (i = 0; i < thread_count; ++i)
356 {
6cb06a8c 357 gdb_printf (_(" %#x\n"), threads[i]);
a80b95ba
TG
358 mach_port_deallocate (task_self (), threads[i]);
359 }
360
361 vm_deallocate (task_self (), (vm_address_t) threads,
dda83cd7 362 (thread_count * sizeof (thread_t)));
a80b95ba
TG
363}
364
365static void
1d12d88f 366info_mach_thread_command (const char *args, int from_tty)
a80b95ba
TG
367{
368 union
369 {
370 struct thread_basic_info basic;
371 } thread_info_data;
372
373 thread_t thread;
374 kern_return_t result;
375 unsigned int info_count;
376
377 CHECK_ARGS (_("Thread"), args);
378 sscanf (args, "0x%x", &thread);
379
6cb06a8c 380 gdb_printf (_("THREAD_BASIC_INFO\n"));
a80b95ba
TG
381 info_count = THREAD_BASIC_INFO_COUNT;
382 result = thread_info (thread,
383 THREAD_BASIC_INFO,
384 (thread_info_t) & thread_info_data.basic,
385 &info_count);
386 MACH_CHECK_ERROR (result);
387
388#if 0
389 PRINT_FIELD (&thread_info_data.basic, user_time);
390 PRINT_FIELD (&thread_info_data.basic, system_time);
391#endif
392 PRINT_FIELD (&thread_info_data.basic, cpu_usage);
393 PRINT_FIELD (&thread_info_data.basic, run_state);
394 PRINT_FIELD (&thread_info_data.basic, flags);
395 PRINT_FIELD (&thread_info_data.basic, suspend_count);
396 PRINT_FIELD (&thread_info_data.basic, sleep_time);
397}
398
399static const char *
400unparse_protection (vm_prot_t p)
401{
402 switch (p)
403 {
404 case VM_PROT_NONE:
405 return "---";
406 case VM_PROT_READ:
407 return "r--";
408 case VM_PROT_WRITE:
409 return "-w-";
410 case VM_PROT_READ | VM_PROT_WRITE:
411 return "rw-";
412 case VM_PROT_EXECUTE:
413 return "--x";
414 case VM_PROT_EXECUTE | VM_PROT_READ:
415 return "r-x";
416 case VM_PROT_EXECUTE | VM_PROT_WRITE:
417 return "-wx";
418 case VM_PROT_EXECUTE | VM_PROT_WRITE | VM_PROT_READ:
419 return "rwx";
420 default:
421 return "???";
422 }
423}
424
425static const char *
426unparse_inheritance (vm_inherit_t i)
427{
428 switch (i)
429 {
430 case VM_INHERIT_SHARE:
431 return _("share");
432 case VM_INHERIT_COPY:
433 return _("copy ");
434 case VM_INHERIT_NONE:
435 return _("none ");
436 default:
437 return _("??? ");
438 }
439}
440
441static const char *
442unparse_share_mode (unsigned char p)
443{
444 switch (p)
445 {
446 case SM_COW:
447 return _("cow");
448 case SM_PRIVATE:
449 return _("private");
450 case SM_EMPTY:
451 return _("empty");
452 case SM_SHARED:
453 return _("shared");
454 case SM_TRUESHARED:
455 return _("true-shrd");
456 case SM_PRIVATE_ALIASED:
457 return _("prv-alias");
458 case SM_SHARED_ALIASED:
459 return _("shr-alias");
460 default:
461 return _("???");
462 }
463}
464
465static const char *
466unparse_user_tag (unsigned int tag)
467{
468 switch (tag)
469 {
470 case 0:
471 return _("default");
472 case VM_MEMORY_MALLOC:
473 return _("malloc");
474 case VM_MEMORY_MALLOC_SMALL:
475 return _("malloc_small");
476 case VM_MEMORY_MALLOC_LARGE:
477 return _("malloc_large");
478 case VM_MEMORY_MALLOC_HUGE:
479 return _("malloc_huge");
480 case VM_MEMORY_SBRK:
481 return _("sbrk");
482 case VM_MEMORY_REALLOC:
483 return _("realloc");
484 case VM_MEMORY_MALLOC_TINY:
485 return _("malloc_tiny");
486 case VM_MEMORY_ANALYSIS_TOOL:
487 return _("analysis_tool");
488 case VM_MEMORY_MACH_MSG:
489 return _("mach_msg");
490 case VM_MEMORY_IOKIT:
491 return _("iokit");
492 case VM_MEMORY_STACK:
493 return _("stack");
494 case VM_MEMORY_GUARD:
495 return _("guard");
496 case VM_MEMORY_SHARED_PMAP:
497 return _("shared_pmap");
498 case VM_MEMORY_DYLIB:
499 return _("dylib");
500 case VM_MEMORY_APPKIT:
501 return _("appkit");
502 case VM_MEMORY_FOUNDATION:
503 return _("foundation");
504 default:
505 return NULL;
506 }
507}
508
509static void
510darwin_debug_regions (task_t task, mach_vm_address_t address, int max)
511{
512 kern_return_t kret;
513 vm_region_basic_info_data_64_t info, prev_info;
514 mach_vm_address_t prev_address;
515 mach_vm_size_t size, prev_size;
516
517 mach_port_t object_name;
518 mach_msg_type_number_t count;
519
520 int nsubregions = 0;
521 int num_printed = 0;
522
523 count = VM_REGION_BASIC_INFO_COUNT_64;
524 kret = mach_vm_region (task, &address, &size, VM_REGION_BASIC_INFO_64,
525 (vm_region_info_t) &info, &count, &object_name);
526 if (kret != KERN_SUCCESS)
527 {
6cb06a8c 528 gdb_printf (_("No memory regions."));
a80b95ba
TG
529 return;
530 }
531 memcpy (&prev_info, &info, sizeof (vm_region_basic_info_data_64_t));
532 prev_address = address;
533 prev_size = size;
534 nsubregions = 1;
535
536 for (;;)
537 {
538 int print = 0;
539 int done = 0;
540
541 address = prev_address + prev_size;
542
0963b4bd 543 /* Check to see if address space has wrapped around. */
a80b95ba 544 if (address == 0)
dda83cd7 545 print = done = 1;
a80b95ba
TG
546
547 if (!done)
dda83cd7
SM
548 {
549 count = VM_REGION_BASIC_INFO_COUNT_64;
550 kret =
551 mach_vm_region (task, &address, &size, VM_REGION_BASIC_INFO_64,
552 (vm_region_info_t) &info, &count, &object_name);
553 if (kret != KERN_SUCCESS)
554 {
555 size = 0;
556 print = done = 1;
557 }
558 }
a80b95ba
TG
559
560 if (address != prev_address + prev_size)
dda83cd7 561 print = 1;
a80b95ba
TG
562
563 if ((info.protection != prev_info.protection)
dda83cd7
SM
564 || (info.max_protection != prev_info.max_protection)
565 || (info.inheritance != prev_info.inheritance)
566 || (info.shared != prev_info.reserved)
567 || (info.reserved != prev_info.reserved))
568 print = 1;
a80b95ba
TG
569
570 if (print)
dda83cd7 571 {
99d9c3b9 572 gdbarch *arch = current_inferior ()->arch ();
6cb06a8c 573 gdb_printf (_("%s-%s %s/%s %s %s %s"),
99d9c3b9
SM
574 paddress (arch, prev_address),
575 paddress (arch, prev_address + prev_size),
6cb06a8c
TT
576 unparse_protection (prev_info.protection),
577 unparse_protection (prev_info.max_protection),
578 unparse_inheritance (prev_info.inheritance),
579 prev_info.shared ? _("shrd") : _("priv"),
580 prev_info.reserved ? _("reserved") : _("not-rsvd"));
dda83cd7
SM
581
582 if (nsubregions > 1)
6cb06a8c 583 gdb_printf (_(" (%d sub-rgn)"), nsubregions);
dda83cd7 584
6cb06a8c 585 gdb_printf (_("\n"));
dda83cd7
SM
586
587 prev_address = address;
588 prev_size = size;
589 memcpy (&prev_info, &info, sizeof (vm_region_basic_info_data_64_t));
590 nsubregions = 1;
591
592 num_printed++;
593 }
a80b95ba 594 else
dda83cd7
SM
595 {
596 prev_size += size;
597 nsubregions++;
598 }
a80b95ba
TG
599
600 if ((max > 0) && (num_printed >= max))
dda83cd7 601 done = 1;
a80b95ba
TG
602
603 if (done)
dda83cd7 604 break;
a80b95ba
TG
605 }
606}
607
608static void
609darwin_debug_regions_recurse (task_t task)
610{
a80b95ba
TG
611 mach_vm_address_t r_start;
612 mach_vm_size_t r_size;
613 natural_t r_depth;
614 mach_msg_type_number_t r_info_size;
615 vm_region_submap_short_info_data_64_t r_info;
616 kern_return_t kret;
6e2f5b22 617 struct ui_out *uiout = current_uiout;
bb00b29d 618
dc9fe180 619 ui_out_emit_table table_emitter (uiout, 9, -1, "regions");
bb00b29d 620
99d9c3b9 621 if (gdbarch_addr_bit (current_inferior ()->arch ()) <= 32)
bb00b29d 622 {
112e8700
SM
623 uiout->table_header (10, ui_left, "start", "Start");
624 uiout->table_header (10, ui_left, "end", "End");
bb00b29d
TG
625 }
626 else
627 {
112e8700
SM
628 uiout->table_header (18, ui_left, "start", "Start");
629 uiout->table_header (18, ui_left, "end", "End");
bb00b29d 630 }
112e8700
SM
631 uiout->table_header (3, ui_left, "min-prot", "Min");
632 uiout->table_header (3, ui_left, "max-prot", "Max");
633 uiout->table_header (5, ui_left, "inheritence", "Inh");
634 uiout->table_header (9, ui_left, "share-mode", "Shr");
635 uiout->table_header (1, ui_left, "depth", "D");
636 uiout->table_header (3, ui_left, "submap", "Sm");
637 uiout->table_header (0, ui_noalign, "tag", "Tag");
bb00b29d 638
112e8700 639 uiout->table_body ();
a80b95ba
TG
640
641 r_start = 0;
642 r_depth = 0;
643 while (1)
644 {
645 const char *tag;
646
647 r_info_size = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64;
648 r_size = -1;
649 kret = mach_vm_region_recurse (task, &r_start, &r_size, &r_depth,
650 (vm_region_recurse_info_t) &r_info,
651 &r_info_size);
652 if (kret != KERN_SUCCESS)
653 break;
bb00b29d 654
dc9fe180
TT
655 {
656 ui_out_emit_tuple tuple_emitter (uiout, "regions-row");
99d9c3b9 657 gdbarch *arch = current_inferior ()->arch ();
dc9fe180 658
99d9c3b9
SM
659 uiout->field_core_addr ("start", arch, r_start);
660 uiout->field_core_addr ("end", arch, r_start + r_size);
dc9fe180
TT
661 uiout->field_string ("min-prot",
662 unparse_protection (r_info.protection));
663 uiout->field_string ("max-prot",
664 unparse_protection (r_info.max_protection));
665 uiout->field_string ("inheritence",
666 unparse_inheritance (r_info.inheritance));
667 uiout->field_string ("share-mode",
668 unparse_share_mode (r_info.share_mode));
381befee 669 uiout->field_signed ("depth", r_depth);
dc9fe180
TT
670 uiout->field_string ("submap",
671 r_info.is_submap ? _("sm ") : _("obj"));
672 tag = unparse_user_tag (r_info.user_tag);
673 if (tag)
674 uiout->field_string ("tag", tag);
675 else
381befee 676 uiout->field_signed ("tag", r_info.user_tag);
dc9fe180 677 }
bb00b29d 678
76761936 679 uiout->text ("\n");
bb00b29d 680
a80b95ba
TG
681 if (r_info.is_submap)
682 r_depth++;
683 else
684 r_start += r_size;
685 }
686}
687
688
689static void
690darwin_debug_region (task_t task, mach_vm_address_t address)
691{
692 darwin_debug_regions (task, address, 1);
693}
694
695static void
1d12d88f 696info_mach_regions_command (const char *args, int from_tty)
a80b95ba
TG
697{
698 task_t task;
699
700 task = get_task_from_args (args);
701 if (task == TASK_NULL)
702 return;
703
704 darwin_debug_regions (task, 0, -1);
705}
706
707static void
1d12d88f 708info_mach_regions_recurse_command (const char *args, int from_tty)
a80b95ba
TG
709{
710 task_t task;
711
712 task = get_task_from_args (args);
713 if (task == TASK_NULL)
714 return;
715
716 darwin_debug_regions_recurse (task);
717}
718
719static void
1d12d88f 720info_mach_region_command (const char *exp, int from_tty)
a80b95ba 721{
a80b95ba
TG
722 struct value *val;
723 mach_vm_address_t address;
bb00b29d 724 struct inferior *inf;
a80b95ba 725
97f00e36 726 expression_up expr = parse_expression (exp);
43048e46 727 val = expr->evaluate ();
d0c97917 728 if (TYPE_IS_REFERENCE (val->type ()))
a80b95ba
TG
729 {
730 val = value_ind (val);
731 }
bb00b29d 732 address = value_as_address (val);
a80b95ba 733
d7e15655 734 if (inferior_ptid == null_ptid)
a80b95ba
TG
735 error (_("Inferior not available"));
736
bb00b29d 737 inf = current_inferior ();
089354bb
SM
738 darwin_inferior *priv = get_darwin_inferior (inf);
739 darwin_debug_region (priv->task, address);
a80b95ba
TG
740}
741
742static void
743disp_exception (const darwin_exception_info *info)
744{
745 int i;
746
6cb06a8c 747 gdb_printf (_("%d exceptions:\n"), info->count);
a80b95ba
TG
748 for (i = 0; i < info->count; i++)
749 {
750 exception_mask_t mask = info->masks[i];
751
6cb06a8c 752 gdb_printf (_("port 0x%04x, behavior: "), info->ports[i]);
a80b95ba
TG
753 switch (info->behaviors[i])
754 {
755 case EXCEPTION_DEFAULT:
6cb06a8c 756 gdb_printf (_("default"));
a80b95ba
TG
757 break;
758 case EXCEPTION_STATE:
6cb06a8c 759 gdb_printf (_("state"));
a80b95ba
TG
760 break;
761 case EXCEPTION_STATE_IDENTITY:
6cb06a8c 762 gdb_printf (_("state-identity"));
a80b95ba
TG
763 break;
764 default:
6cb06a8c 765 gdb_printf (_("0x%x"), info->behaviors[i]);
a80b95ba 766 }
6cb06a8c 767 gdb_printf (_(", masks:"));
a80b95ba 768 if (mask & EXC_MASK_BAD_ACCESS)
6cb06a8c 769 gdb_printf (_(" BAD_ACCESS"));
a80b95ba 770 if (mask & EXC_MASK_BAD_INSTRUCTION)
6cb06a8c 771 gdb_printf (_(" BAD_INSTRUCTION"));
a80b95ba 772 if (mask & EXC_MASK_ARITHMETIC)
6cb06a8c 773 gdb_printf (_(" ARITHMETIC"));
a80b95ba 774 if (mask & EXC_MASK_EMULATION)
6cb06a8c 775 gdb_printf (_(" EMULATION"));
a80b95ba 776 if (mask & EXC_MASK_SOFTWARE)
6cb06a8c 777 gdb_printf (_(" SOFTWARE"));
a80b95ba 778 if (mask & EXC_MASK_BREAKPOINT)
6cb06a8c 779 gdb_printf (_(" BREAKPOINT"));
a80b95ba 780 if (mask & EXC_MASK_SYSCALL)
6cb06a8c 781 gdb_printf (_(" SYSCALL"));
a80b95ba 782 if (mask & EXC_MASK_MACH_SYSCALL)
6cb06a8c 783 gdb_printf (_(" MACH_SYSCALL"));
a80b95ba 784 if (mask & EXC_MASK_RPC_ALERT)
6cb06a8c 785 gdb_printf (_(" RPC_ALERT"));
a80b95ba 786 if (mask & EXC_MASK_CRASH)
6cb06a8c
TT
787 gdb_printf (_(" CRASH"));
788 gdb_printf (_("\n"));
a80b95ba
TG
789 }
790}
791
792static void
1d12d88f 793info_mach_exceptions_command (const char *args, int from_tty)
a80b95ba 794{
a80b95ba
TG
795 kern_return_t kret;
796 darwin_exception_info info;
797
798 info.count = sizeof (info.ports) / sizeof (info.ports[0]);
799
800 if (args != NULL)
801 {
802 if (strcmp (args, "saved") == 0)
803 {
d7e15655 804 if (inferior_ptid == null_ptid)
6cb06a8c 805 gdb_printf (_("No inferior running\n"));
089354bb
SM
806
807 darwin_inferior *priv = get_darwin_inferior (current_inferior ());
808
809 disp_exception (&priv->exception_info);
a80b95ba
TG
810 return;
811 }
812 else if (strcmp (args, "host") == 0)
813 {
405feb71 814 /* FIXME: This needs a privileged host port! */
a80b95ba
TG
815 kret = host_get_exception_ports
816 (darwin_host_self, EXC_MASK_ALL, info.masks,
817 &info.count, info.ports, info.behaviors, info.flavors);
818 MACH_CHECK_ERROR (kret);
819 disp_exception (&info);
820 }
821 else
822 error (_("Parameter is saved, host or none"));
823 }
824 else
825 {
bb00b29d
TG
826 struct inferior *inf;
827
d7e15655 828 if (inferior_ptid == null_ptid)
6cb06a8c 829 gdb_printf (_("No inferior running\n"));
bb00b29d 830 inf = current_inferior ();
a80b95ba 831
089354bb
SM
832 darwin_inferior *priv = get_darwin_inferior (inf);
833
a80b95ba 834 kret = task_get_exception_ports
089354bb 835 (priv->task, EXC_MASK_ALL, info.masks,
a80b95ba
TG
836 &info.count, info.ports, info.behaviors, info.flavors);
837 MACH_CHECK_ERROR (kret);
838 disp_exception (&info);
839 }
840}
841
6c265988 842void _initialize_darwin_info_commands ();
a80b95ba 843void
6c265988 844_initialize_darwin_info_commands ()
a80b95ba
TG
845{
846 add_info ("mach-tasks", info_mach_tasks_command,
dda83cd7 847 _("Get list of tasks in system."));
a80b95ba 848 add_info ("mach-ports", info_mach_ports_command,
dda83cd7 849 _("Get list of ports in a task."));
a80b95ba 850 add_info ("mach-port", info_mach_port_command,
dda83cd7 851 _("Get info on a specific port."));
a80b95ba 852 add_info ("mach-task", info_mach_task_command,
dda83cd7 853 _("Get info on a specific task."));
a80b95ba 854 add_info ("mach-threads", info_mach_threads_command,
dda83cd7 855 _("Get list of threads in a task."));
a80b95ba 856 add_info ("mach-thread", info_mach_thread_command,
dda83cd7 857 _("Get info on a specific thread."));
a80b95ba
TG
858
859 add_info ("mach-regions", info_mach_regions_command,
dda83cd7 860 _("Get information on all mach region for the task."));
a80b95ba 861 add_info ("mach-regions-rec", info_mach_regions_recurse_command,
dda83cd7 862 _("Get information on all mach sub region for the task."));
a80b95ba 863 add_info ("mach-region", info_mach_region_command,
dda83cd7 864 _("Get information on mach region at given address."));
a80b95ba
TG
865
866 add_info ("mach-exceptions", info_mach_exceptions_command,
dda83cd7 867 _("Disp mach exceptions."));
a80b95ba 868}