+/* Horrid hack to extract xnu version if possible - a much cleaner approach
+ * would be to have the integrator run a script which would copy the
+ * xnu version into a string or an int somewhere at project submission
+ * time - makes assumptions about sizeof(version), but will not fail if
+ * it changes, but may be incorrect.
+ */
+/* 2006: Incorporated a change from Darwin user P. Lovell to extract
+ * the minor kernel version numbers from the version string.
+ */
+static int
+kdp_get_xnu_version(char *versionbuf)
+{
+ char *versionpos;
+ char vstr[20];
+ int retval = -1;
+ char *vptr;
+
+ strlcpy(vstr, "custom", 10);
+ if (kdp_machine_vm_read((mach_vm_address_t)(uintptr_t)version, versionbuf, 128)) {
+ versionbuf[127] = '\0';
+ versionpos = strnstr(versionbuf, "xnu-", 115);
+ if (versionpos) {
+ strncpy(vstr, versionpos, sizeof(vstr));
+ vstr[sizeof(vstr)-1] = '\0';
+ vptr = vstr + 4; /* Begin after "xnu-" */
+ while (*vptr && (isdigit(*vptr) || *vptr == '.'))
+ vptr++;
+ *vptr = '\0';
+ /* Remove trailing period, if any */
+ if (*(--vptr) == '.')
+ *vptr = '\0';
+ retval = 0;
+ }
+ }
+ strlcpy(versionbuf, vstr, KDP_MAXPACKET);
+ return retval;
+}
+
+void
+kdp_set_dump_info(const uint32_t flags, const char *filename,
+ const char *destipstr, const char *routeripstr,
+ const uint32_t port)
+{
+ uint32_t cmd;
+
+ if (destipstr && (destipstr[0] != '\0')) {
+ strlcpy(panicd_ip_str, destipstr, sizeof(panicd_ip_str));
+ panicd_specified = 1;
+ }
+
+ if (routeripstr && (routeripstr[0] != '\0')) {
+ strlcpy(router_ip_str, routeripstr, sizeof(router_ip_str));
+ router_specified = 1;
+ }
+
+ if (filename && (filename[0] != '\0')) {
+ strlcpy(corename_str, filename, sizeof(corename_str));
+ corename_specified = TRUE;
+ } else {
+ corename_specified = FALSE;
+ }
+
+ if (port)
+ panicd_port = port;
+
+ /* on a disconnect, should we stay in KDP or not? */
+ noresume_on_disconnect = (flags & KDP_DUMPINFO_NORESUME) ? 1 : 0;
+
+ if ((flags & KDP_DUMPINFO_DUMP) == 0)
+ return;
+
+ /* the rest of the commands can modify kdp_flags */
+ cmd = flags & KDP_DUMPINFO_MASK;
+ if (cmd == KDP_DUMPINFO_DISABLE) {
+ kdp_flag &= ~KDP_PANIC_DUMP_ENABLED;
+ panicd_specified = 0;
+ kdp_trigger_core_dump = 0;
+ return;
+ }
+
+ kdp_flag &= ~REBOOT_POST_CORE;
+ if (flags & KDP_DUMPINFO_REBOOT)
+ kdp_flag |= REBOOT_POST_CORE;
+
+ kdp_flag &= ~PANIC_LOG_DUMP;
+ if (cmd == KDP_DUMPINFO_PANICLOG)
+ kdp_flag |= PANIC_LOG_DUMP;
+
+ kdp_flag &= ~SYSTEM_LOG_DUMP;
+ if (cmd == KDP_DUMPINFO_SYSTEMLOG)
+ kdp_flag |= SYSTEM_LOG_DUMP;
+
+ /* trigger a dump */
+ kdp_flag |= DBG_POST_CORE;
+
+ flag_dont_abort_panic_dump = (flags & KDP_DUMPINFO_NOINTR) ?
+ TRUE : FALSE;
+
+ reattach_wait = 1;
+ logPanicDataToScreen = 1;
+ disableConsoleOutput = 0;
+ disable_debug_output = 0;
+ kdp_trigger_core_dump = 1;
+}
+
+void
+kdp_get_dump_info(kdp_dumpinfo_reply_t *rp)
+{
+ if (rp->destip) {
+ if (panicd_specified)
+ strlcpy(rp->destip, panicd_ip_str,
+ sizeof(panicd_ip_str));
+ else
+ rp->destip[0] = '\0';
+ }
+
+ if (rp->routerip) {
+ if (router_specified)
+ strlcpy(rp->routerip, router_ip_str,
+ sizeof(router_ip_str));
+ else
+ rp->routerip[0] = '\0';
+ }
+
+ if (rp->name) {
+ if (corename_specified)
+ strlcpy(rp->name, corename_str,
+ sizeof(corename_str));
+ else
+ rp->name[0] = '\0';
+
+ }
+
+ rp->port = panicd_port;
+
+ rp->type = 0;
+ if (!panicd_specified)
+ rp->type |= KDP_DUMPINFO_DISABLE;
+ else if (kdp_flag & PANIC_LOG_DUMP)
+ rp->type |= KDP_DUMPINFO_PANICLOG;
+ else
+ rp->type |= KDP_DUMPINFO_CORE;
+
+ if (noresume_on_disconnect)
+ rp->type |= KDP_DUMPINFO_NORESUME;
+}
+
+
+/* Primary dispatch routine for the system dump */
+void
+kdp_panic_dump(void)
+{
+ char coreprefix[10];
+ char coresuffix[4];
+ int panic_error;
+
+ uint64_t abstime;
+ uint32_t current_ip = ntohl((uint32_t)kdp_current_ip_address);
+
+ if (flag_panic_dump_in_progress) {
+ kdb_printf("System dump aborted.\n");
+ goto panic_dump_exit;
+ }
+
+ printf("Entering system dump routine\n");
+
+ /* try a local disk dump */
+ if (kdp_has_polled_corefile()) {
+ flag_panic_dump_in_progress = TRUE;
+ kern_dump(KERN_DUMP_DISK);
+ abort_panic_transfer();
+ }
+
+ if (!strcmp("local", panicd_ip_str)) return; /* disk only request */
+
+ if (!kdp_en_recv_pkt || !kdp_en_send_pkt) {
+ if (!kdp_has_polled_corefile()) {
+ kdb_printf("Error: No transport device registered for kernel crashdump\n");
+ }
+ return;
+ }
+
+ if (!panicd_specified) {
+ if (!kdp_has_polled_corefile()) {
+ kdb_printf("A dump server was not specified in the boot-args, terminating kernel core dump.\n");
+ }
+ goto panic_dump_exit;
+ }
+
+ flag_panic_dump_in_progress = TRUE;
+
+ if (pkt.input)
+ kdp_panic("kdp_panic_dump: unexpected pending input packet");
+
+ kdp_get_xnu_version((char *) &pkt.data[0]);
+
+ if (!corename_specified) {
+ coresuffix[0] = 0;
+ /* Panic log bit takes precedence over core dump bit */
+ if ((panicstr != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP))
+ strlcpy(coreprefix, "paniclog", sizeof(coreprefix));
+ else if (kdp_flag & SYSTEM_LOG_DUMP)
+ strlcpy(coreprefix, "systemlog", sizeof(coreprefix));
+ else {
+ strlcpy(coreprefix, "core", sizeof(coreprefix));
+ if (!kdp_corezip_disabled) strlcpy(coresuffix, ".gz", sizeof(coresuffix));
+ }
+
+ abstime = mach_absolute_time();
+ pkt.data[20] = '\0';
+ snprintf (corename_str,
+ sizeof(corename_str),
+ "%s-%s-%d.%d.%d.%d-%x%s",
+ coreprefix, &pkt.data[0],
+ (current_ip & 0xff000000) >> 24,
+ (current_ip & 0xff0000) >> 16,
+ (current_ip & 0xff00) >> 8,
+ (current_ip & 0xff),
+ (unsigned int) (abstime & 0xffffffff),
+ coresuffix);
+ }
+
+ if (0 == inet_aton(panicd_ip_str, (struct kdp_in_addr *) &panic_server_ip)) {
+ kdb_printf("inet_aton() failed interpreting %s as a panic server IP\n", panicd_ip_str);
+ } else {
+ kdb_printf("Attempting connection to panic server configured at IP %s, port %d\n", panicd_ip_str, panicd_port);
+ }
+
+ destination_mac = router_mac;
+
+ if (kdp_arp_resolve(panic_server_ip, &temp_mac)) {
+ kdb_printf("Resolved %s's (or proxy's) link level address\n", panicd_ip_str);
+ destination_mac = temp_mac;
+ } else {
+ if (!flag_panic_dump_in_progress) goto panic_dump_exit;
+ if (router_specified) {
+ if (0 == inet_aton(router_ip_str, (struct kdp_in_addr *) &parsed_router_ip)) {
+ kdb_printf("inet_aton() failed interpreting %s as an IP\n", router_ip_str);
+ } else {
+ router_ip = parsed_router_ip;
+ if (kdp_arp_resolve(router_ip, &temp_mac)) {
+ destination_mac = temp_mac;
+ kdb_printf("Routing through specified router IP %s (%d)\n", router_ip_str, router_ip);
+ }
+ }
+ }
+ }
+
+ if (!flag_panic_dump_in_progress) goto panic_dump_exit;
+
+ kdb_printf("Transmitting packets to link level address: %02x:%02x:%02x:%02x:%02x:%02x\n",
+ destination_mac.ether_addr_octet[0] & 0xff,
+ destination_mac.ether_addr_octet[1] & 0xff,
+ destination_mac.ether_addr_octet[2] & 0xff,
+ destination_mac.ether_addr_octet[3] & 0xff,
+ destination_mac.ether_addr_octet[4] & 0xff,
+ destination_mac.ether_addr_octet[5] & 0xff);
+
+ kdb_printf("Kernel map size is %llu\n", (unsigned long long) get_vmmap_size(kernel_map));
+ kdb_printf("Sending write request for %s\n", corename_str);
+
+ if ((panic_error = kdp_send_crashdump_pkt(KDP_WRQ, corename_str, 0 , NULL)) < 0) {
+ kdb_printf ("kdp_send_crashdump_pkt failed with error %d\n", panic_error);
+ goto panic_dump_exit;
+ }
+
+ /* Just the panic log requested */
+ if ((panicstr != (char *) 0) && (kdp_flag & PANIC_LOG_DUMP)) {
+ kdb_printf_unbuffered("Transmitting panic log, please wait: ");
+ kdp_send_crashdump_data(KDP_DATA, corename_str,
+ debug_buf_ptr - debug_buf_addr,
+ debug_buf_addr);
+ kdp_send_crashdump_pkt (KDP_EOF, NULL, 0, ((void *) 0));
+ printf("Please file a bug report on this panic, if possible.\n");
+ goto panic_dump_exit;
+ }
+
+ /* maybe we wanted the systemlog */
+ if (kdp_flag & SYSTEM_LOG_DUMP) {
+ long start_off = msgbufp->msg_bufx;
+ long len;
+
+ kdb_printf_unbuffered("Transmitting system log, please wait: ");
+ if (start_off >= msgbufp->msg_bufr) {
+ len = msgbufp->msg_size - start_off;
+ kdp_send_crashdump_data(KDP_DATA, corename_str, len,
+ msgbufp->msg_bufc + start_off);
+ /* seek to remove trailing bytes */
+ kdp_send_crashdump_seek(corename_str, len);
+ start_off = 0;
+ }
+
+ if (start_off != msgbufp->msg_bufr) {
+ len = msgbufp->msg_bufr - start_off;
+ kdp_send_crashdump_data(KDP_DATA, corename_str, len,
+ msgbufp->msg_bufc + start_off);
+ }
+
+ kdp_send_crashdump_pkt (KDP_EOF, NULL, 0, ((void *) 0));
+ goto panic_dump_exit;
+ }
+
+ /* We want a core dump if we're here */
+ kern_dump(KERN_DUMP_NET);
+
+panic_dump_exit:
+ abort_panic_transfer();