jeudi janv. 24, 2013

simple tracing of bad free's using Dtrace

#pragma D option quiet

inline int stack_size = 3; inline int error_stack_size = 10;

pid$target:libc:malloc:entry { self->alloc_size = arg0; ustack(stack_size); } pid$target:libc:malloc:return /self->alloc_size && arg1 != 0/ { printf("alloc of size [%d] on addr 0x%p\n",self->alloc_size,arg1); allocations[arg1] = 1; } pid$target:libc:malloc:return /self->alloc_size && arg1 == 0/ { printf("alloc of size [%d] failed\n",self->alloc_size); } pid$target:libc:free:entry /arg0 != 0 && allocations[arg0]/ { printf("free of addr 0x%p\n",arg0); } pid$target:libc:free:entry /arg0 != 0 && !allocations[arg0]/ { printf("free of unallocated addr 0x%p\n",arg0); ustack(error_stack_size); } pid$target:libc:free:entry /arg0 == 0/ { printf("free of NULL addr !!\n"); ustack(error_stack_size); }

Tracing processes

What this process is doing ? 

Tracing a running process and find out what it is doing is 

also  a good way to prevent a crash or to understand what caused the crash.

Now with Dtrace, processes can be traced a lot more efficiently I agree, but for that you will need a Dtrace script or be a Dtrace expert who can write Dtrace instruction on the fly. This is not the case of everybody and when the question

is simple as "which files are opened by that process" or "what is the port number he is trying to connect to"  a simple call to a tools like truss (strace for linux) is much more quicker.


several commands are available. Man pages are usually good and

drawing will be more efficient than my poor English. I will then only use

pictures which are (I hope) self explanatory.

Using truss(1)

oups

Using Whocalls (1)


oups

Using apptrace (1)/sotruss (1) 

oups

oups

oups


        
    

memory problem with libumem

Debuggers like dbx are quite powerful to diagnose memory problems (try help rtc inside dbx).

The "problems" with debuggers is that they are not always available on the host you are working on.

Plus when you have to diagnose a problem in a shared library , using a debugger may become a little bit heavy.


A nice alternative is the libumem on Solaris. You can get debugging information

about memory usage just by using a LD_PRELOAD statement.

here are pictures describing basic usage. For more information go to the

man page  libumem(3LIB). The first example demonstrate a leak and the second

one a wrong access problem

oups
oups



About

Emmanuel Jannetti blog

Search

Archives
« janvier 2013
lun.mar.mer.jeu.ven.sam.dim.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
25
26
27
28
29
30
31
   
       
Today