Hunting segfaults (for beginners)
Upcoming SlideShare
Loading in...5
×
 

Hunting segfaults (for beginners)

on

  • 7,318 Views

 

Statistiken

Views

Gesamtviews
7,318
Views auf SlideShare
7,310
Views einbetten
8

Actions

Gefällt mir
3
Downloads
21
Kommentare
0

3 Einbettungen 8

https://www.xing.com 6
https://si0.twimg.com 1
https://twitter.com 1

Zugänglichkeit

Kategorien

Details hochladen

Uploaded via as Adobe PDF

Benutzerrechte

© Alle Rechte vorbehalten

Report content

Als unangemessen gemeldet Als unangemessen melden
Als unangemessen melden

Wählen Sie Ihren Grund, warum Sie diese Präsentation als unangemessen melden.

Löschen
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Ihre Nachricht erscheint hier
    Processing...
Kommentar posten
Kommentar bearbeiten

Hunting segfaults (for beginners) Hunting segfaults (for beginners) Presentation Transcript

  • IntroductionDetecting segfaults Devel::Trace gdb Devel::bt The End Hunting segfaults for beginners Uwe V¨lker o XING AG August 2012 Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Devel::Trace gdb Devel::bt The End1 Introduction2 Detecting segfaults3 Devel::Trace4 gdb5 Devel::bt6 The End Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The End1 Introduction What is a segfault? Examples - C Examples - Perl2 Detecting segfaults3 Devel::Trace4 gdb5 Devel::bt6 The End Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The EndWhat is a segfault? segfault = segmentation fault Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The EndWhat is a segfault? segfault = segmentation fault every process has memory pages these pages are mapped to physical memory Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The EndWhat is a segfault? segfault = segmentation fault every process has memory pages these pages are mapped to physical memory if you try to access an invalid address (or write to a protected address) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The EndWhat is a segfault? segfault = segmentation fault every process has memory pages these pages are mapped to physical memory if you try to access an invalid address (or write to a protected address) BOOOM! Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The EndExamples - C using uninitialized pointers dereferencing NULL pointers using ”freed” pointers Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The EndExamples - Perl bug in a XS extension bug in Perl itself (rare) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The EndExamples - Perl bug in a XS extension bug in Perl itself (rare) Perl 5.6.1: perl -e ’undef a’ perl -e ’*::=%::=0’ Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults What is a segfault? Devel::Trace Examples - C gdb Examples - Perl Devel::bt The EndExamples - Perl bug in a XS extension bug in Perl itself (rare) Perl 5.6.1: perl -e ’undef a’ perl -e ’*::=%::=0’ Perlmonks thread: (Golf) Segfault Perl http://perlmonks.org/?node_id=156461 Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults On the shell Devel::Trace Core dump file gdb CGI script Devel::bt The End1 Introduction2 Detecting segfaults On the shell Core dump file CGI script3 Devel::Trace4 gdb5 Devel::bt6 The End Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults On the shell Devel::Trace Core dump file gdb CGI script Devel::bt The EndOn the shell perl segfault . pl S e g m e n t a t i o n f a u l t ( c o r e dumped ) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults On the shell Devel::Trace Core dump file gdb CGI script Devel::bt The EndOn the shell perl segfault . pl S e g m e n t a t i o n f a u l t ( c o r e dumped ) #! / u s r / b i n / p e r l use Debug : : DumpCore ; Debug : : DumpCore : : s e g v ; Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults On the shell Devel::Trace Core dump file gdb CGI script Devel::bt The EndCore dump file $ u l i m i t −c u n l i m i t e d $ perl segfault . pl S e g m e n t a t i o n f a u l t ( c o r e dumped ) $ l l core −rw−r−−−−− 1 uwe uwe 1695744 J u l 26 1 4 : 0 8 c o r e Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults On the shell Devel::Trace Core dump file gdb CGI script Devel::bt The EndCGI script personal story: CGI script in Apache no output, no entry in logfiles (access.log and error.log) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults On the shell Devel::Trace Core dump file gdb CGI script Devel::bt The EndCGI script personal story: CGI script in Apache no output, no entry in logfiles (access.log and error.log) but when I wrote to some file, the content was there so the script was getting executed... Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The End1 Introduction2 Detecting segfaults3 Devel::Trace Usage How do I spot a segfault? Other uses for Devel::Trace4 gdb5 Devel::bt6 The End Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The EndUsage ”Print out each line before it is executed (like sh -x)” Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The EndUsage ”Print out each line before it is executed (like sh -x)” perl -d:Trace program for CGI: put it in your shebang line Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The EndUsage ”Print out each line before it is executed (like sh -x)” perl -d:Trace program for CGI: put it in your shebang line >> ./ test :4: p r i n t ” Statement 1 a t l i n e 4 n” ; >> ./ test :5: p r i n t ” Statement 2 a t l i n e 5 n” ; >> ./ test :6: p r i n t ” C a l l t o sub x r e t u r n s ” , &x ( ) , >> ./ test :12: p r i n t ” I n sub x a t l i n e 1 2 . n” ; >> ./ test :13: return 13; >> ./ test :8: exit 0; Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The EndHow do I spot a segfault? look at the last few lines if it stops immediately, it might be a segfault Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The EndHow do I spot a segfault? look at the last few lines if it stops immediately, it might be a segfault grep for your script name output can be very large, with long lines grep -v site perl Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The EndHow do I spot a segfault? look at the last few lines if it stops immediately, it might be a segfault grep for your script name output can be very large, with long lines grep -v site perl in my case: buggy MSSQL driver (easysoft) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The EndOther uses for Devel::Trace your program is behaving strange and you have no debugger at hand (use grep and grep -v to filter the output) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Usage Devel::Trace How do I spot a segfault? gdb Other uses for Devel::Trace Devel::bt The EndOther uses for Devel::Trace your program is behaving strange and you have no debugger at hand (use grep and grep -v to filter the output) Does this code get executed? Which part of the conditional was taken? Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Introduction Devel::Trace Usage gdb Core dump file - reloaded Devel::bt The End1 Introduction2 Detecting segfaults3 Devel::Trace4 gdb Introduction Usage Core dump file - reloaded5 Devel::bt6 The End Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Introduction Devel::Trace Usage gdb Core dump file - reloaded Devel::bt The EndIntroduction GNU debugger command line debugger we use it to extract the stacktrace from the core dump file Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Introduction Devel::Trace Usage gdb Core dump file - reloaded Devel::bt The EndUsage $ gdb p e r l c o r e Core was g e n e r a t e d by ‘ p e r l p e r l / s e g f a u l t . p l ’ . Program t e r m i n a t e d w i t h s i g n a l 1 1 , S e g m e n t a t i o n f a u #0 0 x 0 0 0 0 7 f 2 f 5 d 0 8 6 7 5 4 i n c r a s h n o w f o r r e a l ( s u i c i d 10 p r i n t f (”%d ” , ∗p ) ; /∗ c a u s e a s e g f a u l t ∗/ ( gdb ) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Introduction Devel::Trace Usage gdb Core dump file - reloaded Devel::bt The EndUsage $ gdb p e r l c o r e Core was g e n e r a t e d by ‘ p e r l p e r l / s e g f a u l t . p l ’ . Program t e r m i n a t e d w i t h s i g n a l 1 1 , S e g m e n t a t i o n f a u #0 0 x 0 0 0 0 7 f 2 f 5 d 0 8 6 7 5 4 i n c r a s h n o w f o r r e a l ( s u i c i d 10 p r i n t f (”%d ” , ∗p ) ; /∗ c a u s e a s e g f a u l t ∗/ ( gdb ) where #0 0 x 0 0 0 0 7 f 2 f 5 d 0 8 6 7 5 4 i n c r a s h n o w f o r r e a l ( s u i c i d #1 0 x 0 0 0 0 7 f 2 f 5 d 0 8 6 7 8 9 i n c r a s h n o w ( s u i c i d e m e s s a g e #2 0 x 0 0 0 0 7 f 2 f 5 d 0 8 6 8 2 0 i n XS Debug DumpCore segv ( #3 0 x0000000000488db3 i n P e r l p p e n t e r s u b ( ) #4 0 x0000000000480a7d i n P e r l r u n o p s s t a n d a r d ( ) #5 0 x00000000004336b4 i n p e r l r u n ( ) #6 0 x000000000041bddc i n main ( ) ( gdb ) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Introduction Devel::Trace Usage gdb Core dump file - reloaded Devel::bt The EndCore dump file - reloaded ulimit -c unlimited Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Introduction Devel::Trace Usage gdb Core dump file - reloaded Devel::bt The EndCore dump file - reloaded ulimit -c unlimited current directory has to be writable (can be tricky with Apache) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Introduction Devel::Trace Usage gdb Core dump file - reloaded Devel::bt The EndCore dump file - reloaded ulimit -c unlimited current directory has to be writable (can be tricky with Apache) ps auxww|grep apache ls -l /proc/1234/cwd Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Devel::Trace Usage gdb How does it work? Devel::bt The End1 Introduction2 Detecting segfaults3 Devel::Trace4 gdb5 Devel::bt Usage How does it work?6 The End Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Devel::Trace Usage gdb How does it work? Devel::bt The EndUsage ”Automatic gdb backtraces on errors” Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Devel::Trace Usage gdb How does it work? Devel::bt The EndUsage ”Automatic gdb backtraces on errors” just use the module it registers signal handlers for SIGSEGV (and a few more) Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Devel::Trace Usage gdb How does it work? Devel::bt The EndHow does it work? the signal handler forks off a process which runs gdb gdb attaches to the parent and outputs the stacktrace Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Devel::Trace Sources gdb Questions? Devel::bt The End1 Introduction2 Detecting segfaults3 Devel::Trace4 gdb5 Devel::bt6 The End Sources Questions? Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Devel::Trace Sources gdb Questions? Devel::bt The EndSources http://en.wikipedia.org/wiki/Segmentation_fault http://modperlbook.org/html/ 21-6-Analyzing-Dumped-core-Files.html http://www.linux-magazin.de/Heft-Abo/Ausgaben/ 2007/01/Getriebeschaden Uwe V¨lker o Hunting segfaults
  • Introduction Detecting segfaults Devel::Trace Sources gdb Questions? Devel::bt The EndQuestions? Uwe V¨lker o Hunting segfaults