2

I have a C file that I want to trace what is going on. I'm trying to use:

strace -o trace.txt random.c

But it says that:

strace: Can't stat 'random.c': No such file or directory

I've been looking up strace for the past hour now, and don't exactly know what I'm doing wrong. Is there an easier command to just trace what is compiled/executed? Does 'strace' only work on shell files?

hiquetj
  • 123
  • 3
    *.c files typically must be sacrificed to a compiler such as gcc, and the outcome of that then traced. – thrig Jan 26 '17 at 17:41
  • see also https://unix.stackexchange.com/questions/418354/understanding-what-a-linux-binary-is-doing/418357#418357 and https://unix.stackexchange.com/questions/419697/why-are-true-and-false-so-large/419704#419704 – Rui F Ribeiro Feb 02 '18 at 05:52

1 Answers1

5

As thrig says, you must compile your C program (using a compiler such as gcc or clang), then use strace to run the compiled binary.

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 +++

You may still of course use whatever options you need for strace, such as -o.

strace will not succeed at running C source code. Even if you give it a path with a / in it to insist that it attempt to do so, it will fail:

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 +++

Even if you marked your .c file executable, it still cannot be run:

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 +++

In short, you have to compile your C program and run the compiled binary rather than the source code file itself. Just as you cannot run a .c file normally (i.e., ./hello.c fails), you cannot run it with strace either.

Eliah Kagan
  • 4,155