Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a segmentation fault

How do I debug a segmentation fault?

Basically this is what happens:

I run my server in background: ./server &

then I run my client: ./client

When I try to login to my server, on correct username and password, everything is okay, but when I type invalid user and password, it results in a segmentation fault.

How do I make the compiler/debugger able to output what error its actually see that causes segmentation core dump.

I know gdb but I try using gdb client but it doesn't seem to work.

like image 767
user1587149 Avatar asked Aug 12 '12 09:08

user1587149


People also ask

What do you do with a segmentation fault?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

Why am I getting a segmentation fault?

The following are some typical causes of a segmentation fault: Attempting to access a nonexistent memory address (outside process's address space) Attempting to access memory the program does not have rights to (such as kernel structures in process context) Attempting to write read-only memory (such as code segment)


2 Answers

A good idea with segmentation faults is to run the program with valgrind for debugging. That way, you'll often get more detailed information about what caused your segmentation fault. For example, it will tell you if you are reading from uninitialized memory.

like image 166
celtschk Avatar answered Oct 19 '22 19:10

celtschk


If you are using g++ first compile your program using the -g option. Then use

 gdb name_of_program core 

to run gdb on the core dump you get (name_of_program is the name of the executable file you just built with g++). This link is useful for how to use gdb.

http://www.ibm.com/developerworks/library/l-gdb/

like image 33
mathematician1975 Avatar answered Oct 19 '22 21:10

mathematician1975