Unix Technical Forum

getting the filename and linenumber of a function out of debug infos

This is a discussion on getting the filename and linenumber of a function out of debug infos within the AIX Operating System forums, part of the Unix Operating Systems category; --> hi all, i am in the process of writting a aix specific memory leak detector, which uses the 'user ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2008, 09:37 AM
Christoph
 
Posts: n/a
Default getting the filename and linenumber of a function out of debug infos

hi all,
i am in the process of writting a aix specific memory leak detector,
which uses the 'user defined malloc replacement' feature of AIX.
https://sourceforge.net/projects/aixmem/
currently extremly unstable :-)

My library replaces the original *alloc functions with my own
implementation.
In my malloc function i log all allocations and free's in a logfile and
print a backtrace of last n functions.

it looks about like this:
$MALLOC 0020049290 165 Bytes 13:23:19 13:23:19 Stack:
printStack() 3553628824
malloc() 3491616196
test1() 268436604
main() 268436476

What i want now is:
if debug info is aviable then print the filename and line number in
addition to the functions address.

$MALLOC 0020049290 165 Bytes 13:23:19 13:23:19 Stack:
printStack() 3553628824 aixmem.c:123
malloc() 3491616196 test.c:3 // this is the important info
test1() 268436604 test.c:8
main() 268436476 test.c:1

My Question is:
How can i get the Filename and Linenumber of the function calling
malloc.
Where is the this Information stored and could i access it?
Since this is a library i can't use defines like
#define malloc mymalloc(size_t, __FILE__, __LINE__)


small example:
test.c:
-------------------
void test(void)
{
malloc(10); // this is main.c line 3
}

int main()
{
test();
}

aixmem.c
--------------
void *__malloc__(size_t size)
{
printStack(); // here i somehow have to print out: "test.c:3"
}


Christoph

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-05-2008, 09:38 AM
Paul Pluzhnikov
 
Posts: n/a
Default Re: getting the filename and linenumber of a function out of debuginfos

"Christoph" <christoph.luder@gmx.ch> writes:

> My Question is:
> How can i get the Filename and Linenumber of the function calling
> malloc.


If that function was compiled with '-g', the file/line info is in
the symbol table. For example, compiling your test.c with 'cc -g -c'
and running 'objdump --sym test.o' produces:

...
[ 27](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 2) 0x00000040 .test
AUX tagndx 0 ttlsiz 0x44 lnnos 632 next 35
AUX indx 17 prmhsh 0 snhsh 0 typ 2 algn 0 clss 0 stb 0 snstb 0
..test :
1 : 0x4c
2 : 0x58
[ 30](sec -2)(fl 0x00)(ty 0)(scl 142) (nx 0) 0x00000040 test:F-11
[ 31](sec 1)(fl 0x00)(ty 0)(scl 101) (nx 1) 0x0000004c .bf
AUX lnno 3 size 0x0 tagndx 0
[ 33](sec 1)(fl 0x00)(ty 0)(scl 101) (nx 1) 0x0000006c .ef
AUX lnno 4 size 0x0 tagndx 0

From this, a debugger can tell that the test() function begins on
line 3, and ends on line 4, the offset from the start of test()
to the first line in the function is 0x4c, and to the second line
is 0x58, etc.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-05-2008, 09:47 AM
Christoph
 
Posts: n/a
Default Re: getting the filename and linenumber of a function out of debug infos

hi,
thanks for your reply.
But that will tell me in which c-file the function test1() is defined.

What i want to know is, in which file / line was malloc() called.

call stack:
__malloc__ // my own malloc function
test1 // in which file /line was test1() called ?
main


Christoph

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-05-2008, 09:48 AM
Paul Pluzhnikov
 
Posts: n/a
Default Re: getting the filename and linenumber of a function out of debuginfos

"Christoph" <christoph.luder@gmx.ch> writes:

> But that will tell me in which c-file the function test1() is defined.


What will tell you that?

> What i want to know is, in which file / line was malloc() called.


You already have the PC values for all functions in the back trace.

Now you must iterate over loaded objects ("man loadquery", L_GETINFO)
to find out which object the given PC belongs to.

Once you find the right object, you search its symbol table to find
out which function that PC belongs to.

Finally, you iterate over AUX records for that function, to find
out file and line number.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:48 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com