This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_backtrace.linux.cc
executable file
·195 lines (155 loc) · 4.98 KB
/
node_backtrace.linux.cc
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
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <nan.h>
using namespace v8;
#define MAX_STACK_DEPTH 200
static const size_t FP_RETURN_ADDRESS_OFFSET = 0x08;
static size_t frames[MAX_STACK_DEPTH * 2];
static int fcount;
static Persistent<Array> jsframes;
size_t strip_low_bit(size_t addr) {
return addr & ~(size_t)1;
}
size_t read_user_size_t(pid_t pid, size_t addr) {
size_t result;
errno = 0;
result = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
if (errno != 0) {
// printf("Error peeking at address 0x%016"PRIx64"\n", addr);
return 0;
}
return result;
}
size_t read_user_pointer(pid_t pid, size_t addr) {
return strip_low_bit(read_user_size_t(pid, strip_low_bit(addr)));
}
long attach_process(pid_t pid) {
return ptrace(PTRACE_SEIZE, (pid_t)pid, NULL, 0);
}
long pause_process(pid_t pid) {
int status;
long ptrace_result = ptrace(PTRACE_INTERRUPT, pid, NULL, NULL);
if (ptrace_result != 0) {
return ptrace_result;
}
waitpid(pid, &status, WSTOPPED);
return ptrace_result;
}
long resume_process(pid_t pid) {
return ptrace(PTRACE_CONT, pid, NULL, NULL);
}
long detach_process(pid_t pid) {
pause_process(pid);
return ptrace(PTRACE_DETACH, pid, NULL, NULL);
}
long take_backtrace(pid_t pid) {
size_t pc;
size_t frame;
struct user_regs_struct regs;
long pause_result = pause_process(pid);
if (pause_result != 0) {
return pause_result;
}
long ptrace_result = ptrace(PTRACE_GETREGS, pid, NULL, ®s);
if (ptrace_result != 0) {
return ptrace_result;
}
pc = regs.rip;
frame = regs.rbp;
for (fcount = 0; fcount < MAX_STACK_DEPTH && frame != 0; fcount++) {
frames[fcount * 2] = pc;
frames[fcount * 2 + 1] = frame;
pc = read_user_pointer(pid, frame + FP_RETURN_ADDRESS_OFFSET);
frame = read_user_pointer(pid, frame);
}
return ptrace_result;
}
NAN_METHOD(PTraceAttach) {
NanScope();
pid_t pid = (pid_t)args[0].As<Integer>()->Int32Value();
long result = attach_process(pid);
if (result == 0) {
NanReturnValue(NanTrue());
} else {
NanReturnValue(NanFalse());
}
}
NAN_METHOD(PTraceBacktrace) {
NanScope();
pid_t pid = (pid_t)args[0].As<Integer>()->Int32Value();
long result = take_backtrace(pid);
if (result == 0) {
jsframes->Set(0, NanNew<Int32>(fcount));
for (int i = 0; i < fcount; i++) {
jsframes->Set(2 * i + 1, NanNew<Number>(frames[2 * i]));
jsframes->Set(2 * i + 2, NanNew<Number>(frames[2 * i + 1]));
}
} else {
jsframes->Set(0, NanNew<Int32>(-1));
}
NanReturnValue(jsframes);
}
NAN_METHOD(PTraceResume) {
NanScope();
pid_t pid = (pid_t)args[0].As<Integer>()->Int32Value();
long result = resume_process(pid);
if (result == 0) {
NanReturnValue(NanTrue());
} else {
NanReturnValue(NanFalse());
}
}
NAN_METHOD(PTraceDetach) {
NanScope();
pid_t pid = (pid_t)args[0].As<Integer>()->Int32Value();
long result = detach_process(pid);
if (result == 0) {
NanReturnValue(NanTrue());
} else {
NanReturnValue(NanFalse());
}
}
void Init(Handle<Object> exports) {
NanAssignPersistent(jsframes, NanNew<Array>(MAX_STACK_DEPTH * 2 + 1));
exports->Set(
NanNew("attach"),
NanNew<FunctionTemplate>(PTraceAttach)->GetFunction()
);
exports->Set(
NanNew("backtrace"),
NanNew<FunctionTemplate>(PTraceBacktrace)->GetFunction()
);
exports->Set(
NanNew("resume"),
NanNew<FunctionTemplate>(PTraceResume)->GetFunction()
);
exports->Set(
NanNew("detach"),
NanNew<FunctionTemplate>(PTraceDetach)->GetFunction()
);
}
NODE_MODULE(node_backtrace, Init);