Linux调试工具
发布时间:2023-06-29 18:10:05.846 文章来源:AiSoftCloud 浏览次数:607 下载次数:1 

strace

下载及安装

github地址:https://gitcode.net/mirrors/strace/strace?utm_source=aisoftcloud

  1. ./configure
  2. make -j
  3. make install

configure过程中如果出现configure: error: Cannot enable m32 personality support错误,使用以下命令:

  1. ./configure --enable-mpers=no

使用

监控程序启动过程

测试代码:

  1. #include <iostream>
  2. #include <unistd.h>
  3. int32_t main(int32_t argc, char** argv) {
  4. ::sleep(1);
  5. return 0;
  6. }

编译:

  1. g++ -g -o main main.cc

运行strace:

  1. sudo strace ./main

输出:

  1. execve("./strace", ["./strace"], 0x7ffd7a487af0 /* 17 vars */) = 0
  2. brk(NULL) = 0x55c694cbd000
  3. access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
  4. access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
  5. openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
  6. fstat(3, {st_mode=S_IFREG|0644, st_size=181974, ...}) = 0
  7. mmap(NULL, 181974, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f2e4d825000
  8. close(3) = 0
  9. access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
  10. openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
  11. read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\304\10\0\0\0\0\0"..., 832) = 832
  12. fstat(3, {st_mode=S_IFREG|0644, st_size=1594864, ...}) = 0
  13. mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e4d823000
  14. mmap(NULL, 3702848, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2e4d2a0000
  15. mprotect(0x7f2e4d419000, 2097152, PROT_NONE) = 0
  16. mmap(0x7f2e4d619000, 49152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x179000) = 0x7f2e4d619000
  17. mmap(0x7f2e4d625000, 12352, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2e4d625000
  18. close(3) = 0
  19. access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
  20. openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
  21. read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\35\2\0\0\0\0\0"..., 832) = 832
  22. fstat(3, {st_mode=S_IFREG|0755, st_size=2030928, ...}) = 0
  23. mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2e4ceaf000
  24. mprotect(0x7f2e4d096000, 2097152, PROT_NONE) = 0
  25. mmap(0x7f2e4d296000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f2e4d296000
  26. mmap(0x7f2e4d29c000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2e4d29c000
  27. close(3) = 0
  28. access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
  29. openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
  30. read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\272\0\0\0\0\0\0"..., 832) = 832
  31. fstat(3, {st_mode=S_IFREG|0644, st_size=1700792, ...}) = 0
  32. mmap(NULL, 3789144, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2e4cb11000
  33. mprotect(0x7f2e4ccae000, 2093056, PROT_NONE) = 0
  34. mmap(0x7f2e4cead000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19c000) = 0x7f2e4cead000
  35. close(3) = 0
  36. access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
  37. openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
  38. read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300*\0\0\0\0\0\0"..., 832) = 832
  39. fstat(3, {st_mode=S_IFREG|0644, st_size=96616, ...}) = 0
  40. mmap(NULL, 2192432, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2e4c8f9000
  41. mprotect(0x7f2e4c910000, 2093056, PROT_NONE) = 0
  42. mmap(0x7f2e4cb0f000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x7f2e4cb0f000
  43. close(3) = 0
  44. mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e4d821000
  45. arch_prctl(ARCH_SET_FS, 0x7f2e4d821d00) = 0
  46. mprotect(0x7f2e4d296000, 16384, PROT_READ) = 0
  47. mprotect(0x7f2e4cb0f000, 4096, PROT_READ) = 0
  48. mprotect(0x7f2e4cead000, 4096, PROT_READ) = 0
  49. mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2e4d81f000
  50. mprotect(0x7f2e4d619000, 40960, PROT_READ) = 0
  51. mprotect(0x55c6933d6000, 4096, PROT_READ) = 0
  52. mprotect(0x7f2e4d852000, 4096, PROT_READ) = 0
  53. munmap(0x7f2e4d825000, 181974) = 0
  54. brk(NULL) = 0x55c694cbd000
  55. brk(0x55c694cde000) = 0x55c694cde000
  56. nanosleep({tv_sec=1, tv_nsec=0}, 0x7ffcf81b9090) = 0
  57. exit_group(0) = ?
  58. +++ exited with 0 +++
  59. strace: option requires an argument -- 'p'
  60. Try 'strace -h' for more information.
  61. strace.sh: 11: kill: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
  62. kill -l [exitstatus]

参考文章

strace 命令详解

更多文章可关注公众号
aisoftcloud