2 * Copyright (c) 2000-2008 Apple 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@
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
35 * Kernel Debugging Protocol UDP implementation.
39 #include <mach/boolean.h>
40 #include <mach/mach_types.h>
41 #include <mach/exception_types.h>
42 #include <kern/cpu_data.h>
43 #include <kern/debug.h>
44 #include <kern/clock.h>
46 #include <kdp/kdp_core.h>
47 #include <kdp/kdp_internal.h>
48 #include <kdp/kdp_en_debugger.h>
49 #include <kdp/kdp_callout.h>
50 #include <kdp/kdp_udp.h>
52 #include <kdp/kdp_serial.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_protos.h>
57 #include <vm/vm_kern.h> /* kernel_map */
59 #include <mach/memory_object_types.h>
63 #define DO_ALIGN 1 /* align all packet data accesses */
65 extern int kdp_getc(void);
66 extern int reattach_wait
;
68 extern int serial_getc(void);
69 extern void serial_putc(char);
70 extern int serial_init(void);
72 static u_short ip_id
; /* ip packet ctr, for ids */
74 /* @(#)udp_usrreq.c 2.2 88/05/23 4.0NFSSRC SMI; from UCB 7.1 6/5/86 */
77 * UDP protocol implementation.
78 * Per RFC 768, August, 1980.
80 #define UDP_TTL 60 /* deflt time to live for UDP packets */
81 int udp_ttl
= UDP_TTL
;
82 static unsigned char exception_seq
;
85 unsigned char data
[KDP_MAXPACKET
];
86 unsigned int off
, len
;
102 *exception_message
[] = {
104 "Memory access", /* EXC_BAD_ACCESS */
105 "Failed instruction", /* EXC_BAD_INSTRUCTION */
106 "Arithmetic", /* EXC_ARITHMETIC */
107 "Emulation", /* EXC_EMULATION */
108 "Software", /* EXC_SOFTWARE */
109 "Breakpoint" /* EXC_BREAKPOINT */
112 volatile int kdp_flag
= 0;
114 static kdp_send_t kdp_en_send_pkt
;
115 static kdp_receive_t kdp_en_recv_pkt
;
118 static u_long kdp_current_ip_address
= 0;
119 static struct ether_addr kdp_current_mac_address
= {{0, 0, 0, 0, 0, 0}};
120 static void *kdp_current_ifp
;
122 static void kdp_handler( void *);
124 static uint32_t panic_server_ip
= 0;
125 static uint32_t parsed_router_ip
= 0;
126 static uint32_t router_ip
= 0;
127 static uint32_t target_ip
= 0;
129 static volatile boolean_t panicd_specified
= FALSE
;
130 static boolean_t router_specified
= FALSE
;
131 static unsigned int panicd_port
= CORE_REMOTE_PORT
;
133 static struct ether_addr etherbroadcastaddr
= {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
135 static struct ether_addr router_mac
= {{0, 0, 0 , 0, 0, 0}};
136 static struct ether_addr destination_mac
= {{0, 0, 0 , 0, 0, 0}};
137 static struct ether_addr temp_mac
= {{0, 0, 0 , 0, 0, 0}};
138 static struct ether_addr current_resolved_MAC
= {{0, 0, 0 , 0, 0, 0}};
140 static boolean_t flag_panic_dump_in_progress
= FALSE
;
141 static boolean_t flag_router_mac_initialized
= FALSE
;
143 static boolean_t flag_arp_resolved
= FALSE
;
145 static unsigned int panic_timeout
= 100000;
146 static unsigned int last_panic_port
= CORE_REMOTE_PORT
;
148 unsigned int SEGSIZE
= 512;
150 __unused
static unsigned int PANIC_PKTSIZE
= 518;
151 static char panicd_ip_str
[20];
152 static char router_ip_str
[20];
154 static unsigned int panic_block
= 0;
155 volatile unsigned int kdp_trigger_core_dump
= 0;
156 __private_extern__
volatile unsigned int flag_kdp_trigger_reboot
= 0;
158 extern unsigned int not_in_kdp
;
160 extern unsigned int disableConsoleOutput
;
162 extern int kdp_vm_read( caddr_t
, caddr_t
, unsigned int);
163 extern void kdp_call(void);
164 extern boolean_t
kdp_call_kdb(void);
165 extern int kern_dump(void);
167 void * kdp_get_interface(void);
168 void kdp_set_gateway_mac(void *);
169 void kdp_set_ip_and_mac_addresses(struct in_addr
*, struct ether_addr
*);
170 void kdp_set_interface(void *);
172 void kdp_disable_arp(void);
173 static void kdp_arp_reply(struct ether_arp
*);
174 static void kdp_process_arp_reply(struct ether_arp
*);
175 static boolean_t
kdp_arp_resolve(uint32_t, struct ether_addr
*);
177 static volatile unsigned kdp_reentry_deadline
;
179 static boolean_t gKDPDebug
= FALSE
;
180 #define KDP_DEBUG(...) if (gKDPDebug) printf(__VA_ARGS__);
182 int kdp_snapshot
= 0;
183 static int stack_snapshot_ret
= 0;
184 static unsigned stack_snapshot_bytes_traced
= 0;
186 static void *stack_snapshot_buf
;
187 static uint32_t stack_snapshot_bufsize
;
188 static int stack_snapshot_pid
;
189 static uint32_t stack_snapshot_options
;
191 static unsigned int old_debugger
;
194 kdp_snapshot_preflight(int pid
, void * tracebuf
, uint32_t tracebuf_size
,
198 kdp_snapshot_postflight(void);
201 kdp_stackshot(int pid
, void *tracebuf
, uint32_t tracebuf_size
,
202 unsigned trace_options
, uint32_t *pbytesTraced
);
205 kdp_stack_snapshot_geterror(void);
208 kdp_stack_snapshot_bytes_traced(void);
214 kdp_ml_enter_debugger_wrapper(__unused
void *param0
, __unused
void *param1
) {
215 kdp_ml_enter_debugger();
219 kdp_timer_callout_init(void) {
220 kdp_timer_call
= thread_call_allocate(kdp_ml_enter_debugger_wrapper
, NULL
);
225 kdp_register_send_receive(
227 kdp_receive_t receive
)
229 unsigned int debug
= 0;
233 kdp_timer_callout_init();
235 PE_parse_boot_argn("debug", &debug
, sizeof (debug
));
241 kdp_en_send_pkt
= send
;
242 kdp_en_recv_pkt
= receive
;
244 if (debug
& DB_KDP_BP_DIS
)
245 kdp_flag
|= KDP_BP_DIS
;
246 if (debug
& DB_KDP_GETC_ENA
)
247 kdp_flag
|= KDP_GETC_ENA
;
251 if (debug
& DB_KERN_DUMP_ON_PANIC
)
252 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
253 if (debug
& DB_KERN_DUMP_ON_NMI
)
254 kdp_flag
|= PANIC_CORE_ON_NMI
;
256 if (debug
& DB_DBG_POST_CORE
)
257 kdp_flag
|= DBG_POST_CORE
;
259 if (debug
& DB_PANICLOG_DUMP
)
260 kdp_flag
|= PANIC_LOG_DUMP
;
262 if (PE_parse_boot_argn("_panicd_ip", panicd_ip_str
, sizeof (panicd_ip_str
)))
263 panicd_specified
= TRUE
;
265 if (PE_parse_boot_argn("_router_ip", router_ip_str
, sizeof (router_ip_str
)))
266 router_specified
= TRUE
;
268 if (!PE_parse_boot_argn("panicd_port", &panicd_port
, sizeof (panicd_port
)))
269 panicd_port
= CORE_REMOTE_PORT
;
271 kdp_flag
|= KDP_READY
;
272 if (current_debugger
== NO_CUR_DB
)
273 current_debugger
= KDP_CUR_DB
;
274 if ((kdp_current_ip_address
!= 0) && halt_in_debugger
) {
281 kdp_unregister_send_receive(
282 __unused kdp_send_t send
,
283 __unused kdp_receive_t receive
)
285 if (current_debugger
== KDP_CUR_DB
)
286 current_debugger
= NO_CUR_DB
;
287 kdp_flag
&= ~KDP_READY
;
288 kdp_en_send_pkt
= NULL
;
289 kdp_en_recv_pkt
= NULL
;
292 /* Cache stack snapshot parameters in preparation for a trace */
294 kdp_snapshot_preflight(int pid
, void * tracebuf
, uint32_t tracebuf_size
, uint32_t options
)
296 stack_snapshot_pid
= pid
;
297 stack_snapshot_buf
= tracebuf
;
298 stack_snapshot_bufsize
= tracebuf_size
;
299 stack_snapshot_options
= options
;
301 /* Mark this debugger as active, since the polled mode driver that
302 * ordinarily does this may not be enabled (yet), or since KDB may be
303 * the primary debugger.
305 old_debugger
= current_debugger
;
306 if (old_debugger
!= KDP_CUR_DB
) {
307 current_debugger
= KDP_CUR_DB
;
312 kdp_snapshot_postflight(void)
315 if ((kdp_en_send_pkt
== NULL
) || (old_debugger
== KDB_CUR_DB
))
316 current_debugger
= old_debugger
;
320 kdp_stack_snapshot_geterror(void)
322 return stack_snapshot_ret
;
326 kdp_stack_snapshot_bytes_traced(void)
328 return stack_snapshot_bytes_traced
;
332 kdp_schedule_debugger_reentry(unsigned interval
) {
335 clock_interval_to_deadline(interval
, 1000 * 1000, &deadline
);
336 thread_call_enter_delayed(kdp_timer_call
, deadline
);
345 bcopy((char *)src
, (char *)dst
, sizeof (struct ether_addr
));
348 static unsigned short
354 unsigned int high
, low
, sum
;
364 sum
= (high
<< 8) + low
;
365 sum
= (sum
>> 16) + (sum
& 65535);
367 return (sum
> 65535 ? sum
- 65535 : sum
);
372 unsigned short reply_port
375 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
376 struct ip aligned_ip
, *ip
= &aligned_ip
;
377 struct in_addr tmp_ipaddr
;
378 struct ether_addr tmp_enaddr
;
379 struct ether_header
*eh
= NULL
;
382 kdp_panic("kdp_reply");
384 pkt
.off
-= sizeof (struct udpiphdr
);
387 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
389 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
391 ui
->ui_next
= ui
->ui_prev
= NULL
;
393 ui
->ui_pr
= IPPROTO_UDP
;
394 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
395 tmp_ipaddr
= ui
->ui_src
;
396 ui
->ui_src
= ui
->ui_dst
;
397 ui
->ui_dst
= tmp_ipaddr
;
398 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
399 ui
->ui_dport
= reply_port
;
400 ui
->ui_ulen
= ui
->ui_len
;
403 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
404 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
406 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
408 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
409 ip
->ip_v
= IPVERSION
;
410 ip
->ip_id
= htons(ip_id
++);
411 ip
->ip_hl
= sizeof (struct ip
) >> 2;
412 ip
->ip_ttl
= udp_ttl
;
414 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
416 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
419 pkt
.len
+= sizeof (struct udpiphdr
);
421 pkt
.off
-= sizeof (struct ether_header
);
423 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
424 enaddr_copy(eh
->ether_shost
, &tmp_enaddr
);
425 enaddr_copy(eh
->ether_dhost
, eh
->ether_shost
);
426 enaddr_copy(&tmp_enaddr
, eh
->ether_dhost
);
427 eh
->ether_type
= htons(ETHERTYPE_IP
);
429 pkt
.len
+= sizeof (struct ether_header
);
431 // save reply for possible retransmission
432 bcopy((char *)&pkt
, (char *)&saved_reply
, sizeof(pkt
));
434 (*kdp_en_send_pkt
)(&pkt
.data
[pkt
.off
], pkt
.len
);
436 // increment expected sequence number
442 unsigned short remote_port
445 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
446 struct ip aligned_ip
, *ip
= &aligned_ip
;
447 struct ether_header
*eh
;
450 kdp_panic("kdp_send");
452 pkt
.off
-= sizeof (struct udpiphdr
);
455 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
457 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
459 ui
->ui_next
= ui
->ui_prev
= NULL
;
461 ui
->ui_pr
= IPPROTO_UDP
;
462 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
463 ui
->ui_src
= adr
.loc
.in
;
464 ui
->ui_dst
= adr
.rmt
.in
;
465 ui
->ui_sport
= htons(KDP_REMOTE_PORT
);
466 ui
->ui_dport
= remote_port
;
467 ui
->ui_ulen
= ui
->ui_len
;
470 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
471 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
473 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
475 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
476 ip
->ip_v
= IPVERSION
;
477 ip
->ip_id
= htons(ip_id
++);
478 ip
->ip_hl
= sizeof (struct ip
) >> 2;
479 ip
->ip_ttl
= udp_ttl
;
481 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
483 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
486 pkt
.len
+= sizeof (struct udpiphdr
);
488 pkt
.off
-= sizeof (struct ether_header
);
490 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
491 enaddr_copy(&adr
.loc
.ea
, eh
->ether_shost
);
492 enaddr_copy(&adr
.rmt
.ea
, eh
->ether_dhost
);
493 eh
->ether_type
= htons(ETHERTYPE_IP
);
495 pkt
.len
+= sizeof (struct ether_header
);
496 (*kdp_en_send_pkt
)(&pkt
.data
[pkt
.off
], pkt
.len
);
499 /* We don't interpret this pointer, we just give it to the
500 bsd stack so it can decide when to set the MAC and IP info. */
502 kdp_set_interface(void *ifp
)
504 kdp_current_ifp
= ifp
;
508 kdp_get_interface(void)
510 return kdp_current_ifp
;
514 kdp_set_ip_and_mac_addresses(
515 struct in_addr
*ipaddr
,
516 struct ether_addr
*macaddr
)
518 kdp_current_ip_address
= ipaddr
->s_addr
;
519 kdp_current_mac_address
= *macaddr
;
520 if ((current_debugger
== KDP_CUR_DB
) && halt_in_debugger
) {
527 kdp_set_gateway_mac(void *gatewaymac
)
529 router_mac
= *(struct ether_addr
*)gatewaymac
;
530 flag_router_mac_initialized
= TRUE
;
534 kdp_get_mac_addr(void)
536 return kdp_current_mac_address
;
540 kdp_get_ip_address(void)
542 return kdp_current_ip_address
;
546 kdp_disable_arp(void)
548 kdp_flag
&= ~(DB_ARP
);
552 kdp_arp_dispatch(void)
554 struct ether_arp aligned_ea
, *ea
= &aligned_ea
;
555 unsigned arp_header_offset
;
557 arp_header_offset
= sizeof(struct ether_header
) + pkt
.off
;
558 memcpy((void *)ea
, (void *)&pkt
.data
[arp_header_offset
], sizeof(*ea
));
560 switch(ntohs(ea
->arp_op
)) {
565 kdp_process_arp_reply(ea
);
573 kdp_process_arp_reply(struct ether_arp
*ea
)
575 /* Are we interested in ARP replies? */
576 if (flag_arp_resolved
== TRUE
)
579 /* Did we receive a reply from the right source? */
580 if (((struct in_addr
*)(ea
->arp_spa
))->s_addr
!= target_ip
)
583 flag_arp_resolved
= TRUE
;
584 current_resolved_MAC
= *(struct ether_addr
*) (ea
->arp_sha
);
589 /* ARP responses are enabled when the DB_ARP bit of the debug boot arg
594 kdp_arp_reply(struct ether_arp
*ea
)
596 struct ether_header
*eh
;
598 struct in_addr isaddr
, itaddr
, myaddr
;
599 struct ether_addr my_enaddr
;
601 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
602 pkt
.off
+= sizeof(struct ether_header
);
604 if(ntohs(ea
->arp_op
) != ARPOP_REQUEST
)
607 myaddr
.s_addr
= kdp_get_ip_address();
608 my_enaddr
= kdp_get_mac_addr();
610 if ((ntohl(myaddr
.s_addr
) == 0) ||
611 ((my_enaddr
.ether_addr_octet
[0] & 0xff) == 0
612 && (my_enaddr
.ether_addr_octet
[1] & 0xff) == 0
613 && (my_enaddr
.ether_addr_octet
[2] & 0xff) == 0
614 && (my_enaddr
.ether_addr_octet
[3] & 0xff) == 0
615 && (my_enaddr
.ether_addr_octet
[4] & 0xff) == 0
616 && (my_enaddr
.ether_addr_octet
[5] & 0xff) == 0
620 (void)memcpy((void *)&isaddr
, (void *)ea
->arp_spa
, sizeof (isaddr
));
621 (void)memcpy((void *)&itaddr
, (void *)ea
->arp_tpa
, sizeof (itaddr
));
623 if (itaddr
.s_addr
== myaddr
.s_addr
) {
624 (void)memcpy((void *)ea
->arp_tha
, (void *)ea
->arp_sha
, sizeof(ea
->arp_sha
));
625 (void)memcpy((void *)ea
->arp_sha
, (void *)&my_enaddr
, sizeof(ea
->arp_sha
));
627 (void)memcpy((void *)ea
->arp_tpa
, (void *) ea
->arp_spa
, sizeof(ea
->arp_spa
));
628 (void)memcpy((void *)ea
->arp_spa
, (void *) &itaddr
, sizeof(ea
->arp_spa
));
630 ea
->arp_op
= htons(ARPOP_REPLY
);
631 ea
->arp_pro
= htons(ETHERTYPE_IP
);
632 (void)memcpy(eh
->ether_dhost
, ea
->arp_tha
, sizeof(eh
->ether_dhost
));
633 (void)memcpy(eh
->ether_shost
, &my_enaddr
, sizeof(eh
->ether_shost
));
634 eh
->ether_type
= htons(ETHERTYPE_ARP
);
635 (void)memcpy(&pkt
.data
[pkt
.off
], ea
, sizeof(*ea
));
636 pkt
.off
-= sizeof (struct ether_header
);
637 /* pkt.len is still the length we want, ether_header+ether_arp */
638 (*kdp_en_send_pkt
)(&pkt
.data
[pkt
.off
], pkt
.len
);
645 struct ether_header
*eh
= NULL
;
646 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
647 struct ip aligned_ip
, *ip
= &aligned_ip
;
648 static int msg_printed
;
651 kdp_panic("kdp_poll");
653 if (!kdp_en_recv_pkt
|| !kdp_en_send_pkt
) {
654 if( msg_printed
== 0) {
656 printf("kdp_poll: no debugger device\n");
661 pkt
.off
= pkt
.len
= 0;
662 (*kdp_en_recv_pkt
)(pkt
.data
, &pkt
.len
, 3/* ms */);
667 if (pkt
.len
>= sizeof(struct ether_header
))
669 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
671 if (kdp_flag
& KDP_ARP
)
673 if (ntohs(eh
->ether_type
) == ETHERTYPE_ARP
)
681 if (pkt
.len
< (sizeof (struct ether_header
) + sizeof (struct udpiphdr
)))
684 pkt
.off
+= sizeof (struct ether_header
);
685 if (ntohs(eh
->ether_type
) != ETHERTYPE_IP
) {
690 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
691 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
693 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
694 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
697 pkt
.off
+= sizeof (struct udpiphdr
);
698 if (ui
->ui_pr
!= IPPROTO_UDP
) {
702 if (ip
->ip_hl
> (sizeof (struct ip
) >> 2)) {
706 if (ntohs(ui
->ui_dport
) != KDP_REMOTE_PORT
) {
707 if (panicd_port
== (ntohs(ui
->ui_dport
)) &&
708 flag_panic_dump_in_progress
) {
709 last_panic_port
= ui
->ui_sport
;
714 /* If we receive a kernel debugging packet whilst a
715 * core dump is in progress, abort the transfer and
716 * enter the debugger.
719 if (flag_panic_dump_in_progress
)
721 abort_panic_transfer();
725 if (!kdp
.is_conn
&& !flag_panic_dump_in_progress
) {
726 enaddr_copy(eh
->ether_dhost
, &adr
.loc
.ea
);
727 adr
.loc
.in
= ui
->ui_dst
;
729 enaddr_copy(eh
->ether_shost
, &adr
.rmt
.ea
);
730 adr
.rmt
.in
= ui
->ui_src
;
734 * Calculate kdp packet length.
736 pkt
.len
= ntohs((u_short
)ui
->ui_ulen
) - sizeof (struct udphdr
);
740 /* Create and transmit an ARP resolution request for the target IP address.
741 * This is modeled on ether_inet_arp()/RFC 826.
745 transmit_ARP_request(uint32_t ip_addr
)
747 struct ether_header
*eh
= (struct ether_header
*) &pkt
.data
[0];
748 struct ether_arp
*ea
= (struct ether_arp
*) &pkt
.data
[sizeof(struct ether_header
)];
750 KDP_DEBUG("Transmitting ARP request\n");
751 /* Populate the ether_header */
752 eh
->ether_type
= htons(ETHERTYPE_ARP
);
753 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
754 enaddr_copy(ðerbroadcastaddr
, eh
->ether_dhost
);
756 /* Populate the ARP header */
757 ea
->arp_pro
= htons(ETHERTYPE_IP
);
758 ea
->arp_hln
= sizeof(ea
->arp_sha
);
759 ea
->arp_pln
= sizeof(ea
->arp_spa
);
760 ea
->arp_hrd
= htons(ARPHRD_ETHER
);
761 ea
->arp_op
= htons(ARPOP_REQUEST
);
764 enaddr_copy(ðerbroadcastaddr
, ea
->arp_tha
);
765 memcpy(ea
->arp_tpa
, (void *) &ip_addr
, sizeof(ip_addr
));
768 enaddr_copy(&kdp_current_mac_address
, ea
->arp_sha
);
769 memcpy(ea
->arp_spa
, (void *) &kdp_current_ip_address
, sizeof(kdp_current_ip_address
));
772 pkt
.len
= sizeof(struct ether_header
) + sizeof(struct ether_arp
);
774 (*kdp_en_send_pkt
)(&pkt
.data
[pkt
.off
], pkt
.len
);
778 kdp_arp_resolve(uint32_t arp_target_ip
, struct ether_addr
*resolved_MAC
)
780 int poll_count
= 256; /* ~770 ms modulo broadcast/delayed traffic? */
783 #define NUM_ARP_TX_RETRIES 5
785 target_ip
= arp_target_ip
;
786 flag_arp_resolved
= FALSE
;
789 pkt
.off
= pkt
.len
= 0;
793 if (tretries
>= NUM_ARP_TX_RETRIES
) {
797 KDP_DEBUG("ARP TX attempt #%d \n", tretries
);
799 transmit_ARP_request(arp_target_ip
);
801 while (!pkt
.input
&& !flag_arp_resolved
&& flag_panic_dump_in_progress
&& --poll_count
) {
805 if (flag_arp_resolved
) {
806 *resolved_MAC
= current_resolved_MAC
;
810 if (!flag_panic_dump_in_progress
|| pkt
.input
) /* we received a debugging packet, bail*/
812 printf("Received a debugger packet,transferring control to debugger\n");
813 /* Indicate that we should wait in the debugger when we return */
814 kdp_flag
|= DBG_POST_CORE
;
818 else /* We timed out */
819 if (0 == poll_count
) {
831 unsigned short reply_port
;
832 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
834 kdp
.saved_state
= saved_state
; // see comment in kdp_raise_exception
841 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
843 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
846 // ignore replies -- we're not expecting them anyway.
851 if (hdr
->request
== KDP_REATTACH
)
852 exception_seq
= hdr
->seq
;
854 // check for retransmitted request
855 if (hdr
->seq
== (exception_seq
- 1)) {
856 /* retransmit last reply */
857 (*kdp_en_send_pkt
)(&saved_reply
.data
[saved_reply
.off
],
860 } else if (hdr
->seq
!= exception_seq
) {
861 printf("kdp: bad sequence %d (want %d)\n",
862 hdr
->seq
, exception_seq
);
866 if (kdp_packet((unsigned char*)&pkt
.data
[pkt
.off
],
868 (unsigned short *)&reply_port
)) {
869 kdp_reply(reply_port
);
874 } while (kdp
.is_halted
);
878 kdp_connection_wait(void)
880 unsigned short reply_port
;
881 struct ether_addr kdp_mac_addr
= kdp_get_mac_addr();
882 unsigned int ip_addr
= ntohl(kdp_get_ip_address());
885 * Do both a printf() and a kprintf() of the MAC and IP so that
886 * they will print out on headless machines but not be added to
890 printf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
891 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
892 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
893 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
894 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
895 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
896 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
898 kprintf( "ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
899 kdp_mac_addr
.ether_addr_octet
[0] & 0xff,
900 kdp_mac_addr
.ether_addr_octet
[1] & 0xff,
901 kdp_mac_addr
.ether_addr_octet
[2] & 0xff,
902 kdp_mac_addr
.ether_addr_octet
[3] & 0xff,
903 kdp_mac_addr
.ether_addr_octet
[4] & 0xff,
904 kdp_mac_addr
.ether_addr_octet
[5] & 0xff);
906 printf( "ip address: %d.%d.%d.%d\n",
907 (ip_addr
& 0xff000000) >> 24,
908 (ip_addr
& 0xff0000) >> 16,
909 (ip_addr
& 0xff00) >> 8,
912 kprintf( "ip address: %d.%d.%d.%d\n",
913 (ip_addr
& 0xff000000) >> 24,
914 (ip_addr
& 0xff0000) >> 16,
915 (ip_addr
& 0xff00) >> 8,
918 printf("\nWaiting for remote debugger connection.\n");
921 if (reattach_wait
== 0) {
922 if((kdp_flag
& KDP_GETC_ENA
) && (0 != kdp_getc()))
924 printf("Options..... Type\n");
925 printf("------------ ----\n");
926 printf("continue.... 'c'\n");
927 printf("reboot...... 'r'\n");
929 printf("enter kdb... 'k'\n");
938 kdp_hdr_t aligned_hdr
, *hdr
= &aligned_hdr
;
941 if (kdp_flag
& KDP_GETC_ENA
) {
944 printf("Continuing...\n");
947 printf("Rebooting...\n");
952 printf("calling kdb...\n");
956 printf("not implemented...\n");
966 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)hdr
, sizeof(*hdr
));
968 hdr
= (kdp_hdr_t
*)&pkt
.data
[pkt
.off
];
970 if (hdr
->request
== KDP_HOSTREBOOT
) {
972 /* should not return! */
974 if (((hdr
->request
== KDP_CONNECT
) || (hdr
->request
== KDP_REATTACH
)) &&
975 !hdr
->is_reply
&& (hdr
->seq
== exception_seq
)) {
976 if (kdp_packet((unsigned char *)&pkt
.data
[pkt
.off
],
978 (unsigned short *)&reply_port
))
979 kdp_reply(reply_port
);
980 if (hdr
->request
== KDP_REATTACH
) {
982 hdr
->request
=KDP_DISCONNECT
;
988 } while (!kdp
.is_conn
);
990 if (current_debugger
== KDP_CUR_DB
)
992 printf("Connected to remote debugger.\n");
997 unsigned int exception
,
1002 unsigned short remote_port
;
1003 unsigned int timeout_count
= 100;
1004 unsigned int poll_timeout
;
1007 pkt
.off
= sizeof (struct ether_header
) + sizeof (struct udpiphdr
);
1008 kdp_exception((unsigned char *)&pkt
.data
[pkt
.off
],
1010 (unsigned short *)&remote_port
,
1011 (unsigned int)exception
,
1013 (unsigned int)subcode
);
1015 kdp_send(remote_port
);
1018 while(!pkt
.input
&& poll_timeout
)
1025 if (!kdp_exception_ack(&pkt
.data
[pkt
.off
], pkt
.len
)) {
1032 if (kdp
.exception_ack_needed
)
1033 kdp_us_spin(250000);
1035 } while (kdp
.exception_ack_needed
&& timeout_count
--);
1037 if (kdp
.exception_ack_needed
) {
1038 // give up & disconnect
1039 printf("kdp: exception ack timeout\n");
1040 if (current_debugger
== KDP_CUR_DB
)
1047 kdp_raise_exception(
1048 unsigned int exception
,
1050 unsigned int subcode
,
1056 /* Was a system trace requested ? */
1057 if (kdp_snapshot
&& (!panic_active()) && (panic_caller
== 0)) {
1058 stack_snapshot_ret
= kdp_stackshot(stack_snapshot_pid
,
1059 stack_snapshot_buf
, stack_snapshot_bufsize
,
1060 stack_snapshot_options
, &stack_snapshot_bytes_traced
);
1064 disable_preemption();
1066 if (saved_state
== 0)
1067 printf("kdp_raise_exception with NULL state\n");
1070 if (exception
!= EXC_BREAKPOINT
) {
1071 if (exception
> EXC_BREAKPOINT
|| exception
< EXC_BAD_ACCESS
) {
1074 printf("%s exception (%x,%x,%x)\n",
1075 exception_message
[index
],
1076 exception
, code
, subcode
);
1081 /* XXX WMG it seems that sometimes it doesn't work to let kdp_handler
1082 * do this. I think the client and the host can get out of sync.
1084 kdp
.saved_state
= saved_state
;
1085 kdp
.kdp_cpu
= cpu_number();
1086 kdp
.kdp_thread
= current_thread();
1089 kdp_panic("kdp_raise_exception");
1092 if (((kdp_flag
& KDP_PANIC_DUMP_ENABLED
) || (kdp_flag
& PANIC_LOG_DUMP
))
1093 && (panicstr
!= (char *) 0)) {
1098 if ((kdp_flag
& PANIC_CORE_ON_NMI
) && (panicstr
== (char *) 0) &&
1101 disable_debug_output
= disableConsoleOutput
= FALSE
;
1104 if (!(kdp_flag
& DBG_POST_CORE
))
1105 goto exit_raise_exception
;
1110 kdp_connection_wait();
1112 kdp_send_exception(exception
, code
, subcode
);
1113 if (kdp
.exception_ack_needed
) {
1114 kdp
.exception_ack_needed
= FALSE
;
1115 kdp_remove_all_breakpoints();
1116 printf("Remote debugger disconnected.\n");
1121 kdp
.is_halted
= TRUE
; /* XXX */
1122 kdp_handler(saved_state
);
1125 kdp_remove_all_breakpoints();
1126 printf("Remote debugger disconnected.\n");
1129 /* Allow triggering a panic core dump when connected to the machine
1130 * Continuing after setting kdp_trigger_core_dump should do the
1134 if (1 == kdp_trigger_core_dump
) {
1135 kdp_flag
&= ~PANIC_LOG_DUMP
;
1136 kdp_flag
|= KDP_PANIC_DUMP_ENABLED
;
1138 kdp_trigger_core_dump
= 0;
1141 /* Trigger a reboot if the user has set this flag through the
1142 * debugger.Ideally, this would be done through the HOSTREBOOT packet
1143 * in the protocol,but that will need gdb support,and when it's
1144 * available, it should work automatically.
1146 if (1 == flag_kdp_trigger_reboot
) {
1148 /* If we're still around, reset the flag */
1149 flag_kdp_trigger_reboot
= 0;
1152 if (kdp_reentry_deadline
) {
1153 kdp_schedule_debugger_reentry(kdp_reentry_deadline
);
1154 printf("Debugger re-entry scheduled in %d milliseconds\n", kdp_reentry_deadline
);
1155 kdp_reentry_deadline
= 0;
1160 if (reattach_wait
== 1)
1163 exit_raise_exception
:
1164 enable_preemption();
1170 kdp
.reply_port
= kdp
.exception_port
= 0;
1171 kdp
.is_halted
= kdp
.is_conn
= FALSE
;
1172 kdp
.exception_seq
= kdp
.conn_seq
= 0;
1176 create_panic_header(unsigned int request
, const char *corename
,
1177 unsigned length
, unsigned int block
)
1179 struct udpiphdr aligned_ui
, *ui
= &aligned_ui
;
1180 struct ip aligned_ip
, *ip
= &aligned_ip
;
1181 struct ether_header
*eh
;
1182 struct corehdr
*coreh
;
1183 const char *mode
= "octet";
1184 char modelen
= strlen(mode
);
1186 pkt
.off
= sizeof (struct ether_header
);
1187 pkt
.len
= length
+ ((request
== KDP_WRQ
) ? modelen
: 0) +
1188 (corename
? strlen(corename
): 0) + sizeof(struct corehdr
);
1191 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ui
, sizeof(*ui
));
1193 ui
= (struct udpiphdr
*)&pkt
.data
[pkt
.off
];
1195 ui
->ui_next
= ui
->ui_prev
= NULL
;
1197 ui
->ui_pr
= IPPROTO_UDP
;
1198 ui
->ui_len
= htons((u_short
)pkt
.len
+ sizeof (struct udphdr
));
1199 ui
->ui_src
.s_addr
= kdp_current_ip_address
;
1200 /* Already in network byte order via inet_aton() */
1201 ui
->ui_dst
.s_addr
= panic_server_ip
;
1202 ui
->ui_sport
= htons(panicd_port
);
1203 ui
->ui_dport
= ((request
== KDP_WRQ
) ? htons(panicd_port
) : last_panic_port
);
1204 ui
->ui_ulen
= ui
->ui_len
;
1207 bcopy((char *)ui
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ui
));
1208 bcopy((char *)&pkt
.data
[pkt
.off
], (char *)ip
, sizeof(*ip
));
1210 ip
= (struct ip
*)&pkt
.data
[pkt
.off
];
1212 ip
->ip_len
= htons(sizeof (struct udpiphdr
) + pkt
.len
);
1213 ip
->ip_v
= IPVERSION
;
1214 ip
->ip_id
= htons(ip_id
++);
1215 ip
->ip_hl
= sizeof (struct ip
) >> 2;
1216 ip
->ip_ttl
= udp_ttl
;
1218 ip
->ip_sum
= htons(~ip_sum((unsigned char *)ip
, ip
->ip_hl
));
1220 bcopy((char *)ip
, (char *)&pkt
.data
[pkt
.off
], sizeof(*ip
));
1223 pkt
.len
+= sizeof (struct udpiphdr
);
1225 pkt
.off
+= sizeof (struct udpiphdr
);
1227 coreh
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1228 coreh
->th_opcode
= htons((u_short
)request
);
1230 if (request
== KDP_WRQ
)
1234 cp
= coreh
->th_u
.tu_rpl
;
1235 cp
+= strlcpy (cp
, corename
, KDP_MAXPACKET
);
1237 cp
+= strlcpy (cp
, mode
, KDP_MAXPACKET
- strlen(corename
));
1242 coreh
->th_block
= htonl((unsigned int) block
);
1245 pkt
.off
-= sizeof (struct udpiphdr
);
1246 pkt
.off
-= sizeof (struct ether_header
);
1248 eh
= (struct ether_header
*)&pkt
.data
[pkt
.off
];
1249 enaddr_copy(&kdp_current_mac_address
, eh
->ether_shost
);
1250 enaddr_copy(&destination_mac
, eh
->ether_dhost
);
1251 eh
->ether_type
= htons(ETHERTYPE_IP
);
1253 pkt
.len
+= sizeof (struct ether_header
);
1257 int kdp_send_crashdump_data(unsigned int request
, char *corename
,
1258 unsigned int length
, caddr_t txstart
)
1260 caddr_t txend
= txstart
+ length
;
1261 int panic_error
= 0;
1263 if (length
<= SEGSIZE
) {
1264 if ((panic_error
= kdp_send_crashdump_pkt(request
, corename
, length
, (caddr_t
) txstart
)) < 0) {
1265 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1266 return panic_error
;
1271 while (txstart
<= (txend
- SEGSIZE
)) {
1272 if ((panic_error
= kdp_send_crashdump_pkt(KDP_DATA
, NULL
, SEGSIZE
, txstart
)) < 0) {
1273 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1277 if (!(panic_block
% 2000))
1280 if (txstart
< txend
) {
1281 kdp_send_crashdump_pkt(request
, corename
, (txend
- txstart
), txstart
);
1288 kdp_send_crashdump_pkt(unsigned int request
, char *corename
,
1289 unsigned int length
, void *panic_data
)
1291 struct corehdr
*th
= NULL
;
1292 int poll_count
= 2500;
1294 char rretries
= 0, tretries
= 0;
1296 pkt
.off
= pkt
.len
= 0;
1298 if (request
== KDP_WRQ
) /* longer timeout for initial request */
1304 if (tretries
>=15) {
1305 /* The crashdump server is unreachable for some reason. This could be a network
1306 * issue or, if we've been especially unfortunate, we've hit Radar 2760413,
1307 * which is a long standing problem with the IOKit polled mode network driver
1308 * shim which can prevent transmits/receives completely.
1310 printf ("Cannot contact panic server, timing out.\n");
1315 printf("TX retry #%d ", tretries
);
1317 th
= create_panic_header(request
, corename
, length
, panic_block
);
1319 if (request
== KDP_DATA
) {
1320 if (!kdp_vm_read((caddr_t
) panic_data
, (caddr_t
) th
->th_data
, length
)) {
1321 memset ((caddr_t
) th
->th_data
, 'X', length
);
1324 else if (request
== KDP_SEEK
) {
1325 *(unsigned int *) th
->th_data
= htonl(*(unsigned int *) panic_data
);
1328 (*kdp_en_send_pkt
)(&pkt
.data
[pkt
.off
], pkt
.len
);
1330 /* Listen for the ACK */
1332 while (!pkt
.input
&& flag_panic_dump_in_progress
&& poll_count
) {
1341 th
= (struct corehdr
*) &pkt
.data
[pkt
.off
];
1343 if (ntohs(th
->th_opcode
) == KDP_ACK
&& ntohl(th
->th_block
) == panic_block
) {
1346 if (ntohs(th
->th_opcode
) == KDP_ERROR
) {
1347 printf("Panic server returned error %d, retrying\n", ntohl(th
->th_code
));
1349 goto TRANSMIT_RETRY
;
1352 if (ntohl(th
->th_block
) == (panic_block
- 1)) {
1353 printf("RX retry ");
1355 goto TRANSMIT_RETRY
;
1361 if (!flag_panic_dump_in_progress
) /* we received a debugging packet, bail*/
1363 printf("Received a debugger packet,transferring control to debugger\n");
1364 /* Configure that if not set ..*/
1365 kdp_flag
|= DBG_POST_CORE
;
1368 else /* We timed out */
1369 if (0 == poll_count
) {
1371 kdp_us_spin ((tretries%4
) * panic_timeout
); /* capped linear backoff */
1372 goto TRANSMIT_RETRY
;
1377 if (request
== KDP_EOF
)
1378 printf("\nTotal number of packets transmitted: %d\n", panic_block
);
1386 return ((c
> 47) && (c
< 58));
1388 /* From user mode Libc - this ought to be in a library */
1390 strnstr(char *s
, const char *find
, size_t slen
)
1395 if ((c
= *find
++) != '\0') {
1399 if ((sc
= *s
++) == '\0' || slen
-- < 1)
1404 } while (strncmp(s
, find
, len
) != 0);
1410 extern char version
[];
1412 /* Horrid hack to extract xnu version if possible - a much cleaner approach
1413 * would be to have the integrator run a script which would copy the
1414 * xnu version into a string or an int somewhere at project submission
1415 * time - makes assumptions about sizeof(version), but will not fail if
1416 * it changes, but may be incorrect.
1418 /* 2006: Incorporated a change from Darwin user P. Lovell to extract
1419 * the minor kernel version numbers from the version string.
1422 kdp_get_xnu_version(char *versionbuf
)
1429 strlcpy(vstr
, "custom", 10);
1430 if (strlcpy(versionbuf
, version
, 95) < 95) {
1431 versionpos
= strnstr(versionbuf
, "xnu-", 90);
1433 strncpy(vstr
, versionpos
, sizeof(vstr
));
1434 vstr
[sizeof(vstr
)-1] = '\0';
1435 vptr
= vstr
+ 4; /* Begin after "xnu-" */
1436 while (*vptr
&& (isdigit(*vptr
) || *vptr
== '.'))
1439 /* Remove trailing period, if any */
1440 if (*(--vptr
) == '.')
1445 strlcpy(versionbuf
, vstr
, KDP_MAXPACKET
);
1449 extern char *inet_aton(const char *cp
, struct in_addr
*pin
);
1451 /* Primary dispatch routine for the system dump */
1453 kdp_panic_dump(void)
1456 char coreprefix
[10];
1460 uint32_t current_ip
= ntohl(kdp_current_ip_address
);
1462 if (flag_panic_dump_in_progress
) {
1463 printf("System dump aborted.\n");
1464 goto panic_dump_exit
;
1467 printf("Entering system dump routine\n");
1469 if (!panicd_specified
) {
1470 printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
1471 goto panic_dump_exit
;
1474 flag_panic_dump_in_progress
= TRUE
;
1478 kdp_panic("kdp_panic_dump: unexpected pending input packet");
1480 kdp_get_xnu_version((char *) &pkt
.data
[0]);
1482 /* Panic log bit takes precedence over core dump bit */
1483 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
))
1484 strncpy(coreprefix
, "paniclog", sizeof(coreprefix
));
1486 strncpy(coreprefix
, "core", sizeof(coreprefix
));
1488 abstime
= mach_absolute_time();
1489 pkt
.data
[20] = '\0';
1490 snprintf (corename
, sizeof(corename
), "%s-%s-%d.%d.%d.%d-%x",
1491 coreprefix
, &pkt
.data
[0],
1492 (current_ip
& 0xff000000) >> 24,
1493 (current_ip
& 0xff0000) >> 16,
1494 (current_ip
& 0xff00) >> 8,
1495 (current_ip
& 0xff),
1496 (unsigned int) (abstime
& 0xffffffff));
1498 if (0 == inet_aton(panicd_ip_str
, (struct in_addr
*) &panic_server_ip
)) {
1499 printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str
);
1502 printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str
, panicd_port
);
1504 destination_mac
= router_mac
;
1506 if (kdp_arp_resolve(panic_server_ip
, &temp_mac
)) {
1507 printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str
);
1508 destination_mac
= temp_mac
;
1511 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1512 if (router_specified
) {
1513 if (0 == inet_aton(router_ip_str
, (struct in_addr
*) &parsed_router_ip
))
1514 printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str
);
1516 router_ip
= parsed_router_ip
;
1517 if (kdp_arp_resolve(router_ip
, &temp_mac
)) {
1518 destination_mac
= temp_mac
;
1519 printf("Routing through specified router IP %s (%d)\n", router_ip_str
, router_ip
);
1525 if (!flag_panic_dump_in_progress
) goto panic_dump_exit
;
1527 printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
1528 destination_mac
.ether_addr_octet
[0] & 0xff,
1529 destination_mac
.ether_addr_octet
[1] & 0xff,
1530 destination_mac
.ether_addr_octet
[2] & 0xff,
1531 destination_mac
.ether_addr_octet
[3] & 0xff,
1532 destination_mac
.ether_addr_octet
[4] & 0xff,
1533 destination_mac
.ether_addr_octet
[5] & 0xff);
1535 printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map
));
1536 printf("Sending write request for %s\n", corename
);
1538 if ((panic_error
= kdp_send_crashdump_pkt(KDP_WRQ
, corename
, 0 , NULL
)) < 0) {
1539 printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error
);
1540 goto panic_dump_exit
;
1543 /* Just the panic log requested */
1544 if ((panicstr
!= (char *) 0) && (kdp_flag
& PANIC_LOG_DUMP
)) {
1545 printf("Transmitting panic log, please wait: ");
1546 kdp_send_crashdump_data(KDP_DATA
, corename
, (debug_buf_ptr
- debug_buf
), debug_buf
);
1547 kdp_send_crashdump_pkt (KDP_EOF
, NULL
, 0, ((void *) 0));
1548 printf("Please file a bug report on this panic, if possible.\n");
1549 goto panic_dump_exit
;
1552 /* We want a core dump if we're here */
1555 abort_panic_transfer();
1563 abort_panic_transfer(void)
1565 flag_panic_dump_in_progress
= FALSE
;
1570 #if CONFIG_SERIAL_KDP
1572 static boolean_t needs_serial_init
= TRUE
;
1575 kdp_serial_send(void *rpkt
, unsigned int rpkt_len
)
1577 if (needs_serial_init
)
1580 needs_serial_init
= FALSE
;
1584 kdp_serialize_packet((unsigned char *)rpkt
, rpkt_len
, serial_putc
);
1588 kdp_serial_receive(void *rpkt
, unsigned int *rpkt_len
, unsigned int timeout
)
1591 uint64_t now
, deadline
;
1593 if (needs_serial_init
)
1596 needs_serial_init
= FALSE
;
1599 clock_interval_to_deadline(timeout
, 1000 * 1000 /* milliseconds */, &deadline
);
1602 for(clock_get_uptime(&now
); now
< deadline
; clock_get_uptime(&now
))
1604 readkar
= serial_getc();
1607 unsigned char *packet
;
1608 // printf("got char %02x\n", readkar);
1609 if((packet
= kdp_unserialize_packet(readkar
,rpkt_len
)))
1611 memcpy(rpkt
, packet
, *rpkt_len
);
1619 static void kdp_serial_callout(__unused
void *arg
, kdp_event_t event
)
1621 /* When we stop KDP, set the bit to re-initialize the console serial port
1622 * the next time we send/receive a KDP packet. We don't do it on
1623 * KDP_EVENT_ENTER directly because it also gets called when we trap to KDP
1624 * for non-external debugging, i.e., stackshot or core dumps.
1626 * Set needs_serial_init on exit (and initialization, see above) and not
1627 * enter because enter is sent multiple times and causes excess reinitialization.
1632 case KDP_EVENT_PANICLOG
:
1633 case KDP_EVENT_ENTER
:
1635 case KDP_EVENT_EXIT
:
1636 needs_serial_init
= TRUE
;
1641 #endif /* CONFIG_SERIAL_KDP */
1646 #if CONFIG_SERIAL_KDP
1648 struct in_addr ipaddr
;
1649 struct ether_addr macaddr
;
1652 //serial will be the debugger, unless match name is explicitly provided, and it's not "serial"
1653 if(PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
)) && strncmp(kdpname
, "serial", sizeof(kdpname
)) != 0)
1656 // serial must be explicitly requested
1657 if(!PE_parse_boot_argn("kdp_match_name", kdpname
, sizeof(kdpname
)) || strncmp(kdpname
, "serial", sizeof(kdpname
)) != 0)
1661 kprintf("Intializing serial KDP\n");
1663 kdp_register_callout(kdp_serial_callout
, NULL
);
1664 kdp_register_send_receive(kdp_serial_send
, kdp_serial_receive
);
1666 /* fake up an ip and mac for early serial debugging */
1667 macaddr
.ether_addr_octet
[0] = 's';
1668 macaddr
.ether_addr_octet
[1] = 'e';
1669 macaddr
.ether_addr_octet
[2] = 'r';
1670 macaddr
.ether_addr_octet
[3] = 'i';
1671 macaddr
.ether_addr_octet
[4] = 'a';
1672 macaddr
.ether_addr_octet
[5] = 'l';
1673 ipaddr
.s_addr
= 0xABADBABE;
1674 kdp_set_ip_and_mac_addresses(&ipaddr
, &macaddr
);
1675 #endif /* CONFIG_SERIAL_KDP */