Operating System - HP-UX
1753865 Members
7502 Online
108809 Solutions
New Discussion юеВ

Re: Line and File number of a Calling function! how to get it?

 
SOLVED
Go to solution
Wkdunreal
Advisor

Line and File number of a Calling function! how to get it?

Hi,

I am writting a wrapper function for Malloc and I need to get the line number and file number of Malloc function where ever it is called , to keep track of where Malloc is being called and where free is being called.

This is being done in order to check for memory leaks.

IS there a way i can find out the File number and line number where every it is called. (I will be modifying malloc in the way mentioned).

7 REPLIES 7
Dennis Handly
Acclaimed Contributor

Re: Line and File number of a Calling function! how to get it?

Is this for PA or Integrity?
I would suggest you use gdb's leak detection tools and not invent your own.

>Is there a way I can find out the file number and line number

(I assume you mean filename.)
If you are using PA, you can only find this if you have debug info, -g. And then you would have to use gdb to extract it from the hex addresses.

For IPF, you can get this info from unwind(5).
Rajesh K Chaurasia
Valued Contributor

Re: Line and File number of a Calling function! how to get it?

You can use __FILE__ and __LINE__ to get the file name and line no. information. This does not require compilation with -g and works on PA-RISC and Itanium HP-UX with HP compilers.

For example,

#include
#include

main ()
{
printf ("This is sample output from %s:%d\n", __FILE__, __LINE__);
}

would give following output

This is sample output from test_line.c:6

Regards
-Rajesh
Wkdunreal
Advisor

Re: Line and File number of a Calling function! how to get it?

@Dennis

The gdb is not working on our shell for some reason.Hence we are beign forced to try and create our own wrapper function. I guess one of the libraries is missing .

Currently I was trying to use dladdr to get info about the adresses of object and function from which the call might have been made.

Is there any way to use the 4 parameters specified by dladdr to pinpoint the line number from where the call is made? (we get the function address from dladdr)

@Rajesh

__LINE__ gives the line number where the macro __LINE__ is hard coded, it would not give the line number of the function calls made.

example:

int main()
{
hello();
....
}

int hello()
{
printf("line number is:%d",__LINE__);
...
}

This would give the line number of where in hello the __LINE__ is hardcoded.

What i want is the line number where hello(); is called.

Thanks for your reply guys Really appreciate it :)
Martin Vorlaender
Honored Contributor

Re: Line and File number of a Calling function! how to get it?

For a similar project, we did the following:

#define malloc( size ) \
memtrc_malloc( (size_t)(size), __FILE__, __LINE__ )

You get the idea.

(The whole thing is available at http://vms.pdv-systeme.de/users/martinv/memtrc.zip )

HTH,
Martin
Rajesh K Chaurasia
Valued Contributor

Re: Line and File number of a Calling function! how to get it?

You need to pass __FILE__ and __LINE__ as arguments to the wrapper function which would call real function. The wrapper function can print the call info along with other details.

Regards
-Rajesh
Dennis Handly
Acclaimed Contributor
Solution

Re: Line and File number of a Calling function! how to get it?

>gdb is not working on our shell for some reason. Hence we are being forced to try and create our own wrapper function. I guess one of the libraries is missing.

Have you downloaded wdb 6.0?
It would be much easier to get gdb working than to try to implement something that took years to develop and may not even be documented.
Are you using PA or IPF?

>Is there any way to use the 4 parameters specified by dladdr to pinpoint the line number from where the call is made?

No directly. Not unless you used the unwind(5) functions.

Note: There is no real need to get the function, file and line number in your instrumentation. You can simply play back the hex address and ask gdb for the position.
Dennis Handly
Acclaimed Contributor

Re: Line and File number of a Calling function! how to get it?

If you are happy with the answers you were given, please read the following about how to assign points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33