From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9126 invoked by alias); 13 Jan 2009 03:54:13 -0000 Received: (qmail 9117 invoked by uid 22791); 13 Jan 2009 03:54:13 -0000 X-SWARE-Spam-Status: No, hits=-1.6 required=5.0 tests=AWL,BAYES_00,FB_WORD1_END_DOLLAR,J_CHICKENPOX_13,SARE_MSGID_LONG40,SPF_PASS X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (216.239.33.17) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 13 Jan 2009 03:53:34 +0000 Received: from wpaz5.hot.corp.google.com (wpaz5.hot.corp.google.com [172.24.198.69]) by smtp-out.google.com with ESMTP id n0D3rUs8010438 for ; Tue, 13 Jan 2009 03:53:31 GMT Received: from rv-out-0506.google.com (rvbf6.prod.google.com [10.140.82.6]) by wpaz5.hot.corp.google.com with ESMTP id n0D3rSpZ028204 for ; Mon, 12 Jan 2009 19:53:28 -0800 Received: by rv-out-0506.google.com with SMTP id f6so12619269rvb.59 for ; Mon, 12 Jan 2009 19:53:28 -0800 (PST) MIME-Version: 1.0 Received: by 10.141.146.4 with SMTP id y4mr15047086rvn.88.1231818808307; Mon, 12 Jan 2009 19:53:28 -0800 (PST) In-Reply-To: <21428195.post@talk.nabble.com> References: <21428195.post@talk.nabble.com> Date: Tue, 13 Jan 2009 03:54:00 -0000 Message-ID: Subject: Re: break when opening certain file From: Doug Evans To: peter360 Cc: gdb@sourceware.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2009-01/txt/msg00061.txt.bz2 On Mon, Jan 12, 2009 at 6:11 PM, peter360 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 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)