forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectors.cpp
More file actions
366 lines (293 loc) · 8.85 KB
/
Copy pathvectors.cpp
File metadata and controls
366 lines (293 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Lists embeded STL vectors and pointers to STL vectors found in the given
// memory range.
//
// Linux only, enabled with BUILD_VECTORS cmake option.
#include <dfhack/Core.h>
#include <dfhack/Console.h>
#include <dfhack/Export.h>
#include <dfhack/PluginManager.h>
#include <dfhack/Process.h>
#include <vector>
#include <string>
#include <stdio.h>
using std::vector;
using std::string;
using namespace DFHack;
struct t_vecTriplet
{
uint32_t start;
uint32_t end;
uint32_t alloc_end;
};
DFhackCExport command_result df_vectors (Core * c,
vector <string> & parameters);
DFhackCExport command_result df_clearvec (Core * c,
vector <string> & parameters);
DFhackCExport const char * plugin_name ( void )
{
return "vectors";
}
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("vectors",
"Scan memory for vectors.\
\n 1st param: start of scan\
\n 2nd param: number of bytes to scan",
df_vectors));
commands.push_back(PluginCommand("clearvec",
"Set list of vectors to zero length",
df_clearvec));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}
static bool hexOrDec(string &str, uint32_t &value)
{
if (str.find("0x") == 0 && sscanf(str.c_str(), "%x", &value) == 1)
return true;
else if (sscanf(str.c_str(), "%u", &value) == 1)
return true;
return false;
}
static bool mightBeVec(vector<t_memrange> &heap_ranges,
t_vecTriplet *vec)
{
if ((vec->start > vec->end) || (vec->end > vec->alloc_end))
return false;
// Vector length might not be a multiple of 4 if, for example,
// it's a vector of uint8_t or uint16_t. However, the actual memory
// allocated to the vector should be 4 byte aligned.
if ((vec->start % 4 != 0) || (vec->alloc_end % 4 != 0))
return false;
for (size_t i = 0; i < heap_ranges.size(); i++)
{
t_memrange &range = heap_ranges[i];
if (range.isInRange(vec->start) && range.isInRange(vec->alloc_end))
return true;
}
return false;
}
static bool inAnyRange(vector<t_memrange> &ranges, uint32_t ptr)
{
for (size_t i = 0; i < ranges.size(); i++)
{
if (ranges[i].isInRange(ptr))
return true;
}
return false;
}
static bool getHeapRanges(Core * c, std::vector<t_memrange> &heap_ranges)
{
std::vector<t_memrange> ranges;
c->p->getMemRanges(ranges);
for (size_t i = 0; i < ranges.size(); i++)
{
t_memrange &range = ranges[i];
// Some kernels don't report [heap], and the heap can consist of
// more segments than just the one labeled with [heap], so include
// all segments which *might* be part of the heap.
if (range.read && range.write && !range.shared)
{
if (strlen(range.name) == 0 || strcmp(range.name, "[heap]") == 0)
heap_ranges.push_back(range);
}
}
if (heap_ranges.empty())
{
c->con << "No possible heap segments." << std::endl;
return false;
}
return true;
}
////////////////////////////////////////
// COMMAND: vectors
////////////////////////////////////////
static void vectorsUsage(Console &con)
{
con << "Usage: vectors <start of scan address> <# bytes to scan>"
<< std::endl;
}
static void printVec(Console &con, const char* msg, t_vecTriplet *vec,
uint32_t start, uint32_t pos)
{
uint32_t length = vec->end - vec->start;
uint32_t offset = pos - start;
con.print("%8s offset %06p, addr %010p, start %010p, length %u\n",
msg, offset, pos, vec->start, length);
}
DFhackCExport command_result df_vectors (Core * c, vector <string> & parameters)
{
Console & con = c->con;
if (parameters.size() != 2)
{
vectorsUsage(con);
return CR_FAILURE;
}
uint32_t start = 0, bytes = 0;
if (!hexOrDec(parameters[0], start))
{
vectorsUsage(con);
return CR_FAILURE;
}
if (!hexOrDec(parameters[1], bytes))
{
vectorsUsage(con);
return CR_FAILURE;
}
uint32_t end = start + bytes;
// 4 byte alignment.
while (start % 4 != 0)
start++;
c->Suspend();
std::vector<t_memrange> heap_ranges;
if (!getHeapRanges(c, heap_ranges))
{
return CR_FAILURE;
c->Resume();
}
bool startInRange = false;
for (size_t i = 0; i < heap_ranges.size(); i++)
{
t_memrange &range = heap_ranges[i];
if (!range.isInRange(start))
continue;
// Found the range containing the start
if (!range.isInRange(end))
{
con.print("Scanning %u bytes would read past end of memory "
"range.\n", bytes);
uint32_t diff = end - range.end;
con.print("Cutting bytes down by %u.\n", diff);
end = (uint32_t) range.end;
}
startInRange = true;
break;
} // for (size_t i = 0; i < ranges.size(); i++)
if (!startInRange)
{
con << "Address not in any memory range." << std::endl;
c->Resume();
return CR_FAILURE;
}
uint32_t pos = start;
const uint32_t ptr_size = sizeof(void*);
for (uint32_t pos = start; pos < end; pos += ptr_size)
{
// Is it an embeded vector?
if (pos <= ( end - sizeof(t_vecTriplet) ))
{
t_vecTriplet* vec = (t_vecTriplet*) pos;
if (mightBeVec(heap_ranges, vec))
{
printVec(con, "VEC:", vec, start, pos);
// Skip over rest of vector.
pos += sizeof(t_vecTriplet) - ptr_size;
continue;
}
}
// Is it a vector pointer?
if (pos <= (end - ptr_size))
{
uint32_t ptr = * ( (uint32_t*) pos);
if (inAnyRange(heap_ranges, ptr))
{
t_vecTriplet* vec = (t_vecTriplet*) ptr;
if (mightBeVec(heap_ranges, vec))
{
printVec(con, "VEC PTR:", vec, start, pos);
continue;
}
}
}
} // for (uint32_t pos = start; pos < end; pos += ptr_size)
c->Resume();
return CR_OK;
}
////////////////////////////////////////
// COMMAND: clearvec
////////////////////////////////////////
static void clearUsage(Console &con)
{
con << "Usage: clearvec <vector1 addr> [vector2 addr] ..." << std::endl;
con << "Address can be either for vector or pointer to vector."
<< std::endl;
}
DFhackCExport command_result df_clearvec (Core * c, vector <string> & parameters)
{
Console & con = c->con;
if (parameters.size() == 0)
{
clearUsage(con);
return CR_FAILURE;
}
uint32_t start = 0, bytes = 0;
if (!hexOrDec(parameters[0], start))
{
vectorsUsage(con);
return CR_FAILURE;
}
if (!hexOrDec(parameters[1], bytes))
{
vectorsUsage(con);
return CR_FAILURE;
}
c->Suspend();
std::vector<t_memrange> heap_ranges;
if (!getHeapRanges(c, heap_ranges))
{
c->Resume();
return CR_FAILURE;
}
for (size_t i = 0; i < parameters.size(); i++)
{
std::string addr_str = parameters[i];
uint32_t addr;
if (!hexOrDec(addr_str, addr))
{
con << "'" << addr_str << "' not a number." << std::endl;
continue;
}
if (addr % 4 != 0)
{
con << addr_str << " not 4 byte aligned." << std::endl;
continue;
}
if (!inAnyRange(heap_ranges, addr))
{
con << addr_str << " not in any valid address range." << std::endl;
continue;
}
bool valid = false;
bool ptr = false;
t_vecTriplet* vec = (t_vecTriplet*) addr;
if (mightBeVec(heap_ranges, vec))
valid = true;
else
{
// Is it a pointer to a vector?
addr = * ( (uint32_t*) addr);
vec = (t_vecTriplet*) addr;
if (inAnyRange(heap_ranges, addr) && mightBeVec(heap_ranges, vec))
{
valid = true;
ptr = true;
}
}
if (!valid)
{
con << addr_str << " is not a vector." << std::endl;
continue;
}
vec->end = vec->start;
if (ptr)
con << "Vector pointer ";
else
con << "Vector ";
con << addr_str << " set to zero length." << std::endl;
} // for (size_t i = 0; i < parameters.size(); i++)
c->Resume();
return CR_OK;
}