我有一个 C 文件,我想跟踪正在发生的事情。我正在尝试使用:
strace -o trace.txt random.c
但它说:
strace: Can't stat 'random.c': No such file or directory
过去一个小时我一直在查找 strace,但并不完全知道我做错了什么。是否有更简单的命令来跟踪编译/执行的内容? “strace”仅适用于 shell 文件吗?
答案1
作为特里格 说,您必须编译您的 C 程序(使用诸如gcc
或 之类的编译器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
其中任何一个来运行它。