leak 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // usage: acid -l pool -l leak
  3. //
  4. include("/sys/src/libc/port/pool.acid");
  5. defn
  6. dumppool(p, sum)
  7. {
  8. complex Pool p;
  9. a = p.arenalist;
  10. print("A: ", p.arenalist\X, "\n");
  11. while a != 0 && a < 0xff000000 do {
  12. complex Arena a;
  13. dumparena(a, sum);
  14. a = a.down;
  15. }
  16. }
  17. defn
  18. dumparena(arena, sum)
  19. {
  20. local atail, b, nb;
  21. atail = A2TB(arena);
  22. complex Bhdr arena;
  23. b = a;
  24. print("B: ", b\X, " ", atail\X, "\n");
  25. while b < atail && b.magic != ARENATAIL_MAGIC do {
  26. dumpblock(b, sum);
  27. nb = B2NB(b);
  28. if nb == b then {
  29. print("B2NB(", b\X, ") = b\n");
  30. b = atail; // end loop
  31. }
  32. if nb > atail then {
  33. b = (Bhdr)(b+4);
  34. print("lost at block ", (b-4)\X, ", scanning forward\n");
  35. while b < atail && b.magic != ALLOC_MAGIC && b.magic != FREE_MAGIC do
  36. b = (Bhdr)(b+4);
  37. print("stopped at ", b\X, " ", *b\X, "\n");
  38. }else
  39. b = nb;
  40. }
  41. if b != atail then
  42. print("found wrong tail to arena ", arena\X, " wanted ", atail\X, "\n");
  43. }
  44. defn
  45. isptr(a)
  46. {
  47. if end <= a && a < xbloc then
  48. return 1;
  49. if 0xdefff000 <= a && a < 0xdffff000 then
  50. return 1;
  51. return 0;
  52. }
  53. lastalloc = 0;
  54. lastcount = 0;
  55. lastsize = 0;
  56. defn
  57. emitsum()
  58. {
  59. if lastalloc then
  60. print("summary ", lastalloc\a, " ", lastcount\D, " ", lastsize\D, "\n");
  61. lastalloc = 0;
  62. lastcount = 0;
  63. lastsize = 0;
  64. }
  65. defn
  66. dumpblock(addr, sum)
  67. {
  68. complex Bhdr addr;
  69. if addr.magic == ALLOC_MAGIC || (!sum && addr.magic == FREE_MAGIC) then {
  70. local a, x, s;
  71. a = addr;
  72. complex Alloc a;
  73. x = addr+8;
  74. if sum then {
  75. if *(addr+8) != lastalloc then {
  76. emitsum();
  77. lastalloc = *(addr+8);
  78. }
  79. lastcount = lastcount+1;
  80. lastsize = lastsize+a.size;
  81. }else{
  82. if addr.magic == ALLOC_MAGIC then
  83. s = "block";
  84. else
  85. s = "free";
  86. print(s, " ", addr\X, " ", a.size\X, " ");
  87. print(*(addr+8)\X, " ", *(addr+12)\X, " ",
  88. *(addr+8)\a, " ", *(addr+12)\a, "\n");
  89. }
  90. }
  91. }
  92. defn
  93. dumprange(s, e, type)
  94. {
  95. local x, y;
  96. print("range ", type, " ", s\X, " ", e\X, "\n");
  97. x = s;
  98. while x < e do {
  99. y = *(x\X);
  100. if isptr(y) then print("data ", x\X, " ", y\X, " ", type, "\n");
  101. x = x + 4;
  102. }
  103. }
  104. defn
  105. stacktop()
  106. {
  107. local e, m;
  108. m = map();
  109. while m != {} do {
  110. e = head m;
  111. if e[0] == "*data" then
  112. return e[2];
  113. m = tail m;
  114. }
  115. return 0xdffff000;
  116. }
  117. defn
  118. dumpmem()
  119. {
  120. local s, top;
  121. xbloc = *bloc;
  122. // assume map()[1] is "data"
  123. dumprange(map()[1][1], end, "bss"); // bss
  124. dumprange(end, xbloc, "alloc"); // allocated
  125. top = stacktop() - 8;
  126. if top-0x01000000 < *SP && *SP < top then
  127. s = *SP;
  128. else
  129. s = top-32*1024;
  130. dumprange(s, top, "stack");
  131. }
  132. defn
  133. dumpregs()
  134. {
  135. dumprange(0, sizeofUreg, "reg");
  136. }
  137. defn
  138. leakdump(l)
  139. {
  140. print("==LEAK BEGIN==\n");
  141. dumppool(*mainmem, 0);
  142. dumpmem();
  143. dumpregs();
  144. while l != {} do {
  145. setproc(head l);
  146. dumpregs();
  147. l = tail l;
  148. }
  149. print("==LEAK END==\n");
  150. }
  151. defn
  152. blockdump()
  153. {
  154. print("==BLOCK BEGIN==\n");
  155. dumppool(*mainmem, 0);
  156. print("==BLOCK END==\n");
  157. }
  158. defn
  159. blocksummary()
  160. {
  161. print("==BLOCK BEGIN==\n");
  162. dumppool(*mainmem, 1);
  163. emitsum();
  164. print("==BLOCK END==\n");
  165. }