SlideShare a Scribd company logo
Hooking the signals and dumping the  backtraces Thierry GAYET
GOAL  The main goal of this document is to provide some information about the signal handling used for dumping the bacltrace due after a crash.
A backtrace is a list of the function calls that are currently active in a thread. The usual way to inspect a backtrace of a program is to use an external debugger such as gdb. H owever, sometimes it is useful to obtain a backtrace programmatically from within a program, e.g., for the purposes of logging or diagnostics. The header file `execinfo.h' declares three functions that obtain and manipulate backtraces of the current thread.    Function: int backtrace(void **buffer, int size) The backtrace function obtains a backtrace for the current thread, as a list of pointers, and places the information into buffer. The argument size should be the number of void * elements that will fit into buffer. The return value is the actual number of entries of buffer that are obtained, and is at most size. The pointers placed in buffer are actually return addresses obtained by inspecting the stack, one return address per stack frame. Note that certain compiler optimizations may interfere with obtaining a valid backtrace. Function inlining causes the inlined function to not have a stack frame; tail call optimization replaces one stack frame with another; frame pointer elimination will stop backtrace from interpreting the stack contents correctly. Usage
   Function: char ** backtrace_symbols (void *const *buffer, int size) The backtrace_symbols function translates the information obtained from the backtrace function into an array of strings. The argument buffer should be a pointer to an array of addresses obtained via the backtrace function, and size is the number of entries in that array (the return value of backtrace). The return value is a pointer to an array of strings, which has size entries just like the array buffer. Each string contains a printable representation of the corresponding element of buffer. It includes the function name (if this can be determined), an offset into the function, and the actual return address (in hexadecimal). Currently, the function name and offset only be obtained on systems that use the ELF binary format for programs and libraries. On other systems, only the hexadecimal return address will be present. Also, you may need to pass additional flags to the linker to make the function names available to the program. (For example, on systems using GNU ld, you must pass (-rdynamic.) The return value of backtrace_symbols is a pointer obtained via the malloc function, and it is the responsibility of the caller to free that pointer. Note that only the return value need be freed, not the individual strings. The return value is NULL if sufficient memory for the strings cannot be obtained. Usage
   Function: void backtrace_symbols_fd (void *const *buffer, int size, int fd) The backtrace_symbols_fd function performs the same translation as the function backtrace_symbols function. Instead of returning the strings to the caller, it writes the strings to the file descriptor fd, one per line.  It does not use the malloc function, and can therefore be used in situations where that function might fail. The following program illustrates the use of these functions. Note that the array to contain the return addresses returned by backtrace is allocated on the stack.  Therefore code like this can be used in situations where the memory handling via malloc does not work anymore (in which case the backtrace_symbols has to be replaced by a backtrace_symbols_fd call as well).  The number of return addresses is normally not very large. Even complicated programs rather seldom have a nesting level of more than, say, 50 and with 200 possible entries probably all programs should be covered. Usage
#include <execinfo.h> #include <stdio.h> #include <stdlib.h> /* Obtain a backtrace and print it to stdout. */ void print_trace(void) {   void*  array[10]; size_t size; char** strings; size_t i; size  = backtrace(array, 10); strings = backtrace_symbols(array, size); printf (&quot;Obtained %zd stack frames.&quot;, size); for (i = 0; i < size; i++) { printf (&quot;%s&quot;, strings[i]); } /* FOR */ free (strings); } /* print_trace */ /* A dummy function to make the backtrace more interesting. */ void dummy_function(void) { print_trace(); } /* dummy_function */ Int  main(void) { dummy_function (); return 0; } First example
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <sys/times.h> #include <sys/ucontext.h> #include <asm/unistd.h> #include <execinfo.h> struct dict_entry { int num; const char *s; }; #define DICT_ENTRY_END { -1, NULL } #define PRINTF(fmt...) fprintf(stderr,fmt); static int mainpid=0; /* * Simple dictionary * Search an array terminated by an entry with s field set to NULL * If number not found, return &quot;(unknown)&quot; string */ static const char * search_dict(const struct dict_entry *dict, int num) { const struct dict_entry *p = dict; while (p->s) { if (num == p->num) return p->s; p++; } return &quot;(unknown)&quot;; } Second example 1/6
static const struct dict_entry sig_names[] =  { {SIGHUP, &quot;SIGHUP&quot;}, {SIGINT, &quot;SIGINT&quot;}, {SIGQUIT, &quot;SIGQUIT&quot;}, {SIGILL, &quot;SIGILL&quot;}, {SIGTRAP, &quot;SIGTRAP&quot;}, {SIGABRT, &quot;SIGABRT&quot;}, {SIGBUS, &quot;SIGBUS&quot;}, {SIGFPE, &quot;SIGFPE&quot;}, {SIGKILL, &quot;SIGKILL&quot;}, {SIGSEGV, &quot;SIGSEGV&quot;}, {SIGPIPE, &quot;SIGPIPE&quot;}, {SIGALRM, &quot;SIGALRM&quot;}, {SIGTERM, &quot;SIGTERM&quot;}, {SIGSTKFLT, &quot;SIGSTKFLT&quot;}, {SIGCHLD, &quot;SIGCHLD&quot;}, {SIGCONT, &quot;SIGCONT&quot;}, {SIGSTOP, &quot;SIGSTOP&quot;}, {SIGTSTP, &quot;SIGTSTP&quot;}, {SIGTTIN, &quot;SIGTTIN&quot;}, {SIGTTOU, &quot;SIGTTOU&quot;}, {SIGURG, &quot;SIGURG&quot;}, {SIGXCPU, &quot;SIGXCPU&quot;}, {SIGXFSZ, &quot;SIGXFSZ&quot;}, {SIGVTALRM, &quot;SIGVTALRM&quot;}, {SIGPROF, &quot;SIGPROF&quot;}, {SIGWINCH, &quot;SIGWINCH&quot;}, {SIGPOLL, &quot;SIGPOLL&quot;}, {SIGPWR, &quot;SIGPWR&quot;}, {SIGSYS, &quot;SIGSYS&quot;}, DICT_ENTRY_END }; Second example 2/6
static void show_stack() { void *trace[16]; char **messages = (char **)NULL; int i, trace_size = 0; trace_size = backtrace(trace, 16); /* overwrite sigaction with caller's address */ messages = backtrace_symbols(trace, trace_size); /* skip first stack frame (points here) */ printf(&quot;CALLSTACK:&quot;); for (i=1; i<trace_size; ++i) printf(&quot;%s&quot;, messages[i]); free (messages); } Second example 3/6
Second example 4/6 void sigdemux(int sig, struct siginfo *si, void *v) { const char *signame; //struct ucontext *sc=(struct ucontext *)v; //struct sigcontext *regs; int si_code; si_code = si->si_code & 0xffff;  signame = search_dict(sig_names, sig); if (sig != SIGINT)  { PRINTF(&quot;!==========================!&quot;); PRINTF(&quot;SIG %s (#%i) received in PID %ld&quot;,signame,sig,syscall(__NR_gettid)); PRINTF(&quot;Errno%iPID%iaddr0x%08x&quot;, (int)si->si_errno, (int)si->si_pid, (int)si->si_addr); show_stack(); PRINTF(&quot;!==========================!&quot;); kill(mainpid,SIGINT); exit(-1); } if (getpid()==mainpid)  { exit(0); } return; }
Second example 5/6 int sig_init(int pid) { struct sigaction si_segv; int rc; si_segv.sa_sigaction = sigdemux; si_segv.sa_flags = SA_SIGINFO; mainpid=pid; rc = sigaction(SIGINT,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGSEGV,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGILL,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGFPE,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGBUS,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGPIPE,&si_segv,NULL); if (rc<0) { goto sig_error; } sig_error: return rc; }
[object Object],[object Object],[object Object],[object Object],[object Object],Usage of the second source code (6/6)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Result
More help ,[object Object]

More Related Content

What's hot

Pointers in C
Pointers in CPointers in C
Pointers in C
Kamal Acharya
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
Faisal Shahzad Khan
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
rgnikate
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppteShikshak
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
Tushar B Kute
 
Pointers_c
Pointers_cPointers_c
Pointers_c
ahmed safwat
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
JinTaek Seo
 
C for Java programmers (part 2)
C for Java programmers (part 2)C for Java programmers (part 2)
C for Java programmers (part 2)
Dmitry Zinoviev
 
C for Java programmers (part 1)
C for Java programmers (part 1)C for Java programmers (part 1)
C for Java programmers (part 1)
Dmitry Zinoviev
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
Rumman Ansari
 
Pointer in C
Pointer in CPointer in C
Pointer in C
Sonya Akter Rupa
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)
Dmitry Zinoviev
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
Atul Setu
 
C pointers
C pointersC pointers
C pointers
Aravind Mohan
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
Abdullah Jan
 

What's hot (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
Pointers
PointersPointers
Pointers
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
 
Pointers_c
Pointers_cPointers_c
Pointers_c
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
C for Java programmers (part 2)
C for Java programmers (part 2)C for Java programmers (part 2)
C for Java programmers (part 2)
 
C for Java programmers (part 1)
C for Java programmers (part 1)C for Java programmers (part 1)
C for Java programmers (part 1)
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)
 
C
CC
C
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C pointers
C pointersC pointers
C pointers
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 

Viewers also liked

Google mock training
Google mock trainingGoogle mock training
Google mock training
Thierry Gayet
 
Working with core dump
Working with core dumpWorking with core dump
Working with core dump
Thierry Gayet
 
Bluetooth on GNU Linux
Bluetooth on GNU LinuxBluetooth on GNU Linux
Bluetooth on GNU LinuxThierry Gayet
 
Formation python
Formation pythonFormation python
Formation python
Thierry Gayet
 
Comprendre les scripts shell auto-extractible
Comprendre les scripts shell auto-extractibleComprendre les scripts shell auto-extractible
Comprendre les scripts shell auto-extractible
Thierry Gayet
 
Présentation de la pile réseau sous gnu linux
Présentation de la pile réseau sous gnu linuxPrésentation de la pile réseau sous gnu linux
Présentation de la pile réseau sous gnu linux
Thierry Gayet
 
Utilisation d'un système de tag des objets elf
Utilisation d'un système de tag des objets elfUtilisation d'un système de tag des objets elf
Utilisation d'un système de tag des objets elf
Thierry Gayet
 
Autotools pratical training
Autotools pratical trainingAutotools pratical training
Autotools pratical training
Thierry Gayet
 
Intro to the raspberry pi board
Intro to the raspberry pi boardIntro to the raspberry pi board
Intro to the raspberry pi boardThierry Gayet
 
utilisation des core dump sous linux
utilisation des core dump sous linuxutilisation des core dump sous linux
utilisation des core dump sous linux
Thierry Gayet
 
Formation html3 css3
Formation html3 css3Formation html3 css3
Formation html3 css3
Thierry Gayet
 
A la découverte d'abus
A la découverte d'abusA la découverte d'abus
A la découverte d'abus
Thierry Gayet
 
Anatomie d'une des particularité de la libc
Anatomie d'une des particularité de la libcAnatomie d'une des particularité de la libc
Anatomie d'une des particularité de la libc
Thierry Gayet
 
Compilation noyau linux depuis les sources
Compilation noyau linux depuis les sourcesCompilation noyau linux depuis les sources
Compilation noyau linux depuis les sources
Thierry Gayet
 
A la découverte de redo
A la découverte de redoA la découverte de redo
A la découverte de redo
Thierry Gayet
 
Etude DéTailléé de la pile réseau sous GNU Linux
Etude DéTailléé de la pile réseau sous GNU LinuxEtude DéTailléé de la pile réseau sous GNU Linux
Etude DéTailléé de la pile réseau sous GNU LinuxThierry Gayet
 
Rappels de développements sous GNU Linux
Rappels de développements sous GNU LinuxRappels de développements sous GNU Linux
Rappels de développements sous GNU LinuxThierry Gayet
 
Ecriture de classes javascript
Ecriture de classes javascriptEcriture de classes javascript
Ecriture de classes javascript
Thierry Gayet
 
From gcc to the autotools
From gcc to the autotoolsFrom gcc to the autotools
From gcc to the autotools
Thierry Gayet
 
Développement Noyau Et Driver Sous Gnu Linux
Développement Noyau Et Driver Sous Gnu LinuxDéveloppement Noyau Et Driver Sous Gnu Linux
Développement Noyau Et Driver Sous Gnu LinuxThierry Gayet
 

Viewers also liked (20)

Google mock training
Google mock trainingGoogle mock training
Google mock training
 
Working with core dump
Working with core dumpWorking with core dump
Working with core dump
 
Bluetooth on GNU Linux
Bluetooth on GNU LinuxBluetooth on GNU Linux
Bluetooth on GNU Linux
 
Formation python
Formation pythonFormation python
Formation python
 
Comprendre les scripts shell auto-extractible
Comprendre les scripts shell auto-extractibleComprendre les scripts shell auto-extractible
Comprendre les scripts shell auto-extractible
 
Présentation de la pile réseau sous gnu linux
Présentation de la pile réseau sous gnu linuxPrésentation de la pile réseau sous gnu linux
Présentation de la pile réseau sous gnu linux
 
Utilisation d'un système de tag des objets elf
Utilisation d'un système de tag des objets elfUtilisation d'un système de tag des objets elf
Utilisation d'un système de tag des objets elf
 
Autotools pratical training
Autotools pratical trainingAutotools pratical training
Autotools pratical training
 
Intro to the raspberry pi board
Intro to the raspberry pi boardIntro to the raspberry pi board
Intro to the raspberry pi board
 
utilisation des core dump sous linux
utilisation des core dump sous linuxutilisation des core dump sous linux
utilisation des core dump sous linux
 
Formation html3 css3
Formation html3 css3Formation html3 css3
Formation html3 css3
 
A la découverte d'abus
A la découverte d'abusA la découverte d'abus
A la découverte d'abus
 
Anatomie d'une des particularité de la libc
Anatomie d'une des particularité de la libcAnatomie d'une des particularité de la libc
Anatomie d'une des particularité de la libc
 
Compilation noyau linux depuis les sources
Compilation noyau linux depuis les sourcesCompilation noyau linux depuis les sources
Compilation noyau linux depuis les sources
 
A la découverte de redo
A la découverte de redoA la découverte de redo
A la découverte de redo
 
Etude DéTailléé de la pile réseau sous GNU Linux
Etude DéTailléé de la pile réseau sous GNU LinuxEtude DéTailléé de la pile réseau sous GNU Linux
Etude DéTailléé de la pile réseau sous GNU Linux
 
Rappels de développements sous GNU Linux
Rappels de développements sous GNU LinuxRappels de développements sous GNU Linux
Rappels de développements sous GNU Linux
 
Ecriture de classes javascript
Ecriture de classes javascriptEcriture de classes javascript
Ecriture de classes javascript
 
From gcc to the autotools
From gcc to the autotoolsFrom gcc to the autotools
From gcc to the autotools
 
Développement Noyau Et Driver Sous Gnu Linux
Développement Noyau Et Driver Sous Gnu LinuxDéveloppement Noyau Et Driver Sous Gnu Linux
Développement Noyau Et Driver Sous Gnu Linux
 

Similar to Hooking signals and dumping the callstack

Unit 4
Unit 4Unit 4
Unit 4siddr
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
javaTpoint s
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
SURBHI SAROHA
 
Quiz 9
Quiz 9Quiz 9
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
R696
 
Rpg Pointers And User Space
Rpg Pointers And User SpaceRpg Pointers And User Space
Rpg Pointers And User Space
ramanjosan
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&aKumaran K
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
Python scripting kick off
Python scripting kick offPython scripting kick off
Python scripting kick off
Andrea Gangemi
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorialhughpearse
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212
Mahmoud Samir Fayed
 
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdfQ1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
abdulrahamanbags
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
C programming
C programmingC programming
C programming
Karthikeyan A K
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
C++ Homework Help
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
Haripritha
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
Leandro Schenone
 

Similar to Hooking signals and dumping the callstack (20)

Unit 4
Unit 4Unit 4
Unit 4
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
Input output functions
Input output functionsInput output functions
Input output functions
 
08 -functions
08  -functions08  -functions
08 -functions
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
Rpg Pointers And User Space
Rpg Pointers And User SpaceRpg Pointers And User Space
Rpg Pointers And User Space
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
 
Functions
FunctionsFunctions
Functions
 
Python scripting kick off
Python scripting kick offPython scripting kick off
Python scripting kick off
 
Buffer overflow tutorial
Buffer overflow tutorialBuffer overflow tutorial
Buffer overflow tutorial
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212
 
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdfQ1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
C programming
C programmingC programming
C programming
 
C
CC
C
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 

Recently uploaded

Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 

Recently uploaded (20)

Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 

Hooking signals and dumping the callstack

  • 1. Hooking the signals and dumping the backtraces Thierry GAYET
  • 2. GOAL  The main goal of this document is to provide some information about the signal handling used for dumping the bacltrace due after a crash.
  • 3. A backtrace is a list of the function calls that are currently active in a thread. The usual way to inspect a backtrace of a program is to use an external debugger such as gdb. H owever, sometimes it is useful to obtain a backtrace programmatically from within a program, e.g., for the purposes of logging or diagnostics. The header file `execinfo.h' declares three functions that obtain and manipulate backtraces of the current thread.  Function: int backtrace(void **buffer, int size) The backtrace function obtains a backtrace for the current thread, as a list of pointers, and places the information into buffer. The argument size should be the number of void * elements that will fit into buffer. The return value is the actual number of entries of buffer that are obtained, and is at most size. The pointers placed in buffer are actually return addresses obtained by inspecting the stack, one return address per stack frame. Note that certain compiler optimizations may interfere with obtaining a valid backtrace. Function inlining causes the inlined function to not have a stack frame; tail call optimization replaces one stack frame with another; frame pointer elimination will stop backtrace from interpreting the stack contents correctly. Usage
  • 4. Function: char ** backtrace_symbols (void *const *buffer, int size) The backtrace_symbols function translates the information obtained from the backtrace function into an array of strings. The argument buffer should be a pointer to an array of addresses obtained via the backtrace function, and size is the number of entries in that array (the return value of backtrace). The return value is a pointer to an array of strings, which has size entries just like the array buffer. Each string contains a printable representation of the corresponding element of buffer. It includes the function name (if this can be determined), an offset into the function, and the actual return address (in hexadecimal). Currently, the function name and offset only be obtained on systems that use the ELF binary format for programs and libraries. On other systems, only the hexadecimal return address will be present. Also, you may need to pass additional flags to the linker to make the function names available to the program. (For example, on systems using GNU ld, you must pass (-rdynamic.) The return value of backtrace_symbols is a pointer obtained via the malloc function, and it is the responsibility of the caller to free that pointer. Note that only the return value need be freed, not the individual strings. The return value is NULL if sufficient memory for the strings cannot be obtained. Usage
  • 5. Function: void backtrace_symbols_fd (void *const *buffer, int size, int fd) The backtrace_symbols_fd function performs the same translation as the function backtrace_symbols function. Instead of returning the strings to the caller, it writes the strings to the file descriptor fd, one per line. It does not use the malloc function, and can therefore be used in situations where that function might fail. The following program illustrates the use of these functions. Note that the array to contain the return addresses returned by backtrace is allocated on the stack. Therefore code like this can be used in situations where the memory handling via malloc does not work anymore (in which case the backtrace_symbols has to be replaced by a backtrace_symbols_fd call as well). The number of return addresses is normally not very large. Even complicated programs rather seldom have a nesting level of more than, say, 50 and with 200 possible entries probably all programs should be covered. Usage
  • 6. #include <execinfo.h> #include <stdio.h> #include <stdlib.h> /* Obtain a backtrace and print it to stdout. */ void print_trace(void) { void* array[10]; size_t size; char** strings; size_t i; size = backtrace(array, 10); strings = backtrace_symbols(array, size); printf (&quot;Obtained %zd stack frames.&quot;, size); for (i = 0; i < size; i++) { printf (&quot;%s&quot;, strings[i]); } /* FOR */ free (strings); } /* print_trace */ /* A dummy function to make the backtrace more interesting. */ void dummy_function(void) { print_trace(); } /* dummy_function */ Int main(void) { dummy_function (); return 0; } First example
  • 7. #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <sys/times.h> #include <sys/ucontext.h> #include <asm/unistd.h> #include <execinfo.h> struct dict_entry { int num; const char *s; }; #define DICT_ENTRY_END { -1, NULL } #define PRINTF(fmt...) fprintf(stderr,fmt); static int mainpid=0; /* * Simple dictionary * Search an array terminated by an entry with s field set to NULL * If number not found, return &quot;(unknown)&quot; string */ static const char * search_dict(const struct dict_entry *dict, int num) { const struct dict_entry *p = dict; while (p->s) { if (num == p->num) return p->s; p++; } return &quot;(unknown)&quot;; } Second example 1/6
  • 8. static const struct dict_entry sig_names[] = { {SIGHUP, &quot;SIGHUP&quot;}, {SIGINT, &quot;SIGINT&quot;}, {SIGQUIT, &quot;SIGQUIT&quot;}, {SIGILL, &quot;SIGILL&quot;}, {SIGTRAP, &quot;SIGTRAP&quot;}, {SIGABRT, &quot;SIGABRT&quot;}, {SIGBUS, &quot;SIGBUS&quot;}, {SIGFPE, &quot;SIGFPE&quot;}, {SIGKILL, &quot;SIGKILL&quot;}, {SIGSEGV, &quot;SIGSEGV&quot;}, {SIGPIPE, &quot;SIGPIPE&quot;}, {SIGALRM, &quot;SIGALRM&quot;}, {SIGTERM, &quot;SIGTERM&quot;}, {SIGSTKFLT, &quot;SIGSTKFLT&quot;}, {SIGCHLD, &quot;SIGCHLD&quot;}, {SIGCONT, &quot;SIGCONT&quot;}, {SIGSTOP, &quot;SIGSTOP&quot;}, {SIGTSTP, &quot;SIGTSTP&quot;}, {SIGTTIN, &quot;SIGTTIN&quot;}, {SIGTTOU, &quot;SIGTTOU&quot;}, {SIGURG, &quot;SIGURG&quot;}, {SIGXCPU, &quot;SIGXCPU&quot;}, {SIGXFSZ, &quot;SIGXFSZ&quot;}, {SIGVTALRM, &quot;SIGVTALRM&quot;}, {SIGPROF, &quot;SIGPROF&quot;}, {SIGWINCH, &quot;SIGWINCH&quot;}, {SIGPOLL, &quot;SIGPOLL&quot;}, {SIGPWR, &quot;SIGPWR&quot;}, {SIGSYS, &quot;SIGSYS&quot;}, DICT_ENTRY_END }; Second example 2/6
  • 9. static void show_stack() { void *trace[16]; char **messages = (char **)NULL; int i, trace_size = 0; trace_size = backtrace(trace, 16); /* overwrite sigaction with caller's address */ messages = backtrace_symbols(trace, trace_size); /* skip first stack frame (points here) */ printf(&quot;CALLSTACK:&quot;); for (i=1; i<trace_size; ++i) printf(&quot;%s&quot;, messages[i]); free (messages); } Second example 3/6
  • 10. Second example 4/6 void sigdemux(int sig, struct siginfo *si, void *v) { const char *signame; //struct ucontext *sc=(struct ucontext *)v; //struct sigcontext *regs; int si_code; si_code = si->si_code & 0xffff; signame = search_dict(sig_names, sig); if (sig != SIGINT) { PRINTF(&quot;!==========================!&quot;); PRINTF(&quot;SIG %s (#%i) received in PID %ld&quot;,signame,sig,syscall(__NR_gettid)); PRINTF(&quot;Errno%iPID%iaddr0x%08x&quot;, (int)si->si_errno, (int)si->si_pid, (int)si->si_addr); show_stack(); PRINTF(&quot;!==========================!&quot;); kill(mainpid,SIGINT); exit(-1); } if (getpid()==mainpid) { exit(0); } return; }
  • 11. Second example 5/6 int sig_init(int pid) { struct sigaction si_segv; int rc; si_segv.sa_sigaction = sigdemux; si_segv.sa_flags = SA_SIGINFO; mainpid=pid; rc = sigaction(SIGINT,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGSEGV,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGILL,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGFPE,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGBUS,&si_segv,NULL); if (rc<0) { goto sig_error; } rc = sigaction(SIGPIPE,&si_segv,NULL); if (rc<0) { goto sig_error; } sig_error: return rc; }
  • 12.
  • 13.
  • 14.