|
| 1 | +# Introduce |
| 2 | + |
| 3 | +The libstacktrace is a c library that provides a simple API to output the crash call stack to **STDERR** when process crash. |
| 4 | + |
| 5 | + |
| 6 | +## Build |
| 7 | + |
| 8 | +```shell |
| 9 | +$ make |
| 10 | +``` |
| 11 | + |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Include **stacktrace.h** to project, and call **init_stacktrace()** API to initialize library. |
| 16 | + |
| 17 | +```c |
| 18 | +#include "stacktrace.h" |
| 19 | + |
| 20 | +int main(int argc, char *argv[]) { |
| 21 | +{ |
| 22 | + // initialize library. |
| 23 | + init_stacktrace(); |
| 24 | + |
| 25 | + // do some thing. |
| 26 | +} |
| 27 | +``` |
| 28 | +
|
| 29 | +
|
| 30 | +## Example |
| 31 | +
|
| 32 | +Examples of use are provided in **example.c** of the repository, which make a null pointer access and the process crashes with the following output: |
| 33 | +
|
| 34 | +```shell |
| 35 | +[root@localhost libstacktrace]# ./example |
| 36 | +/home/sharp/libstacktrace/example - STACK TRACE: |
| 37 | +/lib64/libc.so.6(+0x36450) [0x7fca8db52450] |
| 38 | +./example() [0x4036f0] example.c bar() |
| 39 | +./example() [0x403703] example.c foo() |
| 40 | +./example() [0x40371d] ?? main() |
| 41 | +/lib64/libc.so.6(__libc_start_main+0xf5) [0x7fca8db3e555] |
| 42 | +./example() [0x402c9a] ?? _start() |
| 43 | +Segmentation fault |
| 44 | +``` |
| 45 | + |
| 46 | +If **CCFLAGS** is add **-g** flag, more detail information will be output: |
| 47 | + |
| 48 | +```shell |
| 49 | +[root@localhost libstacktrace]# ./example |
| 50 | +/home/sharp/libstacktrace/example - STACK TRACE: |
| 51 | +/lib64/libc.so.6(+0x36450) [0x7fa21a2e0450] |
| 52 | +./example() [0x4036f0] /home/sharp/libstacktrace/example.c bar():8 |
| 53 | +./example() [0x403703] /home/sharp/libstacktrace/example.c foo():15 |
| 54 | +./example() [0x40371d] /home/sharp/libstacktrace/example.c main():24 |
| 55 | +/lib64/libc.so.6(__libc_start_main+0xf5) [0x7fa21a2cc555] |
| 56 | +./example() [0x402c9a] ?? _start() |
| 57 | +Segmentation fault |
| 58 | +``` |
| 59 | + |
| 60 | + |
| 61 | +## Dependence |
| 62 | + |
| 63 | +The libstacktrace dependence on **libbfd** (Binary File Descriptor library). |
| 64 | + |
| 65 | +bfd build elf sections data, find the file name, function name and source line where the stack pointer is located based on the elf sections data. |
| 66 | + |
| 67 | +bfd is provided by the **binutils** package, which can be installed in CentOS distributions with the following command: |
| 68 | + |
| 69 | +```shell |
| 70 | +yum install binutils-devel -y |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +## Reference |
| 75 | + |
| 76 | +[https://man7.org/linux/man-pages/man1/addr2line.1.html](https://man7.org/linux/man-pages/man1/addr2line.1.html) |
| 77 | + |
| 78 | +[https://sourceware.org/binutils/docs/bfd/](https://sourceware.org/binutils/docs/bfd/) |
| 79 | + |
0 commit comments