1
0

trump 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // trace user malloc pool - trace malloc, realloc, and free calls
  2. // if trumpsbrk is set, we trace sbrkalloc and sbrkmerge too.
  3. _stoprunning = 0;
  4. trumphexaddrs = 0;
  5. trumpsbrk = 0;
  6. defn stopped(pid) {
  7. local l;
  8. local pc;
  9. pc = *PC;
  10. if notes then {
  11. if (notes[0]!="sys: breakpoint") then
  12. {
  13. print(pid,": ",reason(*TRAP),"\t");
  14. print(fmt(pc,97),"\t",fmt(pc,105),"\n");
  15. print("Notes pending:\n");
  16. l = notes;
  17. while l do
  18. {
  19. print("\t",head l,"\n");
  20. l = tail l;
  21. }
  22. _stoprunning = 1;
  23. }
  24. }
  25. }
  26. defn printstack() {
  27. local frame, stk, pcs, lst, x;
  28. pcs = {*PC};
  29. stk = strace(*PC,*SP,0);
  30. while stk do {
  31. pcs = append pcs, stk[0][1];
  32. stk = tail stk;
  33. }
  34. print(" #");
  35. lst = pcs;
  36. while lst do {
  37. if trumphexaddrs != 0 then
  38. x = lst[0]\X;
  39. else
  40. x = lst[0]\a;
  41. print(" src(", x, ");");
  42. lst = tail lst;
  43. }
  44. print("\n");
  45. }
  46. defn setuptrump() {
  47. mallocPC = malloc;
  48. malloczPC = mallocz;
  49. freePC = free;
  50. reallocPC = realloc;
  51. sbrkallocPC = sbrkalloc;
  52. sbrkmergePC = sbrkmerge;
  53. // linker might fill delay slot with first instruction
  54. if objtype == "mips" then {
  55. mallocPC = mallocPC+4;
  56. malloczPC = malloczPC+4;
  57. freePC = freePC+4;
  58. reallocPC = reallocPC+4;
  59. sbrkallocPC = sbrkallocPC+4;
  60. sbrkmergePC = sbrkmergePC+4;
  61. }
  62. bpset(mallocPC);
  63. bpset(malloczPC);
  64. bpset(freePC);
  65. bpset(reallocPC);
  66. if trumpsbrk then {
  67. bpset(sbrkallocPC);
  68. bpset(sbrkmergePC);
  69. }
  70. }
  71. defn cleantrump() {
  72. stop(pid);
  73. bpdel(mallocPC);
  74. bpdel(malloczPC);
  75. bpdel(freePC);
  76. bpdel(reallocPC);
  77. bpdel(sbrkallocPC);
  78. bpdel(sbrkmergePC);
  79. }
  80. defn trumpflush() {
  81. stop(pid); // already stopped, but flushes output
  82. }
  83. defn new() {
  84. bplist = {};
  85. newproc(progargs);
  86. bpset(follow(main)[0]);
  87. cont();
  88. bpdel(*PC);
  89. // clear the hang bit, which is left set by newproc, so programs we fork/exec don't hang
  90. printto("/proc/"+itoa(pid)+"/ctl", "nohang");
  91. }
  92. defn trumpfninfo() {
  93. local arg0, arg1, stk, retpc, params;
  94. stk = strace(*PC, *SP, 0);
  95. retpc = stk[0][1];
  96. params = stk[0][2];
  97. arg0 = params[0][1];
  98. arg1 = 0;
  99. if tail params != {} then
  100. arg1 = params[1][1];
  101. return {arg0, arg1, retpc};
  102. }
  103. defn trumpretval() {
  104. if objtype=="386" then
  105. return *AX;
  106. if objtype=="mips" then
  107. return *R1;
  108. if objtype=="power" || objtype=="alpha" then
  109. return *R0;
  110. }
  111. defn trump() {
  112. local arg0, arg1, pc, ret, x;
  113. stop(pid);
  114. _stoprunning = 0;
  115. setuptrump();
  116. while !_stoprunning do {
  117. cont();
  118. if notes[0]!="sys: breakpoint" then {
  119. cleantrump();
  120. return {};
  121. }
  122. pc = *PC;
  123. x = trumpfninfo();
  124. arg0 = x[0];
  125. if pc == reallocPC || pc == sbrkmergePC then
  126. arg1 = x[1];
  127. bpset(x[2]);
  128. cont();
  129. bpdel(x[2]);
  130. ret = trumpretval();
  131. if pc == mallocPC then
  132. print(ret\X, " malloc ", arg0\D);
  133. if pc == malloczPC then
  134. print(ret\X, " mallocz ", arg0\D);
  135. if pc == freePC then
  136. print(arg0\X, " free");
  137. if pc == reallocPC then
  138. print(ret\X, " realloc ", arg0\X, " ", arg1\D);
  139. if pc == sbrkallocPC then
  140. print(ret\X, " sbrkalloc ", arg0\D);
  141. if pc == sbrkmergePC then
  142. print("sbrkmerge ", arg0\X, " ", arg1\X, " = ", ret\D);
  143. printstack();
  144. trumpflush();
  145. }
  146. }
  147. defn untrump() {
  148. cleantrump();
  149. start(pid);
  150. }
  151. print("/sys/lib/acid/trump");