
% unix-dev-refcard.tex - concise carte de référence pour le développement sous UNIX
%
% Copyright (C) 2017 Cyrille Bagard
%
%  This card is free software; you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation; either version 3 of the License, or
%  (at your option) any later version.
%
%  This card is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.
%
%  You should have received a copy of the GNU General Public License
%  along with this program; if not, write to the Free Software
%  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
%


\documentclass[landscape, 10pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[a4paper, top=1.0cm, bottom=0.7cm, left=0.7cm, right=0.7cm]{geometry}

\usepackage{multicol}
\usepackage{url}
\usepackage{scrextend}
\usepackage{xcolor}


% Le code suivant permet de récupérer la police utilisée par les sections.
%
% Cf. http://tex.stackexchange.com/questions/73158/how-to-get-the-size-of-the-section-header

\newcommand{\getsectionfont}{\setbox0=\vbox{\section*{a\xdef\TheSectionFont{\the\font}}}}
\AtBeginDocument{\getsectionfont}

% Pas de numérotation de page
\pagestyle{empty}

\begin{document}

\title{Debugging --- Quick Reference Card}


% Les commandes suivantes permettent de mesurer une zone de texte, en particulier une pseudo section donc.
%
% Cf. https://en.wikibooks.org/wiki/LaTeX/Lengths#Examples

\newlength{\sectionheight}
\newsavebox{\hiddensection}
\savebox{\hiddensection}{\TheSectionFont
  ABC
}
\settoheight{\sectionheight}{ \usebox{\hiddensection} }

% On recrée une section artificielle. Elle n'apparaîtra pas dans un sommaire
% (qui n'existera de toute façon jamais), mais comporte des bordures plus minces
% que celles par défaut pour une section.

\newcommand{\fakesection}[1]{
  \vskip 1em
  \noindent
  \colorbox{black!70}{\parbox[c][1.6\sectionheight]{1.0\linewidth}{
      \begin{addmargin}[1em]{1em}\TheSectionFont
        {\color{white}#1}
      \end{addmargin}
    }
  }
}

\begin{multicols}{3}


% Création d'un titre

\begin{center}
  \huge{Happy UNIX developer life\\Quick Reference Card}
  \vskip 1em
  \large{@laughing\_bit --- \url{http://git.0xdeadc0de.fr/}}
\end{center} 

\vskip 1em
\hrule
\vskip 1em


% Définition des commandes

\newcommand{\cmd}[2]{
  \begin{small}{\tt#1}\dotfill{#2}\par\end{small}
}

\newcommand{\alias}[1]{
  {\tt alias #1}\par
}


\fakesection{Bash Basics}

\cmd{!!}{Refer to the previous command}
\cmd{!*}{Refer to all the arguments of the last command}
\cmd{!\^{}}{Refer to the first word of the last command}
\cmd{!\$}{Refer to the last word of the last command}

\cmd{cmd1 \textbar\& cmd2}{Pipe stdout and stderr of \textit{cmd1} to \textit{cmd2}}

\cmd{echo \$RANDOM}{Display a random number}
\cmd{echo \$(( 0x10 + 1 ))}{Compute math formulas}

\cmd{source \textasciitilde/.bashrc}{Reload the bash initialization file}

\cmd{pushd / popd}{Stack working directories}
\cmd{cp myfile\{,.bak\}}{Skip some writing}
\cmd{ls -lhd !(*avi|*mkv)}{List all files which do not match}

\cmd{\textbackslash command}{Escape any aliases for \textit{command}}

\fakesection{Terminal shortcuts}

\cmd{Ctrl-l}{Clear the screen}
\cmd{Ctrl-a}{Go to the beginning of line}
\cmd{Ctrl-e}{Go to the end of line}

\cmd{Ctrl-k}{Delete from cursor to the end of line}
\cmd{Ctrl-u}{Delete from cursor to the start of line}
\cmd{Ctrl-w}{Delete from cursor to the start of word}
\cmd{Ctrl-y}{Paste text cut using one of the above shortcuts}

\cmd{Esc c / Alt-c}{Capitalize from cursor to end of word}
\cmd{Esc u / Alt-u}{Upcase from cursor to end of word}
\cmd{Esc l / Alt-l}{Downcase from cursor to end of word}

\cmd{Alt-t}{Swap the current word with the previous one}
\cmd{Ctrl-t}{Swap the current character with the previous one}

\cmd{Esc Ctrl-e}{Expand the line content}

\cmd{Ctrl-r}{Enter the reverse-search history}

\fakesection{Useful tools}

\cmd{colorgcc}{Colorize GCC compilation output}
\cmd{hexedit}{View and edit binary files, in hex or ASCII}
\cmd{vbindiff}{Visually compare binary files}
\cmd{kcachegrind}{Visualize valgrind profiling output}
\cmd{kompare}{View file differences using a nice GUI}
\cmd{kmag}{Magnify a part of the screen}
\cmd{screenruler}{Measure objects on the screen}
\cmd{cloc / sloccount}{Count physical source lines of code}


\fakesection{Shell tricks}

\cmd{cat /proc/self/environ | tr "\textbackslash0" "\textbackslash n"}{Same as {\tt env}}
\cmd{sed "s\#//\#/\#"}{Same as {\tt sed "s/\textbackslash/\textbackslash//\textbackslash//"}}
\cmd{mount | column -t}{Show filesystems in a nice layout}
\cmd{getconf LONG\_BIT}{Tell if the system is 32 bits or 64 bits}
\cmd{sed -n 5p file}{Print the 5th line of \textit{file}}
\cmd{tar tf file | xargs rm -rf}{Remove all extracted files}


\fakesection{GDB - \textasciitilde/.gdbinit}

\cmd{set confirm off}{Disable confirmation requests}
\cmd{set disassembly-flavor intel}{Set the disassembly style}
\cmd{set print pretty on}{Print structures in a nice way}
\cmd{set history save}{Record command history in a file}
\cmd{set print array on}{Print one item per line}


\fakesection{GDB commands}

\par{Commands can be run at startup using {\tt gdb -ex cmd}.}

\medskip

\cmd{target remote :1234}{Connect to a local server}

\cmd{b where [if cond]}{Set a breakpoint (triggered if \textit{cond})}
\cmd{commands id}{Run commands when the breakpoint \textit{id} is hit}
\cmd{rb regexp}{Break on functions matching \textit{regexp}}

\cmd{bt [full] [N/-N]}{Show \textit{N} frames of the call stack}
\cmd{t a a cmd}{Apply \textit{cmd} to all threads (e.g. \textit{t a a bt 3})}
\cmd{info f}{Print verbose info about the current frame}
\cmd{finish}{Continue till end of function}

\cmd{pt var}{Print info about the type of \textit{var}}
\cmd{p *array@count}{Print the \textit{count} first items of \textit{array}}
\cmd{info locals}{View all local variables}
\cmd{info args}{View all function arguments}


\fakesection{Valgrind options}

\cmd{-{}-trace-children=yes}{Track wrapped binaries}
\cmd{-{}-vgdb=full}{Activate the "gdbserver" functionality}
\cmd{-{}-vgdb-error=0}{Break the program at startup}

\cmd{-{}-fullpath-after=directory}{Extend path in stack traces}

\cmd{-{}-suppressions=file}{Specify one list of errors to hide}
\cmd{-{}-gen-suppressions=all}{Print error definitions}

\cmd{-{}-read-var-info=yes}{Read variable info from DWARF3}


\fakesection{Valgrind -{}-tool=memcheck}

\cmd{-{}-leak-check=full}{Show each individual leak in detail}
\cmd{-{}-show-reachable=yes}{Track all kinds of memory leak}
\cmd{-{}-track-origins=yes}{Track the origin of uninitialised values}

\fakesection{Valgrind -{}-tool=callgrind}

\cmd{-{}-callgrind-out-file=dest}{Choose \textit{dest} as output}
\cmd{-{}-dump-instr=yes}{Collect at per-instruction granularity}
\cmd{-{}-collect-jumps=yes}{Collect info for conditional jumps}


\fakesection{Valgrind -{}-tool=massif}

\cmd{-{}-massif-out-file=dest}{Choose \textit{dest} as output}

\cmd{-{}-depth=100}{Increase depth of recorded allocation trees}
\cmd{-{}-threshold=0.05}{Set threshold as \% of total memory}

\cmd{-{}-alloc-fn=g\_malloc}{Handle GLib allocations \#0}
\cmd{-{}-alloc-fn=g\_malloc0}{Handle GLib allocations \#1}
\cmd{-{}-alloc-fn=g\_realloc}{Handle GLib allocations \#2}
\cmd{-{}-alloc-fn=g\_try\_malloc}{Handle GLib allocations \#3}
\cmd{-{}-alloc-fn=g\_mem\_chunk\_alloc}{Handle GLib allocations \#4}

\medskip

\par{Use {\tt ms\_print -{}-threshold=X} to process stored data.}


\fakesection{Alias advices}

\cmd{man ascii}{Display the ASCII table}

\cmd{gcc -dM -E - < /dev/null}{Dump preprocessor defines}
\cmd{make -j\$( nproc )}{Run make using all found CPUs}

\cmd{find -name "*\textasciitilde" -delete}{Remove backup files}
\cmd{find -type d -empty -delete}{Remove empty directories}

\cmd{xclip -selection clipboard}{Copy from stdin}
\cmd{xclip -selection clipboard -o}{Paste to stdout}

\cmd{kill -STOP \$( pidof firefox )}{Save some CPU usage}
\cmd{kill -CONT \$( pidof firefox )}{Go to surf on the Web}

\cmd{grep -{}-color=always}{Enjoy colors using {\tt less -R}}
\cmd{grep -{}-include="*.[ch]"}{Search in code source only}

\cmd{readelf -aW file}{Display wide info about \textit{file}}


\vskip 2em
\hrule
\vskip 1em

\centerline{This card may be distributed under the terms of the GPL v3}
\centerline{Copyright \copyright\ {2017} Cyrille BAGARD}

\end{multicols}
\end{document}
