Dealing with segmentation faults

  • Thread starter Thread starter heartless
  • Start date Start date
AI Thread Summary
To diagnose segmentation faults in applications using gdb, it's essential to understand that segfaults typically occur when a program attempts to access memory it shouldn't, often due to uninitialized pointers or exceeding array boundaries. To effectively identify the specific line causing the fault, users should compile their programs with debugging symbols enabled (using gcc -g) to facilitate better debugging. When a segmentation fault occurs, instead of merely running the program, setting breakpoints and stepping through the code line by line can help pinpoint the issue. Additionally, analyzing core dumps with gdb can provide insights into the program's state at the time of the fault, allowing for further investigation into the cause of the segmentation fault.
heartless
Messages
220
Reaction score
2
Hi everyone,
I'd like to know how to prompt gdb to show me where segmentation fault in an application occurs and why. Any ideas?
 
Technology news on Phys.org
Segfaults happen when your program tries to write to memory that doesn't belong to it. Common causes are:

1) Using uninitialized pointers.
2) Using uninitialized variables for pointer arithmentic (array indexing).
3) Letting a counter go beyond the boundaries of an array.

You should be able to use gdb to step through your code until you find the offending line.

- Warren
 
Yepp, that's what wikipedia says. However do you have any ideas how to find specific line or lines that cause segmentation fault in an application. I tried gdb for "normal" aps, and everything works perfectly, but I can't debug any applications with segmentation faults.

For example:
adrian@ubuntu:~/files/asm$ gdb env_show --quiet
(no debugging symbols found)
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) r
Starting program: /home/adrian/files/asm/env_show

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
You can't do that without a process to debug.
(gdb) info registers
The program has no registers now.
(gdb) bt
No stack.
(gdb)

and so on, I'm not very experienced with gdb,
--Thanks,
 
Rather than just running your program, put a breakpoint in and step through it line by line.

- Warren
 
Also, I presume that you compiled your program with gcc -g to include debugging information in the executable.

- Warren
 
Or run gdb on the core dump. Never done it myself, but the core file should have the program state at the time of the segfault.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
Back
Top