-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathllnode_module.cc
More file actions
298 lines (233 loc) · 8.8 KB
/
llnode_module.cc
File metadata and controls
298 lines (233 loc) · 8.8 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
// Javascript module API for llnode/lldb
#include <cinttypes>
#include <cstdlib>
#include "src/llnode_api.h"
#include "src/llnode_module.h"
namespace llnode {
using Napi::Array;
using Napi::CallbackInfo;
using Napi::Function;
using Napi::FunctionReference;
using Napi::HandleScope;
using Napi::Number;
using Napi::Object;
using Napi::ObjectReference;
using Napi::Persistent;
using Napi::Reference;
using Napi::String;
using Napi::Symbol;
using Napi::TypeError;
using Napi::Value;
FunctionReference LLNode::constructor;
template <typename T>
bool HasInstance(Object obj) {
return obj.InstanceOf(T::constructor.Value());
}
Object LLNode::Init(Napi::Env env, Object exports) {
HandleScope scope(env);
Function func = DefineClass(
env, "LLNode",
{
InstanceMethod("getProcessInfo", &LLNode::GetProcessInfo),
InstanceMethod("getProcessObject", &LLNode::GetProcessObject),
InstanceMethod("getHeapTypes", &LLNode::GetHeapTypes),
InstanceMethod("getObjectAtAddress", &LLNode::GetObjectAtAddress),
});
constructor = Persistent(func);
constructor.SuppressDestruct();
exports.Set("fromCoredump",
Function::New(env, LLNode::FromCoreDump, "fromCoredump"));
exports.Set("LLNode", func);
return exports;
}
LLNode::LLNode(const CallbackInfo& args)
: ObjectWrap<LLNode>(args),
heap_initialized_(false),
api_(new llnode::LLNodeApi){};
LLNode::~LLNode() {}
Value LLNode::FromCoreDump(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (!args[0].IsString() || !args[1].IsString()) {
TypeError::New(env, "Must be called as fromCoreDump(filename, executable)")
.ThrowAsJavaScriptException();
return env.Null();
}
Napi::Object llnode_obj = constructor.New({});
LLNode* obj = ObjectWrap<LLNode>::Unwrap(llnode_obj);
obj->heap_initialized_ = false;
std::string filename = args[0].As<String>();
std::string executable = args[1].As<String>();
bool ret = obj->api_->Init(filename.c_str(), executable.c_str());
if (!ret) {
TypeError::New(env, "Failed to load coredump").ThrowAsJavaScriptException();
return env.Null();
}
return llnode_obj;
}
#define CHECK_INITIALIZED(api, env) \
if (!api->IsInitialized()) { \
TypeError::New(env, "LLNode has not been initialized") \
.ThrowAsJavaScriptException(); \
return env.Null(); \
}
Value LLNode::GetProcessInfo(const CallbackInfo& args) {
CHECK_INITIALIZED(this->api_, args.Env())
return String::New(args.Env(), this->api_->GetProcessInfo());
}
Value LLNode::GetProcessObject(const CallbackInfo& args) {
Napi::Env env = args.Env();
CHECK_INITIALIZED(this->api_, env)
uint32_t pid = this->api_->GetProcessID();
std::string state = this->api_->GetProcessState();
uint32_t thread_count = this->api_->GetThreadCount();
Object result = Object::New(env);
result.Set(String::New(env, "pid"), Number::New(env, pid));
result.Set(String::New(env, "state"), String::New(env, state));
result.Set(String::New(env, "threadCount"), Number::New(env, thread_count));
Array thread_list = Array::New(env);
for (size_t i = 0; i < thread_count; i++) {
Object thread = Object::New(env);
thread.Set(String::New(env, "threadId"), Number::New(env, i));
uint32_t frame_count = this->api_->GetFrameCount(i);
thread.Set(String::New(env, "frameCount"), Number::New(env, frame_count));
Array frame_list = Array::New(env);
for (size_t j = 0; j < frame_count; j++) {
Object frame = Object::New(env);
std::string frame_str = this->api_->GetFrame(i, j);
frame.Set(String::New(env, "function"), String::New(env, frame_str));
frame_list.Set(j, frame);
}
thread.Set(String::New(env, "frames"), frame_list);
thread_list.Set(i, thread);
}
result.Set(String::New(env, "threads"), thread_list);
return result;
}
Value LLNode::GetHeapTypes(const CallbackInfo& args) {
Napi::Env env = args.Env();
CHECK_INITIALIZED(this->api_, env)
Object llnode_obj = args.This().As<Object>();
// Initialize the heap and the type iterators
if (!this->heap_initialized_) {
this->api_->ScanHeap();
this->heap_initialized_ = true;
}
uint32_t type_count = this->api_->GetTypeCount();
Array type_list = Array::New(env);
for (size_t i = 0; i < type_count; i++) {
Object type_obj = LLNodeHeapType::constructor.New(
{llnode_obj, Number::New(env, static_cast<uint32_t>(i))});
type_list.Set(i, type_obj);
}
return type_list;
}
Object LLNode::GetObjectAtAddress(Napi::Env env, uint64_t addr) {
Object result = Object::New(env);
char buf[20];
snprintf(buf, sizeof(buf), "0x%016" PRIx64, addr);
result.Set(String::New(env, "address"), String::New(env, buf));
std::string value = this->api_->GetObject(addr);
result.Set(String::New(env, "value"), String::New(env, value));
return result;
}
// TODO: create JS object to introspect core dump
// process/threads/frames
Value LLNode::GetObjectAtAddress(const CallbackInfo& args) {
Napi::Env env = args.Env();
CHECK_INITIALIZED(this->api_, env)
if (!args[0].IsString()) {
TypeError::New(env, "First argument must be a string")
.ThrowAsJavaScriptException();
return env.Null();
}
std::string address_str = args[0].As<String>();
if (address_str[0] != '0' || address_str[1] != 'x' ||
address_str.size() > 18) {
TypeError::New(env, "Invalid address").ThrowAsJavaScriptException();
return env.Null();
}
uint64_t addr = std::strtoull(address_str.c_str(), nullptr, 16);
Object result = this->GetObjectAtAddress(args.Env(), addr);
return result;
}
FunctionReference LLNodeHeapType::constructor;
Object LLNodeHeapType::Init(Napi::Env env, Object exports) {
HandleScope scope(env);
Function func = DefineClass(env, "LLNodeHeapType", {});
constructor = Persistent(func);
constructor.SuppressDestruct();
exports.Set(String::New(env, "LLNodeHeapType"), func);
exports.Set(String::New(env, "nextInstance"),
Function::New(env, LLNodeHeapType::NextInstance, "nextInstance"));
return exports;
}
LLNodeHeapType::~LLNodeHeapType() {}
LLNodeHeapType::LLNodeHeapType(const CallbackInfo& args)
: ObjectWrap<LLNodeHeapType>(args) {
Napi::Env env = args.Env();
if (!args[0].IsObject() || !HasInstance<LLNode>(args[0].As<Object>())) {
TypeError::New(env, "First argument must be a LLNode instance")
.ThrowAsJavaScriptException();
return;
}
if (!args[1].IsNumber()) {
TypeError::New(env, "Second argument must be a number")
.ThrowAsJavaScriptException();
return;
}
Object llnode_obj = args[0].As<Object>();
LLNode* llnode_ptr = ObjectWrap<LLNode>::Unwrap(llnode_obj);
uint32_t index = args[1].As<Number>().Uint32Value();
this->llnode_ = Persistent(llnode_obj);
this->instances_initialized_ = false;
this->current_instance_index_ = 0;
this->type_name_ = llnode_ptr->api_->GetTypeName(index);
this->type_index_ = index;
this->type_ins_count_ = llnode_ptr->api_->GetTypeInstanceCount(index);
this->type_total_size_ = llnode_ptr->api_->GetTypeTotalSize(index);
Object instance = args.This().As<Object>();
String typeName = String::New(env, "typeName");
String instanceCount = String::New(env, "instanceCount");
String totalSize = String::New(env, "totalSize");
instance.Set(typeName, String::New(env, this->type_name_));
instance.Set(instanceCount, Number::New(env, this->type_ins_count_));
instance.Set(totalSize, Number::New(env, this->type_total_size_));
instance.Set(String::New(env, "llnode"), llnode_obj);
}
LLNode* LLNodeHeapType::llnode() {
return ObjectWrap<LLNode>::Unwrap(this->llnode_.Value());
}
void LLNodeHeapType::InitInstances() {
auto instances_set =
this->llnode()->api_->GetTypeInstances(this->type_index_);
this->current_instance_index_ = 0;
for (const uint64_t& addr : *instances_set) {
this->type_instances_.push_back(addr);
}
this->type_ins_count_ = this->type_instances_.size();
this->instances_initialized_ = true;
}
bool LLNodeHeapType::HasMoreInstances() {
return current_instance_index_ < type_ins_count_;
}
Value LLNodeHeapType::NextInstance(const CallbackInfo& args) {
Napi::Env env = args.Env();
if (!args[0].IsObject() ||
!HasInstance<LLNodeHeapType>(args[0].As<Object>())) {
TypeError::New(env, "First argument must be a LLNoteHeapType instance")
.ThrowAsJavaScriptException();
return env.Undefined();
}
LLNodeHeapType* obj =
ObjectWrap<LLNodeHeapType>::Unwrap(args[0].As<Object>());
if (!obj->instances_initialized_) {
obj->InitInstances();
}
if (!obj->HasMoreInstances()) {
return env.Undefined();
}
uint64_t addr = obj->type_instances_[obj->current_instance_index_++];
Object result = obj->llnode()->GetObjectAtAddress(env, addr);
return result;
}
} // namespace llnode