backtrace.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/misc.h>
  19. #include <grub/command.h>
  20. #include <grub/err.h>
  21. #include <grub/dl.h>
  22. #include <grub/mm.h>
  23. #include <grub/term.h>
  24. #include <grub/backtrace.h>
  25. #define MAX_STACK_FRAME 102400
  26. void
  27. grub_backtrace_pointer (void *ebp)
  28. {
  29. void *ptr, *nptr;
  30. unsigned i;
  31. ptr = ebp;
  32. while (1)
  33. {
  34. grub_printf ("%p: ", ptr);
  35. grub_backtrace_print_address (((void **) ptr)[1]);
  36. grub_printf (" (");
  37. for (i = 0; i < 2; i++)
  38. grub_printf ("%p,", ((void **)ptr) [i + 2]);
  39. grub_printf ("%p)\n", ((void **)ptr) [i + 2]);
  40. nptr = *(void **)ptr;
  41. if (nptr < ptr || (void **) nptr - (void **) ptr > MAX_STACK_FRAME
  42. || nptr == ptr)
  43. {
  44. grub_printf ("Invalid stack frame at %p (%p)\n", ptr, nptr);
  45. break;
  46. }
  47. ptr = nptr;
  48. }
  49. }
  50. void
  51. grub_backtrace (void)
  52. {
  53. #ifdef __x86_64__
  54. asm volatile ("movq %%rbp, %%rdi\n"
  55. "callq *%%rax": :"a"(grub_backtrace_pointer));
  56. #else
  57. asm volatile ("movl %%ebp, %%eax\n"
  58. "calll *%%ecx": :"c"(grub_backtrace_pointer));
  59. #endif
  60. }