Thread: List local and global variables

  1. #1
    Registered User
    Join Date
    May 2011

    List local and global variables


    I have a general question here. I am trying to get the list of all global variables in a C code along with the local variables and their scope. I also need the memory address in which they are going to be saved. I already managed to write a script in GDB to run the code step by step and in each step writes the local variable list and their values in a file.
    The thing I am going to need is kinda more comprehensive than that. Actually the only option that I see right now is to write a code to process the output file of the mentioned script and create another script to get the address of the variables. this is a hard job and I am wondering is there any way to get the variable names and addresses (automatically, by using gdb or any other tool). and also I couldn't find a way to get the list of global variables in gdb.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Valgrind Home
    Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools.

    The Valgrind distribution currently includes six production-quality tools: a memory error detector, two thread error detectors, a cache and branch-prediction profiler, a call-graph generating cache and branch-prediction profiler, and a heap profiler. It also includes three experimental tools: a heap/stack/global array overrun detector, a second heap profiler that examines how heap blocks are used, and a SimPoint basic block vector generator. It runs on the following platforms: X86/Linux, AMD64/Linux, ARM/Linux, PPC32/Linux, PPC64/Linux, X86/Darwin and AMD64/Darwin (Mac OS X 10.5 and 10.6).
    Perhaps study how these tools work. In order to know whether something has overrun, it has to know where it is to begin with. From that, it should be a small step to figuring out it's name and contents.

    > and also I couldn't find a way to get the list of global variables in gdb.
    You could pre-parse the output of 'nm prog', which will list all the global symbols in prog.

    I'm not sure what you want to get from the addresses of local variables. For utility functions called from different places, the addresses of it's local variables are going to change from one call to another.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Thanks for the information. I will look into the tool you mentioned.
    I am a computer engineer and right now I am working on a project to investigate the relationship between the contents of the memory (variables in general) and also their addresses, and the power usage of the processor and memory. it is kind of a hard job but this is the first step needed for doing it. as for the local variables, I am aware of the difference in address in different calls. in fact, I am treating them like they are completely different variables.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    *scratches head*
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2011, 10:33 PM
  2. Local vs Global Variables
    By BENCHMARKMAN in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2007, 05:17 AM
  3. local and global variables???
    By geo_c in forum C Programming
    Replies: 5
    Last Post: 08-23-2004, 03:02 PM
  4. Global Vs Local Variables
    By Waldo2k2 in forum C++ Programming
    Replies: 17
    Last Post: 11-11-2002, 07:51 PM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM

Tags for this Thread