C
對 C 文件使用 strace
我有一個 C 文件,我想跟踪正在發生的事情。我正在嘗試使用:
strace -o trace.txt random.c
但它說:
strace: Can't stat 'random.c': No such file or directory
過去一個小時我一直在查找 strace ,並且不完全知道我做錯了什麼。是否有更簡單的命令來跟踪編譯/執行的內容?‘strace’ 僅適用於 shell 文件嗎?
正如thrig 所說,您必須編譯您的 C 程序(使用諸如
gcc
or之類的編譯器clang
),然後用於strace
執行編譯後的二進製文件。ek@Io:~$ cat >hello.c <<'EOF' > #include <stdio.h> > > int main(void) > { > puts("Hello, world!"); > return 0; > } > EOF ek@Io:~$ gcc -ansi -pedantic -Wall -Wextra -g -o hello hello.c ek@Io:~$ strace ./hello execve("./hello", ["./hello"], [/* 19 vars */]) = 0 brk(NULL) = 0x220f000 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8000316000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 fstat(3, {st_mode=S_IFREG|0644, st_size=156046, ...}) = 0 mmap(NULL, 156046, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f80002ef000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\t\2\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0755, st_size=1864888, ...}) = 0 mmap(NULL, 3967392, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f7fffd2a000 mprotect(0x7f7fffee9000, 2097152, PROT_NONE) = 0 mmap(0x7f80000e9000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bf000) = 0x7f80000e9000 mmap(0x7f80000ef000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f80000ef000 close(3) = 0 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f80002ee000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f80002ed000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f80002ec000 arch_prctl(ARCH_SET_FS, 0x7f80002ed700) = 0 mprotect(0x7f80000e9000, 16384, PROT_READ) = 0 mprotect(0x600000, 4096, PROT_READ) = 0 mprotect(0x7f8000318000, 4096, PROT_READ) = 0 munmap(0x7f80002ef000, 156046) = 0 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0 brk(NULL) = 0x220f000 brk(0x2230000) = 0x2230000 write(1, "Hello, world!\n", 14Hello, world! ) = 14 exit_group(0) = ? +++ exited with 0 +++
當然,您仍然可以使用您需要的任何選項
strace
,例如-o
.
strace
將無法成功執行 C 原始碼。即使你給它一個帶有 a 的路徑/
來堅持它試圖這樣做,它也會失敗:ek@Io:~$ strace hello.c strace: Can't stat 'hello.c': No such file or directory ek@Io:~$ strace ./hello.c execve("./hello.c", ["./hello.c"], [/* 19 vars */]) = -1 EACCES (Permission denied) write(2, "strace: exec: Permission denied\n", 32strace: exec: Permission denied ) = 32 exit_group(1) = ? +++ exited with 1 +++
即使您將
.c
文件標記為執行檔,它仍然無法執行:ek@Io:~$ chmod +x hello.c ek@Io:~$ strace ./hello.c execve("./hello.c", ["./hello.c"], [/* 19 vars */]) = -1 ENOEXEC (Exec format error) write(2, "strace: exec: Exec format error\n", 32strace: exec: Exec format error ) = 32 exit_group(1) = ? +++ exited with 1 +++
簡而言之,您必須編譯您的 C 程序並執行編譯後的二進製文件,而不是原始碼文件本身。正如您無法正常執行
.c
文件(即./hello.c
失敗)一樣,您也無法執行它strace
。