How to convert program counter in drmemtrace to its offset in the binary

28 views
Skip to first unread message

Chao Jin

unread,
Jan 28, 2025, 12:36:23 AM1/28/25
to DynamoRIO Users
Good day to everyone reading this email :-)

I'm trying to map pc recorded in drmemetrace (pc of _memref_data_t) to associated source line using bin utilities, like addr2line.

I found it is actually an address used in process. Wondering how to convert it to the address used in the binary file, like the one shown by objdump.

Wondering if there is any API or controller I can use directly?

Thank you for your help in advance!

Chao

Abhinav Sharma

unread,
Jan 28, 2025, 11:42:25 AM1/28/25
to DynamoRIO Users
Hi,

Take a look at raw/modules.log in the trace directory generated by drmemtrace. To convert a given trace_pc to (file, offset), the first step would be to find the entry where start <= trace_pc and trace_pc < end.

For example: for a trace pc 0x7f10e9c08f3d, it is the following entry #13.

$ cat modules.log
Module Table: version 5, count 29
Columns: id, containing_id, start, end, entry, offset, preferred_base, (custom fields), path
...
 13,  12, 0x00007f10e9bee000, 0x00007f10e9c14000, 0x00007f10e9c08320, 0000000000001000, 0x0000000000001000, v#1,0, /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2

The offset within the file can be computed as: offset + trace_pc - start. In the example, the offset would be 0x1000 + 0x7f10e9c08f3d - 0x7f10e9bee000 = 0x1BF3D in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2.

We don't have an API yet for this, but I imagine it would be useful. It can probably be added to module_mapper_t.

Take a look at module_mapper_t::find_mapped_trace_bounds (https://github.com/DynamoRIO/dynamorio/blob/f007194c734b769c7af69df332c9d36583b97281/clients/drcachesim/tracer/raw2trace_shared.cpp#L195). It does not apply directly to your use case, but it is relevant. In the loop when the containing module is found (https://github.com/DynamoRIO/dynamorio/blob/f007194c734b769c7af69df332c9d36583b97281/clients/drcachesim/tracer/raw2trace_shared.cpp#L216), you can use:
mvi->path and mvi->seg_offs + (trace_address -  mvi->orig_seg_base) as the file, offset pair.

We welcome community contributions, so feel free to send a PR to add the new API to module_mapper_t. Let me know if this helps.

Abhinav


Reply all
Reply to author
Forward
0 new messages