Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18b26a3497 | ||
|
|
580b420eb0 | ||
|
|
14b6618564 | ||
|
|
f4fa20aa2d | ||
|
|
151e6bf788 |
BIN
doc/z1.pdf
Normal file
BIN
doc/z1.pdf
Normal file
Binary file not shown.
171
doc/z1.tex
Normal file
171
doc/z1.tex
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
\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 <stdint.h>
|
||||||
|
|
||||||
|
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}
|
||||||
19
main.c
19
main.c
@ -1,29 +1,24 @@
|
|||||||
|
// p26.9 -z1
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
uint32_t x = 1, y =2, z[10];
|
const char * ptr = "informatyk mpabi 0\n";
|
||||||
uint32_t *ip;
|
const char * ptr2 = "indwed mpabi 0\n";
|
||||||
|
|
||||||
char * ptr = "informatyk\0";
|
int slen ( char *s ) {
|
||||||
|
|
||||||
int strlen ( char *s ) {
|
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
for (n = 0; *s != '\0'; s++) {
|
for (n = 0; *s != '\0'; s++)
|
||||||
n++;
|
n++;
|
||||||
}
|
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
|
|
||||||
ip =&x;
|
|
||||||
y = *ip;
|
|
||||||
*ip = 0;
|
|
||||||
|
|
||||||
ip = &z[0];
|
int x = slen (ptr) ;
|
||||||
|
int x2 = slen (ptr2) ;
|
||||||
x = strlen (ptr) ;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user