-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathllscan.h
411 lines (312 loc) · 12.4 KB
/
llscan.h
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#ifndef SRC_LLSCAN_H_
#define SRC_LLSCAN_H_
#include <lldb/API/LLDB.h>
#include <map>
#include <set>
#include <unordered_set>
#include "src/error.h"
#include "src/llnode.h"
#include "src/printer.h"
namespace llnode {
class LLScan;
typedef std::vector<uint64_t> ReferencesVector;
typedef std::unordered_set<uint64_t> ContextVector;
typedef std::map<uint64_t, ReferencesVector*> ReferencesByValueMap;
typedef std::map<std::string, ReferencesVector*> ReferencesByPropertyMap;
typedef std::map<std::string, ReferencesVector*> ReferencesByStringMap;
// New type defining pagination options
// It should be feasible to use it to any commands that output
// a list of information
struct cmd_pagination_t {
int total_entries = 0;
int current_page = 0;
int output_limit = 0;
std::string command = "";
};
char** ParsePrinterOptions(char** cmd, Printer::PrinterOptions* options);
class FindObjectsCmd : public CommandBase {
public:
FindObjectsCmd(LLScan* llscan) : llscan_(llscan) {}
~FindObjectsCmd() override {}
bool DoExecute(lldb::SBDebugger d, char** cmd,
lldb::SBCommandReturnObject& result) override;
void SimpleOutput(lldb::SBCommandReturnObject& result);
void DetailedOutput(lldb::SBCommandReturnObject& result);
private:
LLScan* llscan_;
};
class FindInstancesCmd : public CommandBase {
public:
FindInstancesCmd(LLScan* llscan, bool detailed)
: llscan_(llscan), detailed_(detailed) {}
~FindInstancesCmd() override {}
bool DoExecute(lldb::SBDebugger d, char** cmd,
lldb::SBCommandReturnObject& result) override;
private:
LLScan* llscan_;
bool detailed_;
cmd_pagination_t pagination_;
};
class NodeInfoCmd : public CommandBase {
public:
NodeInfoCmd(LLScan* llscan) : llscan_(llscan) {}
~NodeInfoCmd() override {}
bool DoExecute(lldb::SBDebugger d, char** cmd,
lldb::SBCommandReturnObject& result) override;
private:
LLScan* llscan_;
};
class ScanOptions {
public:
// Defines what are we looking for
enum ScanType { kFieldValue, kPropertyName, kStringValue, kBadOption };
ScanOptions() : scan_type(ScanType::kFieldValue), recursive_scan(false) {}
ScanType scan_type;
bool recursive_scan;
};
class FindReferencesCmd : public CommandBase {
public:
FindReferencesCmd(LLScan* llscan) : llscan_(llscan) {}
~FindReferencesCmd() override {}
bool DoExecute(lldb::SBDebugger d, char** cmd,
lldb::SBCommandReturnObject& result) override;
char** ParseScanOptions(char** cmd, ScanOptions* options);
class ObjectScanner {
public:
virtual ~ObjectScanner() {}
virtual bool AreReferencesLoaded() { return false; };
virtual ReferencesVector* GetReferences() { return nullptr; };
virtual void ScanRefs(v8::JSObject& js_obj, Error& err){};
virtual void ScanRefs(v8::String& str, Error& err){};
virtual void PrintRefs(lldb::SBCommandReturnObject& result,
v8::JSObject& js_obj, Error& err, int level = 0) {}
virtual void PrintRefs(lldb::SBCommandReturnObject& result, v8::String& str,
Error& err, int level = 0) {}
virtual void PrintContextRefs(lldb::SBCommandReturnObject& result,
Error& err, FindReferencesCmd* cli_cmd_,
ScanOptions* options,
ReferencesVector* already_visited_references,
int level = 0) {}
std::string GetPropertyReferenceString(int level = 0);
std::string GetArrayReferenceString(int level = 0);
};
void PrintReferences(lldb::SBCommandReturnObject& result,
ReferencesVector* references, ObjectScanner* scanner,
ScanOptions* options,
ReferencesVector* already_visited_references,
int level = 0);
void ScanForReferences(ObjectScanner* scanner);
void PrintRecursiveReferences(lldb::SBCommandReturnObject& result,
ScanOptions* options,
ReferencesVector* visited_references,
uint64_t address, int level);
class ReferenceScanner : public ObjectScanner {
public:
ReferenceScanner(LLScan* llscan, v8::Value search_value)
: llscan_(llscan), search_value_(search_value) {}
bool AreReferencesLoaded() override;
ReferencesVector* GetReferences() override;
void ScanRefs(v8::JSObject& js_obj, Error& err) override;
void ScanRefs(v8::String& str, Error& err) override;
void PrintRefs(lldb::SBCommandReturnObject& result, v8::JSObject& js_obj,
Error& err, int level = 0) override;
void PrintRefs(lldb::SBCommandReturnObject& result, v8::String& str,
Error& err, int level = 0) override;
void PrintContextRefs(lldb::SBCommandReturnObject& result, Error& err,
FindReferencesCmd* cli_cmd_, ScanOptions* options,
ReferencesVector* already_visited_references,
int level = 0) override;
private:
LLScan* llscan_;
v8::Value search_value_;
};
class PropertyScanner : public ObjectScanner {
public:
PropertyScanner(LLScan* llscan, std::string search_value)
: llscan_(llscan), search_value_(search_value) {}
bool AreReferencesLoaded() override;
ReferencesVector* GetReferences() override;
void ScanRefs(v8::JSObject& js_obj, Error& err) override;
// We only scan properties on objects not Strings, use default no-op impl
// of PrintRefs for Strings.
void PrintRefs(lldb::SBCommandReturnObject& result, v8::JSObject& js_obj,
Error& err, int level = 0) override;
private:
LLScan* llscan_;
std::string search_value_;
};
class StringScanner : public ObjectScanner {
public:
StringScanner(LLScan* llscan, std::string search_value)
: llscan_(llscan), search_value_(search_value) {}
bool AreReferencesLoaded() override;
ReferencesVector* GetReferences() override;
void ScanRefs(v8::JSObject& js_obj, Error& err) override;
void ScanRefs(v8::String& str, Error& err) override;
void PrintRefs(lldb::SBCommandReturnObject& result, v8::JSObject& js_obj,
Error& err, int level = 0) override;
void PrintRefs(lldb::SBCommandReturnObject& result, v8::String& str,
Error& err, int level = 0) override;
static const char* const property_reference_template;
static const char* const array_reference_template;
private:
LLScan* llscan_;
std::string search_value_;
};
private:
LLScan* llscan_; // FindReferencesCmd::llscan_
};
class MemoryVisitor {
public:
virtual ~MemoryVisitor() {}
virtual uint64_t Visit(uint64_t location, uint64_t available) = 0;
};
class DetailedTypeRecord;
class TypeRecord {
public:
TypeRecord(std::string& type_name)
: type_name_(type_name), instance_count_(0), total_instance_size_(0) {}
inline std::string& GetTypeName() { return type_name_; };
inline uint64_t GetInstanceCount() { return instance_count_; };
inline uint64_t GetTotalInstanceSize() { return total_instance_size_; };
inline std::unordered_set<uint64_t>& GetInstances() { return instances_; };
inline void AddInstance(uint64_t address, uint64_t size) {
auto result = instances_.insert(address);
if (result.second) {
instance_count_++;
total_instance_size_ += size;
}
};
/* Sort records by instance count, use the other fields as tie breakers
* to give consistent ordering.
*/
static bool CompareInstanceCounts(TypeRecord* a, TypeRecord* b) {
if (a->instance_count_ == b->instance_count_) {
if (a->total_instance_size_ == b->total_instance_size_) {
return a->type_name_ < b->type_name_;
}
return a->total_instance_size_ < b->total_instance_size_;
}
return a->instance_count_ < b->instance_count_;
}
private:
friend class DetailedTypeRecord;
std::string type_name_;
uint64_t instance_count_;
uint64_t total_instance_size_;
std::unordered_set<uint64_t> instances_;
};
class DetailedTypeRecord : public TypeRecord {
public:
DetailedTypeRecord(std::string& type_name, uint64_t own_descriptors_count,
uint64_t indexed_properties_count)
: TypeRecord(type_name),
own_descriptors_count_(own_descriptors_count),
indexed_properties_count_(indexed_properties_count) {}
uint64_t GetOwnDescriptorsCount() const { return own_descriptors_count_; };
uint64_t GetIndexedPropertiesCount() const {
return indexed_properties_count_;
};
private:
std::vector<std::string> properties_;
uint64_t own_descriptors_count_;
uint64_t indexed_properties_count_;
};
typedef std::map<std::string, TypeRecord*> TypeRecordMap;
typedef std::map<std::string, DetailedTypeRecord*> DetailedTypeRecordMap;
class FindJSObjectsVisitor : MemoryVisitor {
public:
FindJSObjectsVisitor(lldb::SBTarget& target, LLScan* llscan);
~FindJSObjectsVisitor() {}
uint64_t Visit(uint64_t location, uint64_t word);
uint32_t FoundCount() { return found_count_; }
private:
// TODO (mmarchini): this could be an option for findjsobjects
static const size_t kNumberOfPropertiesForDetailedOutput = 3;
struct MapCacheEntry {
enum ShowArrayLength { kShowArrayLength, kDontShowArrayLength };
std::string type_name;
bool is_histogram;
bool is_context;
std::vector<std::string> properties_;
uint64_t own_descriptors_count_ = 0;
uint64_t indexed_properties_count_ = 0;
std::string GetTypeNameWithProperties(
ShowArrayLength show_array_length = kShowArrayLength,
size_t max_properties = 0);
bool Load(v8::Map map, v8::HeapObject heap_object, v8::LLV8* llv8,
Error& err);
};
static bool IsAHistogramType(v8::Map& map, Error& err);
void InsertOnContexts(uint64_t word, Error& err);
void InsertOnMapsToInstances(uint64_t word, v8::Map map,
FindJSObjectsVisitor::MapCacheEntry map_info,
Error& err);
void InsertOnDetailedMapsToInstances(
uint64_t word, v8::Map map, FindJSObjectsVisitor::MapCacheEntry map_info,
Error& err);
lldb::SBTarget& target_;
uint32_t address_byte_size_;
uint32_t found_count_;
LLScan* const llscan_;
std::map<int64_t, MapCacheEntry> map_cache_;
};
class LLScan {
public:
LLScan(v8::LLV8* llv8) : llv8_(llv8) {}
v8::LLV8* v8() { return llv8_; }
bool ScanHeapForObjects(lldb::SBTarget target,
lldb::SBCommandReturnObject& result);
inline TypeRecordMap& GetMapsToInstances() { return mapstoinstances_; };
inline DetailedTypeRecordMap& GetDetailedMapsToInstances() {
return detailedmapstoinstances_;
};
// References By Value
inline bool AreReferencesByValueLoaded() {
return references_by_value_.size() > 0;
};
inline ReferencesVector* GetReferencesByValue(uint64_t address) {
if (references_by_value_.count(address) == 0) {
references_by_value_[address] = new ReferencesVector;
}
return references_by_value_[address];
};
// References By Property
inline bool AreReferencesByPropertyLoaded() {
return references_by_property_.size() > 0;
};
inline ReferencesVector* GetReferencesByProperty(std::string property) {
if (references_by_property_.count(property) == 0) {
references_by_property_[property] = new ReferencesVector;
}
return references_by_property_[property];
};
// References By String
inline bool AreReferencesByStringLoaded() {
return references_by_string_.size() > 0;
};
inline ReferencesVector* GetReferencesByString(std::string string_value) {
if (references_by_string_.count(string_value) == 0) {
references_by_string_[string_value] = new ReferencesVector;
}
return references_by_string_[string_value];
};
// Contexts
inline bool AreContextsLoaded() { return contexts_.size() > 0; };
inline ContextVector* GetContexts() { return &contexts_; }
v8::LLV8* llv8_;
private:
void ScanMemoryRegions(FindJSObjectsVisitor& v);
void ClearMapsToInstances();
void ClearReferences();
lldb::SBTarget target_;
lldb::SBProcess process_;
TypeRecordMap mapstoinstances_;
DetailedTypeRecordMap detailedmapstoinstances_;
ReferencesByValueMap references_by_value_;
ReferencesByPropertyMap references_by_property_;
ReferencesByStringMap references_by_string_;
ContextVector contexts_;
};
} // namespace llnode
#endif // SRC_LLSCAN_H_