backtrace.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. GRUB_MOD_LICENSE ("GPLv3+");
  26. void
  27. grub_backtrace_print_address (void *addr)
  28. {
  29. grub_dl_t mod;
  30. FOR_DL_MODULES (mod)
  31. {
  32. grub_dl_segment_t segment;
  33. for (segment = mod->segment; segment; segment = segment->next)
  34. if (segment->addr <= addr && (grub_uint8_t *) segment->addr
  35. + segment->size > (grub_uint8_t *) addr)
  36. {
  37. grub_printf ("%s.%x+%" PRIxGRUB_SIZE, mod->name, segment->section,
  38. (grub_size_t) ((grub_uint8_t *) addr - (grub_uint8_t *) segment->addr));
  39. return;
  40. }
  41. }
  42. grub_printf ("%p", addr);
  43. }
  44. static grub_err_t
  45. grub_cmd_backtrace (grub_command_t cmd __attribute__ ((unused)),
  46. int argc __attribute__ ((unused)),
  47. char **args __attribute__ ((unused)))
  48. {
  49. grub_backtrace ();
  50. return 0;
  51. }
  52. static grub_command_t cmd;
  53. GRUB_MOD_INIT(backtrace)
  54. {
  55. cmd = grub_register_command ("backtrace", grub_cmd_backtrace,
  56. 0, N_("Print backtrace."));
  57. }
  58. GRUB_MOD_FINI(backtrace)
  59. {
  60. grub_unregister_command (cmd);
  61. }