26

Sometime I need to run an application from terminal for debugging. If I am sure the bug will occur short after launching the application, I can run this application from the Terminal.

However, bugs occur unexpectedly, and then only I need to monitor the buggy application from the Terminal (to see its output).

Can I then pick up an application from terminal, which was not launched using Terminal? If so how?

neydroydrec
  • 4,530
  • 10
  • 36
  • 47
  • I eventually picked up Michał Šrajer's answer because it was the most instructive. But all your answers were just good and appreciated too. Cheers. – neydroydrec Sep 02 '11 at 08:13
  • Previous error messages (to stderr) are written to `~/.xsession-errors` if you launched it via GUI. – Lekensteyn Sep 02 '11 at 10:54

4 Answers4

30

Each process in linux has a special directory /proc/{pid}/fd/. 0 is stdin, 1 is stdout and 2 is stderr. So, assuming you are only interested in diagnostic output you can determine the process pid, and then in the terminal do:

to see stdout:

cat /proc/{pid of process}/fd/1

to see stderr:

cat /proc/{pid of process}/fd/2
Cros
  • 103
  • 3
Michał Šrajer
  • 27,439
  • 4
  • 33
  • 29
  • OK i'm a bit confused, when I run `cat /proc/1840/fd/1` it seems to return output from other processes than the one I picked up. (1840 is currently for Skype according to System Monitor) It shows output of other programs too... :| – neydroydrec Sep 04 '11 at 14:13
9

Or you can use strace like this

sudo strace -p $pid_of_the_process
arrange
  • 14,727
  • 4
  • 42
  • 32
6

You can attach with gdb to a running process.

The syntax is

gdb program pid

Ok, you cannot see source code, if debug information are stripped, which is the default for deployed applications. But you can probably see stdout/stderr and debugger messages, segfaults.

enzotib
  • 92,255
  • 11
  • 164
  • 178
  • I tried that but it didn't work. For instance System Monitor shows Cairo-Dock ID is 1452, so I run `gdb program 1452` (I also tried `gdb 1452`), but it returns "1452: No such file or directory. " – neydroydrec Sep 04 '11 at 14:07
  • The first one should work. What did it return? – enzotib Sep 04 '11 at 14:13
  • This is what it returns fully after asking for pid 1840 (Skype): `GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2 Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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 "i686-linux-gnu". For bug reporting instructions, please see: ... 1840: No such file or directory.` – neydroydrec Sep 04 '11 at 16:50
  • I said the first one, that with `gdb program pid`. I think the output you shown is from `gdb pid` – enzotib Sep 04 '11 at 21:08
  • Sorry, my mistake. `gdb program 3385 (again Skype) returns [this](http://pastebin.com/TNBUsFdi). – neydroydrec Sep 05 '11 at 08:21
  • @Benjamin: I see `Could not attach to process`, it is a `root` owned process? – enzotib Sep 05 '11 at 08:23
  • I can't see that in System Monitor. How can I find out? – neydroydrec Sep 05 '11 at 09:16
  • @Benjamin: `ps -ef | grep [p]rocessname` should show the user in the first field. – enzotib Sep 05 '11 at 09:21
  • it's a user process. – neydroydrec Sep 05 '11 at 11:31
5

You are likely looking for retty. You could also look for "attach tty". Basically it's done using ptrace so you could even roll your own.

nc3b
  • 151
  • 2