]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kdp/kdp.c
00f50e82ae3bb1b9a7c8b56b5007f55b471c93d2
[apple/xnu.git] / osfmk / kdp / kdp.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <mach/mach_types.h>
30 #include <kern/debug.h>
31
32 #include <kdp/kdp_internal.h>
33 #include <kdp/kdp_private.h>
34
35 #include <libsa/types.h>
36
37 #include <string.h> /* bcopy */
38
39 #include <kern/processor.h>
40 #include <kern/thread.h>
41 #include <vm/vm_map.h>
42 #include <vm/vm_kern.h>
43
44 int kdp_vm_read( caddr_t, caddr_t, unsigned int);
45 int kdp_vm_write( caddr_t, caddr_t, unsigned int);
46
47 #define DO_ALIGN 1 /* align all packet data accesses */
48
49 #define KDP_TEST_HARNESS 0
50 #if KDP_TEST_HARNESS
51 #define dprintf(x) kprintf x
52 #else
53 #define dprintf(x)
54 #endif
55
56 static kdp_dispatch_t
57 dispatch_table[KDP_HOSTREBOOT - KDP_CONNECT +1] =
58 {
59 /* 0 */ kdp_connect,
60 /* 1 */ kdp_disconnect,
61 /* 2 */ kdp_hostinfo,
62 /* 3 */ kdp_version,
63 /* 4 */ kdp_maxbytes,
64 /* 5 */ kdp_readmem,
65 /* 6 */ kdp_writemem,
66 /* 7 */ kdp_readregs,
67 /* 8 */ kdp_writeregs,
68 /* 9 */ kdp_unknown,
69 /* A */ kdp_unknown,
70 /* B */ kdp_suspend,
71 /* C */ kdp_resumecpus,
72 /* D */ kdp_unknown,
73 /* E */ kdp_unknown,
74 /* F */ kdp_breakpoint_set,
75 /*10 */ kdp_breakpoint_remove,
76 /*11 */ kdp_regions,
77 /*12 */ kdp_reattach,
78 /*13 */ kdp_reboot
79 };
80
81 kdp_glob_t kdp;
82
83
84 #define MAX_BREAKPOINTS 100
85 #define KDP_MAX_BREAKPOINTS 100
86
87 #define BREAKPOINT_NOT_FOUND 101
88 #define BREAKPOINT_ALREADY_SET 102
89
90 #define KDP_VERSION 10
91
92 typedef struct{
93 unsigned int address;
94 unsigned int old_instruction;
95 } kdp_breakpoint_record_t;
96
97 static kdp_breakpoint_record_t breakpoint_list[MAX_BREAKPOINTS];
98 static unsigned int breakpoints_initialized = 0;
99
100 int reattach_wait = 0;
101 int noresume_on_disconnect = 0;
102
103 #define MAXCOMLEN 16
104
105 struct thread_snapshot {
106 uint32_t snapshot_magic;
107 thread_t thread_id;
108 int32_t state;
109 wait_queue_t wait_queue;
110 event64_t wait_event;
111 vm_offset_t kernel_stack;
112 vm_offset_t reserved_stack;
113 thread_continue_t continuation;
114 uint32_t nkern_frames;
115 char user64_p;
116 uint32_t nuser_frames;
117 int32_t pid;
118 char p_comm[MAXCOMLEN + 1];
119 };
120
121 typedef struct thread_snapshot *thread_snapshot_t;
122
123 extern int
124 machine_trace_thread(thread_t thread, uint32_t tracepos, uint32_t tracebound, int nframes, boolean_t user_p);
125 extern int
126 machine_trace_thread64(thread_t thread, uint32_t tracepos, uint32_t tracebound, int nframes, boolean_t user_p);
127 extern int
128 proc_pid(void *p);
129 extern void
130 proc_name(int pid, char *buf, int size);
131 extern void
132 kdp_snapshot_postflight(void);
133
134 static int
135 pid_from_task(task_t task);
136
137 int
138 kdp_stackshot(int pid, uint32_t tracebuf, uint32_t tracebuf_size, unsigned trace_options, uint32_t *pbytesTraced);
139
140 extern char version[];
141
142 boolean_t
143 kdp_packet(
144 unsigned char *pkt,
145 int *len,
146 unsigned short *reply_port
147 )
148 {
149 static unsigned aligned_pkt[1538/sizeof(unsigned)+1]; // max ether pkt
150 kdp_pkt_t *rd = (kdp_pkt_t *)&aligned_pkt;
151 int plen = *len;
152 unsigned int req;
153 boolean_t ret;
154
155 #if DO_ALIGN
156 bcopy((char *)pkt, (char *)rd, sizeof(aligned_pkt));
157 #else
158 rd = (kdp_pkt_t *)pkt;
159 #endif
160 if (plen < sizeof (rd->hdr) || rd->hdr.len != plen) {
161 printf("kdp_packet bad len pkt %d hdr %d\n", plen, rd->hdr.len);
162
163 return (FALSE);
164 }
165
166 if (rd->hdr.is_reply) {
167 printf("kdp_packet reply recvd req %x seq %x\n",
168 rd->hdr.request, rd->hdr.seq);
169
170 return (FALSE);
171 }
172
173 req = rd->hdr.request;
174 if ((req < KDP_CONNECT) || (req > KDP_HOSTREBOOT)) {
175 printf("kdp_packet bad request %x len %d seq %x key %x\n",
176 rd->hdr.request, rd->hdr.len, rd->hdr.seq, rd->hdr.key);
177
178 return (FALSE);
179 }
180
181 ret = ((*dispatch_table[req - KDP_CONNECT])(rd, len, reply_port));
182 #if DO_ALIGN
183 bcopy((char *)rd, (char *) pkt, *len);
184 #endif
185 return ret;
186 }
187
188 static boolean_t
189 kdp_unknown(
190 kdp_pkt_t *pkt,
191 int *len,
192 unsigned short *reply_port
193 )
194 {
195 kdp_pkt_t *rd = (kdp_pkt_t *)pkt;
196
197 printf("kdp_unknown request %x len %d seq %x key %x\n",
198 rd->hdr.request, rd->hdr.len, rd->hdr.seq, rd->hdr.key);
199
200 return (FALSE);
201 }
202
203 static boolean_t
204 kdp_connect(
205 kdp_pkt_t *pkt,
206 int *len,
207 unsigned short *reply_port
208 )
209 {
210 kdp_connect_req_t *rq = &pkt->connect_req;
211 int plen = *len;
212 kdp_connect_reply_t *rp = &pkt->connect_reply;
213
214 if (plen < sizeof (*rq))
215 return (FALSE);
216
217 dprintf(("kdp_connect seq %x greeting %s\n", rq->hdr.seq, rq->greeting));
218
219 if (kdp.is_conn) {
220 if (rq->hdr.seq == kdp.conn_seq) /* duplicate request */
221 rp->error = KDPERR_NO_ERROR;
222 else
223 rp->error = KDPERR_ALREADY_CONNECTED;
224 }
225 else {
226 kdp.reply_port = rq->req_reply_port;
227 kdp.exception_port = rq->exc_note_port;
228 kdp.is_conn = TRUE;
229 kdp.conn_seq = rq->hdr.seq;
230
231 rp->error = KDPERR_NO_ERROR;
232 }
233
234 rp->hdr.is_reply = 1;
235 rp->hdr.len = sizeof (*rp);
236
237 *reply_port = kdp.reply_port;
238 *len = rp->hdr.len;
239
240 if (current_debugger == KDP_CUR_DB)
241 active_debugger=1;
242
243 return (TRUE);
244 }
245
246 static boolean_t
247 kdp_disconnect(
248 kdp_pkt_t *pkt,
249 int *len,
250 unsigned short *reply_port
251 )
252 {
253 kdp_disconnect_req_t *rq = &pkt->disconnect_req;
254 int plen = *len;
255 kdp_disconnect_reply_t *rp = &pkt->disconnect_reply;
256
257 if (plen < sizeof (*rq))
258 return (FALSE);
259
260 if (!kdp.is_conn)
261 return (FALSE);
262
263 dprintf(("kdp_disconnect\n"));
264
265 *reply_port = kdp.reply_port;
266
267 kdp.reply_port = kdp.exception_port = 0;
268 kdp.is_halted = kdp.is_conn = FALSE;
269 kdp.exception_seq = kdp.conn_seq = 0;
270
271 if (noresume_on_disconnect == 1) {
272 reattach_wait = 1;
273 noresume_on_disconnect = 0;
274 }
275
276 rp->hdr.is_reply = 1;
277 rp->hdr.len = sizeof (*rp);
278
279 *len = rp->hdr.len;
280
281 if (current_debugger == KDP_CUR_DB)
282 active_debugger=0;
283
284 return (TRUE);
285 }
286
287 static boolean_t
288 kdp_reattach(
289 kdp_pkt_t *pkt,
290 int *len,
291 unsigned short *reply_port
292 )
293 {
294 kdp_reattach_req_t *rq = &pkt->reattach_req;
295 kdp_disconnect_reply_t *rp = &pkt->disconnect_reply;
296
297 kdp.is_conn = TRUE;
298 kdp_disconnect(pkt, len, reply_port);
299 *reply_port = rq->req_reply_port;
300 reattach_wait = 1;
301 return (TRUE);
302 }
303
304 static boolean_t
305 kdp_hostinfo(
306 kdp_pkt_t *pkt,
307 int *len,
308 unsigned short *reply_port
309 )
310 {
311 kdp_hostinfo_req_t *rq = &pkt->hostinfo_req;
312 int plen = *len;
313 kdp_hostinfo_reply_t *rp = &pkt->hostinfo_reply;
314
315 if (plen < sizeof (*rq))
316 return (FALSE);
317
318 rp->hdr.is_reply = 1;
319 rp->hdr.len = sizeof (*rp);
320
321 kdp_machine_hostinfo(&rp->hostinfo);
322
323 *reply_port = kdp.reply_port;
324 *len = rp->hdr.len;
325
326 return (TRUE);
327 }
328
329 static boolean_t
330 kdp_suspend(
331 kdp_pkt_t *pkt,
332 int *len,
333 unsigned short *reply_port
334 )
335 {
336 kdp_suspend_req_t *rq = &pkt->suspend_req;
337 int plen = *len;
338 kdp_suspend_reply_t *rp = &pkt->suspend_reply;
339
340 if (plen < sizeof (*rq))
341 return (FALSE);
342
343 rp->hdr.is_reply = 1;
344 rp->hdr.len = sizeof (*rp);
345
346 dprintf(("kdp_suspend\n"));
347
348 kdp.is_halted = TRUE;
349
350 *reply_port = kdp.reply_port;
351 *len = rp->hdr.len;
352
353 return (TRUE);
354 }
355
356 static boolean_t
357 kdp_resumecpus(
358 kdp_pkt_t *pkt,
359 int *len,
360 unsigned short *reply_port
361 )
362 {
363 kdp_resumecpus_req_t *rq = &pkt->resumecpus_req;
364 int plen = *len;
365 kdp_resumecpus_reply_t *rp = &pkt->resumecpus_reply;
366
367 if (plen < sizeof (*rq))
368 return (FALSE);
369
370 rp->hdr.is_reply = 1;
371 rp->hdr.len = sizeof (*rp);
372
373 dprintf(("kdp_resumecpus %x\n", rq->cpu_mask));
374
375 kdp.is_halted = FALSE;
376
377 *reply_port = kdp.reply_port;
378 *len = rp->hdr.len;
379
380 return (TRUE);
381 }
382
383 static boolean_t
384 kdp_writemem(
385 kdp_pkt_t *pkt,
386 int *len,
387 unsigned short *reply_port
388 )
389 {
390 kdp_writemem_req_t *rq = &pkt->writemem_req;
391 int plen = *len;
392 kdp_writemem_reply_t *rp = &pkt->writemem_reply;
393 int cnt;
394
395 if (plen < sizeof (*rq))
396 return (FALSE);
397
398 if (rq->nbytes > MAX_KDP_DATA_SIZE)
399 rp->error = KDPERR_BAD_NBYTES;
400 else {
401 dprintf(("kdp_writemem addr %x size %d\n", rq->address, rq->nbytes));
402
403 cnt = kdp_vm_write((caddr_t)rq->data, (caddr_t)rq->address, rq->nbytes);
404 rp->error = KDPERR_NO_ERROR;
405 }
406
407 rp->hdr.is_reply = 1;
408 rp->hdr.len = sizeof (*rp);
409
410 *reply_port = kdp.reply_port;
411 *len = rp->hdr.len;
412
413 return (TRUE);
414 }
415
416 static boolean_t
417 kdp_readmem(
418 kdp_pkt_t *pkt,
419 int *len,
420 unsigned short *reply_port
421 )
422 {
423 kdp_readmem_req_t *rq = &pkt->readmem_req;
424 int plen = *len;
425 kdp_readmem_reply_t *rp = &pkt->readmem_reply;
426 int cnt;
427 #if __i386__
428 void *pversion = &version;
429 #endif
430 if (plen < sizeof (*rq))
431 return (FALSE);
432
433 rp->hdr.is_reply = 1;
434 rp->hdr.len = sizeof (*rp);
435
436 if (rq->nbytes > MAX_KDP_DATA_SIZE)
437 rp->error = KDPERR_BAD_NBYTES;
438 else {
439 unsigned int n = rq->nbytes;
440
441 dprintf(("kdp_readmem addr %x size %d\n", rq->address, rq->nbytes));
442 #if __i386__
443 /* XXX This is a hack to facilitate the "showversion" macro
444 * on i386, which is used to obtain the kernel version without
445 * symbols - a pointer to the version string should eventually
446 * be pinned at a fixed address when an equivalent of the
447 * VECTORS segment (loaded at a fixed load address, and contains
448 * a table) is implemented on x86, as with PPC.
449 */
450 if (rq->address == (void *)0x501C)
451 rq->address = &pversion;
452 #endif
453 cnt = kdp_vm_read((caddr_t)rq->address, (caddr_t)rp->data, rq->nbytes);
454 rp->error = KDPERR_NO_ERROR;
455
456 rp->hdr.len += cnt;
457 }
458
459 *reply_port = kdp.reply_port;
460 *len = rp->hdr.len;
461
462 return (TRUE);
463 }
464
465 static boolean_t
466 kdp_maxbytes(
467 kdp_pkt_t *pkt,
468 int *len,
469 unsigned short *reply_port
470 )
471 {
472 kdp_maxbytes_req_t *rq = &pkt->maxbytes_req;
473 int plen = *len;
474 kdp_maxbytes_reply_t *rp = &pkt->maxbytes_reply;
475
476 if (plen < sizeof (*rq))
477 return (FALSE);
478
479 rp->hdr.is_reply = 1;
480 rp->hdr.len = sizeof (*rp);
481
482 dprintf(("kdp_maxbytes\n"));
483
484 rp->max_bytes = MAX_KDP_DATA_SIZE;
485
486 *reply_port = kdp.reply_port;
487 *len = rp->hdr.len;
488
489 return (TRUE);
490 }
491
492 static boolean_t
493 kdp_version(
494 kdp_pkt_t *pkt,
495 int *len,
496 unsigned short *reply_port
497 )
498 {
499 kdp_version_req_t *rq = &pkt->version_req;
500 int plen = *len;
501 kdp_version_reply_t *rp = &pkt->version_reply;
502 kdp_region_t *r;
503
504 if (plen < sizeof (*rq))
505 return (FALSE);
506
507 rp->hdr.is_reply = 1;
508 rp->hdr.len = sizeof (*rp);
509
510 dprintf(("kdp_version\n"));
511
512 rp->version = KDP_VERSION;
513 #ifdef __ppc__
514 if (!(kdp_flag & KDP_BP_DIS))
515 rp->feature = KDP_FEATURE_BP;
516 else
517 rp->feature = 0;
518 #else
519 rp->feature = 0;
520 #endif
521
522 *reply_port = kdp.reply_port;
523 *len = rp->hdr.len;
524
525 return (TRUE);
526 }
527
528 static boolean_t
529 kdp_regions(
530 kdp_pkt_t *pkt,
531 int *len,
532 unsigned short *reply_port
533 )
534 {
535 kdp_regions_req_t *rq = &pkt->regions_req;
536 int plen = *len;
537 kdp_regions_reply_t *rp = &pkt->regions_reply;
538 kdp_region_t *r;
539
540 if (plen < sizeof (*rq))
541 return (FALSE);
542
543 rp->hdr.is_reply = 1;
544 rp->hdr.len = sizeof (*rp);
545
546 dprintf(("kdp_regions\n"));
547
548 r = rp->regions;
549 rp->nregions = 0;
550
551 (vm_offset_t)r->address = 0;
552 r->nbytes = 0xffffffff;
553
554 r->protection = VM_PROT_ALL; r++; rp->nregions++;
555
556 rp->hdr.len += rp->nregions * sizeof (kdp_region_t);
557
558 *reply_port = kdp.reply_port;
559 *len = rp->hdr.len;
560
561 return (TRUE);
562 }
563
564 static boolean_t
565 kdp_writeregs(
566 kdp_pkt_t *pkt,
567 int *len,
568 unsigned short *reply_port
569 )
570 {
571 kdp_writeregs_req_t *rq = &pkt->writeregs_req;
572 int plen = *len;
573 int size;
574 kdp_writeregs_reply_t *rp = &pkt->writeregs_reply;
575
576 if (plen < sizeof (*rq))
577 return (FALSE);
578
579 size = rq->hdr.len - sizeof(kdp_hdr_t) - sizeof(unsigned int);
580 rp->error = kdp_machine_write_regs(rq->cpu, rq->flavor, rq->data, &size);
581
582 rp->hdr.is_reply = 1;
583 rp->hdr.len = sizeof (*rp);
584
585 *reply_port = kdp.reply_port;
586 *len = rp->hdr.len;
587
588 return (TRUE);
589 }
590
591 static boolean_t
592 kdp_readregs(
593 kdp_pkt_t *pkt,
594 int *len,
595 unsigned short *reply_port
596 )
597 {
598 kdp_readregs_req_t *rq = &pkt->readregs_req;
599 int plen = *len;
600 kdp_readregs_reply_t *rp = &pkt->readregs_reply;
601 int size;
602
603 if (plen < sizeof (*rq))
604 return (FALSE);
605
606 rp->hdr.is_reply = 1;
607 rp->hdr.len = sizeof (*rp);
608
609 rp->error = kdp_machine_read_regs(rq->cpu, rq->flavor, rp->data, &size);
610 rp->hdr.len += size;
611
612 *reply_port = kdp.reply_port;
613 *len = rp->hdr.len;
614
615 return (TRUE);
616 }
617
618 static boolean_t
619 kdp_breakpoint_set(
620 kdp_pkt_t *pkt,
621 int *len,
622 unsigned short *reply_port
623 )
624 {
625 kdp_breakpoint_req_t *rq = &pkt->breakpoint_req;
626 kdp_breakpoint_reply_t *rp = &pkt->breakpoint_reply;
627 int plen = *len;
628 int cnt, i;
629 unsigned int old_instruction = 0;
630 unsigned int breakinstr = kdp_ml_get_breakinsn();
631
632 if(breakpoints_initialized == 0)
633 {
634 for(i=0;(i < MAX_BREAKPOINTS); breakpoint_list[i].address=0, i++);
635 breakpoints_initialized++;
636 }
637 if (plen < sizeof (*rq))
638 return (FALSE);
639 cnt = kdp_vm_read((caddr_t)rq->address, (caddr_t)(&old_instruction), sizeof(int));
640
641 if (old_instruction==breakinstr)
642 {
643 printf("A trap was already set at that address, not setting new breakpoint\n");
644 rp->error = BREAKPOINT_ALREADY_SET;
645
646 rp->hdr.is_reply = 1;
647 rp->hdr.len = sizeof (*rp);
648 *reply_port = kdp.reply_port;
649 *len = rp->hdr.len;
650
651 return (TRUE);
652 }
653
654 for(i=0;(i < MAX_BREAKPOINTS) && (breakpoint_list[i].address != 0); i++);
655
656 if (i == MAX_BREAKPOINTS)
657 {
658 rp->error = KDP_MAX_BREAKPOINTS;
659
660 rp->hdr.is_reply = 1;
661 rp->hdr.len = sizeof (*rp);
662 *reply_port = kdp.reply_port;
663 *len = rp->hdr.len;
664
665 return (TRUE);
666 }
667 breakpoint_list[i].address = rq->address;
668 breakpoint_list[i].old_instruction = old_instruction;
669
670 cnt = kdp_vm_write((caddr_t)&breakinstr, (caddr_t)rq->address, sizeof(&breakinstr));
671
672 rp->error = KDPERR_NO_ERROR;
673 rp->hdr.is_reply = 1;
674 rp->hdr.len = sizeof (*rp);
675 *reply_port = kdp.reply_port;
676 *len = rp->hdr.len;
677
678 return (TRUE);
679 }
680
681 static boolean_t
682 kdp_breakpoint_remove(
683 kdp_pkt_t *pkt,
684 int *len,
685 unsigned short *reply_port
686 )
687 {
688 kdp_breakpoint_req_t *rq = &pkt->breakpoint_req;
689 kdp_breakpoint_reply_t *rp = &pkt->breakpoint_reply;
690 int plen = *len;
691 int cnt,i;
692
693 if (plen < sizeof (*rq))
694 return (FALSE);
695
696 for(i=0;(i < MAX_BREAKPOINTS) && (breakpoint_list[i].address != rq->address); i++);
697 if (i == MAX_BREAKPOINTS)
698 {
699 rp->error = BREAKPOINT_NOT_FOUND;
700 rp->hdr.is_reply = 1;
701 rp->hdr.len = sizeof (*rp);
702 *reply_port = kdp.reply_port;
703 *len = rp->hdr.len;
704
705 return (TRUE); /* Check if it needs to be FALSE in case of error */
706 }
707
708 breakpoint_list[i].address = 0;
709 cnt = kdp_vm_write((caddr_t)&(breakpoint_list[i].old_instruction), (caddr_t)rq->address, sizeof(int));
710 rp->error = KDPERR_NO_ERROR;
711 rp->hdr.is_reply = 1;
712 rp->hdr.len = sizeof (*rp);
713 *reply_port = kdp.reply_port;
714 *len = rp->hdr.len;
715
716 return (TRUE);
717 }
718
719 boolean_t
720 kdp_remove_all_breakpoints()
721 {
722 int i;
723 boolean_t breakpoint_found = FALSE;
724
725 if (breakpoints_initialized)
726 {
727 for(i=0;i < MAX_BREAKPOINTS; i++)
728 {
729 if (breakpoint_list[i].address)
730 {
731 kdp_vm_write((caddr_t)&(breakpoint_list[i].old_instruction), (caddr_t)breakpoint_list[i].address, sizeof(int));
732 breakpoint_found = TRUE;
733 breakpoint_list[i].address = 0;
734 }
735 }
736 if (breakpoint_found)
737 printf("kdp_remove_all_breakpoints: found extant breakpoints, removing them.\n");
738 }
739 return breakpoint_found;
740 }
741
742
743 #define MAX_FRAMES 1000
744
745 static int pid_from_task(task_t task)
746 {
747 int pid = -1;
748
749 if (task->bsd_info)
750 pid = proc_pid(task->bsd_info);
751
752 return pid;
753 }
754
755 int
756 kdp_stackshot(int pid, uint32_t tracebuf, uint32_t tracebuf_size, unsigned trace_options, uint32_t *pbytesTraced)
757 {
758 uint32_t tracepos = (uint32_t) tracebuf;
759 uint32_t tracebound = tracepos + tracebuf_size;
760 uint32_t tracebytes = 0;
761 int error = 0;
762
763 processor_set_t pset = &default_pset;
764 task_t task = TASK_NULL;
765 thread_t thread = THREAD_NULL;
766 int nframes = trace_options;
767 thread_snapshot_t tsnap = NULL;
768 unsigned framesize = 2 * sizeof(vm_offset_t);
769
770 if ((nframes <= 0) || nframes > MAX_FRAMES)
771 nframes = MAX_FRAMES;
772
773 queue_iterate(&pset->tasks, task, task_t, pset_tasks) {
774 /* Trace everything, unless a process was specified */
775 if ((pid == -1) || (pid == pid_from_task(task)))
776 queue_iterate(&task->threads, thread, thread_t, task_threads){
777 if ((tracepos + 4 * sizeof(struct thread_snapshot)) > tracebound) {
778 error = -1;
779 goto error_exit;
780 }
781 /* Populate the thread snapshot header */
782 tsnap = (thread_snapshot_t) tracepos;
783 tsnap->thread_id = thread;
784 tsnap->state = thread->state;
785 tsnap->wait_queue = thread->wait_queue;
786 tsnap->wait_event = thread->wait_event;
787 tsnap->kernel_stack = thread->kernel_stack;
788 tsnap->reserved_stack = thread->reserved_stack;
789 tsnap->continuation = thread->continuation;
790 /* Add the BSD process identifiers */
791 if ((tsnap->pid = pid_from_task(task)) != -1)
792 proc_name(tsnap->pid, tsnap->p_comm, MAXCOMLEN + 1);
793 else
794 tsnap->p_comm[0] = '\0';
795
796 tsnap->snapshot_magic = 0xfeedface;
797 tracepos += sizeof(struct thread_snapshot);
798
799 /* Call through to the machine specific trace routines
800 * Frames are added past the snapshot header.
801 */
802 if (tsnap->kernel_stack != 0)
803 tracebytes = machine_trace_thread(thread, tracepos, tracebound, nframes, FALSE);
804 tsnap->nkern_frames = tracebytes/(2 * sizeof(vm_offset_t));
805 tracepos += tracebytes;
806 tracebytes = 0;
807 tsnap->user64_p = 0;
808 /* Trace user stack, if any */
809 if (thread->task->map != kernel_map) {
810 /* 64-bit task? */
811 if (task_has_64BitAddr(thread->task)) {
812 tracebytes = machine_trace_thread64(thread, tracepos, tracebound, nframes, TRUE);
813 tsnap->user64_p = 1;
814 framesize = 2 * sizeof(addr64_t);
815 }
816 else {
817 tracebytes = machine_trace_thread(thread, tracepos, tracebound, nframes, TRUE);
818 framesize = 2 * sizeof(vm_offset_t);
819 }
820 }
821 tsnap->nuser_frames = tracebytes/framesize;
822 tracepos += tracebytes;
823 tracebytes = 0;
824 }
825 }
826
827 error_exit:
828 /* Release stack snapshot wait indicator */
829 kdp_snapshot_postflight();
830
831 *pbytesTraced = tracepos - tracebuf;
832
833 return error;
834 }