Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strace with redirection

Tags:

strace

How can I use strace to include output redirection (>), for example:

strace echo Hello > /tmp/file

The output produced is only taking into account echo hello but not > /tmp/file

I have also tried:

alice@vm:~$ strace -e trace=open,close,read,write echo Hello > /tmp/file

close(3)                                = 0
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832
close(3)                                = 0
close(3)                                = 0
write(1, "Hello\n", 6)                  = 6
close(1)                                = 0
close(2)                                = 0
+++ exited with 0 +++

It shows that it is writing to stdout (file descriptor 1), but no where shows that it is redirecting to /tmp/file.

like image 223
myfullname Avatar asked Oct 27 '25 13:10

myfullname


1 Answers

Stracing command with redirection

strace sh -c "echo Hello > /tmp/foo"


Redirecting strace's output to the output stream

From strace man page:

The name of each system call, its arguments and its return value are printed on standard error or to the file specified with the -o option.

So, that's how we can redirect the standard error to some file:

strace echo Hello 2>/tmp/file

Another option is to use the -o flag:

strace -o"/tmp/file2" echo hello

like image 151
Yam Mesicka Avatar answered Oct 30 '25 13:10

Yam Mesicka