gdb command or alternative

This is a discussion on gdb command or alternative within the C Programming forums, part of the General Programming Boards category; I haven't found a way to do this in gdb, so I asking. What I would like to do is ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    gdb command or alternative

    I haven't found a way to do this in gdb, so I asking.

    What I would like to do is run a program and get gdb type output (full trace of the program), but not interactively. In short, a file containing gdb type output, (next and step commands) that shows every line the program executes, but I don't want to sit there and do all the typing myself. Is there a way to do this in gdb, or a program other than gdb that can do it?

    I'm looking for a linux or BSD tool, since I have access to either machine.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from running the application really slowly and avoiding typing, what are you attempting to achieve with this?

    There are probably tools that MAY be able to do what you want, if you explain what you want to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    A full trace of the program. A file which contains a listing of every line (with line numbers) that the program executed in the order that they were executed, from start to finish. I looked at the batch option in gdb. But, didn't find a good way to do it using that. If it's a regular statement, or a standard library function, next over it. If it's a function defined by the program, step into it.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    man expect(1)

    By default, gdb will not step into any function that doesn't have debug symbols associated, so you should be able to use "step" exclusively. Even if you get some bogus functions in your output, it's easier to filter those out after the fact than to do it while trying to drive gdb (unless speed is a huge concern -- you know this will be REALLY slow, right?)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by trinli View Post
    If it's a regular statement, or a standard library function, next over it. If it's a function defined by the program, step into it.
    This is going to be the most difficult part.

    I'm not aware of any program that does exactly that, but a quick google comes up with ctrace from sun, which with another go at google finds this:
    Sun Studio C - ctrace source or binary for x86 linux

    So it seems like we could get the source code for ctrace, which instruments the source code, and it shows what lines are executed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Thanks to both of you. I had not seen ctrace. I'll check on that. Also, I could automate it with an expect script. Hadn't thought of that. Ok, I'll see what I can come up with.

    Thanks again.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Depending on what you want this info for, gcov and gprof seem pretty neat. You compile with "gcc -pg -fprofile-arcs -ftest-coverage", run the binary once and it produces a graph file, then you can run gprof and gcov on the source code.

    Here's some sample gcov output:
    Code:
            -:    0:Source:test.c
            -:    0:Graph:test.gcno
            -:    0:Data:test.gcda
            -:    0:Runs:1
            -:    0:Programs:1
            -:    1:#include <stdio.h>
            -:    2:
          20:    3:void func (int x) {
          20:    4:        printf("-> %d\n",x);
          20:    5:}
            -:    6:
            1:    7:int main() {
            -:    8:        int i;
            1:    9:        for (i=0; i<20; i++) func(i);
            1:   10:        return 0;
            -:   11:}
    I don't know if you can get state information about variables, etc, but as you can see, it will tell you how often an instruction is exectuted.
    Last edited by MK27; 05-15-2009 at 11:06 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    At one point I wrote a little tool for Linux that would single-step any given executable and produce on stdout a list of all instruction addresses in the order they were executed.

    You could combine that with a tool like 'addr2line' to map the addresses back to line numbers, and another little tool to collapse repeated instances of the same line to a single instance (since it steps by instruction, not by line).

    If you're interested, I can try to dig out that tool when I get home tonight.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    If you're interested, I can try to dig out that tool when I get home tonight.
    I am. I'll pm you.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. buffered vs unbuffered question
    By Overworked_PhD in forum Linux Programming
    Replies: 6
    Last Post: 07-04-2008, 04:57 PM
  2. Memory allocation error
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2008, 03:24 PM
  3. Contiguous Array version of Linked List
    By ampersand11 in forum C Programming
    Replies: 19
    Last Post: 10-07-2007, 03:05 AM
  4. Too much output in GDB
    By MacNilly in forum Tech Board
    Replies: 0
    Last Post: 09-13-2006, 12:45 PM
  5. does gdb lie?
    By dinjas in forum C Programming
    Replies: 8
    Last Post: 03-10-2005, 04:17 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21