From: Vladimir 'phcoder' Serbinenko Date: Sat, 18 Feb 2012 19:59:42 +0000 (+0100) Subject: merge backtrace into gdb X-Git-Tag: 2.00~556^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc400bfd74b6e70c20ba7bedc33d5490c6e71498;p=thirdparty%2Fgrub.git merge backtrace into gdb --- fc400bfd74b6e70c20ba7bedc33d5490c6e71498 diff --cc grub-core/Makefile.core.def index c19381e62,cc1d4702d..49651345a --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@@ -1709,6 -1709,13 +1709,12 @@@ module = common = commands/testload.c; }; + module = { + name = backtrace; + common = lib/i386/backtrace.c; - common = lib/i386/backtrace_int.S; + enable = x86; + }; + module = { name = lsapm; common = commands/i386/pc/lsapm.c; diff --cc grub-core/gdb/cstub.c index a13c8220f,000000000..b7dd2200b mode 100644,000000..100644 --- a/grub-core/gdb/cstub.c +++ b/grub-core/gdb/cstub.c @@@ -1,357 -1,0 +1,366 @@@ +/* cstub.c - machine independent portion of remote GDB stub */ +/* + * Copyright (C) 2006 Lubomir Kundrak + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include + +static const char hexchars[] = "0123456789abcdef"; +int grub_gdb_regs[GRUB_MACHINE_NR_REGS]; + +#define GRUB_GDB_COMBUF_SIZE 400 /* At least sizeof(grub_gdb_regs)*2 are needed for + register packets. */ +static char grub_gdb_inbuf[GRUB_GDB_COMBUF_SIZE]; +static char grub_gdb_outbuf[GRUB_GDB_COMBUF_SIZE]; + +struct grub_serial_port *grub_gdb_port; + +static int +hex (char ch) +{ + if ((ch >= 'a') && (ch <= 'f')) + return (ch - 'a' + 10); + if ((ch >= '0') && (ch <= '9')) + return (ch - '0'); + if ((ch >= 'A') && (ch <= 'F')) + return (ch - 'A' + 10); + return (-1); +} + +/* Scan for the sequence $#. */ +static char * +grub_gdb_getpacket (void) +{ + char *buffer = &grub_gdb_inbuf[0]; + unsigned char checksum; + unsigned char xmitcsum; + int count; + int ch; + + while (1) + { + /* Wait around for the start character, ignore all other + characters. */ + while ((ch = grub_serial_port_fetch (grub_gdb_port)) != '$'); + + retry: + checksum = 0; + xmitcsum = -1; + count = 0; + + /* Now read until a # or end of buffer is found. */ + while (count < GRUB_GDB_COMBUF_SIZE) + { + do + ch = grub_serial_port_fetch (grub_gdb_port); + while (ch < 0); + if (ch == '$') + goto retry; + if (ch == '#') + break; + checksum += ch; + buffer[count] = ch; + count = count + 1; + } + buffer[count] = 0; + if (ch == '#') + { + do + ch = grub_serial_port_fetch (grub_gdb_port); + while (ch < 0); + xmitcsum = hex (ch) << 4; + do + ch = grub_serial_port_fetch (grub_gdb_port); + while (ch < 0); + xmitcsum += hex (ch); + + if (checksum != xmitcsum) + grub_serial_port_put (grub_gdb_port, '-'); /* Failed checksum. */ + else + { + grub_serial_port_put (grub_gdb_port, '+'); /* Successful transfer. */ + + /* If a sequence char is present, reply the sequence ID. */ + if (buffer[2] == ':') + { + grub_serial_port_put (grub_gdb_port, buffer[0]); + grub_serial_port_put (grub_gdb_port, buffer[1]); + + return &buffer[3]; + } + return &buffer[0]; + } + } + } +} + +/* Send the packet in buffer. */ +static void +grub_gdb_putpacket (char *buffer) +{ + grub_uint8_t checksum; + + /* $#. */ + do + { + char *ptr; + grub_serial_port_put (grub_gdb_port, '$'); + checksum = 0; + + for (ptr = buffer; *ptr; ptr++) + { + grub_serial_port_put (grub_gdb_port, *ptr); + checksum += *ptr; + } + + grub_serial_port_put (grub_gdb_port, '#'); + grub_serial_port_put (grub_gdb_port, hexchars[checksum >> 4]); + grub_serial_port_put (grub_gdb_port, hexchars[checksum & 0xf]); + } + while (grub_serial_port_fetch (grub_gdb_port) != '+'); +} + +/* Convert the memory pointed to by mem into hex, placing result in buf. + Return a pointer to the last char put in buf (NULL). */ +static char * +grub_gdb_mem2hex (char *mem, char *buf, int count) +{ + int i; + unsigned char ch; + + for (i = 0; i < count; i++) + { + ch = *mem++; + *buf++ = hexchars[ch >> 4]; + *buf++ = hexchars[ch % 16]; + } + *buf = 0; + return (buf); +} + +/* Convert the hex array pointed to by buf into binary to be placed in mem. + Return a pointer to the character after the last byte written. */ +static char * +grub_gdb_hex2mem (char *buf, char *mem, int count) +{ + int i; + unsigned char ch; + + for (i = 0; i < count; i++) + { + ch = hex (*buf++) << 4; + ch = ch + hex (*buf++); + *mem++ = ch; + } + return (mem); +} + +/* Convert hex characters to int and return the number of characters + processed. */ +static int +grub_gdb_hex2int (char **ptr, int *int_value) +{ + int num_chars = 0; + int hex_value; + + *int_value = 0; + + while (**ptr) + { + hex_value = hex (**ptr); + if (hex_value >= 0) + { + *int_value = (*int_value << 4) | hex_value; + num_chars++; + } + else + break; + + (*ptr)++; + } + + return (num_chars); +} + +/* This function does all command procesing for interfacing to gdb. */ +void +grub_gdb_trap (int trap_no) +{ + int sig_no; + int stepping; + int addr; + int length; + char *ptr; + int newPC; + ++ if (!grub_gdb_port) ++ { ++ grub_printf ("Unhandled exception 0x%x at ", trap_no); ++ grub_backtrace_print_address (grub_gdb_regs[PC]); ++ grub_printf ("\n"); ++ grub_backtrace_pointer (grub_gdb_regs[EBP]); ++ grub_abort (); ++ } ++ + sig_no = grub_gdb_trap2sig (trap_no); + + ptr = grub_gdb_outbuf; + + /* Reply to host that an exception has occurred. */ + + *ptr++ = 'T'; /* Notify gdb with signo, PC, FP and SP. */ + + *ptr++ = hexchars[sig_no >> 4]; + *ptr++ = hexchars[sig_no & 0xf]; + + /* Stack pointer. */ + *ptr++ = hexchars[SP]; + *ptr++ = ':'; + ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[ESP], ptr, 4); + *ptr++ = ';'; + + /* Frame pointer. */ + *ptr++ = hexchars[FP]; + *ptr++ = ':'; + ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[EBP], ptr, 4); + *ptr++ = ';'; + + /* Program counter. */ + *ptr++ = hexchars[PC]; + *ptr++ = ':'; + ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[PC], ptr, 4); + *ptr++ = ';'; + + *ptr = '\0'; + + grub_gdb_putpacket (grub_gdb_outbuf); + + stepping = 0; + + while (1) + { + grub_gdb_outbuf[0] = 0; + ptr = grub_gdb_getpacket (); + + switch (*ptr++) + { + case '?': + grub_gdb_outbuf[0] = 'S'; + grub_gdb_outbuf[1] = hexchars[sig_no >> 4]; + grub_gdb_outbuf[2] = hexchars[sig_no % 16]; + grub_gdb_outbuf[3] = 0; + break; + + /* Return values of the CPU registers. */ + case 'g': + grub_gdb_mem2hex ((char *) grub_gdb_regs, grub_gdb_outbuf, + sizeof (grub_gdb_regs)); + break; + + /* Set values of the CPU registers -- return OK. */ + case 'G': + grub_gdb_hex2mem (ptr, (char *) grub_gdb_regs, + sizeof (grub_gdb_regs)); + grub_strcpy (grub_gdb_outbuf, "OK"); + break; + + /* Set the value of a single CPU register -- return OK. */ + case 'P': + { + int regno; + + if (grub_gdb_hex2int (&ptr, ®no) && *ptr++ == '=') + if (regno >= 0 && regno < GRUB_MACHINE_NR_REGS) + { + grub_gdb_hex2mem (ptr, (char *) &grub_gdb_regs[regno], 4); + grub_strcpy (grub_gdb_outbuf, "OK"); + break; + } + /* FIXME: GDB requires setting orig_eax. I don't know what's + this register is about. For now just simulate setting any + registers. */ + grub_strcpy (grub_gdb_outbuf, /*"E01"*/ "OK"); + break; + } + + /* mAA..AA,LLLL: Read LLLL bytes at address AA..AA. */ + case 'm': + /* Try to read %x,%x. Set ptr = 0 if successful. */ + if (grub_gdb_hex2int (&ptr, &addr)) + if (*(ptr++) == ',') + if (grub_gdb_hex2int (&ptr, &length)) + { + ptr = 0; + grub_gdb_mem2hex ((char *) addr, grub_gdb_outbuf, length); + } + if (ptr) + grub_strcpy (grub_gdb_outbuf, "E01"); + break; + + /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA -- return OK. */ + case 'M': + /* Try to read %x,%x. Set ptr = 0 if successful. */ + if (grub_gdb_hex2int (&ptr, &addr)) + if (*(ptr++) == ',') + if (grub_gdb_hex2int (&ptr, &length)) + if (*(ptr++) == ':') + { + grub_gdb_hex2mem (ptr, (char *) addr, length); + grub_strcpy (grub_gdb_outbuf, "OK"); + ptr = 0; + } + if (ptr) + { + grub_strcpy (grub_gdb_outbuf, "E02"); + } + break; + + /* sAA..AA: Step one instruction from AA..AA(optional). */ + case 's': + stepping = 1; + + /* cAA..AA: Continue at address AA..AA(optional). */ + case 'c': + /* try to read optional parameter, pc unchanged if no parm */ + if (grub_gdb_hex2int (&ptr, &addr)) + grub_gdb_regs[PC] = addr; + + newPC = grub_gdb_regs[PC]; + + /* Clear the trace bit. */ + grub_gdb_regs[PS] &= 0xfffffeff; + + /* Set the trace bit if we're stepping. */ + if (stepping) + grub_gdb_regs[PS] |= 0x100; + + return; + + /* Kill the program. */ + case 'k': + /* Do nothing. */ + return; + } + + /* Reply to the request. */ + grub_gdb_putpacket (grub_gdb_outbuf); + } +} + diff --cc grub-core/lib/i386/backtrace.c index 000000000,42b9f9e4c..27f82a85e mode 000000,100644..100644 --- a/grub-core/lib/i386/backtrace.c +++ b/grub-core/lib/i386/backtrace.c @@@ -1,0 -1,186 +1,110 @@@ + /* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + + #include + #include + #include + #include + #include + #include + #include + + #define MAX_STACK_FRAME 102400 + + GRUB_MOD_LICENSE ("GPLv3+"); + -struct idt_descriptor -{ - grub_uint16_t limit; - void *base; -} __attribute__ ((packed)); - -#define GRUB_IDT_ENTRY_TYPE_PRESENT 0x80 -#define GRUB_IDT_ENTRY_TYPE_NOT_PRESENT 0x00 -#define GRUB_IDT_ENTRY_TYPE_RING0 0x00 -#define GRUB_IDT_ENTRY_TYPE_TRAP32 0x0f - -struct idt_entry -{ - grub_uint16_t addr_low; - grub_uint16_t segment; - grub_uint8_t unused; - grub_uint8_t type; - grub_uint16_t addr_high; -} __attribute__ ((packed)); - -void grub_interrupt_handler_real (void *ret, void *ebp); - -static void -print_address (void *addr) ++void ++grub_backtrace_print_address (void *addr) + { + grub_dl_t mod; + + FOR_DL_MODULES (mod) + { + grub_dl_segment_t segment; + for (segment = mod->segment; segment; segment = segment->next) + if (segment->addr <= addr && (grub_uint8_t *) segment->addr + + segment->size > (grub_uint8_t *) addr) + { + grub_printf ("%s.%x+%" PRIxGRUB_SIZE, mod->name, segment->section, + (grub_uint8_t *) addr - (grub_uint8_t *) segment->addr); + return; + } + } + + grub_printf ("%p", addr); + } + + void + grub_backtrace_pointer (void *ebp) + { + void *ptr, *nptr; + unsigned i; + + ptr = ebp; + while (1) + { + grub_printf ("%p: ", ptr); - print_address (((void **) ptr)[1]); ++ grub_backtrace_print_address (((void **) ptr)[1]); + grub_printf (" ("); + for (i = 0; i < 2; i++) + grub_printf ("%p,", ((void **)ptr) [i + 2]); + grub_printf ("%p)\n", ((void **)ptr) [i + 2]); + nptr = *(void **)ptr; + if (nptr < ptr || (void **) nptr - (void **) ptr > MAX_STACK_FRAME + || nptr == ptr) + { + grub_printf ("Invalid stack frame at %p (%p)\n", ptr, nptr); + break; + } + ptr = nptr; + } + } + -void -grub_interrupt_handler_real (void *ret, void *ebp) -{ - grub_printf ("Unhandled exception at "); - print_address (ret); - grub_printf ("\n"); - grub_backtrace_pointer (ebp); - grub_abort (); -} - - + void + grub_backtrace (void) + { + #ifdef __x86_64__ + asm volatile ("movq %rbp, %rdi\n" + "call grub_backtrace_pointer"); + #else + asm volatile ("movl %ebp, %eax\n" + "call grub_backtrace_pointer"); + #endif + } + + static grub_err_t + grub_cmd_backtrace (grub_command_t cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) + { + grub_backtrace (); + return 0; + } + -#define NUM_INTERRUPTS 0x20 - -void grub_int_handler (void); - -struct idt_descriptor idt_desc; - -static grub_err_t -setup_interrupts (void) -{ - struct idt_entry *idt_table; - unsigned long i; - grub_uint16_t seg; - idt_table = grub_memalign (0x10, NUM_INTERRUPTS * sizeof (struct idt_entry)); - if (!idt_table) - return grub_errno; - idt_desc.base = idt_table; - idt_desc.limit = NUM_INTERRUPTS; - - asm volatile ("xorl %%eax, %%eax\n" - "mov %%cs, %%ax\n" :"=a" (seg)); - - for (i = 0; i < NUM_INTERRUPTS; i++) - { - idt_table[i].addr_low = ((grub_addr_t) grub_int_handler) & 0xffff; - idt_table[i].addr_high = ((grub_addr_t) grub_int_handler) >> 16; - idt_table[i].unused = 0; - idt_table[i].segment = seg; - idt_table[i].type = GRUB_IDT_ENTRY_TYPE_PRESENT - | GRUB_IDT_ENTRY_TYPE_RING0 | GRUB_IDT_ENTRY_TYPE_TRAP32; - } - - asm volatile ("lidt %0" : : "m" (idt_desc)); - - return GRUB_ERR_NONE; -} - + static grub_command_t cmd; + + GRUB_MOD_INIT(backtrace) + { - grub_err_t err; - err = setup_interrupts(); - if (err) - { - grub_print_error (); - grub_errno = GRUB_ERR_NONE; - } + cmd = grub_register_command ("backtrace", grub_cmd_backtrace, + 0, "Print backtrace."); + } + + GRUB_MOD_FINI(backtrace) + { + grub_unregister_command (cmd); + }