2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <mach/mach_types.h>
32 #include <kern/debug.h>
34 #include <kdp/kdp_internal.h>
35 #include <kdp/kdp_private.h>
37 #include <libsa/types.h>
39 #include <string.h> /* bcopy */
41 #include <kern/processor.h>
42 #include <kern/thread.h>
43 #include <vm/vm_map.h>
44 #include <vm/vm_kern.h>
46 int kdp_vm_read( caddr_t
, caddr_t
, unsigned int);
47 int kdp_vm_write( caddr_t
, caddr_t
, unsigned int);
49 #define DO_ALIGN 1 /* align all packet data accesses */
51 #define KDP_TEST_HARNESS 0
53 #define dprintf(x) kprintf x
59 dispatch_table
[KDP_HOSTREBOOT
- KDP_CONNECT
+1] =
62 /* 1 */ kdp_disconnect
,
69 /* 8 */ kdp_writeregs
,
73 /* C */ kdp_resumecpus
,
76 /* F */ kdp_breakpoint_set
,
77 /*10 */ kdp_breakpoint_remove
,
86 #define MAX_BREAKPOINTS 100
87 #define KDP_MAX_BREAKPOINTS 100
89 #define BREAKPOINT_NOT_FOUND 101
90 #define BREAKPOINT_ALREADY_SET 102
92 #define KDP_VERSION 10
96 unsigned int old_instruction
;
97 } kdp_breakpoint_record_t
;
99 static kdp_breakpoint_record_t breakpoint_list
[MAX_BREAKPOINTS
];
100 static unsigned int breakpoints_initialized
= 0;
102 int reattach_wait
= 0;
103 int noresume_on_disconnect
= 0;
107 struct thread_snapshot
{
108 uint32_t snapshot_magic
;
111 wait_queue_t wait_queue
;
112 event64_t wait_event
;
113 vm_offset_t kernel_stack
;
114 vm_offset_t reserved_stack
;
115 thread_continue_t continuation
;
116 uint32_t nkern_frames
;
118 uint32_t nuser_frames
;
120 char p_comm
[MAXCOMLEN
+ 1];
123 typedef struct thread_snapshot
*thread_snapshot_t
;
126 machine_trace_thread(thread_t thread
, uint32_t tracepos
, uint32_t tracebound
, int nframes
, boolean_t user_p
);
128 machine_trace_thread64(thread_t thread
, uint32_t tracepos
, uint32_t tracebound
, int nframes
, boolean_t user_p
);
132 proc_name(int pid
, char *buf
, int size
);
134 kdp_snapshot_postflight(void);
137 pid_from_task(task_t task
);
140 kdp_stackshot(int pid
, uint32_t 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 %d 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_CONNECT
) || (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 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 (noresume_on_disconnect
== 1) {
275 noresume_on_disconnect
= 0;
278 rp
->hdr
.is_reply
= 1;
279 rp
->hdr
.len
= sizeof (*rp
);
283 if (current_debugger
== KDP_CUR_DB
)
293 unsigned short *reply_port
296 kdp_reattach_req_t
*rq
= &pkt
->reattach_req
;
297 kdp_disconnect_reply_t
*rp
= &pkt
->disconnect_reply
;
300 kdp_disconnect(pkt
, len
, reply_port
);
301 *reply_port
= rq
->req_reply_port
;
310 unsigned short *reply_port
313 kdp_hostinfo_req_t
*rq
= &pkt
->hostinfo_req
;
315 kdp_hostinfo_reply_t
*rp
= &pkt
->hostinfo_reply
;
317 if (plen
< sizeof (*rq
))
320 rp
->hdr
.is_reply
= 1;
321 rp
->hdr
.len
= sizeof (*rp
);
323 kdp_machine_hostinfo(&rp
->hostinfo
);
325 *reply_port
= kdp
.reply_port
;
335 unsigned short *reply_port
338 kdp_suspend_req_t
*rq
= &pkt
->suspend_req
;
340 kdp_suspend_reply_t
*rp
= &pkt
->suspend_reply
;
342 if (plen
< sizeof (*rq
))
345 rp
->hdr
.is_reply
= 1;
346 rp
->hdr
.len
= sizeof (*rp
);
348 dprintf(("kdp_suspend\n"));
350 kdp
.is_halted
= TRUE
;
352 *reply_port
= kdp
.reply_port
;
362 unsigned short *reply_port
365 kdp_resumecpus_req_t
*rq
= &pkt
->resumecpus_req
;
367 kdp_resumecpus_reply_t
*rp
= &pkt
->resumecpus_reply
;
369 if (plen
< sizeof (*rq
))
372 rp
->hdr
.is_reply
= 1;
373 rp
->hdr
.len
= sizeof (*rp
);
375 dprintf(("kdp_resumecpus %x\n", rq
->cpu_mask
));
377 kdp
.is_halted
= FALSE
;
379 *reply_port
= kdp
.reply_port
;
389 unsigned short *reply_port
392 kdp_writemem_req_t
*rq
= &pkt
->writemem_req
;
394 kdp_writemem_reply_t
*rp
= &pkt
->writemem_reply
;
397 if (plen
< sizeof (*rq
))
400 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
401 rp
->error
= KDPERR_BAD_NBYTES
;
403 dprintf(("kdp_writemem addr %x size %d\n", rq
->address
, rq
->nbytes
));
405 cnt
= kdp_vm_write((caddr_t
)rq
->data
, (caddr_t
)rq
->address
, rq
->nbytes
);
406 rp
->error
= KDPERR_NO_ERROR
;
409 rp
->hdr
.is_reply
= 1;
410 rp
->hdr
.len
= sizeof (*rp
);
412 *reply_port
= kdp
.reply_port
;
422 unsigned short *reply_port
425 kdp_readmem_req_t
*rq
= &pkt
->readmem_req
;
427 kdp_readmem_reply_t
*rp
= &pkt
->readmem_reply
;
430 void *pversion
= &version
;
432 if (plen
< sizeof (*rq
))
435 rp
->hdr
.is_reply
= 1;
436 rp
->hdr
.len
= sizeof (*rp
);
438 if (rq
->nbytes
> MAX_KDP_DATA_SIZE
)
439 rp
->error
= KDPERR_BAD_NBYTES
;
441 unsigned int n
= rq
->nbytes
;
443 dprintf(("kdp_readmem addr %x size %d\n", rq
->address
, rq
->nbytes
));
445 /* XXX This is a hack to facilitate the "showversion" macro
446 * on i386, which is used to obtain the kernel version without
447 * symbols - a pointer to the version string should eventually
448 * be pinned at a fixed address when an equivalent of the
449 * VECTORS segment (loaded at a fixed load address, and contains
450 * a table) is implemented on x86, as with PPC.
452 if (rq
->address
== (void *)0x501C)
453 rq
->address
= &pversion
;
455 cnt
= kdp_vm_read((caddr_t
)rq
->address
, (caddr_t
)rp
->data
, rq
->nbytes
);
456 rp
->error
= KDPERR_NO_ERROR
;
461 *reply_port
= kdp
.reply_port
;
471 unsigned short *reply_port
474 kdp_maxbytes_req_t
*rq
= &pkt
->maxbytes_req
;
476 kdp_maxbytes_reply_t
*rp
= &pkt
->maxbytes_reply
;
478 if (plen
< sizeof (*rq
))
481 rp
->hdr
.is_reply
= 1;
482 rp
->hdr
.len
= sizeof (*rp
);
484 dprintf(("kdp_maxbytes\n"));
486 rp
->max_bytes
= MAX_KDP_DATA_SIZE
;
488 *reply_port
= kdp
.reply_port
;
498 unsigned short *reply_port
501 kdp_version_req_t
*rq
= &pkt
->version_req
;
503 kdp_version_reply_t
*rp
= &pkt
->version_reply
;
506 if (plen
< sizeof (*rq
))
509 rp
->hdr
.is_reply
= 1;
510 rp
->hdr
.len
= sizeof (*rp
);
512 dprintf(("kdp_version\n"));
514 rp
->version
= KDP_VERSION
;
516 if (!(kdp_flag
& KDP_BP_DIS
))
517 rp
->feature
= KDP_FEATURE_BP
;
524 *reply_port
= kdp
.reply_port
;
534 unsigned short *reply_port
537 kdp_regions_req_t
*rq
= &pkt
->regions_req
;
539 kdp_regions_reply_t
*rp
= &pkt
->regions_reply
;
542 if (plen
< sizeof (*rq
))
545 rp
->hdr
.is_reply
= 1;
546 rp
->hdr
.len
= sizeof (*rp
);
548 dprintf(("kdp_regions\n"));
553 (vm_offset_t
)r
->address
= 0;
554 r
->nbytes
= 0xffffffff;
556 r
->protection
= VM_PROT_ALL
; r
++; rp
->nregions
++;
558 rp
->hdr
.len
+= rp
->nregions
* sizeof (kdp_region_t
);
560 *reply_port
= kdp
.reply_port
;
570 unsigned short *reply_port
573 kdp_writeregs_req_t
*rq
= &pkt
->writeregs_req
;
576 kdp_writeregs_reply_t
*rp
= &pkt
->writeregs_reply
;
578 if (plen
< sizeof (*rq
))
581 size
= rq
->hdr
.len
- sizeof(kdp_hdr_t
) - sizeof(unsigned int);
582 rp
->error
= kdp_machine_write_regs(rq
->cpu
, rq
->flavor
, rq
->data
, &size
);
584 rp
->hdr
.is_reply
= 1;
585 rp
->hdr
.len
= sizeof (*rp
);
587 *reply_port
= kdp
.reply_port
;
597 unsigned short *reply_port
600 kdp_readregs_req_t
*rq
= &pkt
->readregs_req
;
602 kdp_readregs_reply_t
*rp
= &pkt
->readregs_reply
;
605 if (plen
< sizeof (*rq
))
608 rp
->hdr
.is_reply
= 1;
609 rp
->hdr
.len
= sizeof (*rp
);
611 rp
->error
= kdp_machine_read_regs(rq
->cpu
, rq
->flavor
, rp
->data
, &size
);
614 *reply_port
= kdp
.reply_port
;
624 unsigned short *reply_port
627 kdp_breakpoint_req_t
*rq
= &pkt
->breakpoint_req
;
628 kdp_breakpoint_reply_t
*rp
= &pkt
->breakpoint_reply
;
631 unsigned int old_instruction
= 0;
632 unsigned int breakinstr
= kdp_ml_get_breakinsn();
634 if(breakpoints_initialized
== 0)
636 for(i
=0;(i
< MAX_BREAKPOINTS
); breakpoint_list
[i
].address
=0, i
++);
637 breakpoints_initialized
++;
639 if (plen
< sizeof (*rq
))
641 cnt
= kdp_vm_read((caddr_t
)rq
->address
, (caddr_t
)(&old_instruction
), sizeof(int));
643 if (old_instruction
==breakinstr
)
645 printf("A trap was already set at that address, not setting new breakpoint\n");
646 rp
->error
= BREAKPOINT_ALREADY_SET
;
648 rp
->hdr
.is_reply
= 1;
649 rp
->hdr
.len
= sizeof (*rp
);
650 *reply_port
= kdp
.reply_port
;
656 for(i
=0;(i
< MAX_BREAKPOINTS
) && (breakpoint_list
[i
].address
!= 0); i
++);
658 if (i
== MAX_BREAKPOINTS
)
660 rp
->error
= KDP_MAX_BREAKPOINTS
;
662 rp
->hdr
.is_reply
= 1;
663 rp
->hdr
.len
= sizeof (*rp
);
664 *reply_port
= kdp
.reply_port
;
669 breakpoint_list
[i
].address
= rq
->address
;
670 breakpoint_list
[i
].old_instruction
= old_instruction
;
672 cnt
= kdp_vm_write((caddr_t
)&breakinstr
, (caddr_t
)rq
->address
, sizeof(&breakinstr
));
674 rp
->error
= KDPERR_NO_ERROR
;
675 rp
->hdr
.is_reply
= 1;
676 rp
->hdr
.len
= sizeof (*rp
);
677 *reply_port
= kdp
.reply_port
;
684 kdp_breakpoint_remove(
687 unsigned short *reply_port
690 kdp_breakpoint_req_t
*rq
= &pkt
->breakpoint_req
;
691 kdp_breakpoint_reply_t
*rp
= &pkt
->breakpoint_reply
;
695 if (plen
< sizeof (*rq
))
698 for(i
=0;(i
< MAX_BREAKPOINTS
) && (breakpoint_list
[i
].address
!= rq
->address
); i
++);
699 if (i
== MAX_BREAKPOINTS
)
701 rp
->error
= BREAKPOINT_NOT_FOUND
;
702 rp
->hdr
.is_reply
= 1;
703 rp
->hdr
.len
= sizeof (*rp
);
704 *reply_port
= kdp
.reply_port
;
707 return (TRUE
); /* Check if it needs to be FALSE in case of error */
710 breakpoint_list
[i
].address
= 0;
711 cnt
= kdp_vm_write((caddr_t
)&(breakpoint_list
[i
].old_instruction
), (caddr_t
)rq
->address
, sizeof(int));
712 rp
->error
= KDPERR_NO_ERROR
;
713 rp
->hdr
.is_reply
= 1;
714 rp
->hdr
.len
= sizeof (*rp
);
715 *reply_port
= kdp
.reply_port
;
722 kdp_remove_all_breakpoints()
725 boolean_t breakpoint_found
= FALSE
;
727 if (breakpoints_initialized
)
729 for(i
=0;i
< MAX_BREAKPOINTS
; i
++)
731 if (breakpoint_list
[i
].address
)
733 kdp_vm_write((caddr_t
)&(breakpoint_list
[i
].old_instruction
), (caddr_t
)breakpoint_list
[i
].address
, sizeof(int));
734 breakpoint_found
= TRUE
;
735 breakpoint_list
[i
].address
= 0;
738 if (breakpoint_found
)
739 printf("kdp_remove_all_breakpoints: found extant breakpoints, removing them.\n");
741 return breakpoint_found
;
745 #define MAX_FRAMES 1000
747 static int pid_from_task(task_t task
)
752 pid
= proc_pid(task
->bsd_info
);
758 kdp_stackshot(int pid
, uint32_t tracebuf
, uint32_t tracebuf_size
, unsigned trace_options
, uint32_t *pbytesTraced
)
760 uint32_t tracepos
= (uint32_t) tracebuf
;
761 uint32_t tracebound
= tracepos
+ tracebuf_size
;
762 uint32_t tracebytes
= 0;
765 processor_set_t pset
= &default_pset
;
766 task_t task
= TASK_NULL
;
767 thread_t thread
= THREAD_NULL
;
768 int nframes
= trace_options
;
769 thread_snapshot_t tsnap
= NULL
;
770 unsigned framesize
= 2 * sizeof(vm_offset_t
);
772 if ((nframes
<= 0) || nframes
> MAX_FRAMES
)
773 nframes
= MAX_FRAMES
;
775 queue_iterate(&pset
->tasks
, task
, task_t
, pset_tasks
) {
776 /* Trace everything, unless a process was specified */
777 if ((pid
== -1) || (pid
== pid_from_task(task
)))
778 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
){
779 if ((tracepos
+ 4 * sizeof(struct thread_snapshot
)) > tracebound
) {
783 /* Populate the thread snapshot header */
784 tsnap
= (thread_snapshot_t
) tracepos
;
785 tsnap
->thread_id
= thread
;
786 tsnap
->state
= thread
->state
;
787 tsnap
->wait_queue
= thread
->wait_queue
;
788 tsnap
->wait_event
= thread
->wait_event
;
789 tsnap
->kernel_stack
= thread
->kernel_stack
;
790 tsnap
->reserved_stack
= thread
->reserved_stack
;
791 tsnap
->continuation
= thread
->continuation
;
792 /* Add the BSD process identifiers */
793 if ((tsnap
->pid
= pid_from_task(task
)) != -1)
794 proc_name(tsnap
->pid
, tsnap
->p_comm
, MAXCOMLEN
+ 1);
796 tsnap
->p_comm
[0] = '\0';
798 tsnap
->snapshot_magic
= 0xfeedface;
799 tracepos
+= sizeof(struct thread_snapshot
);
801 /* Call through to the machine specific trace routines
802 * Frames are added past the snapshot header.
804 if (tsnap
->kernel_stack
!= 0)
805 tracebytes
= machine_trace_thread(thread
, tracepos
, tracebound
, nframes
, FALSE
);
806 tsnap
->nkern_frames
= tracebytes
/(2 * sizeof(vm_offset_t
));
807 tracepos
+= tracebytes
;
810 /* Trace user stack, if any */
811 if (thread
->task
->map
!= kernel_map
) {
813 if (task_has_64BitAddr(thread
->task
)) {
814 tracebytes
= machine_trace_thread64(thread
, tracepos
, tracebound
, nframes
, TRUE
);
816 framesize
= 2 * sizeof(addr64_t
);
819 tracebytes
= machine_trace_thread(thread
, tracepos
, tracebound
, nframes
, TRUE
);
820 framesize
= 2 * sizeof(vm_offset_t
);
823 tsnap
->nuser_frames
= tracebytes
/framesize
;
824 tracepos
+= tracebytes
;
830 /* Release stack snapshot wait indicator */
831 kdp_snapshot_postflight();
833 *pbytesTraced
= tracepos
- tracebuf
;