Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·455 lines (402 loc) · 14.6 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·455 lines (402 loc) · 14.6 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "common.h"
#include "command_script.h"
#include "version.h"
#include <chrono>
int g_qid_recv = -1;
int g_qid_send = -1;
int g_pid;
std::string g_sohandle = "";
std::string g_validfuncaddrvalue = "";
std::string g_lstr = "";
std::string g_hookso = "";
const int MAX_FIND_WAIT_SECONDS = 20;
const int MAX_CONN_WAIT_SECONDS = 10;
int g_int;
std::string g_command = "";
std::string g_last_command = "";
int g_quit;
const std::vector <std::string> g_lua_hook_func = {"luaV_execute", "luaD_call", "luaD_precall"};
const int MAX_TRIGGER_DLOPEN_SECONDS = 5;
const int MAX_TRIGGER_DLCLOSE_SECONDS = 5;
const int MAX_TRIGGER_ARG_SECONDS = 1;
bool g_agent_started = false;
ScriptOptions g_options;
long long cli_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
bool executable_on_path(const std::string &name) {
const char *raw = getenv("PATH");
if (!raw) return false;
std::istringstream path(raw);
for (std::string dir; std::getline(path, dir, ':');)
if (path_is_executable((dir.empty() ? "." : dir) + "/" + name)) return true;
return false;
}
int doctor() {
int missing = 0;
printf("dLua %s diagnostics (read-only)\n", DLUA_VERSION);
bool gdb = executable_on_path("gdb");
printf("gdb: %s\n", gdb ? "found" : "MISSING"); if (!gdb) ++missing;
std::string error;
std::string hookso = resolve_hookso(error);
printf("hookso: %s\n", hookso.empty() ? "MISSING" : hookso.c_str());
if (hookso.empty()) { ++missing; fprintf(stderr, "%s", error.c_str()); }
std::string agent = resolve_dluaagent(error);
printf("agent: %s\n", agent.empty() ? "MISSING" : agent.c_str());
if (agent.empty()) { ++missing; fprintf(stderr, "%s", error.c_str()); }
printf("Lua CLI: %s (optional for embedded Lua targets)\n", executable_on_path("lua") ? "found" : "not found");
std::ifstream yama("/proc/sys/kernel/yama/ptrace_scope");
std::string scope;
if (yama >> scope) printf("Yama ptrace_scope: %s%s\n", scope.c_str(), scope == "0" ? "" : " (attach may require permission)");
else printf("Yama ptrace_scope: unavailable (kernel/container may restrict ptrace)\n");
printf("Target Lua ABI/exported symbols and actual ptrace access must be verified at attach.\n");
return missing ? 1 : 0;
}
int usage() {
printf("Usage: dlua [-p PID | PID] [--batch | --batch-silent] [-ex COMMAND] [-x FILE]\n");
printf(" --doctor checks dependencies/ptrace; --version prints the version.\n");
printf(" -ex/--eval-command and -x/--command may repeat; source FILE reads debugger commands.\n");
printf(" Scripts support inspection, breakpoint edits and bounded run-control commands.\n");
printf(" Env: DLUA_HOOKSO=path/to/hookso DLUA_AGENT=path/to/dluaagent.so\n");
return -1;
}
std::string find_func_addr(std::string hook_func) {
std::string funcaddrvalue = "";
int out = 0;
std::string cmd = "timeout 5 gdb -p " + std::to_string(g_pid) + " -ex \"p (long)" + hook_func + "\" --batch";
std::string ret = exec_command(cmd, out);
if (out != 0) {
DLOG("exec_command fail pid %d command %s", g_pid, cmd.c_str());
return funcaddrvalue;
}
std::string key = "$1 = ";
int left = ret.find(key);
if (left == std::string::npos) {
DLOG("exec_command ret %s no keyword", ret.c_str());
return funcaddrvalue;
}
ret = ret.substr(left);
int right = ret.find("\n");
if (right == std::string::npos) {
ret = ret.substr(key.length());
} else {
ret = ret.substr(key.length(), right - key.length());
}
ret.erase(std::remove(ret.begin(), ret.end(), '\n'), ret.end());
ret.erase(std::remove(ret.begin(), ret.end(), ' '), ret.end());
DLOG("func addr ret %s", ret.c_str());
funcaddrvalue = ret;
return funcaddrvalue;
}
long long find_luastate_breakpoint(std::string hook_func, std::string &validfuncaddrvalue) {
DLOG("find_luastate_breakpoint start %d", g_pid);
// get hook_func func addr
std::string funcaddrvalue = find_func_addr(hook_func);
long long lvalue = -1;
// get hook_func func param
{
int out = 0;
std::string cmd = "timeout " + std::to_string(MAX_TRIGGER_ARG_SECONDS) + " " + g_hookso +
" argp " + std::to_string(g_pid) + " " + funcaddrvalue + " 1";
std::string ret = exec_command(cmd, out);
if (out != 0) {
DLOG("exec_command fail pid %d command %s", g_pid, cmd.c_str());
return -1;
}
ret.erase(std::remove(ret.begin(), ret.end(), '\n'), ret.end());
DLOG("exec_command hookso call ret %s", ret.c_str());
lvalue = strtoll(ret.c_str(), NULL, 10);
if (lvalue <= 0) return -1;
validfuncaddrvalue = funcaddrvalue;
}
return lvalue;
}
long long find_luastate(std::string &validfuncaddrvalue) {
long long ret = -1;
validfuncaddrvalue = "";
for (int i = 0; i < g_lua_hook_func.size(); ++i) {
if (ret == -1) {
ret = find_luastate_breakpoint(g_lua_hook_func[i], validfuncaddrvalue);
}
}
return ret;
}
int fini_env() {
int result = 0;
if (g_agent_started && kill(g_pid, 0) == 0) {
int out = 0;
exec_command("timeout 5 " + g_hookso + " call " + std::to_string(g_pid) +
" dluaagent.so stop_agent", out);
if (out != 0) result = -1;
bool stopped = false;
long long deadline = cli_ms() + 10000;
while (out == 0 && cli_ms() < deadline) {
std::string state = exec_command("timeout 2 " + g_hookso + " call " +
std::to_string(g_pid) + " dluaagent.so is_stop_agent", out);
if (script_trim(state) == "1") { stopped = true; break; }
usleep(10000);
}
if (!stopped) { fprintf(stderr, "agent detach did not complete\n"); result = -1; }
// Keep the library mapped: dormant coroutines may still hold copied
// hooks. Unmapping the library here would leave dangling pointers.
}
remove_msg_queue(g_qid_send); g_qid_send = -1;
remove_msg_queue(g_qid_recv); g_qid_recv = -1;
unlink(dlua_ipc_tmp_path(g_pid, 1).c_str());
unlink(dlua_ipc_tmp_path(g_pid, 2).c_str());
return result;
}
void int_handler(int sig) {
signal(SIGINT, SIG_IGN);
g_int = 1;
signal(SIGINT, int_handler);
}
int init_env() {
DLOG("init_env start %d", g_pid);
// get lua state
long long lvalue = -1;
int last = time(0);
std::string validfuncaddrvalue = "";
while (time(0) - last < MAX_FIND_WAIT_SECONDS) {
if (kill(g_pid, 0) != 0) return -1;
lvalue = find_luastate(validfuncaddrvalue);
if (lvalue == -1 || validfuncaddrvalue == "") {
usleep(100);
} else {
break;
}
}
if (lvalue == -1 || validfuncaddrvalue == "") {
DERR("can not find pid %d lua state", g_pid);
return -1;
}
std::string lstr = std::to_string(lvalue);
DLOG("lstr %s", lstr.c_str());
g_lstr = lstr;
signal(SIGINT, int_handler);
std::string agent_err;
std::string agent_path = resolve_dluaagent(agent_err);
if (agent_path.empty()) {
fprintf(stderr, "%s", agent_err.c_str());
DERR("resolve_dluaagent fail");
return -1;
}
DLOG("dluaagent path %s", agent_path.c_str());
// inject so (absolute path for dlopen; post-inject calls still use soname dluaagent.so)
{
int out = 0;
std::string cmd = "timeout " + std::to_string(MAX_TRIGGER_DLOPEN_SECONDS) + " " + g_hookso +
" triggerp " + std::to_string(g_pid) + " " + validfuncaddrvalue +
" dlopen " + agent_path;
std::string ret = exec_command(cmd, out);
if (out != 0) {
DERR("exec_command fail pid %d command %s", g_pid, cmd.c_str());
return -1;
}
ret.erase(std::remove(ret.begin(), ret.end(), '\n'), ret.end());
if (ret.empty() || ret == "0" || ret == "-1") return -1;
g_sohandle = ret;
g_validfuncaddrvalue = validfuncaddrvalue;
DLOG("exec_command hookso dlopen ret %s", ret.c_str());
}
// open agent
{
int out = 0;
std::string cmd = "timeout 5 " + g_hookso + " call " + std::to_string(g_pid) +
" dluaagent.so start_agent i=" + lstr + " i=" + std::to_string(g_pid);
std::string ret = exec_command(cmd, out);
if (out != 0) {
DERR("exec_command fail pid %d command %s", g_pid, cmd.c_str());
return -1;
}
ret.erase(std::remove(ret.begin(), ret.end(), '\n'), ret.end());
DLOG("exec_command hookso call ret %s", ret.c_str());
if (script_trim(ret) != "0") return -1;
g_agent_started = true;
}
return 0;
}
int process_msg(long type, char data[QUEUED_MESSAGE_MSG_LEN]) {
if (type == SHOW_MSG) {
std::string tmp = data;
if (tmp != "") {
if (tmp[tmp.length() - 1] != '\n') {
tmp += "\n";
}
printf("%s", tmp.c_str());
}
} else if (type == INPUT_MSG) {
g_int = 1;
}
return 0;
}
int get_command() {
signal(SIGINT, SIG_IGN);
char *command = readline("\n(dlua) ");
signal(SIGINT, int_handler);
if (!command) {
g_quit = 1;
return 0;
}
int oldpos = history_search_pos(command, 1, 0);
if (oldpos < 0) {
add_history(command);
} else {
remove_history(oldpos);
add_history(command);
}
g_command = command;
free(command);
if (g_command.size() == 0) {
g_command = g_last_command;
return 0;
}
g_last_command = g_command;
return 0;
}
int run_script_commands(const std::vector<std::string> &commands) {
struct Release {
~Release() { send_msg(g_qid_send, RELEASE_MSG, "", IPC_NOWAIT); }
} release;
for (const auto &command : commands) {
if (command == "q" || command == "quit") { g_quit = 1; break; }
if (send_msg(g_qid_send, SCRIPT_MSG, command.c_str(), IPC_NOWAIT) != 0) return -1;
bool done = false;
long long deadline = cli_ms() + 10000;
while (cli_ms() < deadline) {
long type = 0;
char data[QUEUED_MESSAGE_MSG_LEN] = {0};
if (recv_msg(g_qid_recv, type, data) != 0 || kill(g_pid, 0) != 0) return -1;
if (type == RESULT_MSG) {
if (std::string(data) != "ok") {
fprintf(stderr, "command failed: %s\n", command.c_str()); return -1;
}
fflush(stdout); // make batch progress observable through a pipe/file
done = true; break;
}
if (type == SHOW_MSG && !g_options.silent) process_msg(type, data);
if (!type) usleep(1000);
}
if (!done) { fprintf(stderr, "command timed out: %s\n", command.c_str()); return -1; }
}
return 0;
}
int process_command() {
if (g_command == "") {
return 0;
}
std::vector <std::string> result;
std::istringstream iss(g_command);
for (std::string s; iss >> s;) {
result.push_back(s);
}
if (result.empty()) {
return 0;
}
std::string token = result[0];
DLOG("command begin %s", token.c_str());
if (token == "source") {
std::vector<std::string> commands;
std::string error;
if (!expand_script_command(g_command, commands, error)) fprintf(stderr, "%s\n", error.c_str());
else if (run_script_commands(commands) != 0) fprintf(stderr, "source stopped after error\n");
} else if (token == "q" || token == "quit") {
g_quit = 1;
} else {
send_msg(g_qid_send, COMMAND_MSG, g_command.c_str());
}
g_command = "";
return 0;
}
int process() {
DLOG("process start");
char msg[QUEUED_MESSAGE_MSG_LEN] = {0};
long msgtype = 0;
bool ini_ok = false;
int last_time = time(0);
while (time(0) - last_time < MAX_CONN_WAIT_SECONDS) {
if (kill(g_pid, 0) != 0) return -1;
usleep(1000);
if (recv_msg(g_qid_recv, msgtype, msg) != 0) {
break;
}
if (msgtype == LOGIN_MSG) {
if (std::to_string(g_pid) == std::string(msg)) {
ini_ok = true;
break;
}
}
}
if (!ini_ok) {
DERR("can not connect agent %d", g_pid);
return -1;
}
DLOG("connect ok %d", g_pid);
if (!g_options.batch) printf("attach to %d ok, use ctrl+c to input command, eg: h\n", g_pid);
if (run_script_commands(g_options.commands) != 0) return -1;
if (g_options.batch || g_quit) return 0;
while (g_quit != 1) {
if (kill(g_pid, 0) != 0) return -1;
if (g_int != 0) {
g_int = 0;
get_command();
process_command();
}
if (recv_msg(g_qid_recv, msgtype, msg) != 0) {
DERR("recv_msg fail");
return -1;
}
if (msgtype == 0) {
usleep(1);
continue;
} else {
if (process_msg(msgtype, msg) != 0) {
DERR("process_msg fail");
return -1;
}
}
}
DLOG("disconnect from %d", g_pid);
return 0;
}
int main(int argc, char **argv) {
std::string option_error;
if (!parse_script_options(argc, argv, g_options, option_error)) {
fprintf(stderr, "%s\n", option_error.c_str()); usage(); return 2;
}
if (g_options.help) { usage(); return 0; }
if (g_options.version) { printf("dLua %s\n", DLUA_VERSION); return 0; }
if (g_options.doctor) return doctor();
int pid = g_options.pid;
DLOG("start main %d", pid);
g_pid = pid;
std::string hookso_err;
g_hookso = resolve_hookso(hookso_err);
if (g_hookso.empty()) {
fprintf(stderr, "%s", hookso_err.c_str());
return 1;
}
DLOG("hookso path %s", g_hookso.c_str());
// Per-pid paths so ftok keys cannot collide across pids (proj_id is 8-bit).
std::string path1 = dlua_ipc_tmp_path(g_pid, 1);
std::string path2 = dlua_ipc_tmp_path(g_pid, 2);
int qid1 = open_msg_queue(path1.c_str(), 1, 0600);
if (qid1 < 0) {
DERR("open_msg_queue fail");
fini_env(); return 1;
}
g_qid_send = qid1;
int qid2 = open_msg_queue(path2.c_str(), 1, 0600);
if (qid2 < 0) {
DERR("open_msg_queue fail");
fini_env(); return 1;
}
g_qid_send = qid1;
g_qid_recv = qid2;
int result = send_msg(g_qid_send, CLIENT_MSG, std::to_string(getpid()).c_str(), IPC_NOWAIT);
if (result == 0) result = init_env();
if (result == 0) result = process();
int cleanup = fini_env();
return result == 0 && cleanup == 0 ? 0 : 1;
}