Skip to content

Commit

Permalink
slides: debugging: add a slide showing dlopen/dlsym usage
Browse files Browse the repository at this point in the history
Trainees often ask for clarifications about how to access the real
function when using the LD_PRELOAD mechanism

Add a slide showing a small example snippet calling the read function
from C library, from a custom read function.

Fixes #224

Signed-off-by: Alexis Lothoré <[email protected]>
  • Loading branch information
Tropicao committed Jan 3, 2025
1 parent e1c39eb commit fe06d53
Showing 1 changed file with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ \subsection{LD\_PRELOAD}
\end{frame}

\begin{frame}[fragile]
\frametitle{{\em LD\_PRELOAD} example}
\frametitle{{\em LD\_PRELOAD} example 1/2}
\begin{itemize}
\item Library snippet that we want to preload using {\em LD\_PRELOAD}:
\end{itemize}
Expand Down Expand Up @@ -88,6 +88,44 @@ \subsection{LD\_PRELOAD}
\end{block}
\end{frame}

\begin{frame}[fragile]
\frametitle{{\em LD\_PRELOAD} example 2/2}
\begin{itemize}
\item Chaining a call to the real symbol to avoid altering the
application behavior:
\end{itemize}
\begin{block}{}
\begin{minted}[fontsize=\tiny]{c}
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>

ssize_t read(int fd, void *data, size_t size)
{
size_t (*read_func)(int, void *, size_t);
void *handle;
char *error;

handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
if (!handle) {
fprintf(stderr, "Can not find overriden library\n");
return 0;
}
dlerror();
read_func = dlsym(handle, "read");
error = dlerror();
if (error) {
fprintf(stderr, "Can not find overriden symbol: %s\n", error);
return 0;
}
fprintf(stderr, "Trying to read %lu bytes to %p from file descriptor %d\n", size, data, fd);
return read_func(fd, data, size);
}

\end{minted}
\end{block}
\end{frame}

\subsection{uprobes and perf}

\begin{frame}{Probes in linux}
Expand Down

0 comments on commit fe06d53

Please sign in to comment.