0

I just want to learn how I can dynamically to redirect linux process input/output/err(0/1/2) and I found that answer in in stack overflow How to redirect output of an already running process

As you can see the guide ask me to attach to the process trough gdb and invoke close() on the file descriptor and the create new one. Unfortunately cat (the test app) is not build with debug info and gdb complains that there is no such symbol as close. The same is true for the rest of the system commands that I tried.

What I need to do to have access to those symbols in gdb Also if you are able to provide alternative way to redirect the output of already running process without installing exotic tools or using gdb will be nice. Thanks

edit: gdb session

gdb -p 6836 /bin/cat                                                                                                                                                                                                                                              
GNU gdb (GDB) 11.1
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
<http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute 
it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /bin/cat...
(No debugging symbols found in /bin/cat)
Attaching to program: /usr/bin/cat, process 6836
ptrace: No such process.
(gdb) p close(1)
No symbol table is loaded.  Use the "file" command.
(gdb) file /bin/cat
Reading symbols from /bin/cat...
(No debugging symbols found in /bin/cat)
(gdb) p close(1)
No symbol table is loaded.  Use the "file" command.
(gdb) 
themean
  • 1,313
  • 4
  • 17
  • 33
  • "Unfortunately cat (the test app) is not build with debug info and gdb complains that there is no such symbol as close." -- is your `cat` statically linked? It's likely you are doing something wrong. Showing _complete_ GDB session will likely help answer your question. – Employed Russian Nov 10 '21 at 00:47
  • done, I add the gdb session. I don't have any idea how my cat binary is linked. I expect to by dynamic linked because it is common practice for many distros. For reference I use manjaro if that speak something to you. I don't have idea how they link their common utils – themean Nov 10 '21 at 20:04

1 Answers1

1

Your problem starts here:

Attaching to program: /usr/bin/cat, process 6836
ptrace: No such process.

This is most likely failing because you've either mistyped the process id, or your /usr/bin/cat exited for other reasons.

Try doing it like this:

cat > /tmp/foo &
[1] 1084976   # Note the PID

gdb /usr/bin/cat -p 1084976  # use the PID printed by the shell

Here is what you should see:

Attaching to program: /bin/cat, process 1084976

Program received signal SIGTTIN, Stopped (tty input).
0x00007f96c36178be in __GI___libc_read (fd=0, buf=0x7f96c36f6000, nbytes=131072) at ../sysdeps/unix/sysv/linux/read.c:26
26      ../sysdeps/unix/sysv/linux/read.c: No such file or directory.

(gdb) info shared
From                To                  Syms Read   Shared Object Library
0x00007f96c354f330  0x00007f96c3696df9  Yes         /lib/x86_64-linux-gnu/libc.so.6
0x00007f96c3718050  0x00007f96c3737cbe  Yes         /lib64/ld-linux-x86-64.so.2

The reason you don't have close defined is that it's defined in libc.so.6, but your GDB has not loaded symbols from libc.so.6 because it failed to attach the process.

Without a process, you can't call anything even if close was defined in the main executable.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Thanks I definitely miswrite the pid yet I still have the problem with no such symbol. I tried to load some additional symbols (gdb) file /lib64/ld-linux-x86-64.so.2 Reading symbols from /lib64/ld-linux-x86-64.so.2... But it's look like my distro build everything with high level optimizations and no debug symbols which I guess is good. Maybe I need another way to do that – themean Nov 11 '21 at 19:05