symbols.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <mach.h>
  5. #include "arm.h"
  6. #define STRINGSZ 128
  7. /*
  8. * print the value of dot as file:line
  9. */
  10. void
  11. printsource(long dot)
  12. {
  13. char str[STRINGSZ];
  14. if (fileline(str, STRINGSZ, dot))
  15. Bprint(bioout, "%s", str);
  16. }
  17. void
  18. printlocals(Symbol *fn, ulong fp)
  19. {
  20. int i;
  21. Symbol s;
  22. s = *fn;
  23. for (i = 0; localsym(&s, i); i++) {
  24. if (s.class != CAUTO)
  25. continue;
  26. Bprint(bioout, "\t%s=#%lux\n", s.name, getmem_4(fp-s.value));
  27. }
  28. }
  29. void
  30. printparams(Symbol *fn, ulong fp)
  31. {
  32. int i;
  33. Symbol s;
  34. int first;
  35. fp += mach->szreg; /* skip saved pc */
  36. s = *fn;
  37. for (first = i = 0; localsym(&s, i); i++) {
  38. if (s.class != CPARAM)
  39. continue;
  40. if (first++)
  41. Bprint(bioout, ", ");
  42. Bprint(bioout, "%s=#%lux", s.name, getmem_4(fp+s.value));
  43. }
  44. Bprint(bioout, ") ");
  45. }
  46. #define STARTSYM "_main"
  47. #define FRAMENAME ".frame"
  48. void
  49. stktrace(int modif)
  50. {
  51. ulong pc, sp;
  52. Symbol s, f;
  53. int i;
  54. char buf[512];
  55. pc = reg.r[15];
  56. sp = reg.r[13];
  57. i = 0;
  58. while (findsym(pc, CTEXT, &s)) {
  59. if(strcmp(STARTSYM, s.name) == 0) {
  60. Bprint(bioout, "%s() at #%llux\n", s.name, s.value);
  61. break;
  62. }
  63. if (pc == s.value) /* at first instruction */
  64. f.value = 0;
  65. else if (findlocal(&s, FRAMENAME, &f) == 0)
  66. break;
  67. if (s.type == 'L' || s.type == 'l' || pc <= s.value+4)
  68. pc = reg.r[14];
  69. else pc = getmem_4(sp);
  70. sp += f.value;
  71. Bprint(bioout, "%s(", s.name);
  72. printparams(&s, sp);
  73. printsource(s.value);
  74. Bprint(bioout, " called from ");
  75. symoff(buf, sizeof(buf), pc-8, CTEXT);
  76. Bprint(bioout, buf);
  77. printsource(pc-8);
  78. Bprint(bioout, "\n");
  79. if(modif == 'C')
  80. printlocals(&s, sp);
  81. if(++i > 40){
  82. Bprint(bioout, "(trace truncated)\n");
  83. break;
  84. }
  85. }
  86. }