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