Operating System - HP-UX
1752782 Members
5909 Online
108789 Solutions
New Discussion юеВ

Re: Can i dynamically load a library half way through a running process?

 
SOLVED
Go to solution
Wkdunreal
Advisor

Can i dynamically load a library half way through a running process?

Hi stalwarts,

There is this process(lets say A) which keeps running on a system for weeks together, when a transaction is made through the process, I want to check memory leaks and stuff.

For this purpose I made a library which contains wrappers for memory allocations etc (lets call it lib.so ).Currently I am able to get my results when i use LD_PRELOAD=./lib.so and dynamically load it just before the execution of the process( A ).

Now I require to load this library somewhere in between the run of the process (A).

example:

T=0 process A starts
T=10 I want to load my library lib.so
T=20 I want to unload my library.
T = 100000000 process still running

Between times T=10 and T=20 I want my libraby to be accessed and not the original libc.so .
So between T=10 and T=20 my wrappers which i have written for malloc, calloc, realloc and free will be called and not the original frees and mallocs.

before T=10 and afteer T=20 the original mallocs , frees etc would be working.

Is this situation possible?
Can anyone give me pointers how to proceed with this requirement of mine?

Thanks a bunch
Regards,
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: Can i dynamically load a library half way through a running process?

>Between times T=10 and T=20 I want my library to be accessed and not the original libc.so.

You can't do this. Once a function is called, from a load module, it will continue to call what it found the first time.

Otherwise you will have to use LD_PRELOAD=./lib.so and have it call the libc functions. Then upon loading a shlib, you could have lib.so change what it does.

Note: If you have allocated something with libc then change to deallocate it with your lib.so, you may have problems. Or you may have accounting errors. I.e. a free without it being in your table. Or a malloc assumed without a free.
Wkdunreal
Advisor

Re: Can i dynamically load a library half way through a running process?

Hi Dennis,

Thanks for replying :)

The lib.so which I have made actually contains the wrappers for libc.so calls.
So when I do LD_PRELOAD=./lib.so to record all the data within these wrappers, I have to use LD_PRELOAD just before i run the exe for the process :(.

From what I could get from your post, I think i am doing the same thing (ie: using LD_PRELOAD and loading lib.so before the start of the process)

I have two doubts.
1) Is there any way i could do LD_PRELOAD while the process is running.
So that the moment i do LD_PRELOAD=./lib.so , the process starts picking up the wrappers for malloc, realloc, calloc and free from lib.so the moment I do LD_PRELOAD. and vice versa for unset LD_PRELOAD.

2)When I open a shell and set LD_PRELOAD, is there any way for me to open one more shell so that both these shells are connected and behave the same way with respect to enviornment variabls and shlibs etc.

example:
lets say I opened shell1 and in this i set LD_PRELOAD=./lib.so


when i open shell2 , is there any way so that i can get to see echo $LD_PRELOAD give the output ./lib.so without havign to set this in shell2?

Why i want to do this is so that on shell1 i shall run the process and on shell two i will dynamically load lib.so when the process is already up. So that the process on shell1 picks up this lib.so on shell2.

Is there any way to get such a kind of situation.

Thanks a million.
Regards
Dennis Handly
Acclaimed Contributor
Solution

Re: Can i dynamically load a library half way through a running process?

>1) Is there any way I could do LD_PRELOAD while the process is running.

No. As I said: Once a function is called, from a load module, it will continue to call what it found the first time.

>the process starts picking up the wrappers for malloc, realloc, calloc and free from lib.so the moment I do LD_PRELOAD

You need to always do LD_PRELOAD and dynamically sense whether your wrappers do their normal work or just bypass to libc.

>2) When I open a shell and set LD_PRELOAD, is there any way for me to open one more shell so that both these shells are connected

Not that I know. But a shell isn't important, only your process is.

You should never export LD_PRELOAD. You must set it just for one command:
LD_PRELOAD=./lib.so command ...

>is there any way so that I can get to see echo $LD_PRELOAD give the output ./lib.so without having to set this in shell2?

I'm not sure how good this will do:
$ (LD_PRELOAD=xx; echo $LD_PRELOAD)

>So that the process on shell1 picks up this lib.so on shell2.

No, you can't do that.

As I mentioned above, always use lib.so. And have the wrappers there dynamically sense what you want to do. Possibly looking at shared memory, or a user signal.