gdb-intro-kwlug/gdb-intro-kwlug.tex
2019-06-03 10:41:10 -04:00

401 lines
8.2 KiB
TeX

% Created 2019-06-03 Mon 10:40
% Intended LaTeX compiler: pdflatex
\documentclass[presentation]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{color}
\usepackage{listings}
\usetheme{default}
\author{Sergio Durigan Junior \\ sergiodj@\{sergiodj.net,redhat.com,debian.org\}}
\date{}
\title{GDB Intro}
\hypersetup{
pdfauthor={Sergio Durigan Junior \\ sergiodj@\{sergiodj.net,redhat.com,debian.org\}},
pdftitle={GDB Intro},
pdfkeywords={},
pdfsubject={},
pdfcreator={Emacs 26.1 (Org mode 9.1.9)},
pdflang={English}}
\begin{document}
\maketitle
\begin{frame}[label={sec:orgeac0a69}]{License}
\begin{itemize}
\item License: \alert{Creative Commons Attribution 4.0 International License (CC-BY-4.0)}
\item \url{https://creativecommons.org/licenses/by/4.0/}
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:org071d481}]{Introduction}
\begin{itemize}
\item \texttt{GDB}: \texttt{GNU} project's Debugger \pause (it is \alert{not} a
\emph{database}\ldots{}). Supports several programming languages.
\item Started around 1986 by Richard Stallman (after \alert{GNU Emacs}, but
likely before \alert{GCC}).
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:org2d0fe11}]{Compiling your program for GDB}
\begin{itemize}
\item Your program needs to contain \alert{debug information} (also called
\texttt{DWARF}) for GDB to consume.
\end{itemize}
\pause
\begin{itemize}
\item The GCC flag to include debug information is \texttt{-g}. We also use
\texttt{-g3}, which includes information about macros (\texttt{\#define}).
\end{itemize}
\pause
\begin{itemize}
\item It's common to \alert{disable optimizations} when building the binary, by
using the flag \texttt{-O0} (it's \emph{dash-oh-zero}).
\pause
\begin{itemize}
\item \texttt{\# gcc -O0 -g program.c -o program}, \emph{or}
\item \texttt{\# CFLAGS='-O0 -g' ./configure \&\& make}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:org8bba82f}]{Running your program using GDB}
\begin{itemize}
\item In GDB's parlance, the program being debugged is called the
\alert{inferior}.
\end{itemize}
\pause
\begin{itemize}
\item Some ways to start the debugger:
\pause
\begin{itemize}
\item \texttt{\# gdb ./program}
\end{itemize}
\pause
\begin{itemize}
\item \texttt{\# gdb -{}-args ./program arg1 arg2}
\end{itemize}
\pause
\begin{itemize}
\item \texttt{\# gdb} \\
\texttt{(gdb) file ./program} \\
\texttt{(gdb) run arg1 arg2}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:org5c690dd}]{\{Break,Catch,Watch\}points}
\begin{itemize}
\item A \alert{breakpoint} is related to \emph{source code} (location, function). A
\alert{watchpoint} is related to \emph{data} (read/write of a variable). A
\alert{catchpoint} is related to an \emph{event} (enter/exit a syscall, fork,
receive a signal).
\end{itemize}
\pause
\begin{itemize}
\item Breakpoints (\emph{code})
\begin{itemize}
\item \texttt{break}
\item \texttt{tbreak} (temporary)
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item Watchpoints (\emph{data})
\begin{itemize}
\item \texttt{watch} (write), \texttt{rwatch} (read), \texttt{awatch} (access)
\item Conditional watchpoints are supported.
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item Catchpoints (\emph{events})
\begin{itemize}
\item \texttt{catch fork}
\item \texttt{catch syscall}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:orgfc53d4a}]{Resuming the execution}
\begin{itemize}
\item After GDB has stopped the inferior (because a \texttt{*point} has been hit,
for example), you will probably want to resume its execution.
\end{itemize}
\pause
\begin{itemize}
\item You may just want to continue the program:
\begin{itemize}
\item \texttt{continue}
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item Or maybe go to the next statement/instruction:
\begin{itemize}
\item \texttt{next} (\emph{statement}), or \texttt{nexti} (\emph{instruction})
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item Or step into a function:
\begin{itemize}
\item \texttt{step} (\emph{statement}), or \texttt{stepi} (\emph{instruction})
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item Or finish executing the current function, but stop at the end:
\begin{itemize}
\item \texttt{finish}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:org9dcb344}]{Examining data}
\begin{itemize}
\item The inferior has stopped\ldots{} Now what?
\end{itemize}
\pause
\begin{itemize}
\item You may want to print the value of some variable:
\begin{itemize}
\item \texttt{print VAR}
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item Or examine a memory location:
\begin{itemize}
\item \texttt{x ADDRESS}
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item The type of a variable? Easy:
\begin{itemize}
\item \texttt{whatis VARIABLE}
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item Hint: you may want to enable pretty-printing:
\begin{itemize}
\item \texttt{set print pretty on}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:orga54b38a}]{Examining the code}
\begin{itemize}
\item Yes, we have \texttt{ncurses}! The \textbf{T}ext
\textbf{U}user \textbf{I}nterface!
\begin{itemize}
\item \texttt{C-x a} (that's \texttt{CTRL x a}).
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item If you want to list the current region, or if you don't want/can't
to use TUI:
\begin{itemize}
\item \texttt{list}
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item You can also disassemble code:
\begin{itemize}
\item \texttt{disassemble}
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item If GDB can't find the source code, you can specify its location
using the \texttt{dir} command.
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:orgab39cfc}]{Examining the call stack}
\begin{itemize}
\item If you want to see the call stack (A.K.A. stack trace) that lead to
the current function:
\begin{itemize}
\item \texttt{bt}
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item And you can move through it:
\begin{itemize}
\item \texttt{up} and \texttt{down}
\item You can also go to a specific frame: \texttt{frame NUMBER}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:org66fa673}]{Corefiles}
\begin{itemize}
\item \alert{Corefiles} are frozen images of the inferior. You can inspect
everything that was happening when the process was running (but you
can't resurrect it).
\end{itemize}
\pause
\begin{itemize}
\item You can generate them \emph{outside} GDB, when a program crashes. Make
sure you:
\begin{itemize}
\item \texttt{ulimit -c unlimited}
\item Check if \texttt{systemd} is handling them
(\texttt{/proc/sys/kernel/core\_pattern}).
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item You can also generate them \emph{inside} GDB, at any moment:
\begin{itemize}
\item \texttt{generate-core-file}
\end{itemize}
\end{itemize}
\pause
\begin{itemize}
\item You can open a corefile using GDB:
\begin{itemize}
\item \texttt{\# gdb program -c corefile.PID}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:orgb366da5}]{Other interesting information}
\begin{itemize}
\item \texttt{info breakpoints}
\item \texttt{info locals}
\item \texttt{info registers}
\item Many others!
\end{itemize}
\end{frame}
\begin{frame}[fragile,label={sec:org6c7407d}]{Who you gonna call?}
\begin{itemize}
\item Our online documentation (\texttt{info}) is very good!
\item Every command has a \texttt{help}.
\item You can also use \texttt{apropos} when searching for a term.
\item \texttt{TAB}-completion is also useful.
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org9103f37}]{Other advanced features}
\begin{itemize}
\item Python support.
\item Reverse debugging.
\item Support for \emph{SystemTap SDT probes}.
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org462e66a}]{Thank you}
\begin{itemize}
\item Thanks to Red Hat for the support.
\item Thanks to Paul Nijjar and Bob Jonkman for the invitation.
\item Thanks to you for watching!
\end{itemize}
\end{frame}
\end{document}