2 * Copyright (c) 2000-2007 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/mach_types.h>
30 #include <kern/debug.h>
32 #include <kdp/kdp_internal.h>
33 #include <kdp/kdp_private.h>
35 #include <libsa/types.h>
37 #include <string.h> /* bcopy */
39 #include <kern/processor.h>
40 #include <kern/thread.h>
41 #include <vm/vm_map.h>
42 #include <vm/vm_kern.h>
44 int kdp_vm_read( caddr_t
, caddr_t
, unsigned int);
45 int kdp_vm_write( caddr_t
, caddr_t
, unsigned int);
47 #define DO_ALIGN 1 /* align all packet data accesses */
49 #define KDP_TEST_HARNESS 0
51 #define dprintf(x) kprintf x
57 dispatch_table
[KDP_HOSTREBOOT
- KDP_CONNECT
+1] =
60 /* 1 */ kdp_disconnect
,
67 /* 8 */ kdp_writeregs
,
71 /* C */ kdp_resumecpus
,
74 /* F */ kdp_breakpoint_set
,
75 /*10 */ kdp_breakpoint_remove
,
78 /*13 */ (kdp_dispatch_t
)kdp_reboot
84 #define MAX_BREAKPOINTS 100
85 #define KDP_MAX_BREAKPOINTS 100
87 #define BREAKPOINT_NOT_FOUND 101
88 #define BREAKPOINT_ALREADY_SET 102
90 #define KDP_VERSION 10
94 unsigned int old_instruction
;
95 } kdp_breakpoint_record_t
;
97 static kdp_breakpoint_record_t breakpoint_list
[MAX_BREAKPOINTS
];
98 static unsigned int breakpoints_initialized
= 0;
100 int reattach_wait
= 0;
101 int noresume_on_disconnect
= 0;
102 extern unsigned int return_on_panic
;
106 struct thread_snapshot
{
107 uint32_t snapshot_magic
;
110 wait_queue_t wait_queue
;
111 event64_t wait_event
;
112 vm_offset_t kernel_stack
;
113 vm_offset_t reserved_stack
;
114 thread_continue_t continuation
;
115 uint32_t nkern_frames
;
117 uint32_t nuser_frames
;
119 char p_comm
[MAXCOMLEN
+ 1];
122 typedef struct thread_snapshot
*thread_snapshot_t
;
125 machine_trace_thread(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
);
127 machine_trace_thread64(thread_t thread
, char *tracepos
, char *tracebound
, int nframes
, boolean_t user_p
);
131 proc_name_kdp(task_t task
, char *buf
, int size
);
134 kdp_snapshot_postflight(void);
137 pid_from_task(task_t task
);
140 kdp_stackshot(int pid
, void *tracebuf
, uint32_t tracebuf_size
, unsigned trace_options
, uint32_t *pbytesTraced
);
142 extern char version
[];
148 unsigned short *reply_port
151 static unsigned aligned_pkt
[1538/sizeof(unsigned)+1]; // max ether pkt
152 kdp_pkt_t
*rd
= (kdp_pkt_t
*)&aligned_pkt
;
158 bcopy((char *)pkt
, (char *)rd
, sizeof(aligned_pkt
));
160 rd
= (kdp_pkt_t
*)pkt
;
162 if (plen
< sizeof (rd
->hdr
) || rd
->hdr
.len
!= plen
) {
163 printf("kdp_packet bad len pkt %lu hdr %d\n", plen
, rd
->hdr
.len
);
168 if (rd
->hdr
.is_reply
) {
169 printf("kdp_packet reply recvd req %x seq %x\n",
170 rd
->hdr
.request
, rd
->hdr
.seq
);
175 req
= rd
->hdr
.request
;
176 if (req
> KDP_HOSTREBOOT
) {
177 printf("kdp_packet bad request %x len %d seq %x key %x\n",
178 rd
->hdr
.request
, rd
->hdr
.len
, rd
->hdr
.seq
, rd
->hdr
.key
);
183 ret
= ((*dispatch_table
[req
- KDP_CONNECT
])(rd
, len
, reply_port
));
185 bcopy((char *)rd
, (char *) pkt
, *len
);
194 __unused
unsigned short *reply_port
197 kdp_pkt_t
*rd
= (kdp_pkt_t
*)pkt
;
199 printf("kdp_unknown request %x len %d seq %x key %x\n",
200 rd
->hdr
.request
, rd
->hdr
.len
, rd
->hdr
.seq
, rd
->hdr
.key
);
209 unsigned short *reply_port
212 kdp_connect_req_t
*rq
= &pkt
->connect_req
;
214 kdp_connect_reply_t
*rp
= &pkt
->connect_reply
;
216 if (plen
< sizeof (*rq
))
219 dprintf(("kdp_connect seq %x greeting %s\n", rq
->hdr
.seq
, rq
->greeting
));
222 if (rq
->hdr
.seq
== kdp
.conn_seq
) /* duplicate request */
223 rp
->error
= KDPERR_NO_ERROR
;
225 rp
->error
= KDPERR_ALREADY_CONNECTED
;
228 kdp
.reply_port
= rq
->req_reply_port
;
229 kdp
.exception_port
= rq
->exc_note_port
;
231 kdp
.conn_seq
= rq
->hdr
.seq
;
233 rp
->error
= KDPERR_NO_ERROR
;
236 rp
->hdr
.is_reply
= 1;
237 rp
->hdr
.len
= sizeof (*rp
);
239 *reply_port
= kdp
.reply_port
;
242 if (current_debugger
== KDP_CUR_DB
)
252 unsigned short *reply_port
255 kdp_disconnect_req_t
*rq
= &pkt
->disconnect_req
;
257 kdp_disconnect_reply_t
*rp
= &pkt
->disconnect_reply
;
259 if (plen
< sizeof (*rq
))
265 dprintf(("kdp_disconnect\n"));
267 *reply_port
= kdp
.reply_port
;
269 kdp
.reply_port
= kdp
.exception_port
= 0;
270 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
271 kdp
.exception_seq
= kdp
.conn_seq
= 0;
273 if ((panicstr
!= NULL
) && (return_on_panic
== 0))
276 if (noresume_on_disconnect
== 1) {
278 noresume_on_disconnect
= 0;
281 rp
->hdr
.is_reply
= 1;
282 rp
->hdr
.len
= sizeof (*rp
);
286 if (current_debugger
== KDP_CUR_DB
)
296 unsigned short *reply_port
299 kdp_reattach_req_t
*rq
= &pkt
->reattach_req
;
302 kdp_disconnect(pkt
, len
, reply_port
);
303 *reply_port
= rq
->req_reply_port
;
312 unsigned short *reply_port
315 kdp_hostinfo_req_t
*rq
= &pkt
->hostinfo_req
;
317 kdp_hostinfo_reply_t
*rp
= &pkt
->hostinfo_reply
;
319 if (plen
< sizeof (*rq
))
322 rp
->hdr
.is_reply
= 1;
323 rp
->hdr
.len
= sizeof (*rp
);
325 kdp_machine_hostinfo(&rp
->hostinfo
);
327 *reply_port
= kdp
.reply_port
;
337 unsigned short *reply_port
340 kdp_suspend_req_t
*rq
= &pkt
->suspend_req
;
342 kdp_suspend_reply_t
*rp
= &pkt
->suspend_reply
;
344 if (plen
< sizeof (*rq
))
347 rp
->hdr
.is_reply
= 1;
348 rp
->hdr
.len
= sizeof (*rp
);
350 dprintf(("kdp_suspend\n"));
352 kdp
.is_halted
= TRUE
;
354 *reply_port
= kdp
.reply_port
;
364 unsigned short *reply_port
367 kdp_resumecpus_req_t
*rq
= &pkt
->resumecpus_req
;
369 kdp_resumecpus_reply_t
*rp
= &pkt
->resumecpus_reply
;
371 if (plen
< sizeof (*rq
))
374 rp
->hdr
.is_reply
= 1;
375 rp
->hdr
.len
= sizeof (*rp
);
377 dprintf(("kdp_resumecpus %x\n", rq
->cpu_mask
));
379 kdp
.is_halted
= FALSE
;
381 *reply_port
= kdp
.reply_port
;
391 unsigned short *reply_port
394 kdp_writemem_req_t
*rq
= &pkt
->writemem_req
;
396 kdp_writemem_reply_t
*rp
= &pkt
->writemem_reply
;
399 if (plen
< sizeof (*rq
))
402 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
403 rp
->error
= KDPERR_BAD_NBYTES
;
405 dprintf(("kdp_writemem addr %x size %d\n", rq
->address
, rq
->nbytes
));
407 cnt
= kdp_vm_write((caddr_t
)rq
->data
, (caddr_t
)rq
->address
, rq
->nbytes
);
408 rp
->error
= KDPERR_NO_ERROR
;
411 rp
->hdr
.is_reply
= 1;
412 rp
->hdr
.len
= sizeof (*rp
);
414 *reply_port
= kdp
.reply_port
;
424 unsigned short *reply_port
427 kdp_readmem_req_t
*rq
= &pkt
->readmem_req
;
429 kdp_readmem_reply_t
*rp
= &pkt
->readmem_reply
;
431 #if __i386__ || __arm__
432 void *pversion
= &version
;
434 if (plen
< sizeof (*rq
))
437 rp
->hdr
.is_reply
= 1;
438 rp
->hdr
.len
= sizeof (*rp
);
440 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
441 rp
->error
= KDPERR_BAD_NBYTES
;
443 unsigned int n
= rq
->nbytes
;
445 dprintf(("kdp_readmem addr %x size %d\n", rq
->address
, n
));
446 #if __i386__ || __arm__
447 /* XXX This is a hack to facilitate the "showversion" macro
448 * on i386/ARM, which is used to obtain the kernel version without
449 * symbols - a pointer to the version string should eventually
450 * be pinned at a fixed address when an equivalent of the
451 * VECTORS segment (loaded at a fixed load address, and contains
452 * a table) is implemented on these architectures, as with PPC.
453 * N.B.: x86 now has a low global page, and the version indirection
454 * is pinned at 0x201C. We retain the 0x501C address override
457 if (rq
->address
== (void *)0x501C)
458 rq
->address
= &pversion
;
460 cnt
= kdp_vm_read((caddr_t
)rq
->address
, (caddr_t
)rp
->data
, n
);
461 rp
->error
= KDPERR_NO_ERROR
;
466 *reply_port
= kdp
.reply_port
;
476 unsigned short *reply_port
479 kdp_maxbytes_req_t
*rq
= &pkt
->maxbytes_req
;
481 kdp_maxbytes_reply_t
*rp
= &pkt
->maxbytes_reply
;
483 if (plen
< sizeof (*rq
))
486 rp
->hdr
.is_reply
= 1;
487 rp
->hdr
.len
= sizeof (*rp
);
489 dprintf(("kdp_maxbytes\n"));
491 rp
->max_bytes
= MAX_KDP_DATA_SIZE
;
493 *reply_port
= kdp
.reply_port
;
503 unsigned short *reply_port
506 kdp_version_req_t
*rq
= &pkt
->version_req
;
508 kdp_version_reply_t
*rp
= &pkt
->version_reply
;
510 if (plen
< sizeof (*rq
))
513 rp
->hdr
.is_reply
= 1;
514 rp
->hdr
.len
= sizeof (*rp
);
516 dprintf(("kdp_version\n"));
518 rp
->version
= KDP_VERSION
;
520 if (!(kdp_flag
& KDP_BP_DIS
))
521 rp
->feature
= KDP_FEATURE_BP
;
528 *reply_port
= kdp
.reply_port
;
538 unsigned short *reply_port
541 kdp_regions_req_t
*rq
= &pkt
->regions_req
;
543 kdp_regions_reply_t
*rp
= &pkt
->regions_reply
;
546 if (plen
< sizeof (*rq
))
549 rp
->hdr
.is_reply
= 1;
550 rp
->hdr
.len
= sizeof (*rp
);
552 dprintf(("kdp_regions\n"));
558 r
->nbytes
= 0xffffffff;
560 r
->protection
= VM_PROT_ALL
; r
++; rp
->nregions
++;
562 rp
->hdr
.len
+= rp
->nregions
* sizeof (kdp_region_t
);
564 *reply_port
= kdp
.reply_port
;
574 unsigned short *reply_port
577 kdp_writeregs_req_t
*rq
= &pkt
->writeregs_req
;
580 kdp_writeregs_reply_t
*rp
= &pkt
->writeregs_reply
;
582 if (plen
< sizeof (*rq
))
585 size
= rq
->hdr
.len
- sizeof(kdp_hdr_t
) - sizeof(unsigned int);
586 rp
->error
= kdp_machine_write_regs(rq
->cpu
, rq
->flavor
, rq
->data
, &size
);
588 rp
->hdr
.is_reply
= 1;
589 rp
->hdr
.len
= sizeof (*rp
);
591 *reply_port
= kdp
.reply_port
;
601 unsigned short *reply_port
604 kdp_readregs_req_t
*rq
= &pkt
->readregs_req
;
606 kdp_readregs_reply_t
*rp
= &pkt
->readregs_reply
;
609 if (plen
< sizeof (*rq
))
612 rp
->hdr
.is_reply
= 1;
613 rp
->hdr
.len
= sizeof (*rp
);
615 rp
->error
= kdp_machine_read_regs(rq
->cpu
, rq
->flavor
, rp
->data
, &size
);
618 *reply_port
= kdp
.reply_port
;
628 unsigned short *reply_port
631 kdp_breakpoint_req_t
*rq
= &pkt
->breakpoint_req
;
632 kdp_breakpoint_reply_t
*rp
= &pkt
->breakpoint_reply
;
635 unsigned int old_instruction
= 0;
636 unsigned int breakinstr
= kdp_ml_get_breakinsn();
638 if(breakpoints_initialized
== 0)
640 for(i
=0;(i
< MAX_BREAKPOINTS
); breakpoint_list
[i
].address
=0, i
++);
641 breakpoints_initialized
++;
643 if (plen
< sizeof (*rq
))
645 cnt
= kdp_vm_read((caddr_t
)rq
->address
, (caddr_t
)(&old_instruction
), sizeof(int));
647 if (old_instruction
==breakinstr
)
649 printf("A trap was already set at that address, not setting new breakpoint\n");
650 rp
->error
= BREAKPOINT_ALREADY_SET
;
652 rp
->hdr
.is_reply
= 1;
653 rp
->hdr
.len
= sizeof (*rp
);
654 *reply_port
= kdp
.reply_port
;
660 for(i
=0;(i
< MAX_BREAKPOINTS
) && (breakpoint_list
[i
].address
!= 0); i
++);
662 if (i
== MAX_BREAKPOINTS
)
664 rp
->error
= KDP_MAX_BREAKPOINTS
;
666 rp
->hdr
.is_reply
= 1;
667 rp
->hdr
.len
= sizeof (*rp
);
668 *reply_port
= kdp
.reply_port
;
673 breakpoint_list
[i
].address
= rq
->address
;
674 breakpoint_list
[i
].old_instruction
= old_instruction
;
676 cnt
= kdp_vm_write((caddr_t
)&breakinstr
, (caddr_t
)rq
->address
, sizeof(&breakinstr
));
678 rp
->error
= KDPERR_NO_ERROR
;
679 rp
->hdr
.is_reply
= 1;
680 rp
->hdr
.len
= sizeof (*rp
);
681 *reply_port
= kdp
.reply_port
;
688 kdp_breakpoint_remove(
691 unsigned short *reply_port
694 kdp_breakpoint_req_t
*rq
= &pkt
->breakpoint_req
;
695 kdp_breakpoint_reply_t
*rp
= &pkt
->breakpoint_reply
;
699 if (plen
< sizeof (*rq
))
702 for(i
=0;(i
< MAX_BREAKPOINTS
) && (breakpoint_list
[i
].address
!= rq
->address
); i
++);
703 if (i
== MAX_BREAKPOINTS
)
705 rp
->error
= BREAKPOINT_NOT_FOUND
;
706 rp
->hdr
.is_reply
= 1;
707 rp
->hdr
.len
= sizeof (*rp
);
708 *reply_port
= kdp
.reply_port
;
711 return (TRUE
); /* Check if it needs to be FALSE in case of error */
714 breakpoint_list
[i
].address
= 0;
715 cnt
= kdp_vm_write((caddr_t
)&(breakpoint_list
[i
].old_instruction
), (caddr_t
)rq
->address
, sizeof(int));
716 rp
->error
= KDPERR_NO_ERROR
;
717 rp
->hdr
.is_reply
= 1;
718 rp
->hdr
.len
= sizeof (*rp
);
719 *reply_port
= kdp
.reply_port
;
726 kdp_remove_all_breakpoints(void)
729 boolean_t breakpoint_found
= FALSE
;
731 if (breakpoints_initialized
)
733 for(i
=0;i
< MAX_BREAKPOINTS
; i
++)
735 if (breakpoint_list
[i
].address
)
737 kdp_vm_write((caddr_t
)&(breakpoint_list
[i
].old_instruction
), (caddr_t
)breakpoint_list
[i
].address
, sizeof(int));
738 breakpoint_found
= TRUE
;
739 breakpoint_list
[i
].address
= 0;
742 if (breakpoint_found
)
743 printf("kdp_remove_all_breakpoints: found extant breakpoints, removing them.\n");
745 return breakpoint_found
;
749 #define MAX_FRAMES 1000
751 static int pid_from_task(task_t task
)
756 pid
= proc_pid(task
->bsd_info
);
762 kdp_stackshot(int pid
, void *tracebuf
, uint32_t tracebuf_size
, unsigned trace_options
, uint32_t *pbytesTraced
)
764 char *tracepos
= (char *) tracebuf
;
765 char *tracebound
= tracepos
+ tracebuf_size
;
766 uint32_t tracebytes
= 0;
769 task_t task
= TASK_NULL
;
770 thread_t thread
= THREAD_NULL
;
771 int nframes
= trace_options
;
772 thread_snapshot_t tsnap
= NULL
;
773 unsigned framesize
= 2 * sizeof(vm_offset_t
);
775 struct thread cthread
;
777 if ((nframes
<= 0) || nframes
> MAX_FRAMES
)
778 nframes
= MAX_FRAMES
;
780 queue_iterate(&tasks
, task
, task_t
, tasks
) {
781 if ((task
== NULL
) || (ml_nofault_copy((vm_offset_t
) task
, (vm_offset_t
) &ctask
, sizeof(struct task
)) != sizeof(struct task
)))
783 /* Trace everything, unless a process was specified */
784 if ((pid
== -1) || (pid
== pid_from_task(task
)))
785 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
){
786 if ((thread
== NULL
) || (ml_nofault_copy((vm_offset_t
) thread
, (vm_offset_t
) &cthread
, sizeof(struct thread
)) != sizeof(struct thread
)))
788 if (((tracepos
+ 4 * sizeof(struct thread_snapshot
)) > tracebound
)) {
792 /* Populate the thread snapshot header */
793 tsnap
= (thread_snapshot_t
) tracepos
;
794 tsnap
->thread_id
= thread
;
795 tsnap
->state
= thread
->state
;
796 tsnap
->wait_queue
= thread
->wait_queue
;
797 tsnap
->wait_event
= thread
->wait_event
;
798 tsnap
->kernel_stack
= thread
->kernel_stack
;
799 tsnap
->reserved_stack
= thread
->reserved_stack
;
800 tsnap
->continuation
= thread
->continuation
;
801 /* Add the BSD process identifiers */
802 if ((tsnap
->pid
= pid_from_task(task
)) != -1)
803 proc_name_kdp(task
, tsnap
->p_comm
, MAXCOMLEN
+ 1);
805 tsnap
->p_comm
[0] = '\0';
807 tsnap
->snapshot_magic
= 0xfeedface;
808 tracepos
+= sizeof(struct thread_snapshot
);
810 /* Call through to the machine specific trace routines
811 * Frames are added past the snapshot header.
813 if (tsnap
->kernel_stack
!= 0)
814 tracebytes
= machine_trace_thread(thread
, tracepos
, tracebound
, nframes
, FALSE
);
815 tsnap
->nkern_frames
= tracebytes
/(2 * sizeof(vm_offset_t
));
816 tracepos
+= tracebytes
;
819 /* Trace user stack, if any */
820 if (thread
->task
->map
!= kernel_map
) {
822 if (task_has_64BitAddr(thread
->task
)) {
823 tracebytes
= machine_trace_thread64(thread
, tracepos
, tracebound
, nframes
, TRUE
);
825 framesize
= 2 * sizeof(addr64_t
);
828 tracebytes
= machine_trace_thread(thread
, tracepos
, tracebound
, nframes
, TRUE
);
829 framesize
= 2 * sizeof(vm_offset_t
);
832 tsnap
->nuser_frames
= tracebytes
/framesize
;
833 tracepos
+= tracebytes
;
839 /* Release stack snapshot wait indicator */
840 kdp_snapshot_postflight();
842 *pbytesTraced
= tracepos
- (char *) tracebuf
;