1

On linux is there a tool to see exactly which characters a certain program is reading from stdin? Preferably logging them to a file with time stamps, but just logging them raw to a file will do.

This input is actually translated characters from an IR remote, and I need to see exactly which ones and how many are actually reaching the application. In this case I'm working with somebody else's binary, and even if I did get a version of the source code and compiled it the issue is not actually in the target program, it is upstream. So a more general solution would be preferred. The intercepting tool can be run as root, if that is required.

Note that in this case the program whose input I want to monitor is normally started by yet another program. So what I'm looking for is a tool that does something like:

   intercept -stdin -pid $PID_OF_RUNNING_PROGRAM -log file
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
mathog
  • 345
  • 3
  • 11
  • Note strace doesn't quite do it here because it only emits the logged stdin characters on an EOL or when the program exits. Ie, like this: " strace -ff -e trace=write -e write=1,2 -p 11856". If there was a way to make it emit input character by character then that would do it. – mathog Feb 01 '18 at 18:53
  • Check out the strace command – Bjorn A. Feb 01 '18 at 18:53
  • 1
    `strace -tt -e trace=read -e read=0 -p #####` will show them as they're read. – Mark Plotnick Feb 01 '18 at 18:56
  • "strace -tt -e trace=read -e read=0 -p ######" as suggested by Mark Plotnick is not working for me. At the first character it emits a time stamp and "read(0," but then it waits for an EOL before emitting the characters. In this application there is no EOL, ever. – mathog Feb 01 '18 at 19:01
  • strace will show when a read completes. If strace is not showing any output, it means the read hasn't completed and your program is still waiting in a read system call. If your program is reading in canonical mode, it'll wait for EOL or the buffer to be filled; no user-level tracing program can change that. Do you actually want to see the characters as they pass through the device driver, before they're read? – Mark Plotnick Feb 01 '18 at 19:11
  • The program in question acts on each character as it is received. It is probably using getc() or some equivalent. – mathog Feb 01 '18 at 19:17
  • An alternative might be to put the serial device into "raw" mode, if the program can tolerate that. This can be done with a separate program. See [How to read a binary data over serial terminal in C program](https://stackoverflow.com/questions/12437593/how-to-read-a-binary-data-over-serial-terminal-in-c-program/) – Mark Plotnick Feb 01 '18 at 19:17
  • You are right, the device mode matters. The strace command works as desired (character by character) when monitoring "nano test.txt" keystrokes. – mathog Feb 01 '18 at 20:07

1 Answers1

0

tee command can log stdin and stdout in command line. this is not the correct format this question want, but write it here may help other people.

root@aliecs:~# echo -e "hello\nworld" | tee in.log | grep hello | tee out.log
hello
root@aliecs:~# cat in.log 
hello
world
root@aliecs:~# cat out.log 
hello
root@aliecs:~#
tinyhare
  • 2,271
  • 21
  • 25