1 /* 2 * Copyright 2016 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <string.h> 8 9 #include <boot/platform.h> 10 #include <boot/stage2.h> 11 #include <boot/stdio.h> 12 13 #include "debug.h" 14 #include "efi_platform.h" 15 #include "serial.h" 16 17 18 static char sBuffer[16384]; 19 static uint32 sBufferPosition; 20 21 22 static void 23 syslog_write(const char* buffer, size_t length) 24 { 25 if (sBufferPosition + length > sizeof(sBuffer)) 26 return; 27 memcpy(sBuffer + sBufferPosition, buffer, length); 28 sBufferPosition += length; 29 } 30 31 32 static void 33 dprintf_args(const char *format, va_list args) 34 { 35 char buffer[512]; 36 int length = vsnprintf(buffer, sizeof(buffer), format, args); 37 if (length == 0) 38 return; 39 40 syslog_write(buffer, length); 41 serial_puts(buffer, length); 42 } 43 44 45 extern "C" void 46 dprintf(const char *format, ...) 47 { 48 va_list args; 49 50 va_start(args, format); 51 dprintf_args(format, args); 52 va_end(args); 53 } 54 55 56 extern "C" void 57 panic(const char *format, ...) 58 { 59 va_list args; 60 61 platform_switch_to_text_mode(); 62 63 puts("*** PANIC ***"); 64 65 va_start(args, format); 66 vprintf(format, args); 67 va_end(args); 68 69 while (true) 70 kBootServices->Stall(1000000); 71 } 72 73 74 void 75 debug_cleanup(void) 76 { 77 gKernelArgs.keep_debug_output_buffer = false; 78 gKernelArgs.debug_output = kernel_args_malloc(sBufferPosition); 79 if (gKernelArgs.debug_output != NULL) { 80 memcpy(gKernelArgs.debug_output, sBuffer, sBufferPosition); 81 gKernelArgs.debug_size = sBufferPosition; 82 } 83 } 84 85 86 char* 87 platform_debug_get_log_buffer(size_t *_size) 88 { 89 if (_size != NULL) 90 *_size = sizeof(sBuffer); 91 92 return sBuffer; 93 } 94