1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include <assert.h> |
6 | #include <unistd.h> |
7 | #include <errno.h> |
8 | #include <string.h> |
9 | #include <dirent.h> |
10 | #include <fcntl.h> |
11 | #include <stdio.h> |
12 | #include <stdlib.h> |
13 | #include <sys/stat.h> |
14 | #include <sys/prctl.h> |
15 | #include <sys/socket.h> |
16 | #include <sys/ptrace.h> |
17 | #include <sys/types.h> |
18 | #include <sys/wait.h> |
19 | #include <linux/unistd.h> |
20 | #include <linux/capability.h> |
21 | #include <libgen.h> |
22 | #include <sys/statvfs.h> |
23 | |
24 | #include <algorithm> |
25 | #include <string> |
26 | #include <vector> |
27 | #include <list> |
28 | #include <memory> |
29 | |
30 | #include "WaitStatus.h" |
31 | #include "ScopedFd.h" |
32 | #include "config.h" |
33 | #include "utils.h" |
34 | #include "session.h" |
35 | #include "replay.h" |
36 | #include "easylogging++.h" |
37 | #include "event_man.h" |
38 | #include "preload/x11preload.h" |
39 | |
40 | INITIALIZE_EASYLOGGINGPP |
41 | |
42 | using namespace std; |
43 | |
44 | static void usage(const char* name) |
45 | { |
46 | printf("usage:\n" ); |
47 | printf("\n%s ps [trace-dir] # list pid of all process recorded\n" , name); |
48 | printf("\n%s dump [trace-dir] [pid] # dump raw event\n" , name); |
49 | printf("\n%s replay [trace-dir] [pid] # view event in interactive console\n" , name); |
50 | printf("\n%s [options] executable-file [executable parameter...] # record event\n" , name); |
51 | printf("options: \n" ); |
52 | printf(" -1: only record current thread stack\n" ); |
53 | printf(" --stack-size=: specified maximum K-bytes of stack to dump\n" ); |
54 | printf(" --heap-size=: specified maximum K-bytes of heap to dump\n" ); |
55 | printf(" --param-size=: specified maximum bytes of syscall parameter to dump\n" ); |
56 | printf(" --var=[*]var1+size1[,[*]var2+size2,...]: Trace global variables\n" ); |
57 | printf(" --func=mangle-name: Trace start after the specified function called(see nm -C)\n" ); |
58 | printf(" --vdso=on/off: Turn on/off intercept function in vdso\n" ); |
59 | printf(" --sys=[!]filter: filter can be one or more of these\n" |
60 | " [file,ipc,network,process,signal,desc,memory, or concrete syscall-name / syscall-no]\n" |
61 | " file, Trace all system calls which take a file name as an argument.\n" |
62 | " process, Trace all system calls which involve process management.\n" |
63 | " network, Trace all the network related system calls.\n" |
64 | " signal, Trace all signal related system calls.\n" |
65 | " ipc, Trace all IPC related system calls.\n" |
66 | " desc, Trace all file descriptor related system calls.\n" |
67 | " memory, Trace all memory mapping related system calls.\n" ); |
68 | printf(" --sig=signal-list: signal-list can be one or more of `kill -l`\n" ); |
69 | printf(" --dbus=type-list: type-list can be one or more of these\n" |
70 | " 1, method call\n" |
71 | " 2, method return\n" |
72 | " 3, error\n" |
73 | " 4, signal\n" ); |
74 | printf(" --x11=event-list: event-list can be one or more of these\n" |
75 | " 2, KeyPress\n" |
76 | " 3, KeyRelease\n" |
77 | " 4, ButtonPress\n" |
78 | " 5, ButtonRelease\n" |
79 | " 9, FocusIn\n" |
80 | " 10, FocusOut\n" |
81 | " 16, CreateNotify\n" |
82 | " 17, DestroyNotify\n" |
83 | " 18, UnmapNotify\n" |
84 | " 19, MapNotify\n" ); |
85 | } |
86 | |
87 | static void rolloutHandler(const char* filename, std::size_t size) |
88 | { |
89 | (void)size; |
90 | static unsigned int log_idx = 0; |
91 | |
92 | #ifdef _DEBUG |
93 | // SHOULD NOT LOG ANYTHING HERE BECAUSE LOG FILE IS CLOSED! |
94 | std::cout << "************** Rolling out [" << filename |
95 | << "] because it reached [" << size << " bytes]" << std::endl; |
96 | #endif |
97 | |
98 | // BACK IT UP |
99 | std::stringstream ss; |
100 | ss << "mv " << filename << " " << filename << "-backup." << ++log_idx; |
101 | system(ss.str().c_str()); |
102 | } |
103 | |
104 | static void spawned_child_fatal_error(const ScopedFd& err_fd, const char* format, ...) |
105 | { |
106 | va_list args; |
107 | va_start(args, format); |
108 | char* buf; |
109 | if (vasprintf(&buf, format, args) < 0) { |
110 | exit(1); |
111 | } |
112 | |
113 | char* buf2; |
114 | if (asprintf(&buf2, "%s (%s)" , buf, strerror(errno)) < 0) { |
115 | exit(1); |
116 | } |
117 | write(err_fd, buf2, strlen(buf2)); |
118 | _exit(1); |
119 | } |
120 | |
121 | string read_spawned_task_error(int fd) { |
122 | char buf[1024] = "" ; |
123 | ssize_t len = 0; |
124 | for (int i = 0; i<2; ++i) { |
125 | len = read(fd, buf, sizeof(buf)); |
126 | if (len > 0) { |
127 | buf[len] = 0; |
128 | LOG(ERROR) << "tracee error message: " << buf; |
129 | return string(buf, len); |
130 | } |
131 | sleep(1); |
132 | } |
133 | return string(); |
134 | } |
135 | |
136 | /** |
137 | * Prepare this process and its ancestors for recording/replay by |
138 | * preventing direct access to sources of nondeterminism, and ensuring |
139 | * that rr bugs don't adversely affect the underlying system. |
140 | */ |
141 | static void set_up_process(const ScopedFd& err_fd, |
142 | int send_sock, int send_sock_fd_number) |
143 | { |
144 | if (send_sock_fd_number != dup2(send_sock, send_sock_fd_number)) { |
145 | spawned_child_fatal_error(err_fd, "error duping to RESERVED_SOCKET_FD" ); |
146 | } |
147 | |
148 | /* If we're in setuid_sudo mode, we have CAP_SYS_ADMIN, so we don't need to |
149 | set NO_NEW_PRIVS here in order to install the seccomp filter later. In, |
150 | emulate any potentially privileged, operations, so we might as well set |
151 | no_new_privs */ |
152 | if (!has_effective_caps()) { |
153 | if (0 > prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { |
154 | spawned_child_fatal_error( err_fd, |
155 | "prctl(NO_NEW_PRIVS) failed, SECCOMP_FILTER is not available: your " |
156 | "kernel is too old. Use `record -n` to disable the filter." ); |
157 | } |
158 | } |
159 | } |
160 | |
161 | int run_child (const ScopedFd& error_fd, |
162 | int send_sock, |
163 | int send_sock_fd_number, |
164 | const char* exe_path_cstr, |
165 | const vector<string>& argv, |
166 | const vector<string>& envp) |
167 | { |
168 | pid_t pid = getpid(); |
169 | StringVectorToCharArray argv_array(argv); |
170 | StringVectorToCharArray envp_array(envp); |
171 | |
172 | set_up_process(error_fd, send_sock, send_sock_fd_number); |
173 | |
174 | /* Signal to tracer that we're configured. |
175 | * Induce a ptrace stop. Tracer (our parent) |
176 | * will resume us with PTRACE_SYSCALL and display |
177 | * the immediately following execve syscall. |
178 | * Can't do this on NOMMU systems, we are after |
179 | * vfork: parent is blocked, stopping would deadlock. |
180 | */ |
181 | kill(pid, SIGSTOP); |
182 | |
183 | execve(exe_path_cstr, argv_array.get(), envp_array.get()); |
184 | |
185 | switch (errno) { |
186 | case ENOENT: |
187 | spawned_child_fatal_error( error_fd, |
188 | "execve failed: '%s' (or interpreter) not found" , exe_path_cstr); |
189 | break; |
190 | default: |
191 | spawned_child_fatal_error(error_fd, "execve of '%s' failed" , exe_path_cstr); |
192 | break; |
193 | } |
194 | // Never returns! |
195 | |
196 | return 0; |
197 | } |
198 | |
199 | int attach_thread(pid_t tid, bool trace_syscall) |
200 | { |
201 | intptr_t options = PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | |
202 | PTRACE_O_TRACEEXEC | PTRACE_O_TRACEEXIT; |
203 | if (trace_syscall) { |
204 | options |= PTRACE_O_TRACESYSGOOD; |
205 | } |
206 | int ret = ptrace(PTRACE_SEIZE, tid, nullptr, (void*)(options | PTRACE_O_EXITKILL)); |
207 | if ((ret < 0) && (errno == EINVAL)) { |
208 | // PTRACE_O_EXITKILL was added in kernel 3.8, and we only need |
209 | // it for more robust cleanup, so tolerate not having it. |
210 | ret = ptrace(PTRACE_SEIZE, tid, nullptr, (void*)options); |
211 | } |
212 | if (ret < 0 ) { |
213 | LOG(ERROR) << "PTRACE_SEIZE failed for tid:" << tid |
214 | << ", ret:" << ret << ", errno:" << errno; |
215 | } |
216 | |
217 | return ret; |
218 | } |
219 | |
220 | static string find_preload_library(const string& name, const char* preload_list) |
221 | { |
222 | string lib_path("./" ); |
223 | size_t pos = name.rfind('/'); |
224 | if ( pos != string::npos) { |
225 | lib_path = name.substr(0, pos+1); |
226 | } |
227 | |
228 | string ret; |
229 | string file_name; |
230 | bool done = false; |
231 | const char* walk = preload_list; |
232 | |
233 | lib_path += "lib" ; |
234 | while (!done) { |
235 | const char* pos = strchr(walk, ','); |
236 | if (pos != nullptr) { |
237 | file_name = lib_path + string(walk, pos - walk) + "preload.so" ; |
238 | walk = pos + 1; |
239 | } |
240 | else { |
241 | file_name = lib_path + string(walk) + "preload.so" ; // last item |
242 | done = true; |
243 | } |
244 | if (access(file_name.c_str(), F_OK) == 0) { |
245 | ret += file_name; |
246 | if (!done) ret += ":" ; |
247 | } |
248 | } |
249 | |
250 | LOG(INFO) << "user preload:" << ret.data(); |
251 | |
252 | return ret; |
253 | } |
254 | |
255 | string find_needed_library_starting_with(const string& exe_file, |
256 | const string& prefix); |
257 | |
258 | static string lookup_by_path(const string& name) { |
259 | if (name.find('/') != string::npos) { |
260 | return name; |
261 | } |
262 | const char* env = getenv("PATH" ); |
263 | if (!env) { |
264 | return name; |
265 | } |
266 | char* p = strdup(env); |
267 | char* s = p; |
268 | while (*s) { |
269 | char* next = strchr(s, ':'); |
270 | if (next) { |
271 | *next = 0; |
272 | } |
273 | string file = string(s) + "/" + name; |
274 | struct stat st; |
275 | if (!stat(file.c_str(), &st) && S_ISREG(st.st_mode) && |
276 | !access(file.c_str(), X_OK)) { |
277 | free(p); |
278 | return file; |
279 | } |
280 | if (!next) { |
281 | break; |
282 | } |
283 | s = next + 1; |
284 | } |
285 | free(p); |
286 | return name; |
287 | } |
288 | |
289 | void init_env(vector<string>& env, |
290 | const string& exe_path, |
291 | const string& preload_lib_path) |
292 | { |
293 | char** envp = environ; |
294 | for (; *envp; ++envp) { |
295 | env.push_back(*envp); |
296 | } |
297 | |
298 | string full_path = lookup_by_path(string(exe_path)); |
299 | if (!preload_lib_path.empty()) { |
300 | string ld_preload = "LD_PRELOAD=" ; |
301 | string libasan = find_needed_library_starting_with(full_path, "libasan" ); |
302 | if (!libasan.empty()) { |
303 | // Put an LD_PRELOAD entry for it before our preload library, because |
304 | // it checks that it's loaded first |
305 | ld_preload += libasan + ":" ; |
306 | } |
307 | // Our preload lib should come first if possible, because that will |
308 | // speed up the loading of the other libraries. We supply a placeholder |
309 | // which is then mutated to the correct filename in |
310 | // Monkeypatcher::patch_after_exec. |
311 | ld_preload += preload_lib_path; |
312 | auto it = env.begin(); |
313 | for (; it != env.end(); ++it) { |
314 | if (it->find("LD_PRELOAD=" ) != 0) { |
315 | continue; |
316 | } |
317 | // Honor old preloads too. This may cause |
318 | // problems, but only in those libs, and |
319 | // that's the user's problem. |
320 | ld_preload += ":" ; |
321 | ld_preload += it->substr(it->find("=" ) + 1); |
322 | break; |
323 | } |
324 | if (it == env.end()) { |
325 | env.push_back(ld_preload); |
326 | } |
327 | else { |
328 | *it = ld_preload; |
329 | } |
330 | LOG(INFO) << "final preload:" << ld_preload.data(); |
331 | } |
332 | |
333 | env.push_back("RUNNING_UNDER_RR=1" ); |
334 | |
335 | // Stop Mesa using the GPU |
336 | env.push_back("LIBGL_ALWAYS_SOFTWARE=1" ); |
337 | |
338 | // Stop sssd from using shared-memory with its daemon |
339 | // env.push_back("SSS_NSS_USE_MEMCACHE=NO"); |
340 | |
341 | // Disable Gecko's "wait for gdb to attach on process crash" behavior, since |
342 | // it is useless when running under rr. |
343 | env.push_back("MOZ_GDB_SLEEP=0" ); |
344 | |
345 | // OpenSSL uses RDRAND, but we can disable it. These bitmasks are inverted |
346 | // and ANDed with the results of CPUID. The number below is 2^62, which is the |
347 | // bit for RDRAND support. |
348 | env.push_back("OPENSSL_ia32cap=~4611686018427387904:~0" ); |
349 | } |
350 | |
351 | void init_log(DumpConfig& cfg, const char* filename) |
352 | { |
353 | el::Configurations defaultConf; |
354 | |
355 | defaultConf.setToDefault(); |
356 | |
357 | if (cfg.log_debug) { |
358 | defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true" ); |
359 | } |
360 | else { |
361 | defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false" ); |
362 | } |
363 | /*To hide %user,%host,%func,%file,%line*/ |
364 | defaultConf.set(el::Level::Debug, el::ConfigurationType::Format, "%datetime %level %msg" ); |
365 | defaultConf.set(el::Level::Warning, el::ConfigurationType::Format, "%datetime %level %msg" ); |
366 | defaultConf.set(el::Level::Error, el::ConfigurationType::Format, "%datetime %level %msg" ); |
367 | defaultConf.set(el::Level::Info, el::ConfigurationType::Format, "%datetime %level %msg" ); |
368 | |
369 | defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, |
370 | cfg.log_to_stdout?"true" :"false" ); |
371 | defaultConf.setGlobally(el::ConfigurationType::ToFile, |
372 | cfg.log_to_file?"true" :"false" ); |
373 | if (cfg.log_to_file) { |
374 | string logfilename = cfg.dump_dir + filename; |
375 | defaultConf.setGlobally(el::ConfigurationType::Filename, |
376 | logfilename.data()); |
377 | } |
378 | |
379 | if (cfg.log_flush_threshold >= 0) { |
380 | auto && oss = std::ostringstream(); |
381 | oss << cfg.log_flush_threshold; |
382 | |
383 | auto log_flush_threshold = oss.str(); |
384 | defaultConf.setGlobally(el::ConfigurationType::LogFlushThreshold, |
385 | log_flush_threshold.data()); |
386 | } |
387 | |
388 | if (cfg.log_file_max_size > 0) { |
389 | auto && oss = std::ostringstream(); |
390 | oss << cfg.log_file_max_size; |
391 | |
392 | auto log_file_max_size = oss.str(); |
393 | |
394 | defaultConf.setGlobally(el::ConfigurationType::MaxLogFileSize, |
395 | log_file_max_size.data()); |
396 | |
397 | el::Helpers::installPreRollOutCallback(rolloutHandler); |
398 | //el::Helpers::uninstallPreRollOutCallback(); |
399 | } |
400 | el::Loggers::reconfigureLogger("default" , defaultConf); |
401 | el::Loggers::setDefaultConfigurations(defaultConf, true); |
402 | } |
403 | |
404 | // This function doesn't really need to do anything. The signal will cause |
405 | // // waitpid to return EINTR and that's all we need. |
406 | static void handle_alarm_signal(__attribute__((unused)) int sig) {} |
407 | |
408 | static void ensure_dir(const string& dir, mode_t mode) |
409 | { |
410 | string d = dir; |
411 | while (!d.empty() && d[d.length() - 1] == '/') { |
412 | d = d.substr(0, d.length() - 1); |
413 | } |
414 | |
415 | struct stat st; |
416 | if (0 > stat(d.c_str(), &st)) { |
417 | if (errno != ENOENT) { |
418 | LOG(ERROR) << "Error accessing " << dir << "'" ; |
419 | } |
420 | |
421 | size_t last_slash = d.find_last_of('/'); |
422 | if (last_slash == string::npos || last_slash == 0) { |
423 | LOG(ERROR) << "Can't find directory `" << dir << "'" ; |
424 | } |
425 | ensure_dir(d.substr(0, last_slash), mode); |
426 | |
427 | // Allow for a race condition where someone else creates the directory |
428 | if (0 > mkdir(d.c_str(), mode) && errno != EEXIST) { |
429 | LOG(ERROR) << "Can't create `" << dir << "'" ; |
430 | } |
431 | if (0 > stat(d.c_str(), &st)) { |
432 | LOG(ERROR) << "Can't stat `" << dir << "'" ; |
433 | } |
434 | } |
435 | |
436 | if (!(S_IFDIR & st.st_mode)) { |
437 | LOG(ERROR) << "`" << dir << "' exists but isn't a directory." ; |
438 | } |
439 | if (access(d.c_str(), W_OK)) { |
440 | LOG(ERROR) << "Can't write to `" << dir << "'." ; |
441 | } |
442 | } |
443 | |
444 | static void create_trace_dir(string& parent_dir, const char* exe_path) |
445 | { |
446 | string dir; |
447 | int ret; |
448 | int nonce = 0; |
449 | const char* pbasename = basename((char*)exe_path); |
450 | |
451 | if (parent_dir[parent_dir.size()-1] != '/') |
452 | parent_dir += '/'; |
453 | do { |
454 | stringstream ss; |
455 | ss << parent_dir.data() << pbasename << "-" |
456 | << nonce++; |
457 | dir = ss.str(); |
458 | ret = mkdir(dir.c_str(), S_IRWXU | S_IRWXG); |
459 | } while (ret && EEXIST == errno); |
460 | |
461 | if (ret) { |
462 | LOG(ERROR) << "Unable to create trace directory '" << dir << "'" ; |
463 | } |
464 | parent_dir = dir + '/'; |
465 | LOG(INFO) << "trace dir:" << parent_dir.data(); |
466 | } |
467 | |
468 | static void check_free_disk_space(DumpConfig& cfg) |
469 | { |
470 | struct statvfs tmpInfo; |
471 | struct statvfs homeInfo; |
472 | int ret = statvfs("/tmp" , &tmpInfo); |
473 | if (ret) { |
474 | fprintf(stderr, "ERROR: failed to statvfs(/tmp) -> %d\n" , errno); |
475 | exit(-1); |
476 | } |
477 | ret = statvfs(cfg.dump_dir.data(), &homeInfo); |
478 | if (ret) { |
479 | fprintf(stderr, "ERROR: failed to statvfs(%s) -> %d\n" , |
480 | cfg.dump_dir.data(), errno); |
481 | exit(-2); |
482 | } |
483 | |
484 | #define MIN_RESERVED_SPACE (256*1024*1024) |
485 | |
486 | long tmpFree = tmpInfo.f_bsize * tmpInfo.f_bavail; |
487 | long homeFree = homeInfo.f_bsize * homeInfo.f_bavail; |
488 | if (tmpFree < MIN_RESERVED_SPACE) { |
489 | fprintf(stderr, "ERROR: /tmp volume too small, must larger than %dMB\n" , |
490 | MIN_RESERVED_SPACE/(1024*1024)); |
491 | exit(-3); |
492 | } |
493 | if (homeFree < MIN_RESERVED_SPACE) { |
494 | fprintf(stderr, "ERROR: %s volume too small, must larger than %dMB\n" , |
495 | cfg.dump_dir.data(), MIN_RESERVED_SPACE/(1024*1024)); |
496 | exit(-4); |
497 | } |
498 | |
499 | if (cfg.max_dump_bytes + MIN_RESERVED_SPACE > tmpFree) { |
500 | cfg.max_dump_bytes = tmpFree - MIN_RESERVED_SPACE; |
501 | } |
502 | if (cfg.max_dump_bytes + MIN_RESERVED_SPACE > homeFree) { |
503 | cfg.max_dump_bytes = homeFree - MIN_RESERVED_SPACE; |
504 | } |
505 | } |
506 | |
507 | |
508 | static void make_latest_trace(const string& trace_dir) |
509 | { |
510 | string link_name(trace_dir.c_str(), trace_dir.size() -1); |
511 | int pos = link_name.rfind('/'); |
512 | link_name.replace(pos + 1, link_name.size() - pos -1, LATEST_TRACE_NAME); |
513 | |
514 | // Try to update the symlink to |this|. We only try attempt |
515 | // to set the symlink once. If the link is re-created after |
516 | // we |unlink()| it, then another rr process is racing with us |
517 | // and it "won". The link is then valid and points at some |
518 | // very-recent trace, so that's good enough. |
519 | unlink(link_name.c_str()); |
520 | |
521 | // Link only the trace name, not the full path, so moving a directory full |
522 | // of traces around doesn't break the latest-trace link. |
523 | string trace_name(trace_dir.c_str() + pos + 1, trace_dir.size() - pos -2); |
524 | int ret = symlink(trace_name.c_str(), link_name.c_str()); |
525 | if (ret < 0 && errno != EEXIST) { |
526 | LOG(ERROR) << "Failed to update symlink `" << link_name << "' to `" |
527 | << trace_dir << "'." ; |
528 | } |
529 | } |
530 | |
531 | extern void set_syscall_filter(const char* filter, bool reset); |
532 | |
533 | |
534 | // [*]var1+size1[,[*]var2+size2,...] |
535 | static void parse_global_var(DumpConfig& cfg, const char* namelist) |
536 | { |
537 | Variable tmp; |
538 | |
539 | char* pos = NULL; |
540 | const char* p = namelist; |
541 | while (*p) { |
542 | tmp.is_pointer = (*p == '*'); |
543 | if (tmp.is_pointer) ++p; |
544 | |
545 | // get size |
546 | pos = (char*)strchr(p, '+'); |
547 | if (NULL == pos) { |
548 | break; |
549 | } |
550 | tmp.sym_name = string(p, int(pos - p)); |
551 | tmp.max_size = strtol(pos + 1, &pos, 10); |
552 | if (tmp.max_size > 0) { |
553 | if (tmp.max_size < 256) tmp.max_size = 256; |
554 | cfg.vars.push_back(tmp); |
555 | } |
556 | |
557 | if (*pos != ',') break; |
558 | p = pos + 1; |
559 | } |
560 | } |
561 | |
562 | static void parse_signal_filter(DumpConfig& cfg, const char* filter) |
563 | { |
564 | const char* walk = filter; |
565 | char* stop = nullptr; |
566 | while (*walk > 0) { |
567 | int v = strtol(walk, &stop, 10); |
568 | cfg.sigs.push_back(v); |
569 | if (0 == *stop) break; |
570 | walk = stop + 1; |
571 | } |
572 | } |
573 | |
574 | #ifndef SIMPLE_X11_HOOK |
575 | static void (*g_stop_record_x11)(void) = nullptr; |
576 | static int (*g_start_record_x11)(const char* displayName, const char* filter) = nullptr; |
577 | |
578 | static void* x11_record_thread(void* param) |
579 | { |
580 | int ret = -1; |
581 | if (g_start_record_x11) { |
582 | ret = g_start_record_x11(":0" , (const char*)param); |
583 | if (ret < 0) { |
584 | } |
585 | } |
586 | |
587 | return nullptr; |
588 | } |
589 | #endif |
590 | |
591 | bool is_elf(const char* filename); |
592 | |
593 | |
594 | int main (int argc, char** argv) |
595 | { |
596 | #ifndef SIMPLE_X11_HOOK |
597 | bool record_x11_running = false; |
598 | #endif |
599 | |
600 | bool hook_vdso = false; |
601 | int exe_index = 1; |
602 | string preload; |
603 | int fds[2]; |
604 | ScopedFd error_fd; |
605 | ScopedFd spawned_task_error_fd; |
606 | vector<string> envp; |
607 | vector<string> argv_child; |
608 | int tid = 0; |
609 | int pid = 0; |
610 | int raw_status = 0; |
611 | WaitStatus status; |
612 | DumpConfig cfg; |
613 | |
614 | cfg.mode = NORMAL; |
615 | load_config(cfg); |
616 | ensure_dir(cfg.dump_dir, S_IRWXU); |
617 | init_log(cfg, "emd.log" ); |
618 | |
619 | if (argc < 2) { |
620 | usage(argv[0]); |
621 | return 0; |
622 | } |
623 | |
624 | if (!strcmp(argv[1], "ps" )) { |
625 | return list_pid(cfg.dump_dir.c_str(), argc < 3 ? nullptr : argv[2]); |
626 | } |
627 | |
628 | if (!strcmp(argv[1], "dump" )) { |
629 | if (argc < 2) { |
630 | usage(argv[0]); |
631 | return 0; |
632 | } |
633 | |
634 | if (argc < 3) { |
635 | return dump(cfg.dump_dir.c_str(), nullptr, 0); |
636 | } |
637 | if (argc < 4) { |
638 | pid = atoi(argv[2]); |
639 | if (pid > 0) { |
640 | return dump(cfg.dump_dir.c_str(), nullptr, pid); |
641 | } |
642 | |
643 | return dump(cfg.dump_dir.c_str(), argv[2], 0); |
644 | } |
645 | |
646 | return dump(cfg.dump_dir.c_str(), argv[2], atoi(argv[3])); |
647 | } |
648 | |
649 | if (!strcmp(argv[1], "replay" )) { |
650 | if (argc < 2) { |
651 | usage(argv[0]); |
652 | return 0; |
653 | } |
654 | |
655 | if (argc < 3) { |
656 | return replay(cfg.dump_dir.c_str(), nullptr, 0); |
657 | } |
658 | if (argc < 4) { |
659 | pid = atoi(argv[2]); |
660 | if (pid > 0) { |
661 | return replay(cfg.dump_dir.c_str(), nullptr, pid); |
662 | } |
663 | |
664 | return replay(cfg.dump_dir.c_str(), argv[2], 0); |
665 | } |
666 | |
667 | return replay(cfg.dump_dir.c_str(), argv[2], atoi(argv[3])); |
668 | } |
669 | |
670 | assert(cfg.mode <= FAST); |
671 | |
672 | set_syscall_filter("!all" , true); /*default clear all syscall*/ |
673 | |
674 | for (int i=1; i<argc; ++i) { |
675 | if (!strncmp(argv[i], "--sys=" , 6)) { |
676 | if (argv[i][6] > 0) { |
677 | set_syscall_filter(argv[i] + 6, true); |
678 | } |
679 | } |
680 | else if (!strncmp(argv[i], "--dbus=" , 7)) { |
681 | if (argv[i][7] > 0) { |
682 | string dbusenv = "ST2_DBUS_FILTER=" ; |
683 | envp.push_back(dbusenv + (argv[i] + 7)); |
684 | preload += "dbus," ; |
685 | } |
686 | } |
687 | else if (!strncmp(argv[i], "--x11=" , 6)) { |
688 | #ifdef SIMPLE_X11_HOOK |
689 | if (argv[i][6] > 0) { |
690 | string x11env = "ST2_X11_FILTER=" ; |
691 | envp.push_back(x11env + (argv[i] + 6)); |
692 | preload += "x11," ; |
693 | } |
694 | #else |
695 | void* handle = dlopen("libx11preload.so" , RTLD_NOW); |
696 | if (handle) { |
697 | void* proc = dlsym(handle, "start_record_x11" ); |
698 | memcpy(&g_start_record_x11, &proc, sizeof(void*)); |
699 | proc = dlsym(handle, "stop_record_x11" ); |
700 | memcpy(&g_stop_record_x11, &proc, sizeof(void*)); |
701 | |
702 | if (g_stop_record_x11 && g_stop_record_x11) { |
703 | pthread_t record_x11_thread; |
704 | pthread_create(&record_x11_thread, nullptr, |
705 | x11_record_thread, (void*)(argv[i] + 6)); |
706 | record_x11_running = true; |
707 | } |
708 | } |
709 | else { |
710 | LOG(ERROR) << "failed to load libx11preload.so, errno=" << errno; |
711 | } |
712 | #endif |
713 | } |
714 | else if (!strncmp(argv[i], "--sig=" , 6)) { |
715 | if (argv[i][6] > 0) { |
716 | parse_signal_filter(cfg, argv[i] + 6); |
717 | } |
718 | } |
719 | else if (!strncmp(argv[i], "-1" , 2)) { |
720 | // will override mode field in configure file |
721 | cfg.current_thread_only = true; |
722 | } |
723 | else if (!strncmp(argv[i], "--var=" , 6)) { |
724 | if (argv[i][6] > 0) { |
725 | parse_global_var(cfg, argv[i] + 6); |
726 | } |
727 | } |
728 | else if (!strncmp(argv[i], "--func=" , 7)) { |
729 | if (argv[i][7] > 0) { |
730 | cfg.break_function = argv[i] + 7; |
731 | } |
732 | } |
733 | else if (!strncmp(argv[i], "--vdso=" , 7)) { |
734 | hook_vdso = !strcmp(argv[i] + 7, "on" ); |
735 | } |
736 | else if (!strncmp(argv[i], "--param-size=" , 13)) { |
737 | cfg.max_param_size = atoi(argv[i] + 13); |
738 | if (cfg.max_param_size < 0) cfg.max_param_size = 0; |
739 | } |
740 | else if (!strncmp(argv[i], "--stack-size=" , 13)) { |
741 | cfg.max_stack_size = atoi(argv[i] + 13)*1024; |
742 | if (cfg.max_stack_size < 0) cfg.max_stack_size = 0; |
743 | } |
744 | else if (!strncmp(argv[i], "--heap-size=" , 12)) { |
745 | cfg.max_heap_size = atoi(argv[i] + 12)*1024; |
746 | if (cfg.max_heap_size < 0) cfg.max_heap_size = 0; |
747 | } |
748 | else { |
749 | break; |
750 | } |
751 | LOG(INFO) << "parameter " << i << ":" << argv[i]; |
752 | |
753 | exe_index = i + 1; |
754 | if (exe_index >= argc) { |
755 | usage(argv[0]); |
756 | #ifndef SIMPLE_X11_HOOK |
757 | if (record_x11_running) g_stop_record_x11(); |
758 | #endif |
759 | return 0; |
760 | } |
761 | } |
762 | if (!is_elf(argv[exe_index])) { |
763 | usage(argv[0]); |
764 | return 0; |
765 | } |
766 | |
767 | string preload_path = find_preload_library(argv[0], "syscall" ); |
768 | bool x11_dbus = preload.find("x11" ) != string::npos || preload.find("dbus" ) != string::npos; |
769 | // X11,DBUS can't use fast mode, because the x11/dbus hook thread is not meaning! |
770 | // if (x11_dbus) cfg.current_thread_only = false; |
771 | if (cfg.current_thread_only && |
772 | !x11_dbus && |
773 | !cfg.max_heap_size && |
774 | cfg.vars.empty() && |
775 | !preload_path.empty()) { |
776 | cfg.mode = FAST; |
777 | if (string::npos == preload.find("syscall" )) { |
778 | preload += "syscall," ; |
779 | } |
780 | string syscallenv = "ST2_SYSCALL_BUFFER_SIZE=" ; |
781 | envp.push_back(syscallenv + to_string(cfg.shared_buffer_size)); |
782 | |
783 | if (hook_vdso) { |
784 | string hookvdsoenv = "ST2_HOOK_VDSO=1" ; |
785 | envp.push_back(hookvdsoenv); |
786 | } |
787 | } |
788 | else if (hook_vdso) { |
789 | #if !defined(__sw_64) |
790 | /* hook these function in vdso |
791 | __vdso_clock_gettime |
792 | __vdso_gettimeofday |
793 | __vdso_time |
794 | __vdso_getcpu |
795 | */ |
796 | preload += "vdso," ; |
797 | #endif |
798 | set_syscall_filter("gettimeofday,time,clock_gettime" , false); |
799 | } |
800 | create_trace_dir(cfg.dump_dir, argv[exe_index]); |
801 | |
802 | check_free_disk_space(cfg); |
803 | |
804 | for (int i = exe_index; i<argc; ++i) { |
805 | argv_child.push_back(argv[i]); |
806 | } |
807 | |
808 | if (0 != pipe2(fds, O_CLOEXEC)) { |
809 | LOG(ERROR) << "failed to create pipe!" ; |
810 | return 0; |
811 | } |
812 | error_fd = fds[1]; |
813 | spawned_task_error_fd = fds[0]; |
814 | if (-1 == fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | O_NONBLOCK)) { |
815 | LOG(WARNING) << "failed to set spawned_task_error_fd to O_NONBLOCK, errno=" << errno; |
816 | } |
817 | |
818 | if (!preload.empty()) { |
819 | if (preload[preload.size() - 1] == ',') |
820 | preload.erase(preload.size() - 1); |
821 | preload_path = find_preload_library(argv[0], preload.data()); |
822 | init_env(envp, string(argv[exe_index]), preload_path); |
823 | } |
824 | else { |
825 | init_env(envp, string(argv[exe_index]), string("" )); |
826 | } |
827 | |
828 | TraceProcess process(&cfg); |
829 | int send_socket_fd_number = 0; |
830 | int send_sock = process.setup_socket(&send_socket_fd_number); |
831 | |
832 | do { |
833 | pid = fork(); |
834 | } while ((pid<0) && (errno == EAGAIN)); |
835 | |
836 | if (0 == pid) { |
837 | run_child(error_fd, send_sock, send_socket_fd_number, |
838 | argv[exe_index], argv_child, envp); |
839 | /*target child process never returns*/ |
840 | } |
841 | |
842 | if (pid < 0) { |
843 | LOG(ERROR) << "Failed to fork:" << errno; |
844 | |
845 | goto fatal_error; |
846 | } |
847 | |
848 | if (attach_thread(pid, cfg.mode != FAST) < 0) { |
849 | goto fatal_error; |
850 | } |
851 | |
852 | // Install signal handler here, so that when creating the first TraceProcess |
853 | // it sees the exact same signal state in the parent as will be in the child. |
854 | struct sigaction sa; |
855 | sa.sa_handler = handle_alarm_signal; |
856 | sigemptyset(&sa.sa_mask); |
857 | sa.sa_flags = 0; // No SA_RESTART, so waitpid() will be interrupted |
858 | sigaction(SIGALRM, &sa, nullptr); |
859 | |
860 | tid = waitpid(-1, &raw_status, __WALL); |
861 | status = WaitStatus(raw_status); |
862 | if ((status.type() == WaitStatus::PTRACE_EVENT) && |
863 | (status.ptrace_event() == PTRACE_EVENT_EXIT)) { |
864 | LOG(ERROR) << "Tracee died before reaching SIGSTOP" ; |
865 | goto fatal_error; |
866 | } |
867 | // SIGSTOP can be reported as a signal-stop or group-stop depending on |
868 | // whether PTRACE_SEIZE happened before or after it was delivered. |
869 | if (SIGSTOP != status.stop_sig() && SIGSTOP != status.group_stop()) { |
870 | LOG(ERROR) << "Unexpected stop " << status.type(); |
871 | goto fatal_error; |
872 | } |
873 | |
874 | make_latest_trace(cfg.dump_dir); |
875 | |
876 | /*restart tracee*/ |
877 | ptrace(PTRACE_SYSCALL, tid, nullptr, 0); |
878 | |
879 | LOG(INFO) << "start trace:" << argv[exe_index] |
880 | << ", pid=" << pid << ", mode=" << cfg.mode; |
881 | |
882 | /*FIXME: is need call prctl(PR_SET_DUMPABLE, 1)*/ |
883 | process.start_record(pid); |
884 | |
885 | for(;;) { |
886 | tid = waitpid(-1, &raw_status, __WALL); |
887 | int wait_errno = errno; |
888 | if (tid < 0) { |
889 | if ((wait_errno == ECHILD) && process.empty()) { |
890 | LOG(INFO) << "waitpid failed:" << wait_errno |
891 | << ", " << strerror(wait_errno); |
892 | break; |
893 | } |
894 | |
895 | continue; |
896 | } |
897 | |
898 | TraceProcess* proc = process.get_process(tid); |
899 | if (proc != nullptr) { |
900 | if (!proc->process_status(raw_status, tid)) { |
901 | break; // fatal error found |
902 | } |
903 | } |
904 | |
905 | if (process.empty()) { |
906 | break; |
907 | } |
908 | } |
909 | |
910 | fatal_error: |
911 | process.stop_record(); |
912 | |
913 | read_spawned_task_error(spawned_task_error_fd); |
914 | |
915 | if (send_sock) close(send_sock); |
916 | if (pid > 0) kill(pid, SIGKILL); |
917 | |
918 | #ifndef SIMPLE_X11_HOOK |
919 | if (record_x11_running) g_stop_record_x11(); |
920 | #endif |
921 | |
922 | return 0; |
923 | } |
924 | |