symbols.c 1.7 KB

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