On Friday 26 Aug 2005 7:30 pm, sachin sonone wrote:
> Hi all,
>
> I have  multithreaded application. (pthread_create).
> I am getting Segmentation fault. (randomly)
> Is there any way to know in which thread Segmentation fault has occoured.
>
> Can i  get thread_id  or anything related  in  signal handler?
>
> Regards,
> sachin.

Hi Sachin!

Have you tried using gdb? Or ddd (and GUI for gdb). Try to use this debugger 
to catch the culprit. Also do this:
$ ulimit -c unlimited
$ <run your programme>

Now if your programme segfaults, it'll dump a core. The core is a memory 
footprint of your programme at the time it crashed. It'll be named are 'core' 
or core.<pid>. It's usually a pretty big file.

You can use this file to debug your programme like this:
$ gdb <your programme> <core-file>

(gdb) bt

'bt' is a gdb command that stands for back-trace. It shows the stack of your 
programme at the time it crashed. This way you can pinpoint the place where 
it crashed. Sometimes it might happen that the function in which your 
programme crashed is not really buggy, but maybe it was passed some value by 
it's caller function. Or the caller function put your programme in an illegal 
state. Looking at the stack trace you'll be able to trace back the entire 
function call sequence that led to a seg fault.

You can do the same with ddd. I'd suggest you use ddd as it is a very nice 
GUI. And you can always use the gdb command prompt that's accessible in the 
bottom part of ddd window.

You can also try this:

$ export MALLOC_CHECK_=2

Combine this with 'ulimit -c unlimited'.
Then run your programme. When the env variable MALLOC_CHECK_ is set to 2, as 
soon as there is memory corruption by your programme, it'll be aborted and a 
core will be dumped. This is very useful as otherwise it might so happen that 
memory leak occurs somewhere, but programme crashes sometime later. That 
makes debugging very difficult. Read the manpage for realloc for more 
information on this.
$ man realloc

Scroll down to the paragraph just above the 'BUGS' section. It explains the 
entire 'MALLOC_CHECK_' things.

You can also use tools such as valgrind, but I'm not sure how useful that will 
be. It is primarily for catching bugs due to memory leaks.

I hope this helps you debug your programme.

Regards,
Kapil

-- 
"The Power to Imagine, is The Power to Create!"
-TTux
--
______________________________________________________________________
Pune GNU/Linux Users Group Mailing List:      ([email protected])
List Information:  http://plug.org.in/mailing-list/listinfo/plug-mail
Send 'help' to [EMAIL PROTECTED] for mailing instructions.

Reply via email to