Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using gdb in an IDE

158 views
Skip to first unread message

Charlie

unread,
Mar 12, 2011, 8:02:05 PM3/12/11
to
I am trying to add gdb support in my IDE and I cannot figure out how I am
supposed to send commands to gdb or get output from gdb from inside another
program (my IDE). I have the gdb manual but it seems to leave that part out.
I have read through the sections on the IM interface and it explains the
commands in detail but not how to send them. I have also checked what
command line arguments kdbg and ddd send to gdb when starting and just the -
fullname argument is in common with both of them. Neither one appears to use
the IM interface.

I have wrote a front end to mplayer and it allowed use of a named pipe that
was specified on the command line. gdb does not seem to offer that.

Can someone please explain how and IDE is to control and read output from
gdb?

Thanks in advance,
Charlie

Tauno Voipio

unread,
Mar 13, 2011, 3:47:47 AM3/13/11
to

Get ddd sources and have a look.

--

Tauno Voipio

Jasen Betts

unread,
Mar 13, 2011, 7:35:16 AM3/13/11
to
On 2011-03-13, Charlie <nos...@embarqmail.com> wrote:
> I am trying to add gdb support in my IDE and I cannot figure out how I am
> supposed to send commands to gdb or get output from gdb from inside another
> program (my IDE). I have the gdb manual but it seems to leave that part out.

I'd say to try some pipe(2)s or a socketpair(2).

> I have read through the sections on the IM interface and it explains the
> commands in detail but not how to send them. I have also checked what
> command line arguments kdbg and ddd send to gdb when starting and just the -
> fullname argument is in common with both of them. Neither one appears to use
> the IM interface.

MI?

> I have wrote a front end to mplayer and it allowed use of a named pipe that
> was specified on the command line. gdb does not seem to offer that.

-tty=device

stdin/stdout will get you GDB.

--
⚂⚃ 100% natural

Charlie

unread,
Mar 13, 2011, 8:24:29 AM3/13/11
to
Tauno Voipio wrote:

Actually I did that but it is lost somewhere in the 7mb of source code. I
starting by looking in GDBAgent.C which has the note "Communicate with
separate GDB process" but I still couldn't find the way it does it. I was
hoping someone knew the answer.

Charlie

unread,
Mar 13, 2011, 9:23:03 AM3/13/11
to
Jasen Betts wrote:

> On 2011-03-13, Charlie <nos...@embarqmail.com> wrote:
>> I am trying to add gdb support in my IDE and I cannot figure out how I am
>> supposed to send commands to gdb or get output from gdb from inside
>> another program (my IDE). I have the gdb manual but it seems to leave
>> that part out.
>
> I'd say to try some pipe(2)s or a socketpair(2).
>
>> I have read through the sections on the IM interface and it explains the
>> commands in detail but not how to send them. I have also checked what
>> command line arguments kdbg and ddd send to gdb when starting and just
>> the - fullname argument is in common with both of them. Neither one
>> appears to use the IM interface.
>
> MI?

Yes MI. I knew I should have verified the name before writing this :-)


>
>> I have wrote a front end to mplayer and it allowed use of a named pipe
>> that was specified on the command line. gdb does not seem to offer that.
>
> -tty=device

I think that is for the program being debugged. The description in the man
page reads: "Run using device for your program's standard input and output."

>
> stdin/stdout will get you GDB.
>

I tried "gdb < file" but as soon as the eof is encountered gdb quits. I
tried "gdb < named_pipe" then sending "echo help > named_pipe" on another
terminal and as soon as the data is written to pipe, gdb closes with an
error on stdin.

Could someone please give me an example on how to do this?

Thanks,
Charlie

Alan Curry

unread,
Mar 13, 2011, 6:28:38 PM3/13/11
to
In article <4d7ccfa6$0$65829$892e...@auth.newsreader.octanews.com>,

Charlie <nos...@embarqmail.com> wrote:
>
>I tried "gdb < file" but as soon as the eof is encountered gdb quits. I
>tried "gdb < named_pipe" then sending "echo help > named_pipe" on another
>terminal and as soon as the data is written to pipe, gdb closes with an
>error on stdin.

If you don't want gdb to terminate, don't give it an input stream that
terminates.

>
>Could someone please give me an example on how to do this?

An example in what language?

Your failed examples above only show shell redirection operators. Is your
gdb front end being written entirely written in shell?

If your front end is going to be written in C, you're just wasting time with
these shell redirection commands because those won't be involved in the final
product. You need to learn to write a pipe(), fork(), dup2(), close()
sequence to get all the communication channels in place.

If you're going to be using a higher-level language, then you might not need
to do all that. perl for example has IPC::Open2 which does it all for you.

--
Alan Curry

Charlie

unread,
Mar 13, 2011, 7:23:36 PM3/13/11
to
Alan Curry wrote:

Sorry, let me clarify. I am using C/C++ for my IDE.

I thought I had a solution with popen() but it doesn't work right either. I
use this to initialize gdb:

g_gdb_fd = popen ("gdb -f --nx >gdboutput.txt", "w");

Then later send a command to gdb (cmd = "help\n"):

/* Sends a command to gdb. Returns true if successful */
bool send_dbg_cmd (char *cmd)
{
unsigned int n;

if (g_gdb_fd == NULL) return false;
n = str_length (cmd);
fprintf (stderr, "n = %d\n", n);
if (fwrite (cmd, 1, n, g_gdb_fd) == n)
{
fprintf (stderr, "success\n");
return true;
}
return false;
}

The command however is ignored by gdb. No change in the gdboutput.txt is
made until "pclose (g_gdb_fd);" is called. The output (gdboutput.txt) is:

GNU gdb 6.8-6mdv2009.1 (Mandriva Linux release 2009.1)
Copyright (C) 2008 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 "i586-mandriva-linux-gnu".
(gdb) Hangup detected on fd 0
error detected on stdin

The code snippets are from an Xlib based application and the command is sent
to gdb when a left button message is received so there is plenty of time for
pcopen () to finish.

I found this...

"What we do in AdaCore's IDE is to create a pseudo-tty, and launch
the debugger using that pseudo-tty. That way, GDB thinks it is in
a terminal, and we get to send commands and receive their output.
I believe that this is also what tools such as "expect" do. The code
we used was originally taken from the emacs sources, so you might
be able to find some C code that does just that."

http://comments.gmane.org/gmane.comp.gdb.devel/27399

So I am looking at the forkpty() function now. Wow this is confusing.

If anyone knows how to do this easily please chime in.

Thanks,
Charlie

Charlie

unread,
Mar 13, 2011, 10:12:56 PM3/13/11
to
Charlie wrote:

I found an example of a creating a pseudo terminal that seems to do the
trick:

http://rmathew.blogspot.com/2006/09/terminal-sickness.html

Thanks again to all that responded.
Charlie

Jasen Betts

unread,
Mar 14, 2011, 4:08:50 AM3/14/11
to
On 2011-03-13, Charlie <nos...@embarqmail.com> wrote:

> I thought I had a solution with popen() but it doesn't work right either. I
> use this to initialize gdb:
>
> g_gdb_fd = popen ("gdb -f --nx >gdboutput.txt", "w");
>
> Then later send a command to gdb (cmd = "help\n"):
>
> /* Sends a command to gdb. Returns true if successful */
> bool send_dbg_cmd (char *cmd)
> {
> unsigned int n;
>
> if (g_gdb_fd == NULL) return false;
> n = str_length (cmd);
> fprintf (stderr, "n = %d\n", n);
> if (fwrite (cmd, 1, n, g_gdb_fd) == n)
> {
> fprintf (stderr, "success\n");
> return true;
> }
> return false;
> }
>
> The command however is ignored by gdb. No change in the gdboutput.txt is
> made until "pclose (g_gdb_fd);" is called.

that's becaue nothing is written until that call.
(you need to call fflush() to force the write)

popen is unlikely to give the behaviour you want
(how will you get the responses?)

you need to do it at a lower level with pipes (from pipe()) or
sockets from (socketpair())

reread the LPG, the concepts you will need are all introduced in there

> So I am looking at the forkpty() function now. Wow this is confusing.

at a glance this looks like doing it the hard way

Don't give gdb a pty, don't let it think it's talking to a human.
it's more likely to give a future consistant behavior over raw pipes or
sockets.

I mentioned -tty=device earlier mainly as a way to separate your
program's I/O from the GDB process I/O

openpty() may be a useful way to get a pty for use here.

You're going to need "non-blocking I/O", select(), poll(), or some equivalent
for this project too.

--
⚂⚃ 100% natural

0 new messages