\documentclass[a4paper,12pt]{article} \usepackage[utf8]{inputenc} \usepackage{listings} \usepackage{xcolor} \usepackage{geometry} \geometry{margin=1in} % Configure listings for C code \lstset{ language=C, basicstyle=\ttfamily\small, keywordstyle=\color{blue}\bfseries, stringstyle=\color{red}, commentstyle=\color{green!50!black}, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt, showspaces=false, showstringspaces=false, frame=single, breaklines=true, breakatwhitespace=true, tabsize=4 } % Configure listings for GDB commands \lstdefinestyle{gdb}{ basicstyle=\ttfamily\small, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt, showspaces=false, showstringspaces=false, frame=single, breaklines=true, breakatwhitespace=true } \title{Debugging the \texttt{slen} Function with GDB} \author{} \date{May 22, 2025} \begin{document} \maketitle \section{Introduction} This document describes the \texttt{slen} function, a C function that calculates the length of a null-terminated string, and details the debugging process using GDB on an embedded target. The debugging session history is included to illustrate the steps taken to verify the function's correctness. \section{The \texttt{slen} Function} The \texttt{slen} function takes a string pointer and returns the number of characters until the null terminator (\texttt{\textbackslash 0}). Below is the source code: \begin{lstlisting} #include const char * ptr = "informatyk mpabi 0\n"; int slen ( char *s ) { int n; for (n = 0; *s != '\0'; s++) n++; return n; } int main () { int x = slen (ptr); return 0; } \end{lstlisting} The string \texttt{ptr} points to \texttt{"informatyk mpabi 0\textbackslash n"}, and \texttt{slen} is called to compute its length, stored in \texttt{x}. \section{Debugging Strategy} The debugging process uses GDB with a remote target (port 3333) to verify the \texttt{slen} function. The strategy includes: \begin{itemize} \item \textbf{Setup}: Connect to the target, load the program, and set breakpoints. \item \textbf{Execution Control}: Step through instructions and continue to breakpoints to inspect \texttt{slen}'s execution. \item \textbf{Memory Inspection}: Verify the string pointer and memory contents. \item \textbf{State Monitoring}: Use the GDB dashboard to track registers, stack, and memory. \item \textbf{Iterative Testing}: Reset and reload the program to ensure consistent behavior. \end{itemize} \section{GDB Command History} The following GDB commands were executed to debug \texttt{slen}: \begin{lstlisting}[style=gdb] target extended-remote :3333 file prog load si b main dashboard -layout registers stack memory c si c si c laod % typo, likely meant load load c c c radix 16 set radix 16 p/x ptr p/x *ptr p/c *ptr p/s *ptr print &ptr p ptr dashboard memory set 0x800000c0 32 dashboard memory watch 0x800000c0 32 dash p/x &ptr p/x ptr p/x *ptr dash dash clear dash si monitor reset halt si si exit target extended-remote :3333 dash dashboard -layout registers stack memory breakpoints file prog load si exit dashboard -layout registers stack memory breakpoints exit dashboard -layout registers memory breakpoints stack file prog load si exit \end{lstlisting} \subsection{Explanation of Commands} \begin{itemize} \item \texttt{target extended-remote :3333}: Connects to the embedded target on port 3333. \item \texttt{file prog}, \texttt{load}: Loads the executable \texttt{prog} onto the target. \item \texttt{b main}, \texttt{c}: Sets a breakpoint at \texttt{main} and continues execution to it. \item \texttt{si}: Steps through instructions, allowing inspection of \texttt{slen}'s loop. \item \texttt{p/x ptr}, \texttt{p/x *ptr}, \texttt{p/c *ptr}, \texttt{p/s *ptr}: Inspects the \texttt{ptr} address, its first character (hex and char), and the full string. \item \texttt{print \&ptr}, \texttt{p/x \&ptr}: Checks the address of \texttt{ptr} itself. \item \texttt{dashboard memory set 0x800000c0 32}, \texttt{dashboard memory watch 0x800000c0 32}: Monitors 32 bytes at address \texttt{0x800000c0}, likely where the string resides. \item \texttt{dashboard -layout ...}: Configures the GDB dashboard to show registers, stack, memory, and breakpoints. \item \texttt{monitor reset halt}: Resets the target and halts execution. \item \texttt{clear}, \texttt{exit}: Clears breakpoints and exits GDB sessions. \item \texttt{radix 16}, \texttt{set radix 16}: Sets hexadecimal output for memory and values. \item \texttt{dash}: Likely a custom alias or typo, possibly for dashboard commands. \end{itemize} \section{Key Checks for \texttt{slen}} The debugging process verifies: \begin{itemize} \item \textbf{Pointer Validity}: \texttt{ptr} points to the correct string (checked via \texttt{p/s *ptr}). \item \textbf{Loop Correctness}: The loop in \texttt{slen} increments \texttt{n} and stops at \texttt{\textbackslash 0} (inspected via \texttt{si}). \item \textbf{Memory Access}: Memory at \texttt{0x800000c0} is valid and uncorrupted (via \texttt{dashboard memory watch}). \end{itemize} \section{Conclusion} The GDB command history provides a detailed view of the debugging process, ensuring \texttt{slen} correctly computes the string length. The iterative use of stepping, memory inspection, and state monitoring confirms the function's behavior on the embedded target. \end{document}