- Segmentation fault
- Stack overflow
- Troubleshooting Segmentation Violations/Faults
- Debugging Go Code with GDB
- Go has a debugger—and it's awesome!
- derekparker/delve
- mailgun/godebug
Segmentation faults have various causes, and are a common problem in programs written in the C programming language, where they arise primarily due to errors in use of pointers for virtual memory addressing, particularly illegal access. Another type of memory access error is a bus error, which also has various causes, but is today much rarer; these occur primarily due to incorrect physical memory addressing, or due to misaligned memory access – these are memory references that the hardware cannot address, rather than references that a process is not allowed to address.
A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).
Segmentation fault by Wikipeida
Then let's create some segmentation fault in Go:
package main
func main() {
var ptr *int = nil
*ptr = 100
}
/*
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x400c02]
*/Let's debug this with gdb:
$ gdb ./00_segmentation_fault
(gdb) run
Starting program: /home/ubuntu/go/src/github.com/gyuho/learn/doc/segmentation_fault_debug/code/00_segmentation_fault
Program received signal SIGSEGV, Segmentation fault.
main.main ()
at /home/ubuntu/go/src/github.com/gyuho/learn/doc/segmentation_fault_debug/code/00_segmentation_fault.go:5
5 *ptr = 100
(gdb)
In C++:
#include <iostream>
using namespace std;
int main()
{
int *ptr = NULL;
*ptr = 100; // Write to invalid memory address
// Segmentation fault (core dumped)
}
Let's debug this with gdb:
$ gdb ./a.out
(gdb) run
Starting program: /home/ubuntu/go/src/github.com/gyuho/learn/doc/segmentation_fault_debug/code/a.out
Traceback (most recent call last):
File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'
Program received signal SIGSEGV, Segmentation fault.
0x00000000004006dd in main ()
(gdb)
In software, a stack overflow occurs if the stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory.
Stack overflow by Wikipedia
Here's an example of stack overflow in Go:
package main
func f() {
g()
}
func g() {
f()
}
func main() {
f()
/*
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
...
*/
}And in C++:
#include <iostream>
int f();
int g();
int f(){
g();
}
int g() {
f();
}
int main()
{
f(); // stack overflows
// Segmentation fault (core dumped)
}