Thread: Calling a function from within GDB

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    Calling a function from within GDB

    I working in GDB to debug my program. At various breakpoints, I want to call a function that I wrote that iterates through my heap at that point in the execution. I have seen that I can call this function from within GDB like this:

    (gdb) call foo()

    But when I do, I get this error:

    (gdb) call foo()
    No symbol "foo" in current context.

    The function clearly exists (and is called later on from within the program, and I know that I am not making any dumb mistakes like typos, but I don't know why it won't execute.

    Am I calling this correctly? Is there somewhere that I can read about this problem? I've searched exhaustively in this, and it looks to me like this error is common for out-of-scope variables, but I don't see it explained with function calls. Maybe it thinks that I am looking for a variable? Very confused.

    Thanks.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I don't know what you did but the following works for me.
    Code:
    #include<iostream>
    using namespace std;
    void foo()
    {
        cout<<"How dare you call foo?"<<std::endl;
    }
    int main()
    {
    
        return 0;
    }
    manasij7479@manasijd ~ $ g++ a.cpp -Wall -g
    manasij7479@manasijd ~ $ gdb a.out
    ...
    Reading symbols from /home/manasij7479/a.out...done.
    (gdb) break 10
    Breakpoint 1 at 0x8048643: file a.cpp, line 10.
    (gdb) run
    Starting program: /home/manasij7479/a.out

    Breakpoint 1, main () at a.cpp:10
    10 return 0;
    (gdb) call foo()
    How dare you call foo?
    (gdb)

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    Thanks. When I call other functions, it seems to work. There is something about the one that I am trying to call that isn't working out. Too much code to post here. I'm probably overlooking something.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Maybe the function's inlined. Then you can't call it unless you turn that optimization off.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    35
    It's not inlined. I got it to work by writing it in a different function. No idea why it worked in one place but not in the other. Thanks for your help.
    Last edited by joatmon; 12-04-2011 at 11:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issues calling a function from within a function.
    By civix in forum C++ Programming
    Replies: 12
    Last Post: 11-06-2010, 12:23 AM
  2. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  3. Calling function a function to do fopen
    By fanoliv in forum C Programming
    Replies: 1
    Last Post: 06-19-2006, 01:44 PM
  4. Calling Cdocument function in Cview function
    By RancidWannaRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 12:09 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM