From: Doug Evans <dje@google.com>
To: peter360 <peter360@fastmail.us>
Cc: gdb@sourceware.org
Subject: Re: break when opening certain file
Date: Tue, 13 Jan 2009 03:54:00 -0000 [thread overview]
Message-ID: <e394668d0901121953q274a740cwea3c02d772233f1c@mail.gmail.com> (raw)
In-Reply-To: <21428195.post@talk.nabble.com>
On Mon, Jan 12, 2009 at 6:11 PM, peter360 <peter360@fastmail.us> wrote:
>
> I want to set a break point when the program I am debugging opens a certain
> file. I can do "break open", but how do I make gdb stop only when file
> "foo" is opened? Thanks!
You want a "conditional breakpoint".
Assuming you're on i386-linux or similar (adjustments are
straightforward for most other platforms),
#include <fcntl.h>
int
main (int argc, char *argv[])
{
int i;
for (i = 1; i < argc; ++i)
open (argv[i], O_RDONLY);
return 0;
}
bash$ gcc -g foo.c
bash$ gdb --args a.out foo bar baz
(gdb) start
(gdb) break open
(gdb) condition 2 strcmp (((char**)$esp)[1], "bar") == 0
(gdb) c
Continuing.
Breakpoint 2, 0x42ce0e80 in open () from /lib/tls/i686/cmov/libc.so.6
(gdb) x/s ((char**)$esp)[1]
0xffffdafe: "bar"
(gdb) c
Continuing.
Program exited normally.
(gdb)
You kinda have to know the i386 calling convention to know that on
entry to open the file name is at ((char**)$esp)[1].
The use of "start" above simplifies a few things, it runs the program
to the start of main(). At this point libc is loaded and any open
calls made while trying to get to main() are skipped.
One caveat is that specifying the condition this way will call
malloc() to allocate space for "bar" so that the call to strcmp will
work. If you don't want to call malloc when testing the breakpoint
condition then one alternative is to manually compare the characters.
A real pain, but if you're debugging a problem and you just need to
have gdb stop at the right file you often don't need to compare every
character in the file name, just enough to get you close. You can
keep hitting continue until the breakpoint hits the right one.
Starting over,
bash$ gdb --args a.out foo bar baz
(gdb) start
(gdb) break open
(gdb) condition 2 ((char**)$esp)[1][0] == 'b'
(gdb) commands 2
x/s ((char**)$esp)[1]
end
(gdb) c
Continuing.
Breakpoint 2, 0x42ce0e80 in open () from /lib/tls/i686/cmov/libc.so.6
0xffffdafe: "bar"
(gdb) c
Continuing.
Breakpoint 2, 0x42ce0e80 in open () from /lib/tls/i686/cmov/libc.so.6
0xffffdb02: "baz"
(gdb) c
Continuing.
Program exited normally.
(gdb)
next prev parent reply other threads:[~2009-01-13 3:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-01-13 2:12 peter360
2009-01-13 3:54 ` Doug Evans [this message]
2009-01-15 7:11 ` peter360
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e394668d0901121953q274a740cwea3c02d772233f1c@mail.gmail.com \
--to=dje@google.com \
--cc=gdb@sourceware.org \
--cc=peter360@fastmail.us \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox