Jason S
Jason S

Reputation: 189626

in Java: programmatically determining addresses of C/C++ variables given a COFF/ELF/DWARF executable

This is a situation I run into now and then:

For an embedded system which does not use virtual addressing, I have an executable file that was compiled from C or C++ code with debugging information included. It's usually in COFF or ELF/DWARF (I get those two mixed up) format.

At runtime, on a PC, I would like to determine the address of a variable given its name. (e.g. "foo.bar[7].baz") This allows me to read/write the variable's value on the embedded system (given a debugging protocol that is beyond the scope of this question). Obviously, any variables that are stack-based or heap-based are out since they don't have static addresses.

I've done this before myself in C++ to parse COFF files from TI's compiler for their 2800 series DSPs, and it was kind of a pain. I was wondering if there was a Java library out there that does this sort of thing already, since I'm facing the same thing with one or two other processors' executable files.


Update: (11/18/2009) A promising clue!

Has anyone out there used the Eclipse CDT ELF parser?

(See http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.cdt.doc.isv/reference/api/org/eclipse/cdt/core/model/IBinary.html for one of the javadoc pgs)

TI's Code Composer 4 (based on Eclipse) seems to use this, so it seems like if I can figure out where the documentation is maybe I can use that to solve my problem.

Upvotes: 5

Views: 1212

Answers (2)

Adam Rosenfield
Adam Rosenfield

Reputation: 400146

You could build a JNI interface to GNU binutils compiled for your platform. However, if the GPL conflicts with your software's license, then this will not be a viable solution.

Upvotes: 1

sigjuice
sigjuice

Reputation: 29749

Does gdb support your target CPU?

If yes, your program that implements the debugging protocol and talks to the target could also implement the GDB Remote Serial Protocol and provide a TCP socket for gdb to communicate with.

The arrangement would be something like this

gdb <--gdb protocol--> java-prog <--your debug protocol--> target

To run the whole thing, assuming your target is already running your program

  1. Run java-prog
  2. Run gdb your-executable and connect to java-prog

    (gdb) target remote 127.0.0.1:port

  3. Ask gdb to read a value

    (gdb) p foo.bar[7].baz

This is translated to gdb packets which are sent to java-prog over TCP. java-prog should do the translation between the gdb protocol and your custom debug protocol.

Upvotes: 2

Related Questions