From 2fb24921d889a3caa988905fdd1717139a831de5 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 27 Dec 2009 02:36:38 +0100 Subject: [PATCH 1/5] Support for backtracing exceptions --- conf/i386.rmk | 6 ++ include/grub/backtrace.h | 25 +++++ kern/i386/realmode.S | 12 +++ lib/i386/backtrace.c | 194 +++++++++++++++++++++++++++++++++++++++ lib/i386/backtrace_int.S | 12 +++ 5 files changed, 249 insertions(+) create mode 100644 include/grub/backtrace.h create mode 100644 lib/i386/backtrace.c create mode 100644 lib/i386/backtrace_int.S diff --git a/conf/i386.rmk b/conf/i386.rmk index c3f036d0f..7031adee7 100644 --- a/conf/i386.rmk +++ b/conf/i386.rmk @@ -25,3 +25,9 @@ pkglib_MODULES += ata.mod ata_mod_SOURCES = disk/ata.c ata_mod_CFLAGS = $(COMMON_CFLAGS) ata_mod_LDFLAGS = $(COMMON_LDFLAGS) + +pkglib_MODULES += backtrace.mod +backtrace_mod_SOURCES = lib/i386/backtrace.c lib/i386/backtrace_int.S +backtrace_mod_CFLAGS = $(COMMON_CFLAGS) +backtrace_mod_ASFLAGS = $(COMMON_ASFLAGS) +backtrace_mod_LDFLAGS = $(COMMON_LDFLAGS) diff --git a/include/grub/backtrace.h b/include/grub/backtrace.h new file mode 100644 index 000000000..a0503b8b7 --- /dev/null +++ b/include/grub/backtrace.h @@ -0,0 +1,25 @@ +/* + * 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 . + */ + +#ifndef GRUB_BACKTRACE_HEADER +#define GRUB_BACKTRACE_HEADER 1 + +void grub_backtrace (void); +void grub_backtrace_pointer (void *ptr); + +#endif diff --git a/kern/i386/realmode.S b/kern/i386/realmode.S index a74eb1217..2a371c581 100644 --- a/kern/i386/realmode.S +++ b/kern/i386/realmode.S @@ -108,6 +108,12 @@ gdt: gdtdesc: .word 0x27 /* limit */ .long gdt /* addr */ +realidt: + .word 0 + .long 0 +protidt: + .word 0 + .long 0 /* * These next two routines, "real_to_prot" and "prot_to_real" are structured @@ -159,6 +165,9 @@ protcseg: /* zero %eax */ xorl %eax, %eax + sidt realidt + lidt protidt + /* return on the old (or initialized) stack! */ ret @@ -166,6 +175,9 @@ prot_to_real: /* just in case, set GDT */ lgdt gdtdesc + sidt protidt + lidt realidt + /* save the protected mode stack */ movl %esp, %eax movl %eax, protstack diff --git a/lib/i386/backtrace.c b/lib/i386/backtrace.c new file mode 100644 index 000000000..06fb6c54c --- /dev/null +++ b/lib/i386/backtrace.c @@ -0,0 +1,194 @@ +/* + * 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 + +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 +print_address (void *addr) +{ + const char *name; + int section; + grub_off_t off; + auto int hook (grub_dl_t mod); + int hook (grub_dl_t 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) + { + name = mod->name; + section = segment->section; + off = (grub_uint8_t *) addr - (grub_uint8_t *) segment->addr; + return 1; + } + return 0; + } + + name = NULL; + grub_dl_iterate (hook); + if (name) + grub_printf ("%s.%x+%lx", name, section, (unsigned long) off); + else + 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 + sizeof (void *))); + grub_printf (" ("); + for (i = 0; i < 2; i++) + grub_printf ("%p,", *(void **) + (ptr + (i + 2) * sizeof (void *))); + grub_printf ("%p", *(void **) + (ptr + (i + 2) * sizeof (void *))); + grub_printf (")\n"); + 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); +} diff --git a/lib/i386/backtrace_int.S b/lib/i386/backtrace_int.S new file mode 100644 index 000000000..a51fc5ee4 --- /dev/null +++ b/lib/i386/backtrace_int.S @@ -0,0 +1,12 @@ +#include + + .text + .p2align 4 + +FUNCTION(grub_int_handler) + /* FIXME: push this only when CPU pushes no error on stack. */ + push $0 + pusha + movl 36(%esp), %eax + movl %ebp, %edx + call EXT_C(grub_interrupt_handler_real) From 5dfadcb34b448e9b767f300f8d7e34b8aaf3d23b Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Wed, 30 Dec 2009 18:11:43 +0100 Subject: [PATCH 2/5] Fix warnings --- lib/i386/backtrace.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/i386/backtrace.c b/lib/i386/backtrace.c index 06fb6c54c..144b1da97 100644 --- a/lib/i386/backtrace.c +++ b/lib/i386/backtrace.c @@ -46,7 +46,9 @@ struct idt_entry grub_uint16_t addr_high; } __attribute__ ((packed)); -void +void grub_interrupt_handler_real (void *ret, void *ebp); + +static void print_address (void *addr) { const char *name; @@ -86,14 +88,11 @@ grub_backtrace_pointer (void *ebp) while (1) { grub_printf ("%p: ", ptr); - print_address (*(void **) (ptr + sizeof (void *))); + print_address (((void **) ptr)[1]); grub_printf (" ("); for (i = 0; i < 2; i++) - grub_printf ("%p,", *(void **) - (ptr + (i + 2) * sizeof (void *))); - grub_printf ("%p", *(void **) - (ptr + (i + 2) * sizeof (void *))); - grub_printf (")\n"); + 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) From c1677cfccf45652833f4906d3361b037acfa2a91 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 14 Nov 2011 14:27:52 +0100 Subject: [PATCH 3/5] Fix a mismerge --- grub-core/Makefile.core.def | 5 +++-- grub-core/lib/i386/backtrace.c | 25 +++++++++---------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 003041ff6..cc1d4702d 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1711,8 +1711,9 @@ module = { module = { name = backtrace; - common = lib/i386/backtrace.c lib/i386/backtrace_int.S; - enable = i386; + common = lib/i386/backtrace.c; + common = lib/i386/backtrace_int.S; + enable = x86; }; module = { diff --git a/grub-core/lib/i386/backtrace.c b/grub-core/lib/i386/backtrace.c index 144b1da97..42b9f9e4c 100644 --- a/grub-core/lib/i386/backtrace.c +++ b/grub-core/lib/i386/backtrace.c @@ -26,6 +26,8 @@ #define MAX_STACK_FRAME 102400 +GRUB_MOD_LICENSE ("GPLv3+"); + struct idt_descriptor { grub_uint16_t limit; @@ -51,31 +53,22 @@ void grub_interrupt_handler_real (void *ret, void *ebp); static void print_address (void *addr) { - const char *name; - int section; - grub_off_t off; - auto int hook (grub_dl_t mod); - int hook (grub_dl_t mod) + 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) { - name = mod->name; - section = segment->section; - off = (grub_uint8_t *) addr - (grub_uint8_t *) segment->addr; - return 1; + grub_printf ("%s.%x+%" PRIxGRUB_SIZE, mod->name, segment->section, + (grub_uint8_t *) addr - (grub_uint8_t *) segment->addr); + return; } - return 0; } - name = NULL; - grub_dl_iterate (hook); - if (name) - grub_printf ("%s.%x+%lx", name, section, (unsigned long) off); - else - grub_printf ("%p", addr); + grub_printf ("%p", addr); } void From 66d6a7937b5d0c5e99d525693dc2fb75f09f0017 Mon Sep 17 00:00:00 2001 From: Lubomir Kundrak Date: Mon, 14 Nov 2011 22:58:11 +0100 Subject: [PATCH 4/5] gdb over serial by Lubomir Kundrak and cleaned-up/updated by me (phcoder) Also-By: Vladimir Serbinenko --- grub-core/Makefile.core.def | 10 + grub-core/gdb/cstub.c | 357 +++++++++++++++++++++++++++++++++ grub-core/gdb/gdb.c | 90 +++++++++ grub-core/gdb/i386/idt.c | 80 ++++++++ grub-core/gdb/i386/machdep.S | 173 ++++++++++++++++ grub-core/gdb/i386/signal.c | 53 +++++ grub-core/kern/i386/realmode.S | 12 ++ include/grub/gdb.h | 39 ++++ include/grub/i386/gdb.h | 79 ++++++++ 9 files changed, 893 insertions(+) create mode 100644 grub-core/gdb/cstub.c create mode 100644 grub-core/gdb/gdb.c create mode 100644 grub-core/gdb/i386/idt.c create mode 100644 grub-core/gdb/i386/machdep.S create mode 100644 grub-core/gdb/i386/signal.c create mode 100644 include/grub/gdb.h create mode 100644 include/grub/i386/gdb.h diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index cae79b381..c19381e62 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1741,3 +1741,13 @@ module = { name = crc64; common = lib/crc64.c; }; + +module = { + name = gdb; + common = gdb/cstub.c; + common = gdb/gdb.c; + i386 = gdb/i386/idt.c; + i386 = gdb/i386/machdep.S; + i386 = gdb/i386/signal.c; + enable = i386; +}; \ No newline at end of file diff --git a/grub-core/gdb/cstub.c b/grub-core/gdb/cstub.c new file mode 100644 index 000000000..a13c8220f --- /dev/null +++ b/grub-core/gdb/cstub.c @@ -0,0 +1,357 @@ +/* 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; + + 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 --git a/grub-core/gdb/gdb.c b/grub-core/gdb/gdb.c new file mode 100644 index 000000000..5f30109f9 --- /dev/null +++ b/grub-core/gdb/gdb.c @@ -0,0 +1,90 @@ +/* gdb.c - gdb remote stub module */ +/* + * Copyright (C) 2003 Free Software Foundation, Inc. + * 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 3 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 +#include +#include +#include +#include +#include +#include +#include + +GRUB_MOD_LICENSE ("GPLv3+"); + +static grub_err_t +grub_cmd_gdbstub (struct grub_command *cmd __attribute__ ((unused)), + int argc, char **args) +{ + struct grub_serial_port *port; + if (argc < 1) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "port required"); + port = grub_serial_find (args[0]); + if (!port) + return grub_errno; + grub_gdb_port = port; + grub_puts_ (N_("Now connect the remote debugger, please.")); + grub_gdb_breakpoint (); + return 0; +} + +static grub_err_t +grub_cmd_gdbstop (struct grub_command *cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + grub_gdb_port = NULL; + return 0; +} + +static grub_err_t +grub_cmd_gdb_break (struct grub_command *cmd __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + if (!grub_gdb_port) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "No GDB stub is running"); + grub_gdb_breakpoint (); + return 0; +} + +static grub_command_t cmd, cmd_stop, cmd_break; + +GRUB_MOD_INIT (gdb) +{ + grub_gdb_idtinit (); + cmd = grub_register_command ("gdbstub", grub_cmd_gdbstub, + N_("PORT"), N_("Start GDB stub on given port")); + cmd_break = grub_register_command ("gdbstub_break", grub_cmd_gdb_break, + 0, N_("Break into GDB")); + cmd_stop = grub_register_command ("gdbstub_stop", grub_cmd_gdbstop, + 0, N_("Stop GDB stub")); +} + +GRUB_MOD_FINI (gdb) +{ + grub_unregister_command (cmd); + grub_unregister_command (cmd_stop); + grub_gdb_idtrestore (); +} + diff --git a/grub-core/gdb/i386/idt.c b/grub-core/gdb/i386/idt.c new file mode 100644 index 000000000..abdc2cebf --- /dev/null +++ b/grub-core/gdb/i386/idt.c @@ -0,0 +1,80 @@ +/* idt.c - routines for constructing IDT fot the 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 struct grub_cpu_interrupt_gate grub_gdb_idt[GRUB_GDB_LAST_TRAP + 1] + __attribute__ ((aligned(16))); + +/* Sets up a gate descriptor in the IDT table. */ +static void +grub_idt_gate (struct grub_cpu_interrupt_gate *gate, void (*offset) (void), + grub_uint16_t selector, grub_uint8_t type, grub_uint8_t dpl) +{ + gate->offset_lo = (int) offset & 0xffff; + gate->selector = selector & 0xffff; + gate->unused = 0; + gate->gate = (type & 0x1f) | ((dpl & 0x3) << 5) | 0x80; + gate->offset_hi = ((int) offset >> 16) & 0xffff; +} + +static struct grub_cpu_idt_descriptor grub_gdb_orig_idt_desc + __attribute__ ((aligned(16))); +static struct grub_cpu_idt_descriptor grub_gdb_idt_desc + __attribute__ ((aligned(16))); + +/* Set up interrupt and trap handler descriptors in IDT. */ +void +grub_gdb_idtinit (void) +{ + int i; + grub_uint16_t seg; + + asm volatile ("xorl %%eax, %%eax\n" + "mov %%cs, %%ax\n" :"=a" (seg)); + + for (i = 0; i <= GRUB_GDB_LAST_TRAP; i++) + { + grub_idt_gate (&grub_gdb_idt[i], + grub_gdb_trapvec[i], seg, + GRUB_CPU_TRAP_GATE, 0); + } + + grub_gdb_idt_desc.base = (grub_addr_t) grub_gdb_idt; + grub_gdb_idt_desc.limit = sizeof (grub_gdb_idt) - 1; + asm volatile ("sidt %0" : : "m" (grub_gdb_orig_idt_desc)); + asm volatile ("lidt %0" : : "m" (grub_gdb_idt_desc)); +} + +void +grub_gdb_idtrestore (void) +{ + asm volatile ("lidt %0" : : "m" (grub_gdb_orig_idt_desc)); +} + +void +grub_gdb_breakpoint (void) +{ + int x = 1, y = 0; + (void) (x / y); + asm volatile ("int $3"); +} diff --git a/grub-core/gdb/i386/machdep.S b/grub-core/gdb/i386/machdep.S new file mode 100644 index 000000000..01ce0b4d8 --- /dev/null +++ b/grub-core/gdb/i386/machdep.S @@ -0,0 +1,173 @@ +/* machdep.S - machine dependent assembly routines for the 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. + */ + +#define ASM 1 +#include + +#define EC_PRESENT 1 +#define EC_ABSENT 0 + +#define GRUB_GDB_STACKSIZE 40000 + +/* + * The .data index for the address vector. + */ + +#define VECTOR 1 + + .globl grub_gdb_trap + .globl grub_gdb_regs + + .bss + .globl grub_gdb_stack + .space GRUB_GDB_STACKSIZE +grub_gdb_stack: + +/* + * Supplemental macros for register saving/restoration + * on exception handler entry/leave. + */ + +.macro save32 reg ndx + movl \reg, grub_gdb_regs+(\ndx * 4) +.endm + +.macro save16 reg ndx + mov $0, %eax + movw \reg, grub_gdb_regs+(\ndx * 4) + movw %ax, grub_gdb_regs+(\ndx * 4 + 2) + movl grub_gdb_regs+(EAX * 4), %eax +.endm + +.macro load32 ndx reg + movl grub_gdb_regs+(\ndx * 4), \reg +.endm + +.macro load16 ndx reg + movw grub_gdb_regs+(\ndx * 4), \reg +.endm + +.macro save_context + save32 %eax EAX + save32 %ecx ECX + save32 %edx EDX + save32 %ebx EBX + save32 %ebp EBP + save32 %esi ESI + save32 %edi EDI + + popl %ebx + save32 %ebx EIP + popl %ebx + save32 %ebx CS + popl %ebx + save32 %ebx EFLAGS + + save32 %esp ESP + + save16 %ds DS + save16 %es ES + save16 %fs FS + save16 %gs GS + save16 %ss SS +.endm + +.macro load_context + load16 SS %ss + load32 ESP %esp + + load32 EBP %ebp + load32 ESI %esi + load32 EDI %edi + + load16 DS %ds + load16 ES %es + load16 FS %fs + load16 GS %gs + + load32 EFLAGS %eax + pushl %eax + load32 CS %eax + pushl %eax + load32 EIP %eax + pushl %eax + + load32 EBX %ebx + load32 EDX %edx + load32 ECX %ecx + load32 EAX %eax +.endm + +/* + * This macro creates handlers for a given range of exception numbers + * and adds their addresses to the grub_gdb_trapvec array. + */ + +.macro ent ec beg end=0 + + /* + * Wrapper body itself. + */ + + .text +1: + .if \ec + add $4,%esp + .endif + + save_context + mov $grub_gdb_stack, %esp + mov $\beg, %eax /* trap number */ + call grub_gdb_trap + load_context + iret + + /* + * Address entry in trapvec array. + */ + + .data VECTOR + .long 1b + + /* + * Next... (recursion). + */ + + .if \end-\beg > 0 + ent \ec "(\beg+1)" \end + .endif +.endm + +/* + * Here does the actual construction of the address array and handlers + * take place. + */ + + .data VECTOR + .globl grub_gdb_trapvec +grub_gdb_trapvec: + ent EC_ABSENT 0 7 + ent EC_PRESENT 8 + ent EC_ABSENT 9 + ent EC_PRESENT 10 14 + /* + * You may have to split this further or as(1) + * will complain about nesting being too deep. + */ + ent EC_ABSENT 15 GRUB_GDB_LAST_TRAP diff --git a/grub-core/gdb/i386/signal.c b/grub-core/gdb/i386/signal.c new file mode 100644 index 000000000..23b7c3506 --- /dev/null +++ b/grub-core/gdb/i386/signal.c @@ -0,0 +1,53 @@ +/* idt.c - routines for constructing IDT fot the 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 + +/* Converting CPU trap number to UNIX signal number as + described in System V ABI i386 Processor Supplement, 3-25. */ +int +grub_gdb_trap2sig (int trap_no) +{ + const int signals[] = { + SIGFPE, /* 0: Divide error fault */ + SIGTRAP, /* 1: Single step trap fault */ + SIGABRT, /* 2: # Nonmaskable interrupt */ + SIGTRAP, /* 3: Breakpoint trap */ + SIGSEGV, /* 4: Overflow trap */ + SIGSEGV, /* 5: Bounds check fault */ + SIGILL, /* 6: Invalid opcode fault */ + SIGFPE, /* 7: No coprocessor fault */ + SIGABRT, /* 8: # Double fault abort */ + SIGSEGV, /* 9: Coprocessor overrun abort */ + SIGSEGV, /* 10: Invalid TSS fault */ + SIGSEGV, /* 11: Segment not present fault */ + SIGSEGV, /* 12: Stack exception fault */ + SIGSEGV, /* 13: General protection fault abort */ + SIGSEGV, /* 14: Page fault */ + SIGABRT, /* 15: (reserved) */ + SIGFPE, /* 16: Coprocessor error fault */ + SIGUSR1 /* other */ + }; + + return signals[trap_no < 17 ? trap_no : 17]; +} + diff --git a/grub-core/kern/i386/realmode.S b/grub-core/kern/i386/realmode.S index 2e16847c8..ecfdf4f19 100644 --- a/grub-core/kern/i386/realmode.S +++ b/grub-core/kern/i386/realmode.S @@ -117,6 +117,12 @@ gdt: gdtdesc: .word 0x27 /* limit */ .long gdt /* addr */ +realidt: + .word 0 + .long 0 +protidt: + .word 0 + .long 0 /* * These next two routines, "real_to_prot" and "prot_to_real" are structured @@ -168,6 +174,9 @@ protcseg: /* zero %eax */ xorl %eax, %eax + sidt realidt + lidt protidt + /* return on the old (or initialized) stack! */ ret /* @@ -194,6 +203,9 @@ prot_to_real: /* just in case, set GDT */ lgdt gdtdesc + sidt protidt + lidt realidt + /* save the protected mode stack */ movl %esp, %eax movl %eax, protstack diff --git a/include/grub/gdb.h b/include/grub/gdb.h new file mode 100644 index 000000000..59fd456f9 --- /dev/null +++ b/include/grub/gdb.h @@ -0,0 +1,39 @@ +/* gdb.h - Various definitions for the 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. + */ + +#ifndef GRUB_GDB_HEADER +#define GRUB_GDB_HEADER 1 + +#define SIGFPE 8 +#define SIGTRAP 5 +#define SIGABRT 6 +#define SIGSEGV 11 +#define SIGILL 4 +#define SIGUSR1 30 +/* We probably don't need other ones. */ + +struct grub_serial_port; + +extern struct grub_serial_port *grub_gdb_port; + +void grub_gdb_breakpoint (void); +int grub_gdb_trap2sig (int); + +#endif /* ! GRUB_GDB_HEADER */ + diff --git a/include/grub/i386/gdb.h b/include/grub/i386/gdb.h new file mode 100644 index 000000000..e37a8c74d --- /dev/null +++ b/include/grub/i386/gdb.h @@ -0,0 +1,79 @@ +/* i386/gdb.h - i386 specific definitions for the 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. + */ + +#ifndef GRUB_GDB_CPU_HEADER +#define GRUB_GDB_CPU_HEADER 1 + +#define GRUB_GDB_LAST_TRAP 31 +/* You may have to edit the bottom of machdep.S when adjusting + GRUB_GDB_LAST_TRAP. */ +#define GRUB_MACHINE_NR_REGS 16 + +#define EAX 0 +#define ECX 1 +#define EDX 2 +#define EBX 3 +#define ESP 4 +#define EBP 5 +#define ESI 6 +#define EDI 7 +#define EIP 8 +#define EFLAGS 9 +#define CS 10 +#define SS 11 +#define DS 12 +#define ES 13 +#define FS 14 +#define GS 15 + +#define PC EIP +#define FP EBP +#define SP ESP +#define PS EFLAGS + +#ifndef ASM + +#include + +#define GRUB_CPU_TRAP_GATE 15 + +struct grub_cpu_interrupt_gate +{ + grub_uint16_t offset_lo; + grub_uint16_t selector; + grub_uint8_t unused; + grub_uint8_t gate; + grub_uint16_t offset_hi; +} __attribute__ ((packed)); + +struct grub_cpu_idt_descriptor +{ + grub_uint16_t limit; + grub_uint32_t base; +} __attribute__ ((packed)); + +extern void (*grub_gdb_trapvec[]) (void); +void grub_gdb_breakpoint (void); +void grub_gdb_idtinit (void); +void grub_gdb_idtrestore (void); +void grub_gdb_trap (int trap_no); + +#endif /* ! ASM */ +#endif /* ! GRUB_GDB_CPU_HEADER */ + From 1948a3b714380853791e9dae6eae29e40e3bf5a0 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sun, 26 Feb 2012 19:10:52 +0100 Subject: [PATCH 5/5] Several cleanups --- grub-core/Makefile.core.def | 3 ++- grub-core/gdb/cstub.c | 27 +++++++++++++-------------- grub-core/gdb/i386/machdep.S | 1 - grub-core/kern/i386/int.S | 17 ----------------- grub-core/lib/i386/backtrace.c | 23 ----------------------- include/grub/backtrace.h | 1 + include/grub/i386/gdb.h | 3 +-- 7 files changed, 17 insertions(+), 58 deletions(-) diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 585567d56..a9d596b0f 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1755,7 +1755,8 @@ module = { module = { name = backtrace; - common = lib/i386/backtrace.c; + x86 = lib/i386/backtrace.c; + common = lib/backtrace.c; enable = x86; }; diff --git a/grub-core/gdb/cstub.c b/grub-core/gdb/cstub.c index b7dd2200b..551101de5 100644 --- a/grub-core/gdb/cstub.c +++ b/grub-core/gdb/cstub.c @@ -21,6 +21,7 @@ #include #include #include +#include static const char hexchars[] = "0123456789abcdef"; int grub_gdb_regs[GRUB_MACHINE_NR_REGS]; @@ -140,9 +141,9 @@ grub_gdb_putpacket (char *buffer) /* 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) +grub_gdb_mem2hex (char *mem, char *buf, grub_size_t count) { - int i; + grub_size_t i; unsigned char ch; for (i = 0; i < count; i++) @@ -175,7 +176,7 @@ grub_gdb_hex2mem (char *buf, char *mem, int count) /* Convert hex characters to int and return the number of characters processed. */ static int -grub_gdb_hex2int (char **ptr, int *int_value) +grub_gdb_hex2int (char **ptr, grub_uint64_t *int_value) { int num_chars = 0; int hex_value; @@ -205,17 +206,16 @@ grub_gdb_trap (int trap_no) { int sig_no; int stepping; - int addr; - int length; + grub_uint64_t addr; + grub_uint64_t 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_backtrace_print_address ((void *) grub_gdb_regs[PC]); grub_printf ("\n"); - grub_backtrace_pointer (grub_gdb_regs[EBP]); + grub_backtrace_pointer ((void *) grub_gdb_regs[EBP]); grub_abort (); } @@ -284,10 +284,10 @@ grub_gdb_trap (int trap_no) /* Set the value of a single CPU register -- return OK. */ case 'P': { - int regno; + grub_uint64_t regno; if (grub_gdb_hex2int (&ptr, ®no) && *ptr++ == '=') - if (regno >= 0 && regno < GRUB_MACHINE_NR_REGS) + if (regno < GRUB_MACHINE_NR_REGS) { grub_gdb_hex2mem (ptr, (char *) &grub_gdb_regs[regno], 4); grub_strcpy (grub_gdb_outbuf, "OK"); @@ -308,7 +308,8 @@ grub_gdb_trap (int trap_no) if (grub_gdb_hex2int (&ptr, &length)) { ptr = 0; - grub_gdb_mem2hex ((char *) addr, grub_gdb_outbuf, length); + grub_gdb_mem2hex ((char *) (grub_addr_t) addr, + grub_gdb_outbuf, length); } if (ptr) grub_strcpy (grub_gdb_outbuf, "E01"); @@ -322,7 +323,7 @@ grub_gdb_trap (int trap_no) if (grub_gdb_hex2int (&ptr, &length)) if (*(ptr++) == ':') { - grub_gdb_hex2mem (ptr, (char *) addr, length); + grub_gdb_hex2mem (ptr, (char *) (grub_addr_t) addr, length); grub_strcpy (grub_gdb_outbuf, "OK"); ptr = 0; } @@ -342,8 +343,6 @@ grub_gdb_trap (int trap_no) 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; diff --git a/grub-core/gdb/i386/machdep.S b/grub-core/gdb/i386/machdep.S index 01ce0b4d8..62e8b2629 100644 --- a/grub-core/gdb/i386/machdep.S +++ b/grub-core/gdb/i386/machdep.S @@ -17,7 +17,6 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#define ASM 1 #include #define EC_PRESENT 1 diff --git a/grub-core/kern/i386/int.S b/grub-core/kern/i386/int.S index 7a2b7808a..7edace404 100644 --- a/grub-core/kern/i386/int.S +++ b/grub-core/kern/i386/int.S @@ -19,9 +19,6 @@ FUNCTION(grub_bios_interrupt) pushf cli -#ifndef GRUB_MACHINE_PCBIOS - sidt protidt -#endif popf pushl %ebp pushl %ecx @@ -51,9 +48,6 @@ FUNCTION(grub_bios_interrupt) .code16 pushf cli -#ifndef GRUB_MACHINE_PCBIOS - lidt realidt -#endif mov %ds, %ax push %ax @@ -126,15 +120,4 @@ intno: popl %eax popl %ecx popl %ebp -#ifndef GRUB_MACHINE_PCBIOS - lidt protidt -#endif ret -#ifndef GRUB_MACHINE_PCBIOS -realidt: - .word 0x100 - .long 0 -protidt: - .word 0 - .long 0 -#endif diff --git a/grub-core/lib/i386/backtrace.c b/grub-core/lib/i386/backtrace.c index 27f82a85e..978693396 100644 --- a/grub-core/lib/i386/backtrace.c +++ b/grub-core/lib/i386/backtrace.c @@ -26,29 +26,6 @@ #define MAX_STACK_FRAME 102400 -GRUB_MOD_LICENSE ("GPLv3+"); - -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) { diff --git a/include/grub/backtrace.h b/include/grub/backtrace.h index a0503b8b7..395519762 100644 --- a/include/grub/backtrace.h +++ b/include/grub/backtrace.h @@ -21,5 +21,6 @@ void grub_backtrace (void); void grub_backtrace_pointer (void *ptr); +void grub_backtrace_print_address (void *addr); #endif diff --git a/include/grub/i386/gdb.h b/include/grub/i386/gdb.h index e37a8c74d..174f4dc09 100644 --- a/include/grub/i386/gdb.h +++ b/include/grub/i386/gdb.h @@ -47,7 +47,7 @@ #define SP ESP #define PS EFLAGS -#ifndef ASM +#ifndef ASM_FILE #include @@ -69,7 +69,6 @@ struct grub_cpu_idt_descriptor } __attribute__ ((packed)); extern void (*grub_gdb_trapvec[]) (void); -void grub_gdb_breakpoint (void); void grub_gdb_idtinit (void); void grub_gdb_idtrestore (void); void grub_gdb_trap (int trap_no);