Retrieving/Listing global variable info

Posted on 2004-11-02
Last Modified: 2012-06-27
I have been tasked with writing a program that will retrieve global variable information from a combination of c and c++ programs (compiled w/gcc).
I need to be able to get the variable names, types, and addresses for all defined global variables.

The only way I see that this can be done is to compile with debug info (gcc -g) and then to use a debugger such as gdb or dbx in batch mode to retrieve the information.
Is there a way to list all of the global variables by name in gdb or dbx?  

Also, if someone has another approach, suggestions are welcome...
The program needs to work in both cygwin and IRIX (both have gcc, gdb, and dbx available).


Question by:str8dn
    LVL 23

    Assisted Solution

    consider using 'nm -g'

    LVL 23

    Assisted Solution

    I don't believe gdb provides what you are looking for

    Suppose you have compiled with debugging symbols...

    objdump -t --demangle programNameHere |awk '{if ($2=="g"&&$3=="O"&&($4==".data" || $4==".bss")){print $0}}' |less

    and include any other symbol types like  .rodata    that you might need to extract also

    Objdump is part of binutils, but you may very well need a different strategy for each runtime

    I don't have Cygwin or IRIX available, so I can't say what will happen exactly.. it may be better to try just
    objdump -t

    first,  then store the output of that in a file, and have your software process and filter the result listing

    LVL 22

    Expert Comment

    What are you REALLY trying to do?

    LVL 17

    Assisted Solution

    For a release build, I guess you need to look at the map file, generated by the linker. The GNU linker ld has the -M option to output the map to stdout.
    LVL 1

    Author Comment

    Thanks for the suggestions.  I have been looking into nm, objdump, and ld, and I seem to be coming up short on documentation
    other than the manual pages that are everywhere.  While man pages are helpful, they do not really let me know what I am
    looking at for several of the tags/info output.  Is there something that lays these out?

    The program I am writing is to take the list of global variables with their addresses, dimensions, and types, and pass them
    to another program. The second program allows changes to be made to the values of the variables already in memory via PERL script.  

    I have already written the second program (c++ and PERL to read/set values to/from memory).  The program that was passing me the
    global variable info is majorly flawed and poorly written (not to mention it requires "dwarfdump", which is not readily
    available on other systems), so I am redoing it.

    The Binutil objdump appears to give me the variable names, dimensions and type with the following:
    objdump --stabs --demangle -e a.exe

    However, I am unsure of what I am really looking at with output such as:

    622    GSYM   0      10     00000000 140134 tmp_testIntArray:G(1,9)=ar(11,7);0;3;(0,1)
    tmp_testIntArray      /usr/include/c++/3.3.1/iostream      0;"      kind:v      type:int |[4]:uint32

    The var tmp_testIntArray I set up, and I assume GSYM means it is a "global symbol".
    It is an int arra with a single dim of 4 elements (from int [4]).

    As for the rest of it, I am quite ignorant, save that uint32 probably means it is 32 bit.  Explanations appreciated.
    I need to do a couple of things yet:
    1. Identify its whereabouts in memory.
    2. Identify which variable were defined in my files (since I am not really interested in all of the std:: and type definitions, etc.
      that are spit out along with my variables.)

    Additinoal suggestions are welcome, and perhaps I have not given nm or ld enough of a look, objdump has been getting most of the attention.


    LVL 23

    Expert Comment

    What, exactly, are you trying to accomplish?

    It sounds like you are writing a perl program that attempts to modify the values
    of variables in a loaded, running application, based upon the un-fixedup offsets
    in the unloaded image.  That would violate the memory protection mechanisms
    of the OS.   Whenever someone asks me a request that seems quite strange, I
    usually inquire as the the overall goal of the application.  So I ask, "What are
    you really trying to accomplish?" (Not "how" you plan accomplish it.)  Perhaps
    there is a better way to accomplish the goal.

    LVL 1

    Author Comment

    All of the memory manipulation is done via c++.  Perl talks to a c++ program and tells it which variables to give which values.
    That way any violations are avoided.

    We currently have a large number of c and c++ programs that use global variables as settings for a simulation.   Theses variables
    are changed often enough that I have been requested to set up a shell through which changes can be made.

    I have the memory manipulation and shell issues resolved.  What I need now is to be able pass the initial information for all of the
    existing global variables to the shell.   In order to avoid compiling some type of master list, I want to be able to read this info directly
    from the programs.  A master list would have to be constantly updated every time someone added a variable - highly undesirable.

    For each global variable I need the following:
    Address in memory

    LVL 17

    Expert Comment

    That sounds horrendous. I guess it's too late in the day to get yor developers to organise their variables?
    LVL 22

    Accepted Solution

    This looks really awful.   It's not going to be easy to get the variable type info.

    Could you do something just a TEENSY bit cleaner, like get the programs to register their variables if a command line parameter is passed?  Something like (in C ):

    #define Register( Var, Type, Len )   \
         printf("Variable %s |  Type %s   | Length %d  | Address  %8x\n", "Var", "Type",  Len, &Var );

    int main() {
    if( option = 'r' ) {
        Register( NumberOfRubbersInMachine, int, 1 );
        Register( InitialCostPerItemPerDayOfWeek,  float, 7 );
        Register( ChartTitle, char, 80 );
        ... and so on for all the variables you want to be visible
       { mainprogram( argc, argv ); }


    This would be almost infinitely cleaner than messing around with load maps.

    The question is do you have that much "juice" to get others to do a TEENSY bit of work.

    If that's too much work, you could write a little perl script, no more than 30 lines or so,
    that would insert these lines for you.

    LVL 1

    Author Comment

    Thanks for all of the comments, I learned a great deal from them.

    After perusing the mess of information that debug makes me cull through, I believe that grg99's solution will be cleanest and
    most portable.  I had considered something similar but thrown it out as too much hassle.  Thanks for helping me to see the


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now