4

I am trying to explore gdb, objdump, valgrind and nm tools for debugging purpose in linux.

I am able to print local variables using info locals in GDB but I need to go into current stack frame to print local variables.

Is there any way to print all the local and global variables (with values) used in a C code (maybe from a coredump if program is crashed) without going into particular stack frame?

1
  • "info variables"----> Print the names and data types of all variables that are declared outside of functions (i.e. excluding local variables).but you won't get values. "info locals"---> Print the local variables of the selected frame, each on a separate line. These are all variables (declared either static or automatic) accessible at the point of execution of the selected frame.Again you won't get values.It's just list Commented May 14, 2013 at 10:07

2 Answers 2

2

As you pointed out, in gdb you can show the local variables for the current frame with info locals. If your restriction is that you don't want to manually go into each frame, then you can use a simple gdb script that does that for you. For example:

define locals-up
  set $n    = ($arg0)
  set $upto = $n
  while $upto > 0
    info locals
    up-silently 1                                                                
    set $upto = $upto - 1
  end
  down-silently $n
end
document locals-up
  locals-up <n>: Lists local variables of n frames
end
Sign up to request clarification or add additional context in comments.

Comments

-1

Well no, since you have to analyze the stack in order to figure out which local variables even exist.

If you have a a function:

static int foo(int a, int b, int c)
{
  const int ab = a + b;
  const int bc = b + c;

  return ab * bc;
}

you can't talk about "the local variables of foo()" unless you have a stack frame that indicates that foo() is running. Otherwise, the local variables won't exist: they're allocated on the stack as the function is entered, after all.

Of course, there could be a "clever" command to walk up the stack from a given breakpoint's frame and print all encountered functions' local variables, but that doesn't sound like what you're after.

2 Comments

as you said local variables are allocated on the stack as function is entered. so is there not a way to save the local variable with values(at the time of execution of function) in a file. this can be useful for debugging.
I am not aware of any debugger that can do that 'automatically'. Could be messy, especially if the function is entered from multiple threads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.