Adding new Python example

This commit is contained in:
Sergio Durigan Junior 2014-10-23 13:16:43 -04:00
parent aee82c1bc0
commit 381c7527a5

View file

@ -382,6 +382,60 @@ CallerIs()
\end{itemize} \end{itemize}
\end{frame} \end{frame}
\begin{frame}[fragile]
\frametitle{\python{}$^5$}
\begin{itemize}
\item{Problem: \texttt{std::string s = "Hello, World";}}
\begin{block}{Printing ``s'' inside \gdb{}}
\tiny
\begin{lstlisting}[language=sh,showstringspaces=false]
(gdb) print s
$1 = {static npos = <optimized out>,
_M_dataplus = {<std::allocator<char>> =
{<__gnu_cxx::new_allocator<char>> = {<No data fields>},
<No data fields>}, _M_p = 0x602028 ``Hello, World''}}
\end{lstlisting}
\end{block}
\pause
\item{\gdb{} knows about \verb|_M_dataplus -> _M_p|. How to tell that this is the only field you are interested in?}
\pause
\item{Python!}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\python{}$^6$}
\begin{block}{print-std-string.py}
\tiny
\begin{lstlisting}[language=python,showstringspaces=false]
class StdStringPrinter:
"Print a std::basic_string of some kind"
def __init__(self, typename, val):
self.val = val
def to_string(self):
# ... snip ...
# Calculate the length of the string so that to_string returns
# the string according to length, not according to first null
# encountered.
(*\textcolor{red}{ptr = self.val ['\_M\_dataplus']['\_M\_p']}*)
realtype = type.unqualified ().strip_typedefs ()
reptype = gdb.lookup_type (str (realtype) + '::_Rep').pointer ()
header = ptr.cast(reptype) - 1
len = header.dereference ()['_M_length']
if hasattr(ptr, "lazy_string"):
return ptr.lazy_string (length = len)
return ptr.string (length = len)
\end{lstlisting}
\end{block}
\begin{itemize}
\pause
\item{Present on \verb|libstdcxx| upstream}
\end{itemize}
\end{frame}
\section{Conclusion} \section{Conclusion}
\begin{frame}[fragile] \begin{frame}[fragile]
\frametitle{Conclusion} \frametitle{Conclusion}