symbols.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <mach.h>
  13. #define Extern extern
  14. #include "mips.h"
  15. #define STRINGSZ 128
  16. /*
  17. * print the value of dot as file:line
  18. */
  19. void
  20. printsource(int32_t dot)
  21. {
  22. char str[STRINGSZ];
  23. if (fileline(str, STRINGSZ, dot))
  24. Bprint(bioout, "%s", str);
  25. }
  26. void
  27. printlocals(Symbol *fn, uint32_t fp)
  28. {
  29. int i;
  30. Symbol s;
  31. s = *fn;
  32. for (i = 0; localsym(&s, i); i++) {
  33. if (s.class != CAUTO)
  34. continue;
  35. Bprint(bioout, "\t%s=#%lux\n", s.name, getmem_4(fp-s.value));
  36. }
  37. }
  38. void
  39. printparams(Symbol *fn, uint32_t fp)
  40. {
  41. int i;
  42. Symbol s;
  43. int first;
  44. fp += mach->szreg; /* skip saved pc */
  45. s = *fn;
  46. for (first = i = 0; localsym(&s, i); i++) {
  47. if (s.class != CPARAM)
  48. continue;
  49. if (first++)
  50. Bprint(bioout, ", ");
  51. Bprint(bioout, "%s=#%lux", s.name, getmem_4(fp+s.value));
  52. }
  53. Bprint(bioout, ") ");
  54. }
  55. #define STARTSYM "_main"
  56. #define FRAMENAME ".frame"
  57. void
  58. stktrace(int modif)
  59. {
  60. uint32_t pc, sp;
  61. Symbol s, f;
  62. int i;
  63. char buf[512];
  64. pc = reg.pc;
  65. sp = reg.r[29];
  66. i = 0;
  67. while (findsym(pc, CTEXT, &s)) {
  68. if(strcmp(STARTSYM, s.name) == 0) {
  69. Bprint(bioout, "%s() at #%llux\n", s.name, s.value);
  70. break;
  71. }
  72. if (pc == s.value) /* at first instruction */
  73. f.value = 0;
  74. else if (findlocal(&s, FRAMENAME, &f) == 0)
  75. break;
  76. if (s.type == 'L' || s.type == 'l' || pc <= s.value+4)
  77. pc = reg.r[31];
  78. else pc = getmem_4(sp);
  79. sp += f.value;
  80. Bprint(bioout, "%s(", s.name);
  81. printparams(&s, sp);
  82. printsource(s.value);
  83. Bprint(bioout, " called from ");
  84. symoff(buf, sizeof(buf), pc-8, CTEXT);
  85. Bprint(bioout, buf);
  86. printsource(pc-8);
  87. Bprint(bioout, "\n");
  88. if(modif == 'C')
  89. printlocals(&s, sp);
  90. if(++i > 40){
  91. Bprint(bioout, "(trace truncated)\n");
  92. break;
  93. }
  94. }
  95. }