13

I'm trying to debug my ncurses application, using gdb. I use tty command to redirect program's I/O to another terminal. Output works like a charm, but I'm having problems with input. I'm using getch() function to retrieve symbols in my app. So, for instance, if I do in my gdb session:

tty /dev/pts/5

I get my output in another tab of my terminal window (gnome-terminal). My gdb sessions is getting stuck, waiting for input, but when I press any key within my /dev/pts/5 I get it printed out, but the app itself does not except it as an input symbol. When running without gdb everything works fine, I'm also using noecho(), so symbols should not be displayed. So, what's the problem? Is it possible to somehow handle input from redirected terminal?

Xentatt
  • 1,264
  • 22
  • 35
  • note that there is an example like this in the book http://www.amazon.com/The-Art-Debugging-GDB-Eclipse/dp/1593271743/ref=sr_1_1?ie=UTF8&qid=1345020329&sr=8-1&keywords=debug+gdb – Xu Wang Aug 15 '12 at 08:45
  • Thank you. I've taken note of this book. – Xentatt Aug 15 '12 at 09:02

2 Answers2

18

You can attach to your process to debug from a different terminal instead of trying to run the application from within gdb.

Run your process as normal. When it is blocked for user input, find its process ID, and then attach to it with gdb from a different window:

gdb -p <PID>

Your problem is due to the program still expecting its interactive input to be coming from your gdb session.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • 1
    Thank you for this solution. But still the problem is not solved. I got connected to my process. Then I hit *continue* in my gdb session and it got stuck -- I can work with my ncurses application, but can't with gdb. – Xentatt Aug 15 '12 at 07:35
  • 1
    @UgnichenkoDmitriy: You have to set a breakpoint, or interrupt the process in `gdb` with Ctrl-C. – jxh Aug 15 '12 at 07:37
  • Yep, it did help. I've loaded my symbols table with *file* and set my bp. Thank you. – Xentatt Aug 15 '12 at 07:45
  • 2
    BTW, for those, who can't connect with gdb to their process and running Ubuntu, you should echo 0 > /proc/sys/kernel/yama/ptrace_scope or make it permanent and set 0 in /etc/sysctl.d/10-ptrace.conf – Xentatt Aug 15 '12 at 07:48
  • 1
    Another way to succeed in attaching gdb to the process is running gdb as root. This is proposed by GDB: `Could not attach to process. If your uid matches the uid of the target process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf` – hdl May 12 '15 at 12:23
8

Maybe it's a bit late to answer, but hope this helps: It took some time to figure out how to debug ncurses applications, finally i made a very comfy way with the help of gdbserver and tmux.

This way I/O of gdb and the application are completely separated:

debug.sh (the script that starts debugging):

#!/bin/bash
tmux splitw -h -p 50 "gdbserver :12345 ./yourapplication"
tmux selectp -t 0
gdb -x debug.gdb

debug.gdb (one-liner gdb scriptfile for comfort):

target remote localhost:12345

So this way, application starts up on right side, gdb on left waiting to hit continue or any other usual gdb stuff :)

Once you exit, tmux automatically closes the gdbserver (therefore right panel as well) and that's all :)

lorenzodarkside
  • 146
  • 2
  • 4