2 * Copyright (c) 2000 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
,
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;
105 struct thread_snapshot
{
106 uint32_t snapshot_magic
;
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
;
116 uint32_t nuser_frames
;
118 char p_comm
[MAXCOMLEN
+ 1];
121 typedef struct thread_snapshot
*thread_snapshot_t
;
124 machine_trace_thread(thread_t thread
, uint32_t tracepos
, uint32_t tracebound
, int nframes
, boolean_t user_p
);
126 machine_trace_thread64(thread_t thread
, uint32_t tracepos
, uint32_t tracebound
, int nframes
, boolean_t user_p
);
130 proc_name(int pid
, char *buf
, int size
);
132 kdp_snapshot_postflight(void);
135 pid_from_task(task_t task
);
138 kdp_stackshot(int pid
, uint32_t tracebuf
, uint32_t tracebuf_size
, unsigned trace_options
, uint32_t *pbytesTraced
);
140 extern char version
[];
146 unsigned short *reply_port
149 static unsigned aligned_pkt
[1538/sizeof(unsigned)+1]; // max ether pkt
150 kdp_pkt_t
*rd
= (kdp_pkt_t
*)&aligned_pkt
;
156 bcopy((char *)pkt
, (char *)rd
, sizeof(aligned_pkt
));
158 rd
= (kdp_pkt_t
*)pkt
;
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
);
166 if (rd
->hdr
.is_reply
) {
167 printf("kdp_packet reply recvd req %x seq %x\n",
168 rd
->hdr
.request
, rd
->hdr
.seq
);
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
);
181 ret
= ((*dispatch_table
[req
- KDP_CONNECT
])(rd
, len
, reply_port
));
183 bcopy((char *)rd
, (char *) pkt
, *len
);
192 unsigned short *reply_port
195 kdp_pkt_t
*rd
= (kdp_pkt_t
*)pkt
;
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
);
207 unsigned short *reply_port
210 kdp_connect_req_t
*rq
= &pkt
->connect_req
;
212 kdp_connect_reply_t
*rp
= &pkt
->connect_reply
;
214 if (plen
< sizeof (*rq
))
217 dprintf(("kdp_connect seq %x greeting %s\n", rq
->hdr
.seq
, rq
->greeting
));
220 if (rq
->hdr
.seq
== kdp
.conn_seq
) /* duplicate request */
221 rp
->error
= KDPERR_NO_ERROR
;
223 rp
->error
= KDPERR_ALREADY_CONNECTED
;
226 kdp
.reply_port
= rq
->req_reply_port
;
227 kdp
.exception_port
= rq
->exc_note_port
;
229 kdp
.conn_seq
= rq
->hdr
.seq
;
231 rp
->error
= KDPERR_NO_ERROR
;
234 rp
->hdr
.is_reply
= 1;
235 rp
->hdr
.len
= sizeof (*rp
);
237 *reply_port
= kdp
.reply_port
;
240 if (current_debugger
== KDP_CUR_DB
)
250 unsigned short *reply_port
253 kdp_disconnect_req_t
*rq
= &pkt
->disconnect_req
;
255 kdp_disconnect_reply_t
*rp
= &pkt
->disconnect_reply
;
257 if (plen
< sizeof (*rq
))
263 dprintf(("kdp_disconnect\n"));
265 *reply_port
= kdp
.reply_port
;
267 kdp
.reply_port
= kdp
.exception_port
= 0;
268 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
269 kdp
.exception_seq
= kdp
.conn_seq
= 0;
271 if (noresume_on_disconnect
== 1) {
273 noresume_on_disconnect
= 0;
276 rp
->hdr
.is_reply
= 1;
277 rp
->hdr
.len
= sizeof (*rp
);
281 if (current_debugger
== KDP_CUR_DB
)
291 unsigned short *reply_port
294 kdp_reattach_req_t
*rq
= &pkt
->reattach_req
;
295 kdp_disconnect_reply_t
*rp
= &pkt
->disconnect_reply
;
298 kdp_disconnect(pkt
, len
, reply_port
);
299 *reply_port
= rq
->req_reply_port
;
308 unsigned short *reply_port
311 kdp_hostinfo_req_t
*rq
= &pkt
->hostinfo_req
;
313 kdp_hostinfo_reply_t
*rp
= &pkt
->hostinfo_reply
;
315 if (plen
< sizeof (*rq
))
318 rp
->hdr
.is_reply
= 1;
319 rp
->hdr
.len
= sizeof (*rp
);
321 kdp_machine_hostinfo(&rp
->hostinfo
);
323 *reply_port
= kdp
.reply_port
;
333 unsigned short *reply_port
336 kdp_suspend_req_t
*rq
= &pkt
->suspend_req
;
338 kdp_suspend_reply_t
*rp
= &pkt
->suspend_reply
;
340 if (plen
< sizeof (*rq
))
343 rp
->hdr
.is_reply
= 1;
344 rp
->hdr
.len
= sizeof (*rp
);
346 dprintf(("kdp_suspend\n"));
348 kdp
.is_halted
= TRUE
;
350 *reply_port
= kdp
.reply_port
;
360 unsigned short *reply_port
363 kdp_resumecpus_req_t
*rq
= &pkt
->resumecpus_req
;
365 kdp_resumecpus_reply_t
*rp
= &pkt
->resumecpus_reply
;
367 if (plen
< sizeof (*rq
))
370 rp
->hdr
.is_reply
= 1;
371 rp
->hdr
.len
= sizeof (*rp
);
373 dprintf(("kdp_resumecpus %x\n", rq
->cpu_mask
));
375 kdp
.is_halted
= FALSE
;
377 *reply_port
= kdp
.reply_port
;
387 unsigned short *reply_port
390 kdp_writemem_req_t
*rq
= &pkt
->writemem_req
;
392 kdp_writemem_reply_t
*rp
= &pkt
->writemem_reply
;
395 if (plen
< sizeof (*rq
))
398 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
399 rp
->error
= KDPERR_BAD_NBYTES
;
401 dprintf(("kdp_writemem addr %x size %d\n", rq
->address
, rq
->nbytes
));
403 cnt
= kdp_vm_write((caddr_t
)rq
->data
, (caddr_t
)rq
->address
, rq
->nbytes
);
404 rp
->error
= KDPERR_NO_ERROR
;
407 rp
->hdr
.is_reply
= 1;
408 rp
->hdr
.len
= sizeof (*rp
);
410 *reply_port
= kdp
.reply_port
;
420 unsigned short *reply_port
423 kdp_readmem_req_t
*rq
= &pkt
->readmem_req
;
425 kdp_readmem_reply_t
*rp
= &pkt
->readmem_reply
;
428 void *pversion
= &version
;
430 if (plen
< sizeof (*rq
))
433 rp
->hdr
.is_reply
= 1;
434 rp
->hdr
.len
= sizeof (*rp
);
436 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
437 rp
->error
= KDPERR_BAD_NBYTES
;
439 unsigned int n
= rq
->nbytes
;
441 dprintf(("kdp_readmem addr %x size %d\n", rq
->address
, rq
->nbytes
));
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.
450 if (rq
->address
== (void *)0x501C)
451 rq
->address
= &pversion
;
453 cnt
= kdp_vm_read((caddr_t
)rq
->address
, (caddr_t
)rp
->data
, rq
->nbytes
);
454 rp
->error
= KDPERR_NO_ERROR
;
459 *reply_port
= kdp
.reply_port
;
469 unsigned short *reply_port
472 kdp_maxbytes_req_t
*rq
= &pkt
->maxbytes_req
;
474 kdp_maxbytes_reply_t
*rp
= &pkt
->maxbytes_reply
;
476 if (plen
< sizeof (*rq
))
479 rp
->hdr
.is_reply
= 1;
480 rp
->hdr
.len
= sizeof (*rp
);
482 dprintf(("kdp_maxbytes\n"));
484 rp
->max_bytes
= MAX_KDP_DATA_SIZE
;
486 *reply_port
= kdp
.reply_port
;
496 unsigned short *reply_port
499 kdp_version_req_t
*rq
= &pkt
->version_req
;
501 kdp_version_reply_t
*rp
= &pkt
->version_reply
;
504 if (plen
< sizeof (*rq
))
507 rp
->hdr
.is_reply
= 1;
508 rp
->hdr
.len
= sizeof (*rp
);
510 dprintf(("kdp_version\n"));
512 rp
->version
= KDP_VERSION
;
514 if (!(kdp_flag
& KDP_BP_DIS
))
515 rp
->feature
= KDP_FEATURE_BP
;
522 *reply_port
= kdp
.reply_port
;
532 unsigned short *reply_port
535 kdp_regions_req_t
*rq
= &pkt
->regions_req
;
537 kdp_regions_reply_t
*rp
= &pkt
->regions_reply
;
540 if (plen
< sizeof (*rq
))
543 rp
->hdr
.is_reply
= 1;
544 rp
->hdr
.len
= sizeof (*rp
);
546 dprintf(("kdp_regions\n"));
551 (vm_offset_t
)r
->address
= 0;
552 r
->nbytes
= 0xffffffff;
554 r
->protection
= VM_PROT_ALL
; r
++; rp
->nregions
++;
556 rp
->hdr
.len
+= rp
->nregions
* sizeof (kdp_region_t
);
558 *reply_port
= kdp
.reply_port
;
568 unsigned short *reply_port
571 kdp_writeregs_req_t
*rq
= &pkt
->writeregs_req
;
574 kdp_writeregs_reply_t
*rp
= &pkt
->writeregs_reply
;
576 if (plen
< sizeof (*rq
))
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
);
582 rp
->hdr
.is_reply
= 1;
583 rp
->hdr
.len
= sizeof (*rp
);
585 *reply_port
= kdp
.reply_port
;
595 unsigned short *reply_port
598 kdp_readregs_req_t
*rq
= &pkt
->readregs_req
;
600 kdp_readregs_reply_t
*rp
= &pkt
->readregs_reply
;
603 if (plen
< sizeof (*rq
))
606 rp
->hdr
.is_reply
= 1;
607 rp
->hdr
.len
= sizeof (*rp
);
609 rp
->error
= kdp_machine_read_regs(rq
->cpu
, rq
->flavor
, rp
->data
, &size
);
612 *reply_port
= kdp
.reply_port
;
622 unsigned short *reply_port
625 kdp_breakpoint_req_t
*rq
= &pkt
->breakpoint_req
;
626 kdp_breakpoint_reply_t
*rp
= &pkt
->breakpoint_reply
;
629 unsigned int old_instruction
= 0;
630 unsigned int breakinstr
= kdp_ml_get_breakinsn();
632 if(breakpoints_initialized
== 0)
634 for(i
=0;(i
< MAX_BREAKPOINTS
); breakpoint_list
[i
].address
=0, i
++);
635 breakpoints_initialized
++;
637 if (plen
< sizeof (*rq
))
639 cnt
= kdp_vm_read((caddr_t
)rq
->address
, (caddr_t
)(&old_instruction
), sizeof(int));
641 if (old_instruction
==breakinstr
)
643 printf("A trap was already set at that address, not setting new breakpoint\n");
644 rp
->error
= BREAKPOINT_ALREADY_SET
;
646 rp
->hdr
.is_reply
= 1;
647 rp
->hdr
.len
= sizeof (*rp
);
648 *reply_port
= kdp
.reply_port
;
654 for(i
=0;(i
< MAX_BREAKPOINTS
) && (breakpoint_list
[i
].address
!= 0); i
++);
656 if (i
== MAX_BREAKPOINTS
)
658 rp
->error
= KDP_MAX_BREAKPOINTS
;
660 rp
->hdr
.is_reply
= 1;
661 rp
->hdr
.len
= sizeof (*rp
);
662 *reply_port
= kdp
.reply_port
;
667 breakpoint_list
[i
].address
= rq
->address
;
668 breakpoint_list
[i
].old_instruction
= old_instruction
;
670 cnt
= kdp_vm_write((caddr_t
)&breakinstr
, (caddr_t
)rq
->address
, sizeof(&breakinstr
));
672 rp
->error
= KDPERR_NO_ERROR
;
673 rp
->hdr
.is_reply
= 1;
674 rp
->hdr
.len
= sizeof (*rp
);
675 *reply_port
= kdp
.reply_port
;
682 kdp_breakpoint_remove(
685 unsigned short *reply_port
688 kdp_breakpoint_req_t
*rq
= &pkt
->breakpoint_req
;
689 kdp_breakpoint_reply_t
*rp
= &pkt
->breakpoint_reply
;
693 if (plen
< sizeof (*rq
))
696 for(i
=0;(i
< MAX_BREAKPOINTS
) && (breakpoint_list
[i
].address
!= rq
->address
); i
++);
697 if (i
== MAX_BREAKPOINTS
)
699 rp
->error
= BREAKPOINT_NOT_FOUND
;
700 rp
->hdr
.is_reply
= 1;
701 rp
->hdr
.len
= sizeof (*rp
);
702 *reply_port
= kdp
.reply_port
;
705 return (TRUE
); /* Check if it needs to be FALSE in case of error */
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
;
720 kdp_remove_all_breakpoints()
723 boolean_t breakpoint_found
= FALSE
;
725 if (breakpoints_initialized
)
727 for(i
=0;i
< MAX_BREAKPOINTS
; i
++)
729 if (breakpoint_list
[i
].address
)
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;
736 if (breakpoint_found
)
737 printf("kdp_remove_all_breakpoints: found extant breakpoints, removing them.\n");
739 return breakpoint_found
;
743 #define MAX_FRAMES 1000
745 static int pid_from_task(task_t task
)
750 pid
= proc_pid(task
->bsd_info
);
756 kdp_stackshot(int pid
, uint32_t tracebuf
, uint32_t tracebuf_size
, unsigned trace_options
, uint32_t *pbytesTraced
)
758 uint32_t tracepos
= (uint32_t) tracebuf
;
759 uint32_t tracebound
= tracepos
+ tracebuf_size
;
760 uint32_t tracebytes
= 0;
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
);
770 if ((nframes
<= 0) || nframes
> MAX_FRAMES
)
771 nframes
= MAX_FRAMES
;
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
) {
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);
794 tsnap
->p_comm
[0] = '\0';
796 tsnap
->snapshot_magic
= 0xfeedface;
797 tracepos
+= sizeof(struct thread_snapshot
);
799 /* Call through to the machine specific trace routines
800 * Frames are added past the snapshot header.
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
;
808 /* Trace user stack, if any */
809 if (thread
->task
->map
!= kernel_map
) {
811 if (task_has_64BitAddr(thread
->task
)) {
812 tracebytes
= machine_trace_thread64(thread
, tracepos
, tracebound
, nframes
, TRUE
);
814 framesize
= 2 * sizeof(addr64_t
);
817 tracebytes
= machine_trace_thread(thread
, tracepos
, tracebound
, nframes
, TRUE
);
818 framesize
= 2 * sizeof(vm_offset_t
);
821 tsnap
->nuser_frames
= tracebytes
/framesize
;
822 tracepos
+= tracebytes
;
828 /* Release stack snapshot wait indicator */
829 kdp_snapshot_postflight();
831 *pbytesTraced
= tracepos
- tracebuf
;