Write strace output to a file

15,399

By default strace outputs to stderr. By simply typing man strace, you will have the full documentation of strace. In the manual page, it states that the -o option can be used to output it to a file instead of stderr. You can type man [insert command here] for the vast majority of programs, and have all the documentation you will need to effectively use them.

Share:
15,399
yeled zevel
Author by

yeled zevel

Updated on September 18, 2022

Comments

  • yeled zevel
    yeled zevel almost 2 years

    I'm trying to write the output of strace ls to a file. I know that I need to use > in order to forward output of a command to a file, but it doesn't work. It creates a file but the command prints the output of strace ls to stdout but writes the file name into the file.

    $ strace ls > ls_sys.txt
    ...
    strace output
    ...
    $ cat ls_sys.txt
    ls_sys.txt
    
    • smw
      smw almost 4 years
      Related: piping strace to grep however it will likely be simpler to use the strace -o option rather than shell redirection
    • fpmurphy
      fpmurphy almost 4 years
      Have you tried strace -o ls_sys.txt ls?
  • yeled zevel
    yeled zevel almost 4 years
    By default strace outputs to stderr, which is not accessible via grep or file redirection. Good to know! And I looked at the manual page of strace but I didn't see the -o option, my fault. Thanks!
  • john doe
    john doe almost 4 years
    no problem, I actually just figured out this strace option myself yesterday. When you are in a man page, you can press / and it will bring a dialog that you can search. In my case, I simply searched for output and found the part of the man page I was looking for.
  • JdeBP
    JdeBP almost 4 years
    What on Earth makes you think that you cannot redirect standard error or arrange for it to be sent through a pipe to grep?
  • Stephen Kitt
    Stephen Kitt almost 4 years
    stderr can be redirected, using 2>. See this question.
  • Stéphane Chazelas
    Stéphane Chazelas over 2 years
    Note that it redirects both the strace tracing and the ./elf_file errors to strace_output.txt. For that reason, it's better to use strace -o strace_output.txt ./elf_file or strace -o strace_output.txt ./elf_file 1> stdout.txt 2> stderr.txt if you want traces, stdout and stderr in different files.
  • alkino
    alkino over 2 years
    A bash-ism I guess is command |& command it merges and pipes both stdout and stderr to the next command.