<!DOCTYPE HTML>
<html lang="en" class="light" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Notes</title>
<meta name="robots" content="noindex">
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
</head>
<body class="sidebar-visible no-js">
<div id="body-container">
<!-- Provide site root to javascript -->
<script>
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('light')
html.classList.add(theme);
var body = document.querySelector('body');
body.classList.remove('no-js')
body.classList.add('js');
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var body = document.querySelector('body');
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
body.classList.remove('sidebar-visible');
body.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item expanded affix "><a href="intro.html">Introduction</a></li><li class="chapter-item expanded "><a href="tools/index.html"><strong aria-hidden="true">1.</strong> Tools</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="tools/zsh.html"><strong aria-hidden="true">1.1.</strong> zsh</a></li><li class="chapter-item expanded "><a href="tools/bash.html"><strong aria-hidden="true">1.2.</strong> bash</a></li><li class="chapter-item expanded "><a href="tools/fish.html"><strong aria-hidden="true">1.3.</strong> fish</a></li><li class="chapter-item expanded "><a href="tools/tmux.html"><strong aria-hidden="true">1.4.</strong> tmux</a></li><li class="chapter-item expanded "><a href="tools/git.html"><strong aria-hidden="true">1.5.</strong> git</a></li><li class="chapter-item expanded "><a href="tools/awk.html"><strong aria-hidden="true">1.6.</strong> awk</a></li><li class="chapter-item expanded "><a href="tools/emacs.html"><strong aria-hidden="true">1.7.</strong> emacs</a></li><li class="chapter-item expanded "><a href="tools/gpg.html"><strong aria-hidden="true">1.8.</strong> gpg</a></li><li class="chapter-item expanded "><a href="tools/gdb.html"><strong aria-hidden="true">1.9.</strong> gdb</a></li><li class="chapter-item expanded "><a href="tools/gdbserver.html"><strong aria-hidden="true">1.10.</strong> gdbserver</a></li><li class="chapter-item expanded "><a href="tools/radare2.html"><strong aria-hidden="true">1.11.</strong> radare2</a></li><li class="chapter-item expanded "><a href="tools/qemu.html"><strong aria-hidden="true">1.12.</strong> qemu</a></li><li class="chapter-item expanded "><a href="tools/pacman.html"><strong aria-hidden="true">1.13.</strong> pacman</a></li><li class="chapter-item expanded "><a href="tools/dot.html"><strong aria-hidden="true">1.14.</strong> dot</a></li><li class="chapter-item expanded "><a href="tools/ffmpeg.html"><strong aria-hidden="true">1.15.</strong> ffmpeg</a></li><li class="chapter-item expanded "><a href="tools/column.html"><strong aria-hidden="true">1.16.</strong> column</a></li><li class="chapter-item expanded "><a href="tools/sort.html"><strong aria-hidden="true">1.17.</strong> sort</a></li><li class="chapter-item expanded "><a href="tools/sed.html"><strong aria-hidden="true">1.18.</strong> sed</a></li><li class="chapter-item expanded "><a href="tools/gnuplot.html"><strong aria-hidden="true">1.19.</strong> gnuplot</a></li></ol></li><li class="chapter-item expanded "><a href="monitor/index.html"><strong aria-hidden="true">2.</strong> Resource analysis & monitor</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="monitor/lsof.html"><strong aria-hidden="true">2.1.</strong> lsof</a></li><li class="chapter-item expanded "><a href="monitor/ss.html"><strong aria-hidden="true">2.2.</strong> ss</a></li><li class="chapter-item expanded "><a href="monitor/pidstat.html"><strong aria-hidden="true">2.3.</strong> pidstat</a></li><li class="chapter-item expanded "><a href="monitor/pgrep.html"><strong aria-hidden="true">2.4.</strong> pgrep</a></li><li class="chapter-item expanded "><a href="monitor/ps.html"><strong aria-hidden="true">2.5.</strong> ps</a></li><li class="chapter-item expanded "><a href="monitor/pmap.html"><strong aria-hidden="true">2.6.</strong> pmap</a></li><li class="chapter-item expanded "><a href="monitor/pstack.html"><strong aria-hidden="true">2.7.</strong> pstack</a></li></ol></li><li class="chapter-item expanded "><a href="trace_profile/index.html"><strong aria-hidden="true">3.</strong> Trace and Profile</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="trace_profile/time.html"><strong aria-hidden="true">3.1.</strong> time</a></li><li class="chapter-item expanded "><a href="trace_profile/strace.html"><strong aria-hidden="true">3.2.</strong> strace</a></li><li class="chapter-item expanded "><a href="trace_profile/ltrace.html"><strong aria-hidden="true">3.3.</strong> ltrace</a></li><li class="chapter-item expanded "><a href="trace_profile/perf.html"><strong aria-hidden="true">3.4.</strong> perf</a></li><li class="chapter-item expanded "><a href="trace_profile/oprofile.html"><strong aria-hidden="true">3.5.</strong> OProfile</a></li><li class="chapter-item expanded "><a href="trace_profile/callgrind.html"><strong aria-hidden="true">3.6.</strong> callgrind</a></li></ol></li><li class="chapter-item expanded "><a href="binary/index.html"><strong aria-hidden="true">4.</strong> Binary</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="binary/od.html"><strong aria-hidden="true">4.1.</strong> od</a></li><li class="chapter-item expanded "><a href="binary/xxd.html"><strong aria-hidden="true">4.2.</strong> xxd</a></li><li class="chapter-item expanded "><a href="binary/readelf.html"><strong aria-hidden="true">4.3.</strong> readelf</a></li><li class="chapter-item expanded "><a href="binary/objdump.html"><strong aria-hidden="true">4.4.</strong> objdump</a></li><li class="chapter-item expanded "><a href="binary/nm.html"><strong aria-hidden="true">4.5.</strong> nm</a></li></ol></li><li class="chapter-item expanded "><a href="development/index.html"><strong aria-hidden="true">5.</strong> Development</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="development/c++filt.html"><strong aria-hidden="true">5.1.</strong> c++filt</a></li><li class="chapter-item expanded "><a href="development/c++.html"><strong aria-hidden="true">5.2.</strong> c++</a></li><li class="chapter-item expanded "><a href="development/glibc.html"><strong aria-hidden="true">5.3.</strong> glibc</a></li><li class="chapter-item expanded "><a href="development/gcc.html"><strong aria-hidden="true">5.4.</strong> gcc</a></li><li class="chapter-item expanded "><a href="development/cmake.html"><strong aria-hidden="true">5.5.</strong> cmake</a></li><li class="chapter-item expanded "><a href="development/make.html"><strong aria-hidden="true">5.6.</strong> make</a></li><li class="chapter-item expanded "><a href="development/ld.so.html"><strong aria-hidden="true">5.7.</strong> ld.so</a></li><li class="chapter-item expanded "><a href="development/symbolver.html"><strong aria-hidden="true">5.8.</strong> symbol versioning</a></li><li class="chapter-item expanded "><a href="development/python.html"><strong aria-hidden="true">5.9.</strong> python</a></li><li class="chapter-item expanded "><a href="development/gcov.html"><strong aria-hidden="true">5.10.</strong> gcov</a></li><li class="chapter-item expanded "><a href="development/pgo.html"><strong aria-hidden="true">5.11.</strong> pgo</a></li></ol></li><li class="chapter-item expanded "><a href="linux/index.html"><strong aria-hidden="true">6.</strong> Linux</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="linux/systemd.html"><strong aria-hidden="true">6.1.</strong> systemd</a></li><li class="chapter-item expanded "><a href="linux/coredump.html"><strong aria-hidden="true">6.2.</strong> coredump</a></li><li class="chapter-item expanded "><a href="linux/ptrace_scope.html"><strong aria-hidden="true">6.3.</strong> ptrace_scope</a></li><li class="chapter-item expanded "><a href="linux/cryptsetup.html"><strong aria-hidden="true">6.4.</strong> cryptsetup</a></li><li class="chapter-item expanded "><a href="linux/swap.html"><strong aria-hidden="true">6.5.</strong> swap</a></li><li class="chapter-item expanded "><a href="linux/input.html"><strong aria-hidden="true">6.6.</strong> input</a></li><li class="chapter-item expanded "><a href="linux/acl.html"><strong aria-hidden="true">6.7.</strong> acl</a></li><li class="chapter-item expanded "><a href="linux/zfs.html"><strong aria-hidden="true">6.8.</strong> zfs</a></li></ol></li><li class="chapter-item expanded "><a href="network/index.html"><strong aria-hidden="true">7.</strong> Network</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="network/tcpdump.html"><strong aria-hidden="true">7.1.</strong> tcpdump</a></li><li class="chapter-item expanded "><a href="network/tshark.html"><strong aria-hidden="true">7.2.</strong> tshark</a></li><li class="chapter-item expanded "><a href="network/firewall-cmd.html"><strong aria-hidden="true">7.3.</strong> firewall-cmd</a></li><li class="chapter-item expanded "><a href="network/nftables.html"><strong aria-hidden="true">7.4.</strong> nftables</a></li></ol></li><li class="chapter-item expanded "><a href="web/index.html"><strong aria-hidden="true">8.</strong> Web</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="web/html.html"><strong aria-hidden="true">8.1.</strong> html</a></li><li class="chapter-item expanded "><a href="web/css.html"><strong aria-hidden="true">8.2.</strong> css</a></li><li class="chapter-item expanded "><a href="web/chartjs.html"><strong aria-hidden="true">8.3.</strong> chartjs</a></li></ol></li><li class="chapter-item expanded "><a href="arch/index.html"><strong aria-hidden="true">9.</strong> Arch</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="arch/x86_64.html"><strong aria-hidden="true">9.1.</strong> x86_64</a></li><li class="chapter-item expanded "><a href="arch/arm64.html"><strong aria-hidden="true">9.2.</strong> arm64</a></li><li class="chapter-item expanded "><a href="arch/armv7.html"><strong aria-hidden="true">9.3.</strong> armv7</a></li><li class="chapter-item expanded "><a href="arch/riscv.html"><strong aria-hidden="true">9.4.</strong> riscv</a></li></ol></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<!-- Track and set sidebar scroll position -->
<script>
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
sidebarScrollbox.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
sidebarScrollbox.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
</script>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Notes</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/johannst/notes" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="notes"><a class="header" href="#notes">Notes</a></h1>
<p>A personal collection of notes and cheatsheets.</p>
<p>Source code is located at <a href="https://github.com/johannst/notes">johannst/notes</a>.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="tools"><a class="header" href="#tools">Tools</a></h1>
<ul>
<li><a href="tools/./zsh.html">zsh</a></li>
<li><a href="tools/./bash.html">bash</a></li>
<li><a href="tools/./fish.html">fish</a></li>
<li><a href="tools/./tmux.html">tmux</a></li>
<li><a href="tools/./git.html">git</a></li>
<li><a href="tools/./awk.html">awk</a></li>
<li><a href="tools/./emacs.html">emacs</a></li>
<li><a href="tools/./gpg.html">gpg</a></li>
<li><a href="tools/./gdb.html">gdb</a></li>
<li><a href="tools/./gdbserver.html">gdbserver</a></li>
<li><a href="tools/./radare2.html">radare2</a></li>
<li><a href="tools/./qemu.html">qemu</a></li>
<li><a href="tools/./pacman.html">pacman</a></li>
<li><a href="tools/./dot.html">dot</a></li>
<li><a href="tools/./ffmpeg.html">ffmpeg</a></li>
<li><a href="tools/./column.html">column</a></li>
<li><a href="tools/./sort.html">sort</a></li>
<li><a href="tools/./sed.html">sed</a></li>
<li><a href="tools/./gnuplot.html">gnuplot</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="zsh1"><a class="header" href="#zsh1">zsh(1)</a></h1>
<h2 id="keybindings"><a class="header" href="#keybindings">Keybindings</a></h2>
<p>Change input mode:</p>
<pre><code class="language-zsh">bindkey -v change to vi keymap
bindkey -e change to emacs keymap
</code></pre>
<p>Define key-mappings:</p>
<pre><code class="language-zsh">bindkey list mappings in current keymap
bindkey in-str cmd create mapping for `in-str` to `cmd`
bindkey -r in-str remove binding for `in-str`
# C-v <key> dump <key> code, which can be used in `in-str`
# zle -l list all functions for keybindings
# man zshzle(1) STANDARD WIDGETS: get description of functions
</code></pre>
<p>Access edit buffer in zle widget:</p>
<pre><code class="language-zsh">$BUFFER # Entire edit buffer content
$LBUFFER # Edit buffer content left to cursor
$RBUFFER # Edit buffer content right to cursor
# create zle widget which adds text right of the cursor
function add-text() {
RBUFFER="some text $RBUFFER"
}
zle -N add-text
bindkey "^p" add-text
</code></pre>
<h2 id="parameter"><a class="header" href="#parameter">Parameter</a></h2>
<p>Default value:</p>
<pre><code class="language-zsh"># default value
echo ${foo:-defval} # defval
foo=bar
echo ${foo:-defval} # bar
</code></pre>
<p>Alternative value:</p>
<pre><code class="language-zsh">echo ${foo:+altval} # ''
foo=bar
echo ${foo:+altval} # altval
</code></pre>
<p>Check variable set, error if not set:</p>
<pre><code class="language-zsh">echo ${foo:?msg} # print `msg` and return errno `1`
foo=bar
echo ${foo:?msg} # bar
</code></pre>
<p>Sub-string <code>${var:offset:length}</code>:</p>
<pre><code class="language-zsh">foo=abcdef
echo ${foo:1:3} # bcd
</code></pre>
<p>Trim prefix <code>${var#prefix}</code>:</p>
<pre><code class="language-zsh">foo=bar.baz
echo ${foo#bar} # .baz
</code></pre>
<p>Trim suffix <code>${var%suffix}</code>:</p>
<pre><code class="language-zsh">foo=bar.baz
echo ${foo%.baz} # bar
</code></pre>
<p>Substitute pattern <code>${var/pattern/replace}</code>:</p>
<pre><code class="language-zsh">foo=aabbccbbdd
echo ${foo/bb/XX} # aaXXccbbdd
echo ${foo//bb/XX} # aaXXccXXdd
# replace prefix
echo ${foo/#bb/XX} # aabbccbbdd
echo ${foo/#aa/XX} # XXbbccbbdd
# replace suffix
echo ${foo/%bb/XX} # aabbccbbdd
echo ${foo/%dd/XX} # aabbccbbXX
</code></pre>
<blockquote>
<p>Note: <code>prefix</code>/<code>suffix</code>/<code>pattern</code> are expanded as pathnames.</p>
</blockquote>
<h2 id="variables"><a class="header" href="#variables">Variables</a></h2>
<pre><code class="language-zsh"># Variable with local scope
local var=val
# Read-only variable
readonly var=bal
</code></pre>
<p>Indexed arrays:</p>
<pre><code class="language-zsh">arr=(aa bb cc dd)
echo $arr[1] # aa
echo $arr[-1] # dd
arr+=(ee)
echo $arr[-1] # ee
echo $arr[1,3] # aa bb cc
</code></pre>
<p>Associative arrays:</p>
<pre><code class="language-zsh">typeset -A arr
arr[x]='aa'
arr[y]='bb'
echo $arr[x] # aa
</code></pre>
<p>Tied arrays:</p>
<pre><code class="language-zsh">typeset -T VEC vec=(1 2 3) '|'
echo $vec # 1 2 3
echo $VEC # 1|2|3
</code></pre>
<p>Unique arrays (set):</p>
<pre><code>typeset -U vec=(1 2 3)
echo $vec # 1 2 3
vec+=(1 2 4)
echo $vec # 1 2 3 4
</code></pre>
<h3 id="expansion-flags"><a class="header" href="#expansion-flags">Expansion Flags</a></h3>
<p>Join array to string <code>j:sep:</code>:</p>
<pre><code class="language-zsh">foo=(1 2 3 4)
echo ${(j:-:)foo} # 1-2-3-4
echo ${(j:\n:)foo} # join with new lines
</code></pre>
<p>Split string to array <code>s:sep</code>:</p>
<pre><code class="language-zsh">foo='1-2-3-4'
bar=(${(s:-:)foo}) # capture as array
echo $bar # 1 2 3 4
echo $bar[2] # 2
</code></pre>
<p>Upper/Lower case string:</p>
<pre><code class="language-zsh">foo=aaBB
echo ${(L)foo} # aabb
echo ${(U)foo} # AABB
</code></pre>
<p>Key/values in associative arrays:</p>
<pre><code class="language-zsh">typeset -A vec; vec[a]='aa'; vec[b]='bb'
echo ${(k)vec} # a b
echo ${(v)vec} # aa bb
echo ${(kv)vec} # a aa b bb
# Iterate over key value pairs.
for k v in ${(kv)vec)}; do ...; done
</code></pre>
<h2 id="io-redirections"><a class="header" href="#io-redirections">I/O redirections</a></h2>
<p>See <a href="tools/bash.html#io-redirection">bash - I/O redirection</a></p>
<h2 id="process-substitution"><a class="header" href="#process-substitution">Process substitution</a></h2>
<p>Process substitution allows to redirect the stdout of multiple processes at
once.</p>
<pre><code class="language-bash">vim -d <(grep foo bar) <(grep foo moose)
</code></pre>
<h2 id="argument-parsing-with-zparseopts"><a class="header" href="#argument-parsing-with-zparseopts">Argument parsing with <code>zparseopts</code></a></h2>
<pre><code class="language-zsh">zparseopts [-D] [-E] [-A assoc] specs
</code></pre>
<p>Arguments are copied into the associative array <code>assoc</code> according to <code>specs</code>.
Each spec is described by an entry as <code>opt[:][=array]</code>.</p>
<ul>
<li><code>opt</code> is the option without the <code>-</code> char. Passing <code>-f</code> is matched against <code>f</code>
opt, <code>--long</code> is matched against <code>-long</code>.</li>
<li>Using <code>:</code> means the option will take an argument.</li>
<li>The optional <code>=array</code> specifies an alternate storage container where this
option should be stored.</li>
</ul>
<blockquote>
<p>Documentation can be found in <code>man zshmodules</code>.</p>
</blockquote>
<h3 id="example"><a class="header" href="#example">Example</a></h3>
<pre><code class="language-zsh">#!/bin/zsh
function test() {
zparseopts -D -E -A opts f=flag o: -long:
echo "flag $flag"
echo "o $opts[-o]"
echo "long $opts[--long]"
echo "pos $1"
}
test -f -o OPTION --long LONG_OPT POSITIONAL
# Outputs:
# flag -f
# o OPTION
# long LONG_OPT
# pos POSITIONAL
</code></pre>
<h2 id="regular-expressions"><a class="header" href="#regular-expressions">Regular Expressions</a></h2>
<p>Zsh supports regular expression matching with the binary operator <code>=~</code>.
The match results can be accessed via the <code>$MATCH</code> variable and
<code>$match</code> indexed array:</p>
<ul>
<li><code>$MATCH</code> contains the full match</li>
<li><code>$match[1]</code> contains match of the first capture group</li>
</ul>
<pre><code class="language-zsh">INPUT='title foo : 1234'
REGEX='^title (.+) : ([0-9]+)$'
if [[ $INPUT =~ $REGEX ]]; then
echo "$MATCH" # title foo : 1234
echo "$match[1]" # foo
echo "$match[2]" # 1234
fi
</code></pre>
<h2 id="completion"><a class="header" href="#completion">Completion</a></h2>
<h3 id="installation"><a class="header" href="#installation">Installation</a></h3>
<p>Completion functions are provided via files and need to be placed in a location
covered by <code>$fpath</code>. By convention the completion files are names as <code>_<CMD></code>.</p>
<p>A completion skeleton for the command <code>foo</code>, stored in <code>_foo</code></p>
<pre><code class="language-zsh">#compdef _foo foo
function _foo() {
...
}
</code></pre>
<p>Alternatively one can install a completion function explicitly by calling <code>compdef <FUNC> <CMD></code>.</p>
<h3 id="completion-variables"><a class="header" href="#completion-variables">Completion Variables</a></h3>
<p>Following variables are available in Completion functions:</p>
<pre><code class="language-zsh">$words # array with command line in words
$#words # number words
$CURRENT # index into $words for cursor position
$words[CURRENT-1] # previous word (relative to cursor position)
</code></pre>
<h3 id="completion-functions"><a class="header" href="#completion-functions">Completion Functions</a></h3>
<ul>
<li><code>_describe</code> simple completion, just words + description</li>
<li><code>_arguments</code> sophisticated completion, allow to specify actions</li>
</ul>
<h4 id="completion-with-_describe"><a class="header" href="#completion-with-_describe">Completion with <a href="http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions"><code>_describe</code></a></a></h4>
<pre><code class="language-zsh">_describe MSG COMP
</code></pre>
<ul>
<li><code>MSG</code> simple string with header message</li>
<li><code>COMP</code> array of completions where each entry is <code>"opt:description"</code></li>
</ul>
<pre><code class="language-zsh">function _foo() {
local -a opts
opts=('bla:desc for bla' 'blu:desc for blu')
_describe 'foo-msg' opts
}
compdef _foo foo
foo <TAB><TAB>
-- foo-msg --
bla -- desc for bla
blu -- desc for blu
</code></pre>
<h4 id="completion-with-_arguments"><a class="header" href="#completion-with-_arguments">Completion with <a href="http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions"><code>_arguments</code></a></a></h4>
<pre><code class="language-zsh">_arguments SPEC [SPEC...]
</code></pre>
<p>where <code>SPEC</code> can have one of the following forms:</p>
<ul>
<li><code>OPT[DESC]:MSG:ACTION</code> for option flags</li>
<li><code>N:MSG:ACTION</code> for positional arguments</li>
</ul>
<p>Available actions</p>
<pre><code class="language-zsh">(op1 op2) list possible matches
->VAL set $state=VAL and continue, `$state` can be checked later in switch case
FUNC call func to generate matches
{STR} evaluate `STR` to generate matches
</code></pre>
<h3 id="example-1"><a class="header" href="#example-1">Example</a></h3>
<p>Skeleton to copy/paste for writing simple completions.</p>
<p>Assume a program <code>foo</code> with the following interface:</p>
<pre><code class="language-zsh">foo -c green|red|blue -s low|high -f <file> -d <dir> -h
</code></pre>
<p>The completion handler could be implemented as follows in a file called <code>_foo</code>:</p>
<pre><code class="language-zsh">#compdef _foo foo
function _foo_color() {
local colors=()
colors+=('green:green color')
colors+=('red:red color')
colors+=('blue:blue color')
_describe "color" colors
}
function _foo() {
_arguments \
"-c[define color]:color:->s_color" \
"-s[select sound]:sound:(low high)" \
"-f[select file]:file:_files" \
"-d[select dir]:dir:_files -/" \
"-h[help]"
case $state in
s_color) _foo_color;;
esac
}
</code></pre>
<h3 id="example-with-optional-arguments"><a class="header" href="#example-with-optional-arguments">Example with optional arguments</a></h3>
<p>For this example we assume that the command <code>foo</code> takes at least three optional
arguments such as</p>
<pre><code class="language-zsh">foo arg1 arg2 arg3 [argN..]
</code></pre>
<pre><code class="language-zsh">function _foo() {
_arguments \
"1:opt 1:(a b c)" \
":opt next:(d e f)" \
"*:opt all:(u v w)"
}
</code></pre>
<p>Explanation:</p>
<ul>
<li><code>1:MSG:ACTION</code> sets completion for the <strong>first</strong> optional argument</li>
<li><code>:MSG:ACTION</code> sets completion for the <strong>next</strong> optional argument</li>
<li><code>*:MSG:ACTION</code> sets completion for the optional argument where none of the
previous rules apply, so in our example for <code>arg3, argN..</code>.</li>
</ul>
<blockquote>
<p><code>_files</code> is a zsh builtin utility function to complete files/dirs see</p>
<ul>
<li><a href="http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions">zsh completion functions</a></li>
<li><a href="https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org#utility-functions">zsh completion utility functions</a></li>
</ul>
</blockquote>
<div style="break-before: page; page-break-before: always;"></div><h1 id="bash1"><a class="header" href="#bash1">bash(1)</a></h1>
<h2 id="expansion"><a class="header" href="#expansion">Expansion</a></h2>
<h3 id="generator"><a class="header" href="#generator">Generator</a></h3>
<pre><code class="language-bash"># generate sequence from n to m
{n..m}
# generate sequence from n to m step by s
{n..m..s}
# expand cartesian product
{a,b}{c,d}
</code></pre>
<h3 id="parameter-1"><a class="header" href="#parameter-1">Parameter</a></h3>
<pre><code class="language-bash"># default value
bar=${foo:-some_val} # if $foo set, then bar=$foo else bar=some_val
# alternate value
bar=${foo:+bla $foo} # if $foo set, then bar="bla $foo" else bar=""
# check param set
bar=${foo:?msg} # if $foo set, then bar=$foo else exit and print msg
# indirect
FOO=foo
BAR=FOO
bar=${!BAR} # deref value of BAR -> bar=$FOO
# prefix
${foo#prefix} # remove prefix when expanding $foo
# suffix
${foo%suffix} # remove suffix when expanding $foo
# substitute
${foo/pattern/string} # replace pattern with string when expanding foo
# pattern starts with
# '/' replace all occurences of pattern
# '#' pattern match at beginning
# '%' pattern match at end
</code></pre>
<blockquote>
<p>Note: <code>prefix</code>/<code>suffix</code>/<code>pattern</code> are expanded as <a href="tools/bash.html#pathname">pathnames</a>.</p>
</blockquote>
<h3 id="pathname"><a class="header" href="#pathname">Pathname</a></h3>
<pre><code class="language-bash">* match any string
? match any single char
\\ match backslash
[abc] match any char of 'a' 'b' 'c'
[a-z] match any char between 'a' - 'z'
[^ab] negate, match all not 'a' 'b'
[:class:] match any char in class, available:
alnum,alpha,ascii,blank,cntrl,digit,graph,lower,
print,punct,space,upper,word,xdigit
</code></pre>
<p>With <code>extglob</code> shell option enabled it is possible to have more powerful
patterns. In the following <code>pattern-list</code> is one ore more patterns separated
by <code>|</code> char.</p>
<pre><code class="language-bash">?(pattern-list) matches zero or one occurrence of the given patterns
*(pattern-list) matches zero or more occurrences of the given patterns
+(pattern-list) matches one or more occurrences of the given patterns
@(pattern-list) matches one of the given patterns
!(pattern-list) matches anything except one of the given patterns
</code></pre>
<blockquote>
<p>Note: <code>shopt -s extglob</code>/<code>shopt -u extglob</code> to enable/disable <code>extglob</code>
option.</p>
</blockquote>
<h2 id="io-redirection"><a class="header" href="#io-redirection">I/O redirection</a></h2>
<blockquote>
<p>Note: The trick with bash I/O redirection is to interpret from left-to-right.</p>
</blockquote>
<pre><code class="language-bash"># stdout & stderr to file
command >file 2>&1
# equivalent
command &>file
# stderr to stdout & stdout to file
command 2>&1 >file
</code></pre>
<blockquote>
<p>The article <a href="https://catonmat.net/bash-one-liners-explained-part-three">Bash One-Liners Explained, Part III: All about
redirections</a>
contains some nice visualization to explain bash redirections.</p>
</blockquote>
<h3 id="explanation"><a class="header" href="#explanation">Explanation</a></h3>
<pre><code class="language-bash">j>&i
</code></pre>
<p>Duplicate <code>fd i</code> to <code>fd j</code>, making <code>j</code> a copy of <code>i</code>. See <a href="http://man7.org/linux/man-pages/man2/dup.2.html">dup2(2)</a>.</p>
<p>Example:</p>
<pre><code class="language-bash">command 2>&1 >file
</code></pre>
<ol>
<li>duplicate <code>fd 1</code> to <code>fd 2</code>, effectively redirecting <code>stderr</code> to <code>stdout</code></li>
<li>redirect <code>stdout</code> to <code>file</code></li>
</ol>
<h2 id="process-substitution-ref"><a class="header" href="#process-substitution-ref">Process substitution (<a href="https://tldp.org/LDP/abs/html/process-sub.html">ref</a>)</a></h2>
<p>Process substitution allows to redirect the stdout of multiple processes at
once.</p>
<pre><code class="language-bash">vim -d <(grep foo bar) <(grep foo moose)
</code></pre>
<h2 id="command-grouping"><a class="header" href="#command-grouping">Command grouping</a></h2>
<p>Execute commands in a group with or without subshell. Can be used to easily
redirect stdout/stderr of all commands in the group into one file.</p>
<pre><code class="language-bash"># Group commands without subshell.
v=abc ; { v=foo; echo $v; } ; echo $v
# foo
# foo
# Group commands with subshell.
v=abc ; ( v=foo; echo $v; ) ; echo $v
# foo
# abc
</code></pre>
<h2 id="argument-parsing-with-getopts"><a class="header" href="#argument-parsing-with-getopts">Argument parsing with <code>getopts</code></a></h2>
<p>The <code>getopts</code> builtin uses following global variables:</p>
<ul>
<li><code>OPTARG</code>, value of last option argument</li>
<li><code>OPTIND</code>, index of the next argument to process (user must reset)</li>
<li><code>OPTERR</code>, display errors if set to <code>1</code></li>
</ul>
<pre><code class="language-bash">getopts <optstring> <param> [<args>]
</code></pre>
<ul>
<li><code><optstring></code> specifies the names of supported options, eg <code>f:c</code>
<ul>
<li><code>f:</code> means <code>-f</code> option with an argument</li>
<li><code>c</code> means <code>-c</code> option without an argument</li>
</ul>
</li>
<li><code><param></code> specifies a variable name which <code>getopts</code> fills with the last parsed option argument</li>
<li><code><args></code> optionally specify argument string to parse, by default <code>getopts</code> parses <code>$@</code></li>
</ul>
<h3 id="example-2"><a class="header" href="#example-2">Example</a></h3>
<pre><code class="language-bash">#!/bin/bash
function parse_args() {
while getopts "f:c" PARAM; do
case $PARAM in
f) echo "GOT -f $OPTARG";;
c) echo "GOT -c";;
*) echo "ERR: print usage"; exit 1;;
esac
done
# users responsibility to reset OPTIND
OPTIND=1
}
parse_args -f xxx -c
parse_args -f yyy
</code></pre>
<h2 id="regular-expressions-1"><a class="header" href="#regular-expressions-1">Regular Expressions</a></h2>
<p>Bash supports regular expression matching with the binary operator <code>=~</code>.
The match results can be accessed via the <code>$BASH_REMATCH</code> variable:</p>
<ul>
<li><code>${BASH_REMATCH[0]}</code> contains the full match</li>
<li><code>${BASH_REMATCH[1]}</code> contains match of the first capture group</li>
</ul>
<pre><code class="language-bash">INPUT='title foo : 1234'
REGEX='^title (.+) : ([0-9]+)$'
if [[ $INPUT =~ $REGEX ]]; then
echo "${BASH_REMATCH[0]}" # title foo : 1234
echo "${BASH_REMATCH[1]}" # foo
echo "${BASH_REMATCH[2]}" # 1234
fi
</code></pre>
<blockquote>
<p><strong>Caution</strong>: When specifying a <code>regex</code> in the <code>[[ ]]</code> block directly, quotes will be treated as part of the pattern.
<code>[[ $INPUT =~ "foo" ]]</code> will match against <code>"foo"</code> not <code>foo</code>!</p>
</blockquote>
<h2 id="completion-1"><a class="header" href="#completion-1">Completion</a></h2>
<p>The <code>complete</code> builtin is used to interact with the completion system.</p>
<pre><code class="language-bash">complete # print currently installed completion handler
complete -F <func> <cmd> # install <func> as completion handler for <cmd>
complete -r <cmd> # uninstall completion handler for <cmd>
</code></pre>
<p>Variables available in completion functions:</p>
<pre><code class="language-bash"># in
$1 # <cmd>
$2 # current word
$3 # privous word
COMP_WORDS # array with current command line words
COMP_CWORD # index into COMP_WORDS with current cursor position
# out
COMPREPLY # array with possible completions
</code></pre>
<p>The <code>compgen</code> builtin is used to generate possible matches by comparing <code>word</code>
against words generated by <code>option</code>.</p>
<pre><code class="language-bash">compgen <option> <word>
# usefule options:
# -W <list> specify list of possible completions
# -d generate list with dirs
# -f generate list with files
# -u generate list with users
# -e generate list with exported variables
# compare "f" against words "foo" "foobar" "bar" and generate matches
compgen -W "foo foobar bar" "f"
# compare "hom" against file/dir names and generate matches
compgen -d -f "hom"
</code></pre>
<h3 id="example-3"><a class="header" href="#example-3">Example</a></h3>
<p>Skeleton to copy/paste for writing simple completions.</p>
<p>Assume a program <code>foo</code> with the following interface:</p>
<pre><code class="language-bash">foo -c green|red|blue -s low|high -f <file> -h
</code></pre>
<p>The completion handler could be implemented as follows:</p>
<pre><code class="language-bash">function _foo() {
local curr=$2
local prev=$3
local opts="-c -s -f -h"
case $prev in
-c) COMPREPLY=( $(compgen -W "green red blue" -- $curr) );;
-s) COMPREPLY=( $(compgen -W "low high" -- $curr) );;
-f) COMPREPLY=( $(compgen -f -- $curr) );;
*) COMPREPLY=( $(compgen -W "$opts" -- $curr) );;
esac
}
complete -F _foo foo
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="fish1"><a class="header" href="#fish1">fish(1)</a></h1>
<h2 id="quick-info"><a class="header" href="#quick-info">Quick Info</a></h2>
<p>Fish initialization file <code>~/.config/fish/config.fish</code></p>
<p>Switch between different key bindings:</p>
<ul>
<li><code>fish_default_key_bindings</code> to use default key bindings</li>
<li><code>fish_vi_key_bindings</code> to use vi key bindings</li>
</ul>
<h2 id="variables-1"><a class="header" href="#variables-1">Variables</a></h2>
<p>Available scopes</p>
<ul>
<li><code>local</code> variable local to a block</li>
<li><code>global</code> variable global to shell instance</li>
<li><code>universal</code> variable universal to all shell instances + preserved across
shell restart</li>
</ul>
<h3 id="setunset-variables"><a class="header" href="#setunset-variables">Set/Unset Variables</a></h3>
<pre><code class="language-text">set <name> [<values>]
-l local scope
-g global scope
-U universal scope
-e erase variable
-S show verbose info
-x export to ENV
-u unexport from ENV
</code></pre>
<h3 id="special-variables-ref"><a class="header" href="#special-variables-ref">Special Variables <a href="https://fishshell.com/docs/current/language.html#special-variables">ref</a></a></h3>
<pre><code class="language-sh">$status # exit code of last command
$pipestatus # list of exit codes of pipe chain
$fish_pid # pid of parent fish shell ($$ in bash)
$last_pid # pid of last started process ($! in bash)
$CMD_DURATION # runtime of last command in ms
</code></pre>
<h3 id="lists"><a class="header" href="#lists">Lists</a></h3>
<p>In <code>fish</code> all variables are lists (start with index <code>1</code>, but lists can't
contain lists.</p>
<pre><code class="language-sh">set foo a b c d
echo $foo[1] # a
echo $foo[-1] # d
echo $foo[2..3] # b c
echo $foo[1 3] # a c
</code></pre>
<p><code>$</code> can be seen as dereference operator.</p>
<pre><code class="language-sh">set foo a; set a 1337
echo $$foo # outputs 1337
</code></pre>
<p>Cartesian product.</p>
<pre><code class="language-sh">echo file.{h,cc}
# file.h file.cc
echo {a,b}{1,2}
# a1 b1 a2 b2
</code></pre>
<h4 id="path-ref"><a class="header" href="#path-ref"><code>*PATH</code> <a href="https://fishshell.com/docs/current/language.html#path-variables">ref</a></a></h4>
<p>Lists ending with <code>PATH</code> are automatically split at <code>:</code> when used and joined
with <code>:</code> when quoted or exported to the environment.</p>
<pre><code class="language-sh">set -x BLA_PATH a:b:c:d
echo $BLA_PATH # a b c d
echo "$BLA_PATH" # a:b:c:d (quoted)
env | grep BLA_PATH # BLA_PATH=a:b:c:d (env)
set FOO_PATH x y z
echo $FOO_PATH # x y z
echo "$FOO_PATH" # x:y:z
</code></pre>
<h2 id="command-handling"><a class="header" href="#command-handling">Command Handling</a></h2>
<pre><code class="language-sh"># sub-commands are not run in quotes
echo "ls output: "(ls)
</code></pre>
<h3 id="io-redirection-1"><a class="header" href="#io-redirection-1">I/O redirection</a></h3>
<pre><code class="language-sh"># 'noclobber', fail if 'log' already exists
echo foo >? log
</code></pre>
<h3 id="process-substitution-1"><a class="header" href="#process-substitution-1">Process substitution</a></h3>
<p>Redirect output of multiple processes. Same as <code><(..)</code> in bash.</p>
<pre><code class="language-sh">diff (sort a | psub) (sort b | psub)
</code></pre>
<h2 id="control-flow"><a class="header" href="#control-flow">Control Flow</a></h2>
<h3 id="if--else"><a class="header" href="#if--else"><code>if</code> / <code>else</code></a></h3>
<pre><code class="language-sh">if grep foo bar
# do sth
else if grep foobar bar
# do sth else
else
# do sth else
end
</code></pre>
<h3 id="switch"><a class="header" href="#switch"><code>switch</code></a></h3>
<pre><code class="language-sh">switch (echo foo)
case 'foo*'
# do start with foo
case bar dudel
# do bar and dudel
case '*'
# do else
end
</code></pre>
<h3 id="while-loop"><a class="header" href="#while-loop"><code>while</code> Loop</a></h3>
<pre><code class="language-sh">while true
echo foo
end
</code></pre>
<h3 id="for-loop"><a class="header" href="#for-loop"><code>for</code> Loop</a></h3>
<pre><code class="language-sh">for f in (ls)
echo $f
end
</code></pre>
<h2 id="functions"><a class="header" href="#functions">Functions</a></h2>
<p>Function arguments are passed via <code>$argv</code> list.</p>
<pre><code class="language-sh">function fn_foo
echo $argv
end
</code></pre>
<h3 id="autoloading"><a class="header" href="#autoloading">Autoloading</a></h3>
<p>When running a command fish attempts to autoload a function. The shell looks
for <code><cmd>.fish</code> in the locations defined by <code>$fish_function_path</code> and loads
the function lazily if found.</p>
<p>This is the preferred way over monolithically defining all functions in a
startup script.</p>
<h3 id="helper"><a class="header" href="#helper">Helper</a></h3>
<pre><code class="language-sh">functions # list al functions
functions foo # describe function 'foo'
functions -e foo # erase function 'foo'
funced foo # edit function 'foo'
# '-e vim' to edit in vim
</code></pre>
<h3 id="argument-parsing-and-completion"><a class="header" href="#argument-parsing-and-completion">Argument parsing and completion</a></h3>
<p><code>argparse</code> puts options into variables of name <code>_flag_NAME</code>.</p>
<p>References:</p>
<ul>
<li><a href="https://fishshell.com/docs/current/language.html#argument-handling">Argument Handling</a></li>
<li><a href="https://fishshell.com/docs/current/cmds/argparse.html"><code>argparse</code></a></li>
<li><a href="https://fishshell.com/docs/current/completions.html">Writing your own completions</a></li>
<li><a href="https://fishshell.com/docs/current/cmds/complete.html"><code>complete</code></a></li>
</ul>
<pre><code class="language-sh">function moose --d "my moose fn"
# h/help : short / long option (boolean)
# color : only long option (boolean)
# u/user= : option with required argument, only last specified is taken
# f/file+= : option with required argument, can be specified multiple times
#
argparse h/help color= u/user= f/file=+ -- $argv
or return
if set -ql _flag_help
echo "usage ..."
return 0
end
set -ql _flag_file
and echo "file=$_flag_file | cnt:" (count $_flag_file)
set -ql _flag_color
and echo "color=$_flag_color"
set -ql _flag_user
and echo "user=$_flag_user"
end
# Delete all previous defined completions for 'moose'.
complete -c moose -e
# Don't complete files for command.
complete -c moose --no-files
# Help completion.
# -n specifies a conditions. The completion is only active if the command
# returns 0.
complete -c moose -s h -l help -n "not __fish_contains_opt -s h help" \
--description "Print usage help and exit"
# File completion.
# -F force complete files (overwrite --no-files).
# -r requires argument.
complete -c moose -s f -l file -F -r \
--description "Specify file (can be passed multiple times)"
# Color completion.
# -a options for completion.
# -x short for -r and --no-files (-f)
complete -c moose -x -l color -a "red blue" \
--description "Specify a color."
# User completion.
# -a options for completion. Call a function to generate arguments.
complete -c moose -x -s u -l user -a "(__fish_complete_users)" \
--description "Specify a user"
</code></pre>
<h2 id="prompt"><a class="header" href="#prompt">Prompt</a></h2>
<p>The prompt is defined by the output of the <code>fish_prompt</code> function.</p>
<pre><code class="language-sh">function fish_prompt
set -l cmd_ret
echo "> "(pwd) $cmd_ret" "
end
</code></pre>
<blockquote>
<p>Use <code>set_color</code> to manipulate terminal colors and <code>set_color -c</code> to print the
current colors.</p>
</blockquote>
<h2 id="useful-builtins"><a class="header" href="#useful-builtins">Useful Builtins</a></h2>
<p>List all builtins with <code>builtins -n</code>.</p>
<pre><code class="language-sh"># history
history search <str> # search history for <str>
history merge # merge histories from fish sessions
# list
count $var # count elements in list
contains /bin $PATH # return 0 (true) 1 (false)
contains -i /bin $PATH # additionally print index on stdout
# string
string split SEP STRING
# math
math -b hex 4096 # output dec as hex
math 0x1000 # output hex as dec
math "log2(1024)" # call functions
math -s0 7/3 # integer division (by default float)
# status
status -f # abs path of current file
</code></pre>
<h2 id="keymaps"><a class="header" href="#keymaps">Keymaps</a></h2>
<pre><code class="language-text"> Shift-Tab .............. tab-completion with search
Alt-Up / Alt-Down ...... search history with token under the cursor
Alt-l .................. list content of dir under cursor
Alt-p .................. append '2>&1 | less;' to current cmdline
Alt-Left / Alt - Right . prevd / nextd, walk dir history
</code></pre>
<h2 id="debug"><a class="header" href="#debug">Debug</a></h2>
<pre><code class="language-text"> status print-stack-trace .. prints function stacktrace (can be used in scripts)
breakpoint ................ halt script execution and gives shell (C-d | exit
to continue)
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="tmux1"><a class="header" href="#tmux1">tmux(1)</a></h1>
<p>Terminology:</p>
<ul>
<li><code>session</code> is a collection of pseudo terminals which can have multiple
<code>windows</code></li>
<li><code>window</code> uses the entire screen and can be split into rectangular <code>panes</code></li>
<li><code>pane</code> is a single pseudo terminal instance</li>
</ul>
<h1 id="tmux-cli"><a class="header" href="#tmux-cli">Tmux cli</a></h1>
<pre><code class="language-markdown"># Session
tmux creates new session
tmux ls list running sessions
tmux kill-session -t <s> kill running session <s>
tmux attach -t <s> [-d] attach to session <s>, detach other clients [-d]
tmux detach -s <s> detach all clients from session <s>
# Environment
tmux showenv -g show global tmux environment variables
tmux setenv -g <var> <val> set variable in global tmux env
# Misc
tmux source-file <file> source config <file>
tmux lscm list available tmux commnds
tmux show -g show global tmux options
tmux display <msg> display message in tmux status line
</code></pre>
<h2 id="scripting"><a class="header" href="#scripting">Scripting</a></h2>
<pre><code class="language-markdown"># Session
tmux list-sessions -F '#S' list running sessions, only IDs
# Window
tmux list-windows -F '#I' -t <s> list window IDs for session <s>
tmux selectw -t <s>:<w> select window <w> in session <s>
# Pane
tmux list-panes -F '#P' -t <s>:<w> list pane IDs for window <w> in session <s>
tmux selectp -t <s>:<w>.<p> select pane <p> in window <w> in session <s>
# Run commands
tmux send -t <s>:<w>.<p> "ls" C-m send cmds/keys to pane
tmux run -t <p> <sh-cmd> run shell command <sh-cmd> in background and report output on pane -t <p>
</code></pre>
<p>For example cycle through all panes in all windows in all sessions:</p>
<pre><code class="language-bash"># bash
for s in $(tmux list-sessions -F '#S'); do
for w in $(tmux list-windows -F '#I' -t $s); do
for p in $(tmux list-panes -F '#P' -t $s:$w); do
echo $s:$w.$p
done
done
done
</code></pre>
<h1 id="bindings"><a class="header" href="#bindings">Bindings</a></h1>
<pre><code class="language-markdown">prefix d detach from current session
prefix c create new window
prefix w open window list
prefix $ rename session
prefix , rename window
prefix . move current window
</code></pre>
<p>Following bindings are specific to my <a href="https://github.com/johannst/dotfiles/blob/master/tmux.conf"><code>tmux.conf</code></a>:</p>
<pre><code class="language-markdown">C-s prefix
# Panes
prefix s horizontal split
prefix v vertical split
prefix f toggle maximize/minimize current pane
# Movement
prefix Tab toggle between window
prefix h move to pane left
prefix j move to pane down
prefix k move to pane up
prefix l move to pane right
# Resize
prefix C-h resize pane left
prefix C-j resize pane down
prefix C-k resize pane up
prefix C-l resize pane right
# Copy/Paste
prefix C-v enter copy mode
prefix C-p paste yanked text
prefix C-b open copy-buffer list
# In Copy Mode
v enable visual mode
y yank selected text
</code></pre>
<h1 id="command-mode"><a class="header" href="#command-mode">Command mode</a></h1>
<p>To enter command mode <code>prefix :</code>.</p>
<p>Some useful commands are:</p>
<pre><code class="language-markdown">setw synchronize-panes on/off enables/disables synchronized input to all panes
list-keys -t vi-copy list keymaps for vi-copy mode
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="git1"><a class="header" href="#git1">git(1)</a></h1>
<h2 id="working-areas"><a class="header" href="#working-areas">Working areas</a></h2>
<pre><code class="language-text">+-------------------+ --- stash -----> +-------+
| working directory | | stash | // Shelving area.
+-------------------+ <-- stash pop -- +-------+
| ^
add |
| reset
v |
+-------------------+
| staging area |
+-------------------+
|
commit
|
v
+-------------------+
| local repository |
+-------------------+
| ^
push |
| fetch /
| pull
v |
+-------------------+
| remote repository |
+-------------------+
</code></pre>
<h2 id="clean"><a class="header" href="#clean">Clean</a></h2>
<pre><code class="language-markdown"> git clean -X ......... remove only ignored files (-n for dry run)
git clean -f -d -x ... remove untracked & ignored files / folders
git clean -e <pat> ... exclude pattern from deletion
</code></pre>
<h2 id="staging"><a class="header" href="#staging">Staging</a></h2>
<pre><code class="language-markdown"> git add -p [<file>] ............ partial staging (interactive)
</code></pre>
<h2 id="remote"><a class="header" href="#remote">Remote</a></h2>
<pre><code class="language-markdown"> git remote -v .................. list remotes verbose (with URLs)
git remote show [-n] <remote> .. list info for <remote> (like remote HEAD,
remote branches, tracking mapping)
</code></pre>
<h2 id="branching"><a class="header" href="#branching">Branching</a></h2>
<pre><code class="language-markdown"> git branch [-a] ................ list available branches; -a to include
remote branches
git branch -vv ................. list branch & annotate with head sha1 &
remote tracking branch
git branch <bname> ............. create local branch with name <bname>
git branch -d <bname> .......... delete local branch with name <bname>
git checkout <bname> ........... switch to branch with name <bname>
git checkout --track <branch> .. start to locally track a remote branch
git branch --unset-upstream .... unset remote tracking branch
# Remote
git push -u origin <rbname> ........ push local branch to origin (or other
remote), and setup <rbname> as tracking
branch
git push origin --delete <rbname> .. delete branch <rbname> from origin (or
other remote)
</code></pre>
<h2 id="update-local-from-remote"><a class="header" href="#update-local-from-remote">Update local from remote</a></h2>
<pre><code class="language-markdown"> git fetch --prune .................. update all remote references and
remove delete non-existing ones
(does not merge into local tracking branch)
git pull [--rebase] ................ fetch remote references and merge into
local tracking branch (fast-forward by default).
Optionally rebase local tracking branch
on-top of remote branch (in case local
branch has additional commits compared to remote branch).
</code></pre>
<h2 id="tags"><a class="header" href="#tags">Tags</a></h2>
<pre><code class="language-markdown"> git tag -a <tname> -m "descr" ........ creates an annotated tag (full object
containing tagger, date, ...)
git tag -l ........................... list available tags
git checkout tag/<tname> ............. checkout specific tag
git checkout tag/<tname> -b <bname> .. checkout specific tag in a new branch
# Remote
git push origin --tags .... push local tags to origin (or other remote)
</code></pre>
<h2 id="merging"><a class="header" href="#merging">Merging</a></h2>
<pre><code class="language-markdown"> git merge [opt] <commit> .... integrate changes from <commit> since
opt: current branch and <commit> diverged
--squash ................ merge all commits into a single one
--no-commit ............. dont generate commit if the merge succeeds
git merge-base <commit> <commit>
get the common ancestor, since both commits diverged
git rebase -i <upstream> .... interactively rebase on <upstream>, also supports actions
like squashing, editing, rewording, etc of commits
git cherry-pick <commit> .... apply commit on current branch
</code></pre>
<h2 id="worktree"><a class="header" href="#worktree">Worktree</a></h2>
<p>Worktrees allow to maintain multiple working trees in the filesystem linked to
the same repository (shared .git folder).</p>
<pre><code class="language-markdown"> git worktree add <path> .............. create a tree at <path> with a new branch
checked out (bname is basename of <path>)
git worktree add <path> <bname> ...... create a tree at <path> from existing <bname>
git worktree list .................... list existing work trees
git worktree remove <tree> ........... remove work tree
git worktree prune ................... remove stale bookkeeping files
</code></pre>
<h2 id="log--commit-history"><a class="header" href="#log--commit-history">Log & Commit History</a></h2>
<pre><code class="language-markdown"> git log --oneline ......... shows log in single line per commit -> alias for
'--pretty=oneline --abbrev-commit'
git log --graph ........... text based graph of commit history
git log --decorate ........ decorate log with REFs
git log -p <file> ......... show commit history + diffs for <file>
git log --oneline <file> .. show commit history for <file> in compact format
</code></pre>
<h2 id="diff--commit-info"><a class="header" href="#diff--commit-info">Diff & Commit Info</a></h2>
<pre><code class="language-markdown"> git diff <commit>..<commit> [<file>] .... show changes between two arbitrary
commits. If one <commit> is omitted
it is if HEAD is specified.
git diff -U$(wc -l <file>) <file> ....... shows complete file with diffs
instead of usual diff snippets
git diff --staged ....................... show diffs of staged files
git show --stat <commit> ................ show files changed by <commit>
git show <commit> [<file>] .............. show diffs for <commit>
git show <commit>:<file> ................ show <file> at <commit>
</code></pre>
<h2 id="patching"><a class="header" href="#patching">Patching</a></h2>
<pre><code class="language-markdown"> git format-patch <opt> <since>/<revision range>
opt:
-N ................... use [PATCH] instead [PATCH n/m] in subject when
generating patch description (for patches spanning
multiple commits)
--start-number <n> ... start output file generation with <n> as start
number instead '1'
since spcifier:
-3 .................. e.g: create a patch from last three commits
<commit hash> ....... create patch with commits starting after <commit hash>
git am <patch> ......... apply patch and create a commit for it
git apply --stat <PATCH> ... see which files the patch would change
git apply --check <PATCH> .. see if the patch can be applied cleanly
git apply [-3] <PATCH> ..... apply the patch locally without creating a commit,
if the patch does not cleanly apply -3 allows for
a 3-way merge
# eg: generate patches for each commit from initial commit on
git format-patch -N $(git rev-list --max-parents=0 HEAD)
# generate single patch file from a certain commit/ref
git format-patch <COMMIT/REF> --stdout > my-patch.patch
</code></pre>
<h2 id="resetting"><a class="header" href="#resetting">Resetting</a></h2>
<pre><code class="language-markdown"> git reset [opt] <ref|commit>
opt:
--mixed .................... resets index, but not working tree
--hard ..................... matches the working tree and index to that
of the tree being switched to any changes to
tracked files in the working tree since
<commit> are lost
git reset HEAD <file> .......... remove file from staging
git reset --soft HEAD~1 ........ delete most recent commit but keep work
git reset --hard HEAD~1 ........ delete most recent commit and delete work
</code></pre>
<h2 id="submodules"><a class="header" href="#submodules">Submodules</a></h2>
<pre><code class="language-markdown"> git submodule add <url> [<path>] .......... add new submodule to current project
git clone --recursive <url> ............... clone project and recursively all
submodules (same as using
'git submodule update --init
--recursive' after clone)
git submodule update --init --recursive ... checkout submodules recursively
using the commit listed in the
super-project (in detached HEAD)
git submodule update --remote <submod> .... fetch & merge remote changes for
<submod>, this will pull
origin/HEAD or a branch specified
for the submodule
git diff --submodule ...................... show commits that are part of the
submodule diff
</code></pre>
<h2 id="inspection"><a class="header" href="#inspection">Inspection</a></h2>
<pre><code class="language-markdown"> git ls-tree [-r] <ref> .... show git tree for <ref>, -r to recursively ls sub-trees
git show <obj> ............ show <obj>
git cat-file -p <obj> ..... print content of <obj>
</code></pre>
<h2 id="revision-specifier"><a class="header" href="#revision-specifier">Revision Specifier</a></h2>
<pre><code class="language-markdown"> HEAD ........ last commit
HEAD~1 ...... last commit-1
HEAD~N ...... last commit-N (linear backwards when in tree structure, check
difference between HEAD^ and HEAD~)
git rev-list --max-parents=0 HEAD ........... first commit
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="awk1"><a class="header" href="#awk1">awk(1)</a></h1>
<pre><code class="language-markdown">awk [opt] program [input]
-F <sepstr> field separator string (can be regex)
program awk program
input file or stdin if not file given
</code></pre>
<h2 id="input-processing"><a class="header" href="#input-processing">Input processing</a></h2>
<p>Input is processed in two stages:</p>
<ol>
<li>Splitting input into a sequence of <code>records</code>.
By default split at <code>newline</code> character, but can be changed via the
builtin <code>RS</code> variable.</li>
<li>Splitting a <code>record</code> into <code>fields</code>. By default strings without <code>whitespace</code>,
but can be changed via the builtin variable <code>FS</code> or command line option
<code>-F</code>.</li>
</ol>
<p>Fields are accessed as follows:</p>
<ul>
<li><code>$0</code> whole <code>record</code></li>
<li><code>$1</code> field one</li>
<li><code>$2</code> field two</li>
<li>...</li>
</ul>
<h2 id="program"><a class="header" href="#program">Program</a></h2>
<p>An <code>awk</code> program is composed of pairs of the form:</p>
<pre><code class="language-markdown">pattern { action }
</code></pre>
<p>The program is run against each <code>record</code> in the input stream. If a <code>pattern</code>
matches a <code>record</code> the corresponding <code>action</code> is executed and can access the
<code>fields</code>.</p>
<pre><code class="language-markdown">INPUT
|
v
record ----> ∀ pattern matched
| |
v v
fields ----> run associated action
</code></pre>
<p>Any valid awk <code>expr</code> can be a <code>pattern</code>.</p>
<p>An example is the regex pattern <code>/abc/ { print $1 }</code> which prints the first
field if the record matches the regex <code>/abc/</code>. This form is actually a short
version for <code>$0 ~ /abc/ { print $1 }</code>, see the regex comparison operator
below.</p>
<h3 id="special-pattern"><a class="header" href="#special-pattern">Special pattern</a></h3>
<p>awk provides two special patterns, <code>BEGIN</code> and <code>END</code>, which can be used
multiple times. Actions with those patterns are <strong>executed exactly once</strong>.</p>
<ul>
<li><code>BEGIN</code> actions are run before processing the first record</li>
<li><code>END</code> actions are run after processing the last record</li>
</ul>
<h3 id="special-variables"><a class="header" href="#special-variables">Special variables</a></h3>
<ul>
<li><code>RS</code> <em>record separator</em>: first char is the record separator, by default
<newline></li>
<li><code>FS</code> <em>field separator</em>: regex to split records into fields, by default
<space></li>
<li><code>NR</code> <em>number record</em>: number of current record</li>
<li><code>NF</code> <em>number fields</em>: number of fields in the current record</li>
</ul>
<h3 id="special-statements--functions"><a class="header" href="#special-statements--functions">Special statements & functions</a></h3>
<ul>
<li>
<p><code>printf "fmt", args...</code></p>
<p>Print format string, args are comma separated.</p>
<ul>
<li><code>%s</code> string</li>
<li><code>%d</code> decimal</li>
<li><code>%x</code> hex</li>
<li><code>%f</code> float</li>
</ul>
<p>Width can be specified as <code>%Ns</code>, this reserves <code>N</code> chars for a string.
For floats one can use <code>%N.Mf</code>, <code>N</code> is the total number including <code>.</code> and
<code>M</code>.</p>
</li>
<li>
<p><code>sprintf("fmt", expr, ...)</code></p>
<p>Format the expressions according to the format string. Similar as <code>printf</code>,
but this is a function and return value can be assigned to a variable.</p>
</li>
<li>
<p><code>strftime("fmt")</code></p>
<p>Print time stamp formatted by <code>fmt</code>.</p>
<ul>
<li><code>%Y</code> full year (eg 2020)</li>
<li><code>%m</code> month (01-12)</li>
<li><code>%d</code> day (01-31)</li>
<li><code>%F</code> alias for <code>%Y-%m-%d</code></li>
<li><code>%H</code> hour (00-23)</li>
<li><code>%M</code> minute (00-59)</li>
<li><code>%S</code> second (00-59)</li>
<li><code>%T</code> alias for <code>%H:%M:%S</code></li>
</ul>
</li>
<li>
<p><code>S ~ R</code>, <code>S !~ R</code></p>
<p>The regex comparison operator, where the former returns true if the string
<code>S</code> matches the regex <code>R</code>, and the latter is the negated form.
The regex can be either a
<a href="https://www.gnu.org/software/gawk/manual/html_node/Regexp-Usage.html">constant</a>
or <a href="https://www.gnu.org/software/gawk/manual/html_node/Computed-Regexps.html">dynamic</a>
regex.</p>
</li>
</ul>
<h2 id="examples"><a class="header" href="#examples">Examples</a></h2>
<h3 id="filter-records"><a class="header" href="#filter-records">Filter records</a></h3>
<pre><code class="language-bash">awk 'NR%2 == 0 { print $0 }' <file>
</code></pre>
<p>The pattern <code>NR%2 == 0</code> matches every second record and the action <code>{ print $0 }</code>
prints the whole record.</p>
<h3 id="negative-patterns"><a class="header" href="#negative-patterns">Negative patterns</a></h3>
<pre><code class="language-bash">awk '!/^#/ { print $1 }' <file>
</code></pre>
<p>Matches records not starting with <code>#</code>.</p>
<h3 id="range-patterns"><a class="header" href="#range-patterns">Range patterns</a></h3>
<pre><code class="language-bash">echo -e "a\nFOO\nb\nc\nBAR\nd" | \
awk '/FOO/,/BAR/ { print }'
</code></pre>
<p><code>/FOO/,/BAR/</code> define a range pattern of <code>begin_pattern, end_pattern</code>. When
<code>begin_pattern</code> is matched the range is <strong>turned on</strong> and when the
<code>end_pattern</code> is matched the range is <strong>turned off</strong>. This matches every record
in the range <em>inclusive</em>.</p>
<p>An <em>exclusive</em> range must be handled explicitly, for example as follows.</p>
<pre><code class="language-bash">echo -e "a\nFOO\nb\nc\nBAR\nd" | \
awk '/FOO/,/BAR/ { if (!($1 ~ "FOO") && !($1 ~ "BAR")) { print } }'
</code></pre>
<h3 id="access-last-fields-in-records"><a class="header" href="#access-last-fields-in-records">Access last fields in records</a></h3>
<pre><code class="language-bash">echo 'a b c d e f' | awk '{ print $NF $(NF-1) }'
</code></pre>
<p>Access last fields with arithmetic on the <code>NF</code> number of fields variable.</p>
<h3 id="split-on-multiple-tokens"><a class="header" href="#split-on-multiple-tokens">Split on multiple tokens</a></h3>
<pre><code class="language-bash">echo 'a,b;c:d' | awk -F'[,;:]' '{ printf "1=%s | 4=%s\n", $1, $4 }'
</code></pre>
<p>Use regex as field separator.</p>
<h3 id="capture-in-variables"><a class="header" href="#capture-in-variables">Capture in variables</a></h3>
<pre><code class="language-bash"># /proc/<pid>/status
# Name: cat
# ...
# VmRSS: 516 kB
# ...
for f in /proc/*/status; do
cat $f | awk '
/^VmRSS/ { rss = $2/1024 }
/^Name/ { name = $2 }
END { printf "%16s %6d MB\n", name, rss }';
done | sort -k2 -n
</code></pre>
<p>We capture values from <code>VmRSS</code> and <code>Name</code> into variables and print them at the
<code>END</code> once processing all records is done.</p>
<h3 id="capture-in-array"><a class="header" href="#capture-in-array">Capture in array</a></h3>
<pre><code class="language-bash">echo 'a 10
b 2
b 4
a 1' | awk '{
vals[$1] += $2
cnts[$1] += 1
}
END {
for (v in vals)
printf "%s %d\n", v, vals[v] / cnts [v]
}'
</code></pre>
<p>Capture keys and values from different columns and some up the values.
At the <code>END</code> we compute the average of each key.</p>
<h3 id="run-shell-command-and-capture-output"><a class="header" href="#run-shell-command-and-capture-output">Run shell command and capture output</a></h3>
<pre><code class="language-bash">cat /proc/1/status | awk '
/^Pid/ {
"ps --no-header -o user " $2 | getline user;
print user
}'
</code></pre>
<p>We build a <code>ps</code> command line and capture the first line of the processes output
in the <code>user</code> variable and then print it.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="emacs1"><a class="header" href="#emacs1">emacs(1)</a></h1>
<h2 id="help"><a class="header" href="#help">help</a></h2>
<pre><code class="language-markdown"> C-h ? list available help modes
C-h e show message output (`*Messages*` buffer)
C-h f describe function
C-h v describe variable
C-h w describe which key invoke function (where-is)
C-h c <KEY> print command bound to <KEY>
C-h k <KEY> describe command bound to <KEY>
C-h b list buffer local key-bindings
<kseq> C-h list possible key-bindings with <kseq>
eg C-x C-h -> list key-bindings beginning with C-x
</code></pre>
<h2 id="package-manager"><a class="header" href="#package-manager">package manager</a></h2>
<pre><code class="language-markdown"> key fn description
------------------------------------------------
package-refresh-contents refresh package list
package-list-packages list available/installed packages
`U x` to mark packages for Upgrade & eXecute
</code></pre>
<h2 id="window"><a class="header" href="#window">window</a></h2>
<pre><code class="language-markdown"> key fn description
----------------------------------------------
C-x 0 delete-window kill focused window
C-x 1 delete-other-windows kill all other windows
C-x 2 split-window-below split horizontal
C-x 3 split-window-right split vertical
C-x o other-window other window (cycle)
</code></pre>
<h2 id="buffer"><a class="header" href="#buffer">buffer</a></h2>
<pre><code class="language-markdown"> key fn description
---------------------------------------------
C-x C-q read-only-mode toggle read-only mode for buffer
C-x k kill-buffer kill buffer
C-x s save-some-buffers save buffer
C-x w write-file write buffer (save as)
C-x b switch-to-buffer switch buffer
C-x C-b list-buffers buffer list
</code></pre>
<h2 id="ibuffer"><a class="header" href="#ibuffer">ibuffer</a></h2>
<p>Builtin advanced buffer selection mode</p>
<pre><code class="language-markdown"> key fn description
--------------------------------------
ibuffer enter buffer selection
h ibuffer help
o open buffer in other window
C-o open buffer in other window keep focus in ibuffer
s a sort by buffer name
s f sort by file name
s v sort by last viewed
s v sort by major mode
, cycle sorting mode
= compare buffer against file on disk (if file is dirty `*`)
/m filter by major mode
/n filter by buffer name
/f filter by file name
// remove all filter
/g create filter group
/\ remove all filter groups
</code></pre>
<h2 id="isearch"><a class="header" href="#isearch">isearch</a></h2>
<pre><code class="language-markdown"> key fn description
-------------------------------------------------
C-s isearch-forward search forward from current position (C-s to go to next match)
C-r isearch-backward search backwards from current position (C-r to go to next match)
C-w isearch-yank-word-or-char feed next word to current search (extend)
M-p isearch-ring-advance previous search input
M-n isearch-ring-retreat next search input
</code></pre>
<h2 id="occur"><a class="header" href="#occur">occur</a></h2>
<pre><code class="language-markdown"> key fn description
-----------------------------------
M-s o occur get matches for regexp in buffer
use during `isearch` to use current search term
C-n goto next line
C-p goto previous line
o open match in other window
C-o open match in other window keep focus in ibuffer
</code></pre>
<pre><code class="language-markdown"> key fn description
---------------------------------------------------------
multi-occur-in-matching-buffers run occur in buffers matching regexp
</code></pre>
<h2 id="grep"><a class="header" href="#grep">grep</a></h2>
<pre><code class="language-markdown"> key fn description
-----------------------------------
rgrep recursive grep
find-grep run find-grep result in *grep* buffer
n/p navigate next/previous match in *grep* buffer
q quit *grep* buffer
</code></pre>
<h2 id="yankpaste"><a class="header" href="#yankpaste">yank/paste</a></h2>
<pre><code class="language-markdown"> key fn description
---------------------------------------------
C-<SPACE> set-mark-command set start mark to select text
M-w kill-ring-save copy selected text
C-w kill-region kill selected text
C-y yank paste selected text
M-y yank-pop cycle through kill-ring (only after paste)
</code></pre>
<h2 id="register"><a class="header" href="#register">register</a></h2>
<pre><code class="language-markdown"> key fn description
------------------------------------------------
C-x r s <reg> copy-to-register save region in register <reg>
C-x r i <reg> insert-register insert content of register <reg>
</code></pre>
<h2 id="blockrect"><a class="header" href="#blockrect">block/rect</a></h2>
<pre><code class="language-markdown"> key fn description
------------------------------------------------
C-x <SPC> rectangle-mark-mode activate rectangle-mark-mode
string-rectangle insert text in marked rect
</code></pre>
<h2 id="mass-edit"><a class="header" href="#mass-edit">mass edit</a></h2>
<pre><code class="language-markdown"> key fn description
------------------------------------------------
C-x h mark-whole-buffer mark whole buffer
delete-matching-line delete lines matching regex
M-% query-replace search & replace
C-M-% query-replace-regexp search & replace regex
</code></pre>
<h2 id="narrow"><a class="header" href="#narrow">narrow</a></h2>
<pre><code class="language-markdown"> key fn description
---------------------------------------------
C-x n n narrow-to-region show only focused region (narrow)
C-x n w widen show whole buffer (wide)
</code></pre>
<h2 id="org"><a class="header" href="#org">org</a></h2>
<pre><code class="language-markdown"> key fn description
------------------------------------
M-up/M-down re-arrange items in same hierarchy
M-left/M-right change item hierarchy
C-RET create new item below current
C-S-RET create new TODO item below current
S-left/S-right cycle TODO states
</code></pre>
<h3 id="org-source"><a class="header" href="#org-source">org source</a></h3>
<pre><code class="language-markdown"> key fn description
------------------------------
<s TAB generate a source block
C-c ' edit source block (in lang specific buffer)
C-c C-c eval source block
</code></pre>
<h2 id="comapny"><a class="header" href="#comapny">comapny</a></h2>
<pre><code class="language-markdown"> key fn description
-------------------------------
C-s search through completion candidates
C-o filter completion candidates based on search term
<f1> get doc for completion condidate
M-<digit> select completion candidate
</code></pre>
<h2 id="tags-1"><a class="header" href="#tags-1">tags</a></h2>
<p>To generate <code>etags</code> using <code>ctags</code></p>
<pre><code class="language-markdown"> ctags -R -e . generate emacs tag file (important `-e`)
</code></pre>
<p>Navigate using tags</p>
<pre><code class="language-markdown"> key fn description
-----------------------------------------------
xref-find-definitions find definition of tag
xref-find-apropos find symbols matching regexp
xref-find-references find references of tag
</code></pre>
<h2 id="lisp"><a class="header" href="#lisp">lisp</a></h2>
<pre><code class="language-markdown"> key fn description
------------------------------
ielm open interactive elips shell
</code></pre>
<p>In <code>lisp-interaction-mode</code> (<code>*scratch*</code> buffer by defult)</p>
<pre><code class="language-markdown"> key fn description
--------------------------------------------------------
C-j eval-print-last-sexp evaluate & print preceeding lisp expr
C-x C-e eval-last-sexp evaluate lisp expr
C-u C-x C-e eval-last-sexp evaluate & print
</code></pre>
<h2 id="ido"><a class="header" href="#ido">ido</a></h2>
<p>Builtin fuzzy completion mode (eg buffer select, dired, ...).</p>
<pre><code class="language-markdown"> key fn description
------------------------------------------
ido-mode toggle ido mode
<Left>/<Right> cycle through available competions
<RET> select completion
</code></pre>
<h2 id="evil"><a class="header" href="#evil">evil</a></h2>
<pre><code class="language-markdown"> key fn description
--------------------------
C-z toggle emacs/evil mode
C-^ toggle between previous and current buffer
C-p after paste cycle kill-ring back
C-n after paste cycle kill-ring forward
</code></pre>
<h2 id="dired"><a class="header" href="#dired">dired</a></h2>
<pre><code class="language-markdown"> key fn description
--------------------------
i open sub-dir in same buffer
+ create new directory
C copy file/dir
q quit
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="gpg1"><a class="header" href="#gpg1">gpg(1)</a></h1>
<pre><code>gpg
-o|--output Specify output file
-a|--armor Create ascii output
-u|--local-user <name> Specify key for signing
-r|--recipient Encrypt for user
</code></pre>
<h2 id="generate-new-keypair"><a class="header" href="#generate-new-keypair">Generate new keypair</a></h2>
<pre><code class="language-bash">gpg --full-generate-key
</code></pre>
<h2 id="list-keys"><a class="header" href="#list-keys">List keys</a></h2>
<pre><code>gpg -k / --list-key # public keys
gpg -K / --list-secret-keys # secret keys
</code></pre>
<h2 id="edit-keys"><a class="header" href="#edit-keys">Edit keys</a></h2>
<pre><code class="language-bash">gpg --edit-key <KEY ID>
</code></pre>
<p>Gives prompt to modify <code>KEY ID</code>, common commands:</p>
<pre><code class="language-bash">help show help
save save & quit
list list keys and user IDs
key <N> select subkey <N>
uid <N> select user ID <N>
expire change expiration of selected key
adduid add user ID
deluid delete selected user ID
addkey add subkey
delkey delete selected subkey
</code></pre>
<h2 id="export--import-keys"><a class="header" href="#export--import-keys">Export & Import Keys</a></h2>
<pre><code class="language-bash">gpg --export --armor --output <KEY.PUB> <KEY ID>
gpg --export-secret-key --armor --output <KEY.PUB> <KEY ID>
gpg --import <FILE>
</code></pre>
<h2 id="search--send-keys"><a class="header" href="#search--send-keys">Search & Send keys</a></h2>
<pre><code class="language-bash">gpg --keyserver <SERVER> --send-keys <KEY ID>
gpg --keyserver <SERVER> --search-keys <KEY ID>
</code></pre>
<h2 id="encrypt-passphrase"><a class="header" href="#encrypt-passphrase">Encrypt (passphrase)</a></h2>
<p>Encrypt file using <code>passphrase</code> and write encrypted data to <code><file>.gpg</code>.</p>
<pre><code class="language-bash">gpg --symmetric <file>
# Decrypt using passphrase
gpg -o <file> --decrypt <file>.gpg
</code></pre>
<h2 id="encrypt-public-key"><a class="header" href="#encrypt-public-key">Encrypt (public key)</a></h2>
<p>Encrypt file with <code>public key</code> of specified <code>recipient</code> and write encrypted
data to <code><file>.gpg</code>.</p>
<pre><code class="language-bash">gpg --encrypt -r foo@bar.de <file>
# Decrypt at foos side (private key required)
gpg -o <file> --decrypt <file>.gpg
</code></pre>
<h2 id="signing"><a class="header" href="#signing">Signing</a></h2>
<p>Generate a signed file and write to <code><file>.gpg</code>.</p>
<pre><code class="language-bash"># Sign with private key of foo@bar.de
gpg --sign -u foor@bar.de <file>
# Verify with public key of foo@bar.de
gpg --verify <file>
# Extract content from signed file
gpg -o <file> --decrypt <file>.gpg
</code></pre>
<blockquote>
<p>Without <code>-u</code> use first private key in list <code>gpg -K</code> for signing.</p>
</blockquote>
<p>Files can also be <code>signed</code> and <code>encrypted</code> at once, gpg will first sign the
file and then encrypt it.</p>
<pre><code class="language-bash">gpg --sign --encrypt -r <recipient> <file>
</code></pre>
<h2 id="signing-detached"><a class="header" href="#signing-detached">Signing (detached)</a></h2>
<p>Generate a <code>detached</code> signature and write to <code><file>.asc</code>.
Send <code><file>.asc</code> along with <code><file></code> when distributing.</p>
<pre><code class="language-bash">gpg --detach-sign --armor -u foor@bar.de <file>
# Verify
gpg --verify <file>.asc <file>
</code></pre>
<blockquote>
<p>Without <code>-u</code> use first private key in list <code>gpg -K</code> for signing.</p>
</blockquote>
<h2 id="abbreviations"><a class="header" href="#abbreviations">Abbreviations</a></h2>
<ul>
<li><code>sec</code> secret key</li>
<li><code>ssb</code> secret subkey</li>
<li><code>pub</code> public key</li>
<li><code>sub</code> public subkey</li>
</ul>
<h2 id="keyservers"><a class="header" href="#keyservers">Keyservers</a></h2>
<ul>
<li>http://pgp.mit.edu</li>
<li>http://keyserver.ubuntu.com</li>
<li>hkps://pgp.mailbox.org</li>
</ul>
<h2 id="examples-1"><a class="header" href="#examples-1">Examples</a></h2>
<h3 id="list-basic-key-information-from-file-with-long-keyids"><a class="header" href="#list-basic-key-information-from-file-with-long-keyids">List basic key information from file with long keyids</a></h3>
<pre><code class="language-bash">gpg --keyid-format 0xlong <key.asc>
</code></pre>
<h3 id="extend-expiring-key"><a class="header" href="#extend-expiring-key">Extend expiring key</a></h3>
<pre><code class="language-bash">gpg --edit-key <key id>
# By default we are on the primary key, can switch to sub key.
gpg> key 1
# Update the expire date.
gpg> expire
gpg> save
# Update keyserver(s) and/or export new pub keyfile.
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="gdb1"><a class="header" href="#gdb1">gdb(1)</a></h1>
<h1 id="cli"><a class="header" href="#cli">CLI</a></h1>
<pre><code class="language-markdown"> gdb [opts] [prg [-c coredump | -p pid]]
gdb [opts] --args prg <prg-args>
opts:
-p <pid> attach to pid
-c <coredump> use <coredump>
-x <file> execute script <file> before prompt
-ex <cmd> execute command <cmd> before prompt
--tty <tty> set I/O tty for debugee
--batch run in batch mode, exit after processing options (eg used
for scripting)
</code></pre>
<h1 id="interactive-usage"><a class="header" href="#interactive-usage">Interactive usage</a></h1>
<h2 id="misc"><a class="header" href="#misc">Misc</a></h2>
<pre><code class="language-markdown"> tty <tty>
Set <tty> as tty for debugee.
Make sure nobody reads from target tty, easiest is to spawn a shell
and run following in target tty:
> while true; do sleep 1024; done
sharedlibrary [<regex>]
Load symbols of shared libs loaded by debugee. Optionally use <regex>
to filter libs for symbol loading.
display [/FMT] <expr>
Print <expr> every time debugee stops. Eg print next instr, see
examples below.
undisplay [<num>]
Delete display expressions either all or one referenced by <num>.
info display
List display expressions.
</code></pre>
<h2 id="breakpoints"><a class="header" href="#breakpoints">Breakpoints</a></h2>
<pre><code class="language-markdown"> break [-qualified] <sym> thread <tnum>
Set a breakpoint only for a specific thread.
-qualified: Treat <sym> as fully qualified symbol (quiet handy to set
breakpoints on C symbols in C++ contexts)
break <sym> if <cond>
Set conditional breakpoint (see examples below).
delete [<num>]
Delete breakpoint either all or one referenced by <num>.
info break
List breakpoints.
cond <bp> <cond>
Make existing breakpoint <bp> conditional with <cond>.
cond <bp>
Remove condition from breakpoint <bp>.
tbreak
Set temporary breakpoint, will be deleted when hit.
Same syntax as `break`.
rbreak <regex>
Set breakpoints matching <regex>, where matching internally is done
on: .*<regex>.*
command [<bp_list>]
Define commands to run after breakpoint hit. If <bp_list> is not
specified attach command to last created breakpoint. Command block
terminated with 'end' token.
<bp_list>: Space separates list, eg 'command 2 5-8' to run command
for breakpoints: 2,5,6,7,8.
save break <file>
Save breakpoints to <file>. Can be loaded with the `source` command.
</code></pre>
<h2 id="watchpoints"><a class="header" href="#watchpoints">Watchpoints</a></h2>
<pre><code class="language-markdown"> watch [-location|-l] <expr> [thread <tnum>]
Create a watchpoint for <expr>, will break if <expr> is written to.
Watchpoints respect scope of variables, -l can be used to watch the
memory location instead.
rwatch ...
Sets a read watchpoint, will break if <expr> is read from.
awatch ...
Sets an access watchpoint, will break if <expr> is written to or read
from.
</code></pre>
<h2 id="catchpoints"><a class="header" href="#catchpoints">Catchpoints</a></h2>
<pre><code class="language-markdown"> catch load [<regex>]
Stop when shared libraries are loaded, optionally specify a <regex>
to stop only on matches.
catch unload [<regex>]
Stop when shared libraries are unloaded, optionally specify a <regex>
to stop only on matches.
catch throw
Stop when an exception is thrown.
catch rethrow
Stop when an exception is rethrown.
catch catch
Stop when an exception is caught.
catch fork
Stop at calls to fork (also stops at clones, as some systems
implement fork via clone).
catch syscall [<syscall> <syscall> ..]
Stop at syscall. If no argument is given, stop at all syscalls.
Optionally give a list of syscalls to stop at.
</code></pre>
<h2 id="inspection-1"><a class="header" href="#inspection-1">Inspection</a></h2>
<pre><code class="language-markdown"> info functions [<regex>]
List functions matching <regex>. List all functions if no <regex>
provided.
info variables [<regex>]
List variables matching <regex>. List all variables if no <regex>
provided.
info register [<reg> <reg> ..]
Dump content of all registers or only the specified <reg>ister.
</code></pre>
<h2 id="signal-handling"><a class="header" href="#signal-handling">Signal handling</a></h2>
<pre><code class="language-markdown"> info handle [<signal>]
Print how to handle <signal>. If no <signal> specified print for all
signals.
handle <signal> <action>
Configure how gdb handles <signal> sent to debugee.
<action>:
stop/nostop Catch signal in gdb and break.
print/noprint Print message when gdb catches signal.
pass/nopass Pass signal down to debugee.
catch signal <signal>
Create a catchpoint for <signal>.
</code></pre>
<h2 id="multi-threading"><a class="header" href="#multi-threading">Multi-threading</a></h2>
<pre><code class="language-markdown">info thread
List all threads.
thread apply <id> [<id>] <command>
Run command on all threads listed by <id> (space separated list).
When 'all' is specified as <id> the <command> is run on all threads.
thread name <name>
The <name> for the current thread.
</code></pre>
<h2 id="multi-process"><a class="header" href="#multi-process">Multi-process</a></h2>
<pre><code class="language-markdown"> set follow-fork-mode <child | parent>
Specify which process to follow when debuggee makes a fork(2)
syscall.
set detach-on-frok <on | off>
Turn on/off detaching from new child processes (on by default).
Turning this off allows to debug multiple processes (inferiors) with
one gdb session.
info inferiors
List all processes gdb debugs.
inferior <id>
Switch to inferior with <id>.
</code></pre>
<h2 id="source-file-locations"><a class="header" href="#source-file-locations">Source file locations</a></h2>
<pre><code class="language-markdown"> dir <path>
Add <path> to the beginning of the searh path for source files.
show dir
Show current search path.
set substitute-path <from> <to>
Add substitution rule checked during source file lookup.
show substitute-path
Show current substitution rules.
</code></pre>
<h2 id="configuration"><a class="header" href="#configuration">Configuration</a></h2>
<pre><code class="language-markdown"> set disassembly-flavor <intel | att>
Set the disassembly style "flavor".
set pagination <on | off>
Turn on/off gdb's pagination.
set breakpoint pending <on | off | auto>
on: always set pending breakpoints.
off: error when trying to set pending breakpoints.
auto: interatively query user to set breakpoint.
set print pretty <on | off>
Turn on/off pertty printing of structures.
set style enabled <on | off>
Turn on/off styling (eg colored output).
set logging <on | off>
Enable output logging to file (default gdb.txt).
set logging file <fname>
Change output log file to <fname>
set logging redirect <on/off>
on: only log to file.
off: log to file and tty.
</code></pre>
<h1 id="text-user-interface-tui"><a class="header" href="#text-user-interface-tui">Text user interface (TUI)</a></h1>
<pre><code class="language-markdown"> C-x a Toggle UI.
C-l Redraw UI (curses UI can be messed up after the debugee prints to
stdout/stderr).
C-x o Change focus.
</code></pre>
<h1 id="user-commands-macros"><a class="header" href="#user-commands-macros">User commands (macros)</a></h1>
<p>Gdb allows to create & document user commands as follows:</p>
<pre><code class="language-markdown"> define <cmd>
# cmds
end
document <cmd>
# docu
end
</code></pre>
<p>To get all user commands or documentations one can use:</p>
<pre><code class="language-markdown"> help user-defined
help <cmd>
</code></pre>
<h1 id="hooks"><a class="header" href="#hooks">Hooks</a></h1>
<p>Gdb allows to create two types of command <code>hooks</code></p>
<ul>
<li><code>hook-</code> will be run before <code><cmd></code></li>
<li><code>hookpost-</code> will be run after <code><cmd></code></li>
</ul>
<pre><code class="language-markdown"> define hook-<cmd>
# cmds
end
define hookpost-<cmd>
# cmds
end
</code></pre>
<h1 id="examples-2"><a class="header" href="#examples-2">Examples</a></h1>
<h2 id="automatically-print-next-instr"><a class="header" href="#automatically-print-next-instr">Automatically print next instr</a></h2>
<p>When ever the debugee stops automatically print the memory at the current
instruction pointer (<code>$rip</code> x86) and format as instruction <code>/i</code>.</p>
<pre><code class="language-markdown"> # rip - x86
display /i $rip
# step instruction, after the step the next instruction is automatically printed
si
</code></pre>
<h2 id="conditional-breakpoints"><a class="header" href="#conditional-breakpoints">Conditional breakpoints</a></h2>
<p>Create conditional breakpoints for a function <code>void foo(int i)</code> in the debugee.</p>
<pre><code class="language-markdown"> # Create conditional breakpoint
b foo if i == 42
b foo # would create bp 2
# Make existing breakpoint conditional
cond 2 i == 7
</code></pre>
<h2 id="set-breakpoint-on-all-threads-except-one"><a class="header" href="#set-breakpoint-on-all-threads-except-one">Set breakpoint on all threads except one</a></h2>
<p>Create conditional breakpoint using the <code>$_thread</code> <a href="https://sourceware.org/gdb/onlinedocs/gdb/Convenience-Vars.html#Convenience-Vars">convenience
variable</a>.</p>
<pre><code class="language-markdown"> # Create conditional breakpoint on all threads except thread 12.
b foo if $_thread != 12
</code></pre>
<h2 id="catch-sigsegv-and-execute-commands"><a class="header" href="#catch-sigsegv-and-execute-commands">Catch SIGSEGV and execute commands</a></h2>
<p>This creates a <code>catchpoint</code> for the <code>SIGSEGV</code> signal and attached the <code>command</code>
to it.</p>
<pre><code class="language-markdown"> catch signal SIGSEGV
command
bt
c
end
</code></pre>
<h2 id="run-backtrace-on-thread-1-batch-mode"><a class="header" href="#run-backtrace-on-thread-1-batch-mode">Run <code>backtrace</code> on thread 1 (batch mode)</a></h2>
<pre><code class="language-markdown"> gdb --batch -ex 'thread 1' -ex 'bt' -p <pid>
</code></pre>
<h2 id="script-gdb-for-automating-debugging-sessions"><a class="header" href="#script-gdb-for-automating-debugging-sessions">Script gdb for automating debugging sessions</a></h2>
<p>To script gdb add commands into a file and pass it to gdb via <code>-x</code>.
For example create <code>run.gdb</code>:</p>
<pre><code class="language-markdown"> set pagination off
break mmap
command
info reg rdi rsi rdx
bt
c
end
#initial drop
c
</code></pre>
<p>This script can be used as:</p>
<pre><code class="language-markdown"> gdb --batch -x ./run.gdb -p <pid>
</code></pre>
<h2 id="hook-to-automatically-save-breakpoints-on-quit"><a class="header" href="#hook-to-automatically-save-breakpoints-on-quit">Hook to automatically save breakpoints on <code>quit</code></a></h2>
<pre><code class="language-markdown">define break-save
save breakpoint $arg0.gdb.bp
end
define break-load
source $arg0.gdb.bp
end
define hook-quit
break-save quit
end
</code></pre>
<h2 id="watchpoint-on-struct--class-member"><a class="header" href="#watchpoint-on-struct--class-member">Watchpoint on struct / class member</a></h2>
<p>A symbolic watchpoint defined on a member variable for debugging is only valid
as long as the expression is in scope. Once out of scope the watchpoint gets
deleted.</p>
<p>When debugging some memory corruption we want to keep the watchpoint even the
expression goes out of scope to find the location that overrides the variable
and introduces the corruption.</p>
<pre><code class="language-markdown">(gdb) l
1 struct S { int v; };
2
3 void set(struct S* s, int v) {
4 s->v = v;
5 }
6
7 int main() {
8 struct S s;
9 set(&s, 1);
10 set(&s, 2);
11 set(&s, 3);
...
(gdb) s
set (s=0x7fffffffe594, v=1) at test.c:4
4 s->v = v;
# Define a new watchpoint on the member of the struct. The expression however
# is only valid in the current functions scope.
(gdb) watch s->v
Hardware watchpoint 2: s->v
(gdb) c
Hardware watchpoint 2: s->v
Old value = 0
New value = 1
set (s=0x7fffffffe594, v=1) at test.c:5
5 }
# The watchpoint gets deleted as soon as we leave the function scope.
(gdb) c
Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
main () at test.c:10
10 set(&s, 2);
# Define the watchpoint on the location of the object to watch.
(gdb) watch -l s->v
# This is equivalent to the following.
(gdb) p &s->v
$1 = (int *) 0x7fffffffe594
# Define a watchpoint to the address of the member variable of the s instance.
# This of course only makes sense as long as the s instance is not moved in memory.
(gdb) watch *0x7fffffffe594
Hardware watchpoint 3: *0x7fffffffe594
(gdb) c
Hardware watchpoint 3: *0x7fffffffe594
Old value = 1
New value = 2
set (s=0x7fffffffe594, v=2) at test.c:5
5 }
(gdb) c
Hardware watchpoint 3: *0x7fffffffe594
Old value = 2
New value = 3
set (s=0x7fffffffe594, v=3) at test.c:5
5 }
</code></pre>
<h1 id="know-bugs"><a class="header" href="#know-bugs">Know Bugs</a></h1>
<h2 id="workaround-command--finish-bug"><a class="header" href="#workaround-command--finish-bug">Workaround <code>command + finish</code> bug</a></h2>
<p>When using <code>finish</code> inside a <code>command</code> block, commands after <code>finish</code> are not
executed. To workaround that bug one can create a wrapper function which calls
<code>finish</code>.</p>
<pre><code class="language-markdown"> define handler
bt
finish
info reg rax
end
command
handler
end
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="gdbserver1"><a class="header" href="#gdbserver1">gdbserver(1)</a></h1>
<h1 id="cli-1"><a class="header" href="#cli-1">CLI</a></h1>
<pre><code class="language-markdown"> gdbserver [opts] comm prog [args]
opts:
--disable-randomization
--no-disable-randomization
--wrapper W --
comm:
host:port
tty
</code></pre>
<h2 id="example-4"><a class="header" href="#example-4">Example</a></h2>
<pre><code class="language-markdown"># Start gdbserver.
gdbserver localhost:1234 /bin/ls
# Attach gdb.
gdb -ex 'target remote localhost:1234'
</code></pre>
<h2 id="wrapper-example-set-environment-variables-just-for-the-debugee"><a class="header" href="#wrapper-example-set-environment-variables-just-for-the-debugee">Wrapper example: Set environment variables just for the debugee</a></h2>
<p>Set <code>env</code> as execution wrapper with some variables.
The wrapper will be executed before the debugee.</p>
<pre><code class="language-markdown">gdbserver --wrapper env FOO=123 BAR=321 -- :12345 /bin/ls
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="radare21"><a class="header" href="#radare21">radare2(1)</a></h1>
<h2 id="print"><a class="header" href="#print">print</a></h2>
<pre><code class="language-markdown">
pd <n> [@ <addr>] # print disassembly for <n> instructions
# with optional temporary seek to <addr>
</code></pre>
<h2 id="flags"><a class="header" href="#flags">flags</a></h2>
<pre><code class="language-markdown"> fs # list flag-spaces
fs <fs> # select flag-space <fs>
f # print flags of selected flag-space
</code></pre>
<h2 id="help-1"><a class="header" href="#help-1">help</a></h2>
<pre><code class="language-markdown"> ?*~<kw> # '?*' list all commands and '~' grep for <kw>
?*~... # '..' less mode /'...' interactive search
</code></pre>
<h2 id="relocation"><a class="header" href="#relocation">relocation</a></h2>
<pre><code class="language-markdown"> > r2 -B <baddr> <exe> # open <exe> mapped to addr <baddr>
oob <addr> # reopen current file at <baddr>
</code></pre>
<h1 id="examples-3"><a class="header" href="#examples-3">Examples</a></h1>
<h2 id="patch-file-alter-bytes"><a class="header" href="#patch-file-alter-bytes">Patch file (alter bytes)</a></h2>
<pre><code class="language-markdown"> > r2 [-w] <file>
oo+ # re-open for write if -w was not passed
s <addr> # seek to position
wv <data> # write 4 byte (dword)
</code></pre>
<h2 id="assemble--disassmble-rasm2"><a class="header" href="#assemble--disassmble-rasm2">Assemble / Disassmble (rasm2)</a></h2>
<pre><code class="language-markdown"> rasm2 -L # list supported archs
> rasm2 -a x86 'mov eax, 0xdeadbeef'
b8efbeadde
> rasm2 -a x86 -d "b8efbeadde"
mov eax, 0xdeadbeef
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="qemu1"><a class="header" href="#qemu1">qemu(1)</a></h1>
<p>All the examples & notes use <code>qemu-system-x86_64</code> but in most cases
this can be swapped with the system emulator for other architectures.</p>
<h2 id="keybindings-1"><a class="header" href="#keybindings-1">Keybindings</a></h2>
<p>Graphic mode:</p>
<pre><code class="language-markdown">Ctrl+Alt+g release mouse capture from VM
Ctrl+Alt+1 switch to display of VM
Ctrl+Alt+2 switch to qemu monitor
</code></pre>
<p>No graphic mode:</p>
<pre><code class="language-markdown">Ctrl+a h print help
Ctrl+a x exit emulator
Ctrl+a c switch between monitor and console
</code></pre>
<h2 id="vm-config-snippet"><a class="header" href="#vm-config-snippet">VM config snippet</a></h2>
<p>Following command-line gives a good starting point to assemble a VM:</p>
<pre><code>qemu-system-x86_64 \
-cpu host -enable-kvm -smp 4 \
-m 8G \
-vga virtio -display sdl,gl=on \
-boot menu=on \
-cdrom <iso> \
-hda <disk> \
-device qemu-xhci,id=xhci \
-device usb-host,bus=xhci.0,vendorid=0x05e1,productid=0x0408,id=capture-card
</code></pre>
<h3 id="cpu--ram"><a class="header" href="#cpu--ram">CPU & RAM</a></h3>
<pre><code class="language-bash"># Emulate host CPU in guest VM, enabling all supported host featured (requires KVM).
# List available CPUs `qemu-system-x86_64 -cpu help`.
-cpu host
# Enable KVM instead software emulation.
-enable-kvm
# Configure number of guest CPUs.
-smp <N>
# Configure size of guest RAM.
-m 8G
</code></pre>
<h3 id="graphic--display"><a class="header" href="#graphic--display">Graphic & Display</a></h3>
<pre><code class="language-bash"># Use sdl window as display and enable openGL context.
-display sdl,gl=on
# Use vnc server as display (eg on display `:42` here).
-display vnc=localhost:42
# Confifure virtio as 3D video graphic accelerator (requires virgl in guest).
-vga virtio
</code></pre>
<h3 id="boot-menu"><a class="header" href="#boot-menu">Boot Menu</a></h3>
<pre><code class="language-bash"># Enables boot menu to select boot device (enter with `ESC`).
-boot menu=on
</code></pre>
<h3 id="block-devices"><a class="header" href="#block-devices">Block devices</a></h3>
<pre><code class="language-bash"># Attach cdrom drive with iso to a VM.
-cdrom <iso>
# Attach disk drive to a VM.
-hda <disk>
# Generic way to configure & attach a drive to a VM.
-drive file=<file>,format=qcow2
</code></pre>
<h4 id="create-a-disk-with-qemu-img"><a class="header" href="#create-a-disk-with-qemu-img">Create a disk with <a href="https://qemu-project.gitlab.io/qemu/tools/qemu-img.html"><code>qemu-img</code></a></a></h4>
<p>To create a <code>qcow2</code> disk (qemu copy-on-write) of size <code>10G</code>:</p>
<pre><code class="language-bash">qemu-img create -f qcow2 disk.qcow2 10G
</code></pre>
<p>The disk does not contain any <code>partitions</code> or a <code>partition table</code>.
We can format the disk from <strong>within the <strong>guest</strong></strong> as following example:</p>
<pre><code class="language-bash"># Create `gpt` partition table.
sudo parted /dev/sda mktable gpt
# Create two equally sized primary partitions.
sudo parted /dev/sda mkpart primary 0% 50%
sudo parted /dev/sda mkpart primary 50% 100%
# Create filesystem on each partition.
sudo mkfs.ext3 /dev/sda1
sudo mkfs.ext4 /dev/sda2
lsblk -f /dev/sda
NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
sda
├─sda1 ext3 ....
└─sda2 ext4 ....
</code></pre>
<h3 id="usb"><a class="header" href="#usb">USB</a></h3>
<h4 id="host-controller"><a class="header" href="#host-controller">Host Controller</a></h4>
<pre><code class="language-bash"># Add XHCI USB controller to the VM (supports USB 3.0, 2.0, 1.1).
# `id=xhci` creates a usb bus named `xhci`.
-device qemu-xhci,id=xhci
</code></pre>
<h4 id="usb-device"><a class="header" href="#usb-device">USB Device</a></h4>
<pre><code class="language-bash"># Pass-through USB device from host identified by vendorid & productid and
# attach to usb bus `xhci.0` (defined with controller `id`).
-device usb-host,bus=xhci.0,vendorid=0x05e1,productid=0x0408
</code></pre>
<h2 id="debugging"><a class="header" href="#debugging">Debugging</a></h2>
<pre><code class="language-bash"># Open gdbstub on tcp `<port>` (`-s` shorthand for `-gdb tcp::1234`).
-gdb tcp::<port>
# Freeze guest CPU at startup and wait for debugger connection.
-S
</code></pre>
<h2 id="io-redirection-2"><a class="header" href="#io-redirection-2">IO redirection</a></h2>
<pre><code class="language-bash"># Create raw tcp server for `serial IO` and wait until a client connects
# before executing the guest.
-serial tcp:localhost:12345,server,wait
# Create telnet server for `serial IO` and wait until a client connects
# before executing the guest.
-serial telnet:localhost:12345,server,wait
# Configure redirection for the QEMU `mointor`, arguments similar to `-serial`
# above.
-monitor ...
</code></pre>
<blockquote>
<p>In <code>server</code> mode use <code>nowait</code> to execute guest without waiting for a client
connection.</p>
</blockquote>
<h2 id="network"><a class="header" href="#network">Network</a></h2>
<pre><code class="language-bash"># Redirect host tcp port `1234` to guest port `4321`.
-nic user,hostfwd=tcp:localhost:1234-:4321
</code></pre>
<h2 id="shared-drives"><a class="header" href="#shared-drives">Shared drives</a></h2>
<pre><code class="language-bash"># Attach a `virtio-9p-pci` device to the VM.
# The guest requires 9p support and can mount the shared drive as:
# mount -t 9p -o trans=virtio someName /mnt
-virtfs local,id=someName,path=<someHostPath>,mount_tag=someName,security_model=none
</code></pre>
<h2 id="debug-logging"><a class="header" href="#debug-logging">Debug logging</a></h2>
<pre><code class="language-bash"># List debug items.
-d help
# Write debug log to file instead stderr.
-D <file>
# Examples
-d in_asm Log executed guest instructions.
</code></pre>
<h2 id="tracing"><a class="header" href="#tracing">Tracing</a></h2>
<pre><code class="language-bash"># List name of all trace points.
-trace help
# Enable trace points matching pattern and optionally write trace to file.
-trace <pattern>[,file=<file>]
# Enable trace points for all events listed in the <events> file.
# File must contain one event/pattern per line.
-trace events=<events>
</code></pre>
<h2 id="vm-snapshots"><a class="header" href="#vm-snapshots">VM snapshots</a></h2>
<p>VM snapshots require that there is at least on <code>qcow2</code> disk attached to the VM
(<a href="https://qemu-project.gitlab.io/qemu/system/images.html#vm-005fsnapshots">VM Snapshots</a>).</p>
<p>Commands for qemu <a href="https://qemu-project.gitlab.io/qemu/system/monitor.html">Monitor</a> or <a href="https://qemu-project.gitlab.io/qemu/interop/qemu-qmp-ref.html">QMP</a>:</p>
<pre><code class="language-bash"># List available snapshots.
info snapshots
# Create/Load/Delete snapshot with name <tag>.
savevm <tag>
loadvm <tag>
delvm <tag>
</code></pre>
<p>The snapshot can also be directly specified when invoking qemu as:</p>
<pre><code class="language-bash">qemu-system-x86_64 \
-loadvm <tag> \
...
</code></pre>
<h2 id="vm-migration"><a class="header" href="#vm-migration">VM Migration</a></h2>
<p><code>Online</code> migration example:</p>
<pre><code class="language-bash"># Start machine 1 on host ABC.
qemu-system-x86_64 -monitor stdio -cdrom <iso>
# Prepare machine 2 on host DEF as migration target.
# Listen for any connection on port 12345.
qemu-system-x86_64 -monitor stdio -incoming tcp:0.0.0.0:12345
# Start migration from the machine 1 monitor console.
(qemu) migrate tcp:DEF:12345
</code></pre>
<p>Save to external file example:</p>
<pre><code class="language-bash">```bash
# Start machine 1.
qemu-system-x86_64 -monitor stdio -cdrom <iso>
# Save VM state to file.
(qemu) migrate "exec:gzip -c > vm.gz"
# Load VM from file.
qemu-system-x86_64 -monitor stdio -incoming "exec: gzip -d -c vm.gz"
</code></pre>
<blockquote>
<p>The migration source machine and the migration target machine should be
launched with the <strong>same</strong> parameters.</p>
</blockquote>
<h2 id="appendix-direct-kernel-boot"><a class="header" href="#appendix-direct-kernel-boot">Appendix: Direct <code>Kernel</code> boot</a></h2>
<p>Example command line to directly boot a <code>Kernel</code> with an <code>initrd</code> ramdisk.</p>
<pre><code class="language-bash">qemu-system-x86_64 \
-cpu host \
-enable-kvm \
-kernel <dir>/arch/x86/boot/bzImage \
-append "earlyprintk=ttyS0 console=ttyS0 nokaslr init=/init debug" \
-initrd <dir>/initramfs.cpio.gz \
...
</code></pre>
<p>Instructions to build a minimal <a href="https://blog.memzero.de/kernel-debugging-qemu"><code>Kernel</code> and <code>initrd</code></a>.</p>
<h2 id="appendix-cheap-instruction-tracer"><a class="header" href="#appendix-cheap-instruction-tracer">Appendix: Cheap instruction tracer</a></h2>
<pre><code class="language-make">test: test.s
as -o test.o test.s
ld -o test test.o testc.o
trace: test
qemu-x86_64 -singlestep -d nochain,cpu ./test 2>&1 | awk '/RIP/ { print $$1; }'
clean:
$(RM) test test-bin test.o
</code></pre>
<pre><code class="language-x86asm">.section .text, "ax"
.global _start
_start:
xor %rax, %rax
mov $0x8, %rax
1:
cmp $0, %rax
je 2f
dec %rax
jmp 1b
2:
# x86-64 exit(2) syscall
mov $0, %rdi
mov $60, %rax
syscall
</code></pre>
<h2 id="references"><a class="header" href="#references">References</a></h2>
<ul>
<li><a href="https://github.com/qemu/qemu/blob/master/docs/usb2.txt">QEMU USB</a></li>
<li><a href="https://qemu-project.gitlab.io/qemu/tools/qemu-img.html">QEMU IMG</a></li>
<li><a href="https://qemu-project.gitlab.io/qemu/tools/index.html">QEMU Tools</a></li>
<li><a href="https://qemu-project.gitlab.io/qemu/system/index.html">QEMU System</a></li>
<li><a href="https://qemu-project.gitlab.io/qemu/system/invocation.html">QEMU Invocation (command line args)</a></li>
<li><a href="https://qemu-project.gitlab.io/qemu/system/monitor.html">QEMU Monitor</a></li>
<li><a href="https://qemu-project.gitlab.io/qemu/interop/qemu-qmp-ref.html">QEMU machine protocol (QMP)</a></li>
<li><a href="https://qemu-project.gitlab.io/qemu/system/images.html#vm-005fsnapshots">QEMU VM Snapshots</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="pacman1"><a class="header" href="#pacman1">pacman(1)</a></h1>
<h2 id="remote-package-repositories"><a class="header" href="#remote-package-repositories">Remote package repositories</a></h2>
<pre><code class="language-text">pacman -Sy refresh package database
pacman -S <pkg> install pkg
pacman -Ss <regex> search remote package database
pacman -Si <pkg> get info for pkg
pacman -Su upgrade installed packages
pacman -Sc clean local package cache
</code></pre>
<h2 id="remove-packages"><a class="header" href="#remove-packages">Remove packages</a></h2>
<pre><code class="language-text">pacman -Rsn <pkg> uninstall package and unneeded deps + config files
</code></pre>
<h2 id="local-package-database"><a class="header" href="#local-package-database">Local package database</a></h2>
<p>Local package database of installed packages.</p>
<pre><code class="language-text">pacman -Q list all installed packages
pacman -Qs <regex> search local package database
pacman -Ql <pkg> list files installed by pkg
pacman -Qo <file> query package that owns file
pacman -Qe only list explicitly installed packages
</code></pre>
<h2 id="local-file-database"><a class="header" href="#local-file-database">Local file database</a></h2>
<p>Local file database which allows to search packages owning certain files.
Also searches non installed packages, but database must be synced.</p>
<pre><code class="language-text">pacman -Fy refresh file database
pacman -Fl <pkg> list files in pkg (must not be installed)
pacman -Fx <regex> search
</code></pre>
<h2 id="hacks"><a class="header" href="#hacks">Hacks</a></h2>
<p>Uninstall all orphaned packages (including config files) that were installed as
dependencies.</p>
<pre><code class="language-text">pacman -Rsn $(pacman -Qtdq)
</code></pre>
<p>List explicitly installed packages that are not required as dependency by any
package and sort by size.</p>
<pre><code class="language-text">pacman -Qetq | xargs pacman -Qi |
awk '/Name/ { name=$3 }
/Installed Size/ { printf "%8.2f%s %s\n", $4, $5, name }' |
sort -h
</code></pre>
<p>Install package into different <code>root</code> directory but keep using the default
database.</p>
<pre><code class="language-text">pacman --root abc --dbpath /var/lib/pacman -S mingw-w64-gcc
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="dot1"><a class="header" href="#dot1">dot(1)</a></h1>
<p><a href="https://edotor.net/">Online playground</a></p>
<h2 id="example-dot-file-to-copy--paste-from"><a class="header" href="#example-dot-file-to-copy--paste-from">Example <code>dot</code> file to copy & paste from.</a></h2>
<p>Can be rendered to <code>svg</code> with the following command.</p>
<pre><code class="language-bash">dot -T svg -o g.svg g.dot
</code></pre>
<p>Example <code>dot</code> file.</p>
<pre><code class="language-dot">// file: g.dot
digraph {
// Render ranks from left to right.
rankdir=LR
// Make background transparent.
bgcolor=transparent
// Global node attributes.
node [shape=box]
// Global edge attributes.
edge [style=dotted,color=red]
// Add nodes & edge.
stage1 -> stage2
// Add multiple edges at once.
stage2 -> { stage3_1, stage3_2 }
// Add edge with custom attributes.
stage3_2 -> stage4 [label="some text"]
// Set custom attributes for specific node.
stage4 [color=green,fillcolor=lightgray,style="filled,dashed",label="s4"]
// Create a subgraph. This can be used to group nodes/edges or as scope for
// global node/edge attributes.
// If the name starts with 'cluster' a border is drawn.
subgraph cluster_1 {
stage5_1
stage5_2
}
// Add some edges to subgraph nodes.
stage3_1 -> { stage5_1, stage5_2 }
}
</code></pre>
<p>Rendered <code>svg</code> file.
<img src="tools/assets/g.svg" alt="g.svg" /></p>
<h2 id="references-1"><a class="header" href="#references-1">References</a></h2>
<ul>
<li><a href="https://graphviz.org/doc/info/lang.html">DOT language</a></li>
<li><a href="https://graphviz.org/doc/info/attrs.html">Attributes</a></li>
<li><a href="https://graphviz.org/doc/info/shapes.html">Node shapes</a></li>
<li><a href="https://graphviz.org/doc/info/colors.html">Colors</a></li>
<li><a href="https://graphviz.org/pdf/dotguide.pdf">User manual</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="ffmpeg-1"><a class="header" href="#ffmpeg-1">ffmpeg (1)</a></h1>
<h2 id="screen-capture-specific-window-x11"><a class="header" href="#screen-capture-specific-window-x11">screen capture specific window (x11)</a></h2>
<p>Following snippet allows to select a window which is then captured.</p>
<pre><code class="language-bash">#!/bin/bash
echo "Click on window to record .."
# Extract window size and x,y offset.
video_args=$(xwininfo \
| awk '/Absolute upper-left X:/ { xoff = $4 }
/Absolute upper-left Y:/ { yoff=$4 }
/Width:/ { if ($2 % 2 == 1) { width=$2-1; } else { width=$2; } }
/Height:/ { if ($2 % 2 == 1) { height=$2-1; } else { height=$2; } }
END { printf "-video_size %dx%d -i :0.0+%d,%d", width, height, xoff, yoff }')
ffmpeg -framerate 25 -f x11grab $video_args -pix_fmt yuv420p $@ output.mp4
</code></pre>
<blockquote>
<p>Use <code>yuv420p</code> pixel format if video is played on the web
(<a href="https://stackoverflow.com/questions/32829514/which-pixel-format-for-web-mp4-video">ref</a>)</p>
</blockquote>
<p>The input <code>-i 0,0+xoff,yoff</code> means to capture <code>$DISPLAY=0.0</code> starting at the
coordinate <code>(xoff, yoff)</code>, which is the left-upper corner, and the size of the
capture is defined by the <code>-video_size</code> argument.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="column1"><a class="header" href="#column1">column(1)</a></h1>
<h2 id="examples-4"><a class="header" href="#examples-4">Examples</a></h2>
<pre><code class="language-sh"># Show as table (aligned columns), with comma as delimiter from stdin.
echo -e 'a,b,c\n111,22222,33' | column -t -s ','
# Show file as table.
column -t -s ',' test.csv
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="sort1"><a class="header" href="#sort1">sort(1)</a></h1>
<pre><code>sort [opts] [file]
opts:
-r reverse output
-b ignore leading blanks
-n sort by numeric
-h sort by human numeric
-V sort by version
-k<N> sort by Nth key
-t<S> field separator
</code></pre>
<h2 id="examples-5"><a class="header" href="#examples-5">Examples</a></h2>
<pre><code class="language-sh"># Sort by directory sizes.
du -sh * | sort -h
</code></pre>
<pre><code class="language-sh"># Sort numeric by second key.
# The default key separator is non-blank to blank transition.
echo 'a 4
d 10
c 21' | sort -k2 -n
# Sort numeric by second key, split at comma.
echo 'a,4
d,10
c,21' | sort -k2 -n -t,
</code></pre>
<blockquote>
<p>Use <code>--debug</code> to annotate part of the line used to sort and hence debug the key usage.</p>
</blockquote>
<div style="break-before: page; page-break-before: always;"></div><h1 id="sed1"><a class="header" href="#sed1">sed(1)</a></h1>
<pre><code>sed [opts] [script] [file]
opts:
-i edit file in place
-i.bk edit file in place and create backup file
(with .bk suffix, can be specified differently)
--follow-symlinks
follow symlinks when editing in place
-e SCRIPT add SCRIPT to commands to be executed
(can be specified multiple times)
-f FILE add content of FILE to command to be executed
--debug annotate program execution
</code></pre>
<h2 id="examples-6"><a class="header" href="#examples-6">Examples</a></h2>
<h3 id="delete-lines"><a class="header" href="#delete-lines">Delete lines</a></h3>
<pre><code class="language-sh"># Delete two lines.
echo -e 'aa\nbb\ncc\ndd' | sed '1d;3d'
# bb
# dd
# Delete last ($) line.
echo -e 'aa\nbb\ncc\ndd' | sed '$d'
# aa
# bb
# cc
# Delete range of lines.
echo -e 'aa\nbb\ncc\ndd' | sed '1,3d'
# dd
# Delete lines matching pattern.
echo -e 'aa\nbb\ncc\ndd' | sed '/bb/d'
# aa
# cc
# dd
# Delete lines NOT matching pattern.
echo -e 'aa\nbb\ncc\ndd' | sed '/bb/!d'
# bb
</code></pre>
<h3 id="insert-lines"><a class="header" href="#insert-lines">Insert lines</a></h3>
<pre><code class="language-sh"># Insert before line.
echo -e 'aa\nbb' | sed '2iABC'
# aa
# ABC
# bb
# Insert after line.
echo -e 'aa\nbb' | sed '2aABC'
# aa
# bb
# ABC
# Replace line.
echo -e 'aa\nbb' | sed '2cABC'
# aa
# ABC
# Insert before pattern match.
echo -e 'aa\nbb' | sed '/bb/i 123'
# aa
# 123
# bb
</code></pre>
<h3 id="substitute-lines"><a class="header" href="#substitute-lines">Substitute lines</a></h3>
<pre><code class="language-sh"># Substitute by regex.
echo -e 'aafooaa\ncc' | sed 's/foo/MOOSE/'
# aaMOOSEaa
# cc
</code></pre>
<h3 id="multiple-scripts"><a class="header" href="#multiple-scripts">Multiple scripts</a></h3>
<pre><code class="language-sh">echo -e 'foo\nbar' | sed -e 's/foo/FOO/' -e 's/FOO/BAR/'
# BAR
# bar
</code></pre>
<h3 id="edit-inplace-through-symlink"><a class="header" href="#edit-inplace-through-symlink">Edit inplace through symlink</a></h3>
<pre><code class="language-sh">touch file
ln -s file link
ls -l link
# lrwxrwxrwx 1 johannst johannst 4 Feb 7 23:02 link -> file
sed -i --follow-symlinks '1iabc' link
ls -l link
# lrwxrwxrwx 1 johannst johannst 4 Feb 7 23:02 link -> file
sed -i '1iabc' link
ls -l link
# -rw-r--r-- 1 johannst johannst 0 Feb 7 23:02 link
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="gnuplot-1"><a class="header" href="#gnuplot-1">gnuplot (1)</a></h1>
<pre><code># Launch interactive shell.
gnuplot
# Launch interactive shell.
gnuplot [opt]
opt:
-p ................ persist plot window
-c <file> ......... run script file
-e "<cmd1>; .." ... run cmd(s)
</code></pre>
<h2 id="frequently-used-configuration"><a class="header" href="#frequently-used-configuration">Frequently used configuration</a></h2>
<pre><code class="language-sh"># Plot title.
set title "the plot"
# Labels.
set xlabel "abc"
set ylabel "def"
# Output format, 'help set term' for all output formats.
set term svg
# Output file.
set output "out.svg"
# Make axis logarithmic to given base.
set logscale x 2
# Change separator, default is whitespace.
set datafile separator ","
</code></pre>
<h2 id="plot"><a class="header" href="#plot">Plot</a></h2>
<pre><code class="language-sh"># With specific style (eg lines, linespoint, boxes, steps, impulses, ..).
plot "<data_file>" with <plot_style>
> cat data.txt
1 1 3
2 2 2
3 3 1
4 2 2
# Plot specific column.
plot "data.txt" using 1:2, "data.txt" using 1:3
# Equivalent using the special file "", which re-uses the previous input file.
plot "data.txt" using 1:2, "" using 1:3
# Plot piped data.
plot "< head -n2 data.txt"
# Plot with alternate title.
plot "data.txt" title "moose"
</code></pre>
<h2 id="example-multiple-data-sets-in-on-plot"><a class="header" href="#example-multiple-data-sets-in-on-plot">Example: multiple data sets in on plot</a></h2>
<pre><code class="language-sh"># file: mem_lat.plot
set title "memory latency (different strides)"
set xlabel "array in KB"
set ylabel "cycles / access"
set logscale x 2
plot "stride_32.txt" title "32" with linespoints, \
"stride_64.txt" title "64" with linespoints, \
"stride_128.txt" title "128" with linespoints, \
"stride_256.txt" title "256" with linespoints, \
"stride_512.txt" title "512" with linespoints
</code></pre>
<p>On Linux x86_64, <a href="tools/gnuplot/mem_lat.c"><code>mem_lat.c</code></a> provides an example which can
be run as follows.</p>
<pre><code class="language-sh">gcc -o mem_lat mem_lat.c -g -O3 -Wall -Werror
for stride in 32 64 128 256 512; do \
taskset -c 1 ./mem_lat 128 $stride | tee stride_$stride.txt ; \
done
gnuplot -p -c mem_lat.plot
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="resource-analysis--monitor"><a class="header" href="#resource-analysis--monitor">Resource analysis & monitor</a></h1>
<ul>
<li><a href="monitor/./lsof.html">lsof</a></li>
<li><a href="monitor/./ss.html">ss</a></li>
<li><a href="monitor/./pidstat.html">pidstat</a></li>
<li><a href="monitor/./pgrep.html">pgrep</a></li>
<li><a href="monitor/./ps.html">ps</a></li>
<li><a href="monitor/./pmap.html">pmap</a></li>
<li><a href="monitor/./pstack.html">pstack</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="lsof8"><a class="header" href="#lsof8">lsof(8)</a></h1>
<pre><code class="language-markdown">lsof
-r <s> ..... repeatedly execute command ervery <s> seconds
-a ......... AND slection filters instead ORing (OR: default)
-p <pid> ... filter by <pid>
+fg ........ show file flags for file descripros
-n ......... don't convert network addr to hostnames
-P ......... don't convert network port to service names
-i <@h[:p]>. show connections to h (hostname|ip addr) with optional port p
-s <p:s> ... in conjunction with '-i' filter for protocol <p> in state <s>
-U ......... show unix domain sockets ('@' indicates abstract sock name, see unix(7))
</code></pre>
<pre><code class="language-markdown">file flags:
R/W/RW ..... read/write/read-write
CR ......... create
AP ......... append
TR ......... truncate
</code></pre>
<pre><code class="language-markdown">-s protocols
TCP, UDP
-s states (TCP)
CLOSED, IDLE, BOUND, LISTEN, ESTABLISHED, SYN_SENT, SYN_RCDV, ESTABLISHED,
CLOSE_WAIT, FIN_WAIT1, CLOSING, LAST_ACK, FIN_WAIT_2, TIME_WAIT
-s states (UDP)
Unbound, Idle
</code></pre>
<h1 id="examples-7"><a class="header" href="#examples-7">Examples</a></h1>
<h2 id="file-flags"><a class="header" href="#file-flags">File flags</a></h2>
<p>Show open files with file flags for process:</p>
<pre><code class="language-markdown">lsof +fg -p <pid>
</code></pre>
<h2 id="open-tcp-connections"><a class="header" href="#open-tcp-connections">Open TCP connections</a></h2>
<p>Show open tcp connections for <code>$USER</code>:</p>
<pre><code class="language-markdown">lsof -a -u $USER -i TCP
</code></pre>
<p><strong>Note</strong>: <code>-a</code> <em>ands</em> the results. If <code>-a</code> is not given all open files matching
<code>$USER</code> and all tcp connections are listed (<em>ored</em>).</p>
<h2 id="open-connection-to-specific-host"><a class="header" href="#open-connection-to-specific-host">Open connection to specific host</a></h2>
<p>Show open connections to <code>localhost</code> for <code>$USER</code>:</p>
<pre><code class="language-markdown">lsof -a -u $USER -i @localhost
</code></pre>
<h2 id="open-connection-to-specific-port"><a class="header" href="#open-connection-to-specific-port">Open connection to specific port</a></h2>
<p>Show open connections to port <code>:1234</code> for <code>$USER</code>:</p>
<pre><code class="language-markdown">lsof -a -u $USER -i :1234
</code></pre>
<h2 id="ipv4-tcp-connections-in-established-state"><a class="header" href="#ipv4-tcp-connections-in-established-state">IPv4 TCP connections in <code>ESTABLISHED</code> state</a></h2>
<pre><code class="language-markdown">lsof -i 4TCP -s TCP:ESTABLISHED
</code></pre>
<h2 id="list-open-files-in-a-mounted-directory"><a class="header" href="#list-open-files-in-a-mounted-directory">List open files in a mounted directory.</a></h2>
<p>This may help to find which processes keep devices busy when trying to unmount
and the device is currently busy.</p>
<pre><code class="language-markdown"># Assuming /proc is a mount point.
lsof /proc
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="ss8"><a class="header" href="#ss8">ss(8)</a></h1>
<pre><code class="language-markdown">ss [option] [filter]
</code></pre>
<pre><code class="language-markdown">[option]
-p ..... Show process using socket
-l ..... Show sockets in listening state
-4/-6 .. Show IPv4/6 sockets
-x ..... Show unix sockets
-n ..... Show numeric ports (no resolve)
-O ..... Oneline output per socket
</code></pre>
<pre><code class="language-markdown">[filter]
dport/sport PORT .... Filter for destination/source port
dst/src ADDR ........ Filter for destination/source address
and/or .............. Logic operator
==/!= ............... Comparison operator
(EXPR) .............. Group exprs
</code></pre>
<h1 id="examples-8"><a class="header" href="#examples-8">Examples</a></h1>
<p>Show all tcp IPv4 sockets connecting to port <code>443</code>:</p>
<pre><code class="language-markdown">ss -4 'dport 443'
</code></pre>
<p>Show all tcp IPv4 sockets that don't connect to port <code>443</code> or connect to address <code>1.2.3.4</code>.</p>
<pre><code class="language-markdown">ss -4 'dport != 443 or dst 1.2.3.4'
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="pidstat1"><a class="header" href="#pidstat1">pidstat(1)</a></h1>
<pre><code class="language-markdown">pidstat [opt] [interval] [cont]
-U [user] show username instead UID, optionally only show for user
-r memory statistics
-d I/O statistics
-h single line per process and no lines with average
</code></pre>
<h1 id="page-fault-and-memory-utilization"><a class="header" href="#page-fault-and-memory-utilization">Page fault and memory utilization</a></h1>
<pre><code class="language-markdown">pidstat -r -p <pid> [interval] [count]
</code></pre>
<pre><code class="language-markdown">minor_pagefault: Happens when the page needed is already in memory but not
allocated to the faulting process, in that case the kernel
only has to create a new page-table entry pointing to the
shared physical page (not required to load a memory page from
disk).
major_pagefault: Happens when the page needed is NOT in memory, the kernel
has to create a new page-table entry and populate the
physical page (required to load a memory page from disk).
</code></pre>
<h1 id="io-statistics"><a class="header" href="#io-statistics">I/O statistics</a></h1>
<pre><code class="language-markdown">pidstat -d -p <pid> [interval] [count]
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="pgrep1"><a class="header" href="#pgrep1">pgrep(1)</a></h1>
<pre><code class="language-markdown">pgrep [opts] <pattern>
-n only list newest matching process
-u <usr> only show matching for user <usr>
-l additionally list command
-a additionally list command + arguments
-x match exactly
</code></pre>
<h2 id="debug-newest-process"><a class="header" href="#debug-newest-process">Debug newest process</a></h2>
<p>For example attach gdb to newest zsh process from <code>$USER</code>.</p>
<pre><code class="language-markdown">gdb -p $(pgrep -n -u $USER zsh)
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="ps1"><a class="header" href="#ps1">ps(1)</a></h1>
<pre><code>ps [opt]
opt:
--no-header .... do not print column header
-o <OUT> ....... comma separated list of output columns
-p <PID> ....... only show pid
-C <name> ...... only show processes matching name
-T ............. list threads
--signames ..... use short signames instead bitmasks
</code></pre>
<blockquote>
<p>Set <code>PS_FORMAT</code> env variable to setup default output columns.</p>
</blockquote>
<p>Frequently used output columns</p>
<pre><code>pid process id
ppid parent process id
pgid process group id
tid thread id
comm name of process
cmd name of process + args (full)
etime elapsed time (since process started)
user user owning process
thcount thread count of process
nice nice value (-20 highest priority to 19 lowest)
pcpu cpu utilization (percent)
pmem physical resident set (rss) (percent)
rss physical memory (in kb)
vsz virtual memory (in kb)
sig mask of pending signals
sigcatch mask of caught signals
sigignore mask of ignored signals
sigmask mask of blocked signals
</code></pre>
<h2 id="example-use-output-for-scripting"><a class="header" href="#example-use-output-for-scripting">Example: Use output for scripting</a></h2>
<pre><code class="language-sh">for tid in $(ps -o tid --no-header -T -p 31084); do
taskset -c -p $tid;
done
</code></pre>
<h2 id="example-watch-processes-by-name"><a class="header" href="#example-watch-processes-by-name">Example: Watch processes by name</a></h2>
<pre><code class="language-sh">watch -n1 ps -o pid,pcpu,pmem,rss,vsz,state,user,comm -C fish
</code></pre>
<h2 id="example-show-signal-information"><a class="header" href="#example-show-signal-information">Example: Show signal information</a></h2>
<pre><code class="language-sh"># With signal masks.
ps -o pid,user,sig,sigcatch,sigignore,sigmask,comm -p 66570
# With signal names.
ps --signames -o pid,user,sig,sigcatch,sigignore,sigmask,comm -p 66570
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="pmap1"><a class="header" href="#pmap1">pmap(1)</a></h1>
<pre><code class="language-markdown">pmap [opts] <pid>
Dump virtual memory map of process.
Compared to /proc/<pid>/maps it shows the size of the mappings.
opts:
-p show full path in the mapping
-x show details (eg RSS usage of each segment)
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="pstack1"><a class="header" href="#pstack1">pstack(1)</a></h1>
<pre><code class="language-markdown">pstack <pid>
Dump stack for all threads of process.
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="trace-and-profile"><a class="header" href="#trace-and-profile">Trace and Profile</a></h1>
<ul>
<li><a href="trace_profile/./time.html">time</a></li>
<li><a href="trace_profile/./strace.html">strace</a></li>
<li><a href="trace_profile/./ltrace.html">ltrace</a></li>
<li><a href="trace_profile/./perf.html">perf</a></li>
<li><a href="trace_profile/./oprofile.html">OProfile</a></li>
<li><a href="trace_profile/./callgrind.html">callgrind</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="usrbintime1"><a class="header" href="#usrbintime1">/usr/bin/time(1)</a></h1>
<pre><code class="language-markdown"># statistics of process run
/usr/bin/time -v <cmd>
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="strace1"><a class="header" href="#strace1">strace(1)</a></h1>
<pre><code class="language-markdown">strace [opts] [prg]
-f .......... follow child processes on fork(2)
-ff ......... follow fork and separate output file per child
-p <pid> .... attach to running process
-s <size> ... max string size, truncate of longer (default: 32)
-e <expr> ... expression for trace filtering
-o <file> ... log output into <file>
-c .......... dump syscall statitics at the end
-C .......... like -c but dump regular ouput as well
-k .......... dump stack trace for each syscall
-P <path> ... only trace syscall accesing path
-y .......... print paths for FDs
-tt ......... print absolute timestamp (with us precision)
-r .......... print relative timestamp
-z .......... log only successful syscalls
-Z .......... log only failed syscalls
-n .......... print syscall numbers
-y .......... translate fds (eg file path, socket)
-yy ......... translate fds with all information (eg IP)
-x .......... print non-ASCII chars as hex string
</code></pre>
<pre><code class="language-markdown"><expr>:
trace=syscall[,syscall] .... trace only syscall listed
trace=file ................. trace all syscall that take a filename as arg
trace=process .............. trace process management related syscalls
trace=signal ............... trace signal related syscalls
signal ..................... trace signals delivered to the process
</code></pre>
<h1 id="examples-9"><a class="header" href="#examples-9">Examples</a></h1>
<p>Trace <code>open(2)</code> & <code>socket(2)</code> syscalls for a running process + child processes:</p>
<pre><code class="language-markdown">strace -f -e trace=open,socket -p <pid>
</code></pre>
<p>Trace signals delivered to a running process:</p>
<pre><code class="language-markdown">strace -e signal -e 'trace=!all' -p <pid>
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="ltrace1"><a class="header" href="#ltrace1">ltrace(1)</a></h1>
<pre><code class="language-markdown">ltrace [opts] [prg]
-f .......... follow child processes on fork(2)
-p <pid> .... attach to running process
-o <file> ... log output into <file>
-l <filter> . show who calls into lib matched by <filter>
-C .......... demangle
</code></pre>
<h1 id="example-5"><a class="header" href="#example-5">Example</a></h1>
<p>List which program/libs call into <code>libstdc++</code>:</p>
<pre><code class="language-bash">ltrace -l '*libstdc++*' -C -o ltrace.log ./main
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="perf1"><a class="header" href="#perf1">perf(1)</a></h1>
<pre><code>perf list show supported hw/sw events & metrics
-v ........ print longer event descriptions
--details . print information on the perf event names
and expressions used internally by events
perf stat
-p <pid> ..... show stats for running process
-o <file> .... write output to file (default stderr)
-I <ms> ...... show stats periodically over interval <ms>
-e <ev> ...... select event(s)
-M <met> ..... print metric(s), this adds the metric events
--all-user ... configure all selected events for user space
--all-kernel . configure all selected events for kernel space
perf top
-p <pid> .. show stats for running process
-F <hz> ... sampling frequency
-K ........ hide kernel threads
perf record
-p <pid> ............... record stats for running process
-o <file> .............. write output to file (default perf.data)
-F <hz> ................ sampling frequency
--call-graph <method> .. [fp, dwarf, lbr] method how to caputre backtrace
fp : use frame-pointer, need to compile with
-fno-omit-frame-pointer
dwarf: use .cfi debug information
lbr : use hardware last branch record facility
-g ..................... short-hand for --call-graph fp
-e <ev> ................ select event(s)
--all-user ............. configure all selected events for user space
--all-kernel ........... configure all selected events for kernel space
-M intel ............... use intel disassembly in annotate
perf report
-n .................... annotate symbols with nr of samples
--stdio ............... report to stdio, if not presen tui mode
-g graph,0.5,callee ... show callee based call chains with value >0.5
</code></pre>
<pre><code>Useful <ev>:
page-faults
minor-faults
major-faults
cpu-cycles`
task-clock
</code></pre>
<h2 id="select-specific-events"><a class="header" href="#select-specific-events">Select specific events</a></h2>
<p>Events to sample are specified with the <code>-e</code> option, either pass a comma
separated list or pass <code>-e</code> multiple times.</p>
<p>Events are specified in the following form <code>name[:modifier]</code>. The list and
description of the <code>modifier</code> can be found in the
<a href="https://man7.org/linux/man-pages/man1/perf-list.1.html"><code>perf-list(1)</code></a> manpage under <code>EVENT MODIFIERS</code>.</p>
<pre><code class="language-sh"># L1 i$ misses in user space
# L2 i$ stats in user/kernel space mixed
# Sample specified events.
perf stat -e L1-icache-load-misses:u \
-e l2_rqsts.all_code_rd:uk,l2_rqsts.code_rd_hit:k,l2_rqsts.code_rd_miss:k \
-- stress -c 2
</code></pre>
<p>The <code>--all-user</code> and <code>--all-kernel</code> options append a <code>:u</code> and <code>:k</code> modifier to
all specified events. Therefore the following two command lines are equivalent.</p>
<pre><code class="language-sh"># 1)
perf stat -e cycles:u,instructions:u -- ls
# 2)
perf stat --all-user -e cycles,instructions -- ls
</code></pre>
<h3 id="raw-events"><a class="header" href="#raw-events">Raw events</a></h3>
<p>In case perf does not provide a <em>symbolic</em> name for an event, the event can be
specified in a <em>raw</em> form as <code>r + UMask + EventCode</code>.</p>
<p>The following is an example for the <a href="https://github.com/intel/perfmon/blob/09c155f72e1b8f14b09aea346a35467a03a7d62b/SNB/events/sandybridge_core.json#L808">L2_RQSTS.CODE_RD_HIT</a> event
with <code>EventCode=0x24</code> and <code>UMask=0x10</code> on my laptop with a <code>sandybridge</code> uarch.</p>
<pre><code class="language-sh">perf stat -e l2_rqsts.code_rd_hit -e r1024 -- ls
# Performance counter stats for 'ls':
#
# 33.942 l2_rqsts.code_rd_hit
# 33.942 r1024
</code></pre>
<h3 id="find-raw-performance-counter-events-intel"><a class="header" href="#find-raw-performance-counter-events-intel">Find raw performance counter events (intel)</a></h3>
<p>The <a href="https://github.com/intel/perfmon"><code>intel/perfmon</code></a> repository provides a performance event
databases for the different intel uarchs.</p>
<p>The table in <a href="https://github.com/intel/perfmon/blob/main/mapfile.csv"><code>mapfile.csv</code></a> can be used to lookup the
corresponding uarch, just grab the family model from the procfs.</p>
<pre><code class="language-sh"> cat /proc/cpuinfo | awk '/^vendor_id/ { V=$3 }
/^cpu family/ { F=$4 }
/^model\s*:/ { printf "%s-%d-%x\n",V,F,$3 }'
</code></pre>
<blockquote>
<p>The table in <a href="https://github.com/intel/perfmon/tree/main#performance-monitoring-events">performance monitoring events</a> describes how
events are sorted into the different files.</p>
</blockquote>
<h3 id="raw-events-for-perfs-own-symbolic-names"><a class="header" href="#raw-events-for-perfs-own-symbolic-names">Raw events for perfs own symbolic names</a></h3>
<p>Perf also defines some own <em>symbolic</em> names for events. An example is the
<code>cache-references</code> event. The <a href="https://man7.org/linux/man-pages/man2/perf_event_open.2.html"><code>perf_event_open(2)</code></a> manpage
gives the following description.</p>
<pre><code class="language-man">perf_event_open(2)
PERF_COUNT_HW_CACHE_REFERENCES
Cache accesses. Usually this indicates Last Level Cache accesses but this
may vary depending on your CPU. This may include prefetches and coherency
messages; again this depends on the design of your CPU.
</code></pre>
<p>The <code>sysfs</code> can be consulted to get the concrete performance counter on the
given system.</p>
<pre><code class="language-sh">cat /sys/devices/cpu/events/cache-misses
# event=0x2e,umask=0x41
</code></pre>
<h2 id="flamegraph"><a class="header" href="#flamegraph"><a href="https://github.com/brendangregg/FlameGraph"><code>Flamegraph</code></a></a></h2>
<h3 id="flamegraph-with-single-event-trace"><a class="header" href="#flamegraph-with-single-event-trace">Flamegraph with single event trace</a></h3>
<pre><code>perf record -g -e cpu-cycles -p <pid>
perf script | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > cycles-flamegraph.svg
</code></pre>
<h3 id="flamegraph-with-multiple-event-traces"><a class="header" href="#flamegraph-with-multiple-event-traces">Flamegraph with multiple event traces</a></h3>
<pre><code class="language-sh">perf record -g -e cpu-cycles,page-faults -p <pid>
perf script --per-event-dump
# fold & generate as above
</code></pre>
<h2 id="examples-10"><a class="header" href="#examples-10">Examples</a></h2>
<h3 id="estimate-max-instructions-per-cycle"><a class="header" href="#estimate-max-instructions-per-cycle">Estimate max instructions per cycle</a></h3>
<pre><code class="language-c">#define NOP4 "nop\nnop\nnop\nnop\n"
#define NOP32 NOP4 NOP4 NOP4 NOP4 NOP4 NOP4 NOP4 NOP4
#define NOP256 NOP32 NOP32 NOP32 NOP32 NOP32 NOP32 NOP32 NOP32
#define NOP2048 NOP256 NOP256 NOP256 NOP256 NOP256 NOP256 NOP256 NOP256
int main() {
for (unsigned i = 0; i < 2000000; ++i) {
asm volatile(NOP2048);
}
}
</code></pre>
<pre><code class="language-sh">perf stat -e cycles,instructions ./noploop
# Performance counter stats for './noploop':
#
# 1.031.075.940 cycles
# 4.103.534.341 instructions # 3,98 insn per cycle
</code></pre>
<h3 id="caller-vs-callee-callstacks"><a class="header" href="#caller-vs-callee-callstacks">Caller vs callee callstacks</a></h3>
<p>The following gives an example for a scenario where we have the following calls</p>
<ul>
<li><code>main -> do_foo() -> do_work()</code></li>
<li><code>main -> do_bar() -> do_work()</code></li>
</ul>
<pre><code class="language-sh">perf report --stdio -g graph,callee
# Children Self Command Shared Object Symbols
# ........ ........ ....... .................... .................
#
# 49.71% 49.66% bench bench [.] do_work
# |
# --49.66%--_start <- callstack bottom
# __libc_start_main
# 0x7ff366c62ccf
# main
# |
# |--25.13%--do_bar
# | do_work <- callstack top
# |
# --24.53%--do_foo
# do_work
perf report --stdio -g graph,callee
# Children Self Command Shared Object Symbols
# ........ ........ ....... .................... .................
#
# 49.71% 49.66% bench bench [.] do_work
# |
# ---do_work <- callstack top
# |
# |--25.15%--do_bar
# | main
# | 0x7ff366c62ccf
# | __libc_start_main
# | _start <- callstack bottom
# |
# --24.55%--do_foo
# main
# 0x7ff366c62ccf
# __libc_start_main
# _start <- callstack bottom
</code></pre>
<h2 id="references-2"><a class="header" href="#references-2">References</a></h2>
<ul>
<li><a href="https://github.com/intel/perfmon">intel/perfmon</a> - intel PMU event database per uarch</li>
<li><a href="https://perfmon-events.intel.com/">intel/perfmon-html</a> - a html rendered version of the PMU
events with search</li>
<li><a href="https://github.com/intel/perfmon/blob/main/mapfile.csv">intel/perfmon/mapfile.csv</a> - processor family to uarch mapping</li>
<li><a href="https://github.com/torvalds/linux/tree/master/tools/perf/pmu-events/arch/x86">linux/perf/events</a> - x86 PMU events known to perf tools</li>
<li><a href="https://github.com/torvalds/linux/blob/master/arch/x86/events/intel/core.c">linux/arch/events</a> - x86 PMU events linux kernel</li>
<li><a href="https://en.wikichip.org/wiki/WikiChip">wikichip</a> - computer architecture wiki</li>
<li><a href="https://man7.org/linux/man-pages/man1/perf-list.1.html">perf-list(1)</a> - manpage</li>
<li><a href="https://man7.org/linux/man-pages/man2/perf_event_open.2.html">perf_event_open(2)</a> - manpage</li>
<li><a href="https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html">intel/sdm</a> - intel software developer manuals (eg Optimization
Reference Manual)</li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="oprofile"><a class="header" href="#oprofile"><a href="https://oprofile.sourceforge.io/">OProfile</a></a></h1>
<pre><code class="language-markdown">operf -g -p <pid>
-g ...... caputre call-graph information
opreport [opt] FILE
show time spent per binary image
-l ...... show time spent per symbol
-c ...... show callgraph information (see below)
-a ...... add column with time spent accumulated over child nodes
ophelp show supported hw/sw events
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="callgrind"><a class="header" href="#callgrind">callgrind</a></h1>
<p>Callgrind is a tracing profiler to record the function call history of a target
program. It is part of the <a href="https://valgrind.org/docs/manual/cl-manual.html">valgrind</a> tool suite.</p>
<p>Profiling data is collected by instrumentation rather than sampling of the
target program.</p>
<p>Callgrind does not capture the actual time spent in a function but computes the
cost of a function based on the instructions fetched (<code>Ir = Instruction read</code>).
Therefore effects like slow IO are not reflected, which should be kept in mind
when analyzing callgrind results.</p>
<p>By default the profiler data is dumped when the target process is terminating,
but <a href="https://valgrind.org/docs/manual/cl-manual.html#cl-manual.callgrind_control-options">callgrind_control</a> allows for interactive control of callgrind.</p>
<pre><code class="language-bash"># Run a program under callgrind.
valgrind --tool=callgrind -- <prog>
# Interactive control of callgrind.
callgrind_control [opts]
opts:
-b ............. show current backtrace
-e ............. show current event counters
-s ............. show current stats
--dump[=file] .. dump current collection
-i=on|off ...... turn instrumentation on|off
</code></pre>
<p>Results can be analyzed by using one of the following tools</p>
<ul>
<li><a href="https://valgrind.org/docs/manual/cl-manual.html#cl-manual.callgrind_annotate-options">callgrind_annotate</a> (cli)</li>
<li><a href="https://kcachegrind.github.io/html/Home.html">kcachegrind</a> (ui)</li>
</ul>
<p>The following is a collection of frequently used callgrind options.</p>
<pre><code class="language-bash">valgrind --tool=callgrind [opts] -- <prog>
opts:
--callgrind-out-file=<file> .... output file, rather than callgrind.out.<pid>
--dump-instr=<yes|no> .......... annotation on instrucion level,
allows for asm annotations
--instr-atstart=<yes|no> ....... control if instrumentation is enabled from
beginning of the program
--separate-threads=<yes|no> .... create separate output files per thread,
appends -<thread_id> to the output file
</code></pre>
<h2 id="profile-specific-part-of-the-target"><a class="header" href="#profile-specific-part-of-the-target">Profile specific part of the target</a></h2>
<p>Programmatically enable/disable instrumentation using the macros defined in
the callgrind header.</p>
<pre><code class="language-c">#include <valgrind/callgrind.h>
int main() {
// init ..
CALLGRIND_START_INSTRUMENTATION;
compute();
CALLGRIND_STOP_INSTRUMENTATION;
// shutdown ..
}
</code></pre>
<blockquote>
<p>In this case, callgrind should be launched with <code>--instr-atstart=no</code>.</p>
</blockquote>
<p>Alternatively instrumentation can be controlled with <code>callgrind_control -i on/off</code>.</p>
<p>The files <a href="trace_profile/callgrind/cg_example.cc">cg_example.cc</a> and
<a href="trace_profile/callgrind/Makefile">Makefile</a> provide a full example.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="binary"><a class="header" href="#binary">Binary</a></h1>
<ul>
<li><a href="binary/./od.html">od</a></li>
<li><a href="binary/./xxd.html">xxd</a></li>
<li><a href="binary/./readelf.html">readelf</a></li>
<li><a href="binary/./objdump.html">objdump</a></li>
<li><a href="binary/./nm.html">nm</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="od1"><a class="header" href="#od1">od(1)</a></h1>
<pre><code class="language-markdown"> od [opts] <file>
-An don't print addr info
-tx4 print hex in 4 byte chunks
-ta print as named character
-tc printable chars or backslash escape
-w4 print 4 bytes per line
-j <n> skip <n> bytes from <file> (hex if start with 0x)
-N <n> dump <n> bytes (hex of start with 0x)
</code></pre>
<h2 id="ascii-to-hex-string"><a class="header" href="#ascii-to-hex-string">ASCII to hex string</a></h2>
<pre><code class="language-markdown"> echo -n AAAABBBB | od -An -w4 -tx4
>> 41414141
>> 42424242
echo -n '\x7fELF\n' | od -tx1 -ta -tc
>> 0000000 7f 45 4c 46 0a # tx1
>> del E L F nl # ta
>> 177 E L F \n # tc
</code></pre>
<h2 id="extract-parts-of-file"><a class="header" href="#extract-parts-of-file">Extract parts of file</a></h2>
<p>For example <code>.rodata</code> section from an elf file. We can use <code>readelf</code> to get the
offset into the file where the <code>.rodata</code> section starts.</p>
<pre><code class="language-markdown"> readelf -W -S foo
>> Section Headers:
>> [Nr] Name Type Address Off Size ES Flg Lk Inf Al
>> ...
>> [15] .rodata PROGBITS 00000000004009c0 0009c0 000030 00 A 0 0 16
</code></pre>
<p>With the offset of <code>-j 0x0009c0</code> we can dump <code>-N 0x30</code> bytes from the beginning of
the <code>.rodata</code> section as follows:</p>
<pre><code class="language-markdown"> od -j 0x0009c0 -N 0x30 -tx4 -w4 foo
>> 0004700 00020001
>> 0004704 00000000
>> *
>> 0004740 00000001
>> 0004744 00000002
>> 0004750 00000003
>> 0004754 00000004
</code></pre>
<p><strong>Note</strong>: Numbers starting with <code>0x</code> will be interpreted as hex by <code>od</code>.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="xxd1"><a class="header" href="#xxd1">xxd(1)</a></h1>
<pre><code class="language-markdown"> xxd [opts]
-p dump continuous hexdump
-r convert hexdump into binary ('revert')
-e dump as little endian mode
-i output as C array
</code></pre>
<h2 id="ascii-to-hex-stream"><a class="header" href="#ascii-to-hex-stream">ASCII to hex stream</a></h2>
<pre><code class="language-markdown"> echo -n 'aabb' | xxd -p
>> 61616262
</code></pre>
<h2 id="hex-to-binary-stream"><a class="header" href="#hex-to-binary-stream">Hex to binary stream</a></h2>
<pre><code class="language-markdown"> echo -n '61616262' | xxd -p -r
>> aabb
</code></pre>
<h2 id="ascii-to-binary"><a class="header" href="#ascii-to-binary">ASCII to binary</a></h2>
<pre><code class="language-markdown"> echo -n '\x7fELF' | xxd -p | xxd -p -r | file -p -
>> ELF
</code></pre>
<h2 id="ascii-to-c-array-hex-encoded"><a class="header" href="#ascii-to-c-array-hex-encoded">ASCII to <code>C</code> array (hex encoded)</a></h2>
<pre><code class="language-markdown"> xxd -i <(echo -n '\x7fELF')
>> unsigned char _proc_self_fd_11[] = {
>> 0x7f, 0x45, 0x4c, 0x46
>> };
>> unsigned int _proc_self_fd_11_len = 4;
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="readelf1"><a class="header" href="#readelf1">readelf(1)</a></h1>
<pre><code class="language-markdown"> readelf [opts] <elf>
-W|--wide wide output, dont break output at 80 chars
-h print ELF header
-S print section headers
-l print program headers + segment mapping
-d print .dynamic section (dynamic link information)
--syms print symbol tables (.symtab .dynsym)
--dyn-syms print dynamic symbol table (exported symbols for dynamic linker)
-r print relocation sections (.rel.*, .rela.*)
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="objdump1"><a class="header" href="#objdump1">objdump(1)</a></h1>
<pre><code class="language-markdown"> objdump [opts] <elf>
-M intel use intil syntax
-d disassemble text section
-D disassemble all sections
--disassemble=<sym> disassemble symbol <sym>
-S mix disassembly with source code
-C demangle
-j <section> display info for section
--[no-]show-raw-insn [dont] show object code next to disassembly
--visualize-jumps[=color] visualize jumps with ascii art, optionally color arrows
</code></pre>
<h2 id="disassemble-section"><a class="header" href="#disassemble-section">Disassemble section</a></h2>
<p>For example <code>.plt</code> section:</p>
<pre><code class="language-markdown"> objdump -j .plt -d <elf>
</code></pre>
<h2 id="example-disassemble-raw-binary"><a class="header" href="#example-disassemble-raw-binary">Example: disassemble raw binary</a></h2>
<p>This can be helpful for example as a cheap analysis tool when toying with JIT
generating code. We could just write thee binary code buffer to a file and
disassemble with <code>objdump</code>.</p>
<p>To re-create that case, we just assemble and link some ELF file and then create
a raw binary of the text section with <code>objcopy</code>.</p>
<pre><code class="language-x86asm"># file: test.s
.section .text, "ax"
.global _start
_start:
xor %rax, %rax
mov $0x8, %rax
1:
cmp $0, %rax
je 2f
dec %rax
jmp 1b
2:
# x86-64 exit(2) syscall
mov $0, %rdi
mov $60, %rax
syscall
</code></pre>
<pre><code class="language-bash"># Assemble & link.
as -o test.o test.s
ld -o test test.o testc.o
# ELF -> binary (only take .text section).
objcopy -O binary --only-section .text test test-bin
# Disassemble raw binary.
objdump -D -b binary -m i386:x86-64 test-bin
</code></pre>
<h2 id="example-disassemble-specific-symbol"><a class="header" href="#example-disassemble-specific-symbol">Example: disassemble specific symbol</a></h2>
<pre><code class="language-bash"># Disassemble main().
objdump --disassemble=main <bin>
# Disassemble 'foo::bar()' (mangled).
objdump --disassemble=_ZN3foo3barEvr <bin>
# Disassemble 'foo::bar()' (demangled), requires -C
objdump -C --disassemble=foo::bar <bin>
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="nm1"><a class="header" href="#nm1">nm(1)</a></h1>
<pre><code class="language-markdown"> nm [opts] <elf>
-C demangle
-u undefined only
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="development"><a class="header" href="#development">Development</a></h1>
<ul>
<li><a href="development/./c++filt.html">c++filt</a></li>
<li><a href="development/./c++.html">c++</a></li>
<li><a href="development/./glibc.html">glibc</a></li>
<li><a href="development/./gcc.html">gcc</a></li>
<li><a href="development/./cmake.html">cmake</a></li>
<li><a href="development/./make.html">make</a></li>
<li><a href="development/./ld.so.html">ld.so</a></li>
<li><a href="development/./symbolver.html">symbol versioning</a></li>
<li><a href="development/./python.html">python</a></li>
<li><a href="development/./gcov.html">gcov</a></li>
<li><a href="development/./pgo.html">pgo</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="cfilt1"><a class="header" href="#cfilt1">c++filt(1)</a></h1>
<h2 id="demangle-symbol"><a class="header" href="#demangle-symbol">Demangle symbol</a></h2>
<pre><code class="language-markdown"> c++-filt <symbol_str>
</code></pre>
<h2 id="demangle-stream"><a class="header" href="#demangle-stream">Demangle stream</a></h2>
<p>For example dynamic symbol table:</p>
<pre><code class="language-markdown"> readelf -W --dyn-syms <elf> | c++filt
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="c"><a class="header" href="#c">c++</a></h1>
<p>openstd <a href="https://www.open-std.org/jtc1/sc22/wg21/docs/standards">cpp standards</a>.</p>
<p>Source files of most examples is available <a href="https://github.com/johannst/notes/tree/master/src/development/c%2B%2B">here</a>.</p>
<h2 id="type-deduction"><a class="header" href="#type-deduction">Type deduction</a></h2>
<p>Force compile error to see what <code>auto</code> is deduced to.</p>
<pre><code class="language-cpp">auto foo = bar();
// force compile error
typename decltype(foo)::_;
</code></pre>
<h2 id="strict-aliasing-and-type-punning"><a class="header" href="#strict-aliasing-and-type-punning">Strict aliasing and type punning</a></h2>
<p>The <code>strict aliasing</code> rules describe via which <code>alias</code> a value can be accessed.</p>
<blockquote>
<p>Informal: an <code>alias</code> is a reference / pointer to a value.</p>
</blockquote>
<p>Accessing a value through an alias that violates the strict aliasing rules is
<code>undefined behavior (UB)</code>.</p>
<p>Examples below on <a href="https://godbolt.org/z/TsvTY9zfj">godbolt</a>.</p>
<pre><code class="language-cpp">int i = 0;
// Valid aliasing (signed / unsigned type).
*reinterpret_cast<signed int*>(&i);
*reinterpret_cast<unsigned int*>(&i);
// Valid aliasing (cv qualified type).
*reinterpret_cast<const int*>(&i);
*reinterpret_cast<const unsigned*>(&i);
// Valid aliasing (byte type).
*reinterpret_cast<char*>(&i);
*reinterpret_cast<std::byte*>(&i);
// Invalid aliasing, dereferencing pointer is UB.
*reinterpret_cast<short*>(&i);
*reinterpret_cast<float*>(&i);
</code></pre>
<blockquote>
<p>NOTE: Casting pointer to invalid aliasing type is not directly UB, but
dereferencing the pointer is UB.</p>
</blockquote>
<pre><code class="language-cpp">short s[2] = { 1, 2 };
// Invalid aliasing (UB) - type punning, UB to deref ptr (int has stricter
// alignment requirements than short).
*reinterpret_cast<int*>(s);
// Arbitrary byte pointer.
char c[4] = { 1, 2, 3, 4 };
// Invalid aliasing (UB) - type punning, UB to deref ptr (int has stricter
// alignment requirements than char).
*reinterpret_cast<int*>(c);
</code></pre>
<p>At the time of writing, the current <a href="http://eel.is/c++draft/basic.lval#11">c++ std draft</a>
contains the following.</p>
<pre><code class="language-text">If a program attempts to access the stored value of an object through a glvalue
whose type is not **similar** (7.3.6) to one of the following types the
behavior is undefined [44]
(11.1) the dynamic type of the object,
(11.2) a type that is the signed or unsigned type corresponding to the dynamic
type of the object, or
(11.3) a char, unsigned char, or std::byte type.
[44]: The intent of this list is to specify those circumstances in which an
object can or cannot be aliased.
</code></pre>
<p>The paragraph is short but one also needs to understand the meaning of
<a href="http://eel.is/c++draft/conv.qual#def:similar_types">similar (<em>similar_types</em>)</a>.</p>
<p>This paragraph is actually somewhat more explicit in the <a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf">c++17 std</a>.</p>
<pre><code class="language-text">If a program attempts to access the stored value of an object through a glvalue
of other than one of the following types the behavior is undefined [63]
(11.1) the dynamic type of the object,
(11.2) a cv-qualified version of the dynamic type of the object,
(11.3) a type similar (as defined in 7.5) to the dynamic type of the object,
(11.4) a type that is the signed or unsigned type corresponding to the dynamic
type of the object,
(11.5) a type that is the signed or unsigned type corresponding to a
cv-qualified version of the dynamic type of the object,
(11.6) an aggregate or union type that includes one of the aforementioned types
among its elements or non- static data members (including, recursively,
an element or non-static data member of a subaggregate or contained
union),
(11.7) a type that is a (possibly cv-qualified) base class type of the dynamic
type of the object,
(11.8) a char, unsigned char, or std::byte type.
[63]: The intent of this list is to specify those circumstances in which an
object may or may not be aliased.
</code></pre>
<p>Additional references:</p>
<ul>
<li>
<p><a href="https://gist.github.com/shafik/848ae25ee209f698763cffee272a58f8">What is the Strict Aliasing Rule and Why do we care</a></p>
<p>The article shows a small example how the compiler may optimized using the
strict aliasing rules.</p>
<pre><code class="language-cpp">int alias(int* i, char* c) {
*i = 1;
*c = 'a'; // char* may alias int*
return *i;
}
int noalias(int* i, short* s) {
*i = 1;
*s = 2; // short* does not alias int*
return *i;
}
</code></pre>
<pre><code class="language-x86asm">alias(int*, char*):
mov DWORD PTR [rdi] ,0x1 ; *i = 1;
mov BYTE PTR [rsi], 0x61 ; *c = 'a';
mov eax,DWORD PTR [rdi] ; Must reload, char* can alias int*.
ret
noalias(int*, short*):
mov DWORD PTR [rdi], 0x1 ; *i = 1;
mov WORD PTR [rsi], 0x2 ; *s = 2;
mov eax,0x1 ; Must not reload, short* can not alias int*.
ret
</code></pre>
</li>
<li>
<p><a href="https://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing">reinterpret_cast</a> type aliasing</p>
<blockquote>
<ol start="5">
<li>Any object pointer type <code>T1*</code> can be converted to another object pointer
type <code>cv T2*</code>. This is exactly equivalent to <code>static_cast<cv T2*>(static_cast<cv void*>(expression))</code> (which implies that if T2's
alignment requirement is not stricter than T1's, the value of the pointer
does not change and conversion of the resulting pointer back to its
original type yields the original value). In any case, the resulting
pointer may only be dereferenced safely if allowed by the type aliasing
rules (see below).</li>
</ol>
</blockquote>
<pre><code class="language-cpp">int I;
char* X = reinterpret_cast<char*>(&I); // Valid, char allowed to alias int.
*X = 42;
int* Y = reinterpret_cast<int*>(X); // Cast back to original type.
*Y = 1337; // safe
char C[4];
int* P = reinterpret_cast<int*>(C); // Cast is ok, not yet UB.
*P = 1337; // UB, violates strict aliasing / alignment rules.
// https://stackoverflow.com/questions/52492229/c-byte-array-to-int
</code></pre>
</li>
<li>
<p>On <code>gcc</code> strict aliasing is enabled starting with <code>-O2</code>.</p>
<pre><code class="language-bash">for i in {0..3} g s; do echo "-O$i $(g++ -Q --help=optimizers -O$i | grep fstrict-aliasing)"; done
-O0 -fstrict-aliasing [disabled]
-O1 -fstrict-aliasing [disabled]
-O2 -fstrict-aliasing [enabled]
-O3 -fstrict-aliasing [enabled]
-Og -fstrict-aliasing [disabled]
-Os -fstrict-aliasing [enabled]
</code></pre>
</li>
</ul>
<h3 id="__restrict-keyword"><a class="header" href="#__restrict-keyword"><code>__restrict</code> keyword</a></h3>
<p>The <code>__restrict</code> keyword allows the programmer to tell the compiler that two
pointer will not alias each other.</p>
<pre><code class="language-cpp">int alias(int* a, int* b) {
*a = 1;
*b = 2;
return *a;
}
// alias(int*, int*): # @alias(int*, int*)
// mov dword ptr [rdi], 1
// mov dword ptr [rsi], 2
// mov eax, dword ptr [rdi]
// ret
int noalias(int* __restrict a, int* __restrict b) {
*a = 1;
*b = 2;
return *a;
}
// noalias(int*, int*): # @noalias(int*, int*)
// mov dword ptr [rdi], 1
// mov dword ptr [rsi], 2
// mov eax, 1
// ret
</code></pre>
<p>However this should only be used with care and in a narrow scope, as it is easy
to violate self defined contract, see <a href="https://godbolt.org/z/e8x1af3Mh">godbolt</a>.</p>
<h3 id="type-punning"><a class="header" href="#type-punning">Type punning</a></h3>
<p>The correct way to do <code>type-punning</code> in c++:</p>
<ol>
<li><a href="https://en.cppreference.com/w/cpp/numeric/bit_cast"><code>std::bit_cast</code></a> (c++20)</li>
<li><a href="https://godbolt.org/z/3PM4jGvEz"><code>std::memcpy</code></a></li>
</ol>
<h2 id="variadic-templates-parameter-pack"><a class="header" href="#variadic-templates-parameter-pack">Variadic templates (<a href="https://en.cppreference.com/w/cpp/language/parameter_pack">parameter pack</a>)</a></h2>
<pre><code class="language-cpp">#include <iostream>
// -- Example 1 - print template value arguments.
// Base case with one parameter.
template<int P>
void show_int() {
printf("%d\n", P);
}
// General case with at least two parameters, to disambiguate from base case.
template<int P0, int P1, int... Params>
void show_int() {
printf("%d, ", P0);
show_int<P1, Params...>();
}
// -- Example 2 - print values of different types.
// Base case with one parameter.
template<typename T>
void show(const T& t) {
std::cout << t << '\n';
}
// General case with at least two parameters, to disambiguate from base case.
template<typename T0, typename T1, typename... Types>
void show(const T0& t0, const T1& t1, const Types&... types) {
std::cout << t0 << ", ";
show(t1, types...);
}
int main() {
show_int<1, 2, 3, 4, 5>();
show(1, 1.0, "foo", 'a');
}
</code></pre>
<h2 id="forwarding-reference-fwd-ref"><a class="header" href="#forwarding-reference-fwd-ref">Forwarding reference (<a href="https://en.cppreference.com/w/cpp/language/reference#Forwarding_references">fwd ref</a>)</a></h2>
<p>A <code>forwarding reference</code> is a special references that preserves the <code>value category</code> of a function parameter and therefore allows for <code>perfect</code>
forwarding.</p>
<p>A forwarding reference is a parameter of a function template, which is declared
as <code>rvalue</code> reference to a <code>non-cv</code> qualified <code>type</code> template parameter.</p>
<pre><code class="language-cpp">template<typename T>
void fn(T&& param); // param is a forwarding reference
</code></pre>
<p>Perfect forwarding can be achieved with <a href="https://en.cppreference.com/w/cpp/utility/forward"><code>std::forward</code></a>. This for
example allows a wrapper function to pass a parameter with the <strong>exact</strong> same
value category to a down-stream function which is being invoked in the wrapper.</p>
<pre><code class="language-cpp">#include <cstdio>
#include <utility>
struct M {};
// -- CONSUMER -----------------------------------------------------------------
void use(M&) {
puts(__PRETTY_FUNCTION__);
}
void use(M&&) {
puts(__PRETTY_FUNCTION__);
}
// -- TESTER -------------------------------------------------------------------
template<typename T>
void wrapper(T&& param) { // forwarding reference
puts(__PRETTY_FUNCTION__);
// PARAM is an lvalue, therefore this always calls use(M&).
use(param);
}
template<typename T>
void fwd_wrapper(T&& param) { // forwarding reference
puts(__PRETTY_FUNCTION__);
// PARAM is an lvalue, but std::forward returns PARAM with the same value
// category as the forwarding reference takes.
use(std::forward<T>(param));
}
// -- MAIN ---------------------------------------------------------------------
int main() {
{
std::puts("==> wrapper rvalue reference");
wrapper(M{});
// calls use(M&).
std::puts("==> wrapper lvalue reference");
struct M m;
wrapper(m);
// calls use(M&).
}
{
std::puts("==> fwd_wrapper rvalue reference");
fwd_wrapper(M{});
// calls use(M&&).
std::puts("==> fwd_wrapper lvalue reference");
struct M m;
fwd_wrapper(m);
// calls use(M&).
}
}
</code></pre>
<h2 id="example-any_of-template-meta-function"><a class="header" href="#example-any_of-template-meta-function">Example: <code>any_of</code> template meta function</a></h2>
<pre><code class="language-cpp">#include <type_traits>
template<typename T, typename... U>
struct any_of : std::false_type {};
// Found our type T in the list of types U.
template<typename T, typename... U>
struct any_of<T, T, U...> : std::true_type {};
// Pop off the first element in the list of types U,
// since it didn't match our type T.
template<typename T, typename U0, typename... U>
struct any_of<T, U0, U...> : any_of<T, U...> {};
// Convenience template variable to invoke meta function.
template<typename T, typename... U>
constexpr bool any_of_v = any_of<T, U...>::value;
static_assert(any_of_v<int, char, bool, int>, "");
static_assert(!any_of_v<int, char, bool, float>, "");
</code></pre>
<h2 id="example-sfinae-enable_if"><a class="header" href="#example-sfinae-enable_if">Example: <a href="https://en.cppreference.com/w/cpp/language/sfinae">SFINAE</a> (<a href="https://en.cppreference.com/w/cpp/types/enable_if">enable_if</a>)</a></h2>
<p>Provide a single entry point <code>Invoke</code> to call some <code>Operations</code>.
Use <code>enable_if</code> to enable/disable the template functions depending on the two
available traits an operation can have:</p>
<ul>
<li>Operation returns a result</li>
<li>Operation requires a context</li>
</ul>
<pre><code class="language-cpp">#include <iostream>
#include <type_traits>
// Helper meta fns.
template<typename T>
using enable_if_bool = std::enable_if_t<T::value, bool>;
template<typename T>
using disable_if_bool = std::enable_if_t<!T::value, bool>;
template<typename T>
using has_dst = std::integral_constant<bool, !std::is_same<typename T::Return, void>::value>;
// Template meta programming invoke machinery.
namespace impl {
// Invoke an OPERATION which *USES* a context.
template<typename Ctx, template<typename> class Op, typename... P,
enable_if_bool<typename Op<Ctx>::HasCtx> = true>
typename Op<Ctx>::Return Invoke(const Ctx& C, P... params) {
return Op<Ctx>()(C, params...);
}
// Invoke an OPERATION which uses *NO* context.
template<typename Ctx, template<typename> class Op, typename... P,
disable_if_bool<typename Op<Ctx>::HasCtx> = true>
typename Op<Ctx>::Return Invoke(const Ctx&, P... params) {
return Op<Ctx>()(params...);
}
} // namespace impl
// Invoke an OPERATION which *HAS* a DESTINATION with arbitrary number of arguments.
template<typename Ctx, template<typename> class Op, typename... P,
enable_if_bool<has_dst<Op<Ctx>>> = true>
void Invoke(const Ctx& C, P... params) {
std::cout << "Invoke " << Op<Ctx>::Name << '\n';
typename Op<Ctx>::Return R = impl::Invoke<Ctx, Op>(C, params...);
std::cout << "returned -> " << R << '\n';
}
// Invoke an OPERATION which has *NOT* a DESTINATION with arbitrary number of arguments.
template<typename Ctx, template<typename> class Op, typename... P,
disable_if_bool<has_dst<Op<Ctx>>> = true>
void Invoke(const Ctx& C, P... params) {
std::cout << "Invoke " << Op<Ctx>::Name << " without destination." << '\n';
impl::Invoke<Ctx, Op>(C, params...);
}
// Custom context.
struct Ctx {
void out(const char* s, unsigned v) const {
printf("%s%x\n", s, v);
}
};
// Operations to invoke.
template<typename Ctx>
struct OpA {
using HasCtx = std::false_type;
using Return = int;
static constexpr const char* const Name = "OpA";
constexpr Return operator()(int a, int b) const {
return a + b;
}
};
template<typename Ctx>
struct OpB {
using HasCtx = std::true_type;
using Return = void;
static constexpr const char* const Name = "OpB";
Return operator()(const Ctx& C, unsigned a) const {
C.out("a = ", a);
}
};
int main() {
Ctx C;
Invoke<Ctx, OpA>(C, 1, 2);
Invoke<Ctx, OpB>(C, 0xf00du);
return 0;
}
</code></pre>
<h2 id="example-minimal-templatized-test-registry"><a class="header" href="#example-minimal-templatized-test-registry">Example: Minimal templatized test registry</a></h2>
<p>A small test function registry bringing together a few different template
features.</p>
<pre><code class="language-cpp">#include <cstdio>
#include <functional>
#include <map>
#include <string>
#include <type_traits>
template<typename R, typename... P>
struct registry {
using FUNC = R (*)(P...);
using SELF = registry<R, P...>;
using RET = R;
static SELF& get() {
static SELF r;
return r;
}
bool add(std::string nm, FUNC fn) {
const auto r = m_fns.insert({std::move(nm), std::move(fn)});
return r.second;
}
R invoke(const std::string& nm, P... p) const {
return invoke_impl<R>(nm, p...);
}
void dump() const {
for (const auto& it : m_fns) {
std::puts(it.first.c_str());
}
}
private:
std::map<std::string, FUNC> m_fns;
template<typename RET>
std::enable_if_t<std::is_same_v<RET, void>> invoke_impl(const std::string& nm, P... p) const {
const auto it = m_fns.find(nm);
if (it == m_fns.end()) {
return;
}
std::invoke(it->second, p...);
}
template<typename RET>
std::enable_if_t<!std::is_same_v<RET, void>, RET> invoke_impl(const std::string& nm,
P... p) const {
const auto it = m_fns.find(nm);
if (it == m_fns.end()) {
static_assert(std::is_default_constructible_v<RET>,
"RET must be default constructible");
return {};
}
return std::invoke(it->second, p...);
}
};
#define TEST_REGISTER(REGISTRY, NAME) \
static bool regfn_##REGISTRY##NAME() { \
const bool r = REGISTRY::get().add(#NAME, NAME); \
if (!r) { \
std::puts("Failed to register test " #NAME ", same name already registered!"); \
std::abort(); \
} \
return r; \
} \
static const bool reg_##REGISTRY##NAME = regfn_##REGISTRY##NAME();
#define TEST(REGISTRY, NAME, ...) \
REGISTRY::RET NAME(__VA_ARGS__); \
TEST_REGISTER(REGISTRY, NAME); \
REGISTRY::RET NAME(__VA_ARGS__)
// -- Usage 1 simple usage.
using REG1 = registry<void>;
TEST(REG1, test1) {
std::puts("REG1::test1");
}
TEST(REG1, test2) {
std::puts("REG1::test2");
}
// -- Usage 2 with convenience macro wrapper.
using REG2 = registry<void, bool>;
#define TEST2(NAME, ...) TEST(REG2, NAME, ##__VA_ARGS__)
TEST2(test1, bool val) {
printf("REG2::test1 val %d\n", val);
}
int main() {
const auto& R1 = REG1::get();
R1.dump();
R1.invoke("test1");
R1.invoke("test2");
const auto& R2 = REG2::get();
R2.dump();
R2.invoke("test1", true);
return 0;
}
</code></pre>
<h2 id="example-concepts-pre-c20"><a class="header" href="#example-concepts-pre-c20">Example: Concepts pre c++20</a></h2>
<p>Prior to c++20's concepts, <code>SFINAE</code> and <code>std::void_t</code> can be leveraged to build
something similar allowing to define an interface (aka trait) for a template
parameter.</p>
<pre><code class="language-cpp">
template<typename T, template<typename> class Checker, typename = void>
struct is_valid : std::false_type {};
template<typename T, template<typename> class Checker>
struct is_valid<T, Checker, std::void_t<Checker<T>>> : std::true_type {};
template<typename T, template<typename> class Checker>
static constexpr bool is_valid_v = is_valid<T, Checker>::value;
// -----------------------------------------------------------------------------
template<typename T, typename R, template<typename> class Checker, typename = void>
struct is_valid_with_ret : std::false_type {};
template<typename T, typename R, template<typename> class Checker>
struct is_valid_with_ret<T, R, Checker, std::void_t<Checker<T>>> : std::is_same<R, Checker<T>> {};
template<typename T, typename R, template<typename> class Checker>
static constexpr bool is_valid_with_ret_v = is_valid_with_ret<T, R, Checker>::value;
// -----------------------------------------------------------------------------
template<typename T>
struct is_entry {
template<typename TT>
using init = decltype(std::declval<TT>().init());
template<typename TT>
using tag = decltype(std::declval<TT>().tag());
template<typename TT>
using val = decltype(std::declval<TT>().val());
static constexpr bool value = is_valid_v<T, init> && is_valid_with_ret_v<T, int, tag> &&
is_valid_with_ret_v<T, typename T::Type, val>;
};
template<typename T>
static constexpr bool is_entry_v = is_entry<T>::value;
template<typename E>
struct Entry {
using Type = E;
void init();
int tag() const;
E val() const;
};
int main() {
static_assert(is_entry_v<Entry<bool>>, "");
}
</code></pre>
<p>The main mechanic can be explained with the following reduced example. If one
of the <code>decltype(std:declval<T>...</code> expressions is ill-formed, the template
specialization for <code>is_valid</code> will be removed from the candidate set due to
<a href="https://en.cppreference.com/w/cpp/language/sfinae">SFINAE</a>.</p>
<pre><code class="language-cpp">#include <type_traits>
// (1) Primary template.
template<typename T, typename = void>
struct is_valid : std::false_type {};
// (2) Partial template specialization.
template<typename T>
struct is_valid<T, std::void_t<decltype(std::declval<T>().some_fun1()),
decltype(std::declval<T>().some_fun2())>> : std::true_type {};
struct A {
void some_fun1() {}
void some_fun2() {}
};
struct B {};
static_assert(is_valid<A>::value, "is true");
// * Compare template arg list with primary template, we only supplied one
// arg, the second one will be defaulted as
// is_valid<A, void>
// * Compare template arg list against available specializations, this will
// try to match the pattern <A, void> against the patterns defined in the
// partial specializations.
// * Try specialization (2)
// * T -> A
// * Evaluate std::void_t -> decltype's are well-formed
// std::void_t<...> -> void
// * Specialization (2) matches <A, void>
// * Pick the most specialized version -> (2)
static_assert(!is_valid<B>::value, "is false");
// * Compare template arg list with primary template, we only supplied one
// arg, the second one will be defaulted as
// is_valid<A, void>
// * Compare template arg list against available specializations, this will
// try to match the pattern <B, void> against the patterns defined in the
// partial specializations.
// * Try specialization (2)
// * T -> B
// * Evaluate std::void_t -> decltype's are ill-formed
// * Specialization (2) is removed from candidate set, no hard error (SFINAE)
// * No specialization matches, take the primary template.
</code></pre>
<blockquote>
<p><code>std::declval<T>()</code> creates an instance of type T in an unevaluated context.</p>
</blockquote>
<p>A more detailed description is available in the SO discussion <a href="https://stackoverflow.com/a/27688405">How does
<code>void_t</code> work</a>.</p>
<h2 id="template-selection-with-partially--fully-specializations"><a class="header" href="#template-selection-with-partially--fully-specializations">Template selection with partially / fully specializations.</a></h2>
<pre><code class="language-cpp">enum Kind {
kPrimary,
kTT,
kIntBool,
kIntInt,
};
// (1) Primary template.
template<typename T, typename U = bool>
struct pair {
static constexpr Kind kind = kPrimary;
};
// (2) Partial template specialization.
template<typename T>
struct pair<T, T> {
static constexpr Kind kind = kTT;
};
// (3) Template specialization.
template<>
struct pair<int, bool> {
static constexpr Kind kind = kIntBool;
};
// (4) Template specialization.
template<>
struct pair<int, int> {
static constexpr Kind kind = kIntInt;
};
int main() {
static_assert(pair<int>::kind == kIntBool, "");
// * Compare template arg list with primary template, we only supplied one
// arg, the second one will be defaulted as
// pair<int, bool>
// * Compare template arg list against available specializations, this will
// try to match the pattern <int, bool> against the patterns defined in the
// partial specializations.
// * (2) <int, bool> pattern does not match
// * (3) <int, bool> pattern does match
// * (4) <int, bool> pattern does not match
// * Pick the most specialized version -> (3)
static_assert(pair<char, char>::kind == kTT, "");
// * Compare template arg list against available specializations, this will
// try to match the pattern <char, char> against the patterns defined in the
// partial specializations.
// * (2) <char, char> pattern does match
// * (3) <char, char> pattern does not match
// * (4) <char, char> pattern does not match
// * Pick the most specialized version -> (2)
static_assert(pair<int, int>::kind == kIntInt, "");
// * Compare template arg list against available specializations, this will
// try to match the pattern <int, int> against the patterns defined in the
// partial specializations.
// * (2) <int, int> pattern does match
// * (3) <int, int> pattern does match
// * (4) <int, int> pattern does not match
// * Pick the most specialized version -> (3)
static_assert(pair<char, short>::kind == kPrimary, "");
// * Compare template arg list against available specializations, this will
// try to match the pattern <char, short> against the patterns defined in the
// partial specializations.
// * (2) <char, short> pattern does not match
// * (3) <char, short> pattern does not match
// * (4) <char, short> pattern does not match
// * No specialization matches, take the primary template.
}
</code></pre>
<h1 id="example-perfect-forwarding"><a class="header" href="#example-perfect-forwarding">Example: Perfect forwarding</a></h1>
<pre><code class="language-cpp">#include <cassert>
#include <cstdio>
#include <new>
#include <type_traits>
#include <utility>
struct S {};
struct M {
M() {
std::puts("M()");
}
M(const M&) {
std::puts("M(M&)");
}
M(M&&) {
std::puts("M(M&&)");
}
M& operator=(const M&) = delete;
M& operator=(M&&) = delete;
M(S&, int) {
std::puts("M(S&)");
}
M(S&&, int) {
std::puts("M(S&&)");
}
~M() {
std::puts("~M()");
}
};
template<typename T>
struct option {
static_assert(!std::is_reference_v<T>);
constexpr option() = default;
template<typename... Params>
constexpr option(Params&&... params) : m_has_val(true) {
// BAD: does not perfectly forward!
// eg, if option(S&&) is invoked, this would invoke M(S&).
// new (&m_val) T(params...);
// GOOD: perfectly forwards params to constructor of T.
new (m_val) T(std::forward<Params>(params)...);
}
~option() {
reset();
}
constexpr T& value() {
assert(m_has_val);
// Placement new starts a new lifetime, launder pointer returned to the
// aligned storage.
//
// [1] https://en.cppreference.com/w/cpp/utility/launder
return *__builtin_launder(reinterpret_cast<T*>(m_val));
}
private:
constexpr void reset() {
if (!m_has_val) {
return;
}
if constexpr (!std::is_trivially_destructible_v<T>) {
value().~T();
};
}
alignas(T) char m_val[sizeof(T)];
bool m_has_val{false};
};
int main() {
std::puts("==> case 1");
// invokes M(S&&, int)
option<M> opt1(S{}, 123);
std::puts("==> case 2");
// invokes M() + M(M&&)
option<M> x /* option(M&&) + M(M&&) */ = M{} /* M() */;
}
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="glibc"><a class="header" href="#glibc">glibc</a></h1>
<h2 id="malloc-tracer-mtrace3"><a class="header" href="#malloc-tracer-mtrace3">malloc tracer <a href="http://man7.org/linux/man-pages/man3/mtrace.3.html"><code>mtrace(3)</code></a></a></h2>
<p>Trace memory allocation and de-allocation to detect memory leaks.
Need to call <code>mtrace(3)</code> to install the tracing hooks.</p>
<p>If we can't modify the binary to call <code>mtrace</code> we can create a small shared
library and pre-load it.</p>
<pre><code class="language-c">// libmtrace.c
#include <mcheck.h>
__attribute__((constructor)) static void init_mtrace() { mtrace(); }
</code></pre>
<p>Compile as:</p>
<pre><code class="language-bash">gcc -shared -fPIC -o libmtrace.so libmtrace.c
</code></pre>
<p>To generate the trace file run:</p>
<pre><code class="language-bash">export MALLOC_TRACE=<file>
LD_PRELOAD=./libmtrace.so <binary>
</code></pre>
<p><strong>Note</strong>: If <code>MALLOC_TRACE</code> is not set <code>mtrace</code> won't install tracing hooks.</p>
<p>To get the results of the trace file:</p>
<pre><code class="language-bash">mtrace <binary> $MALLOC_TRACE
</code></pre>
<h2 id="malloc-check-mallopt3"><a class="header" href="#malloc-check-mallopt3">malloc check <a href="http://man7.org/linux/man-pages/man3/mallopt.3.html"><code>mallopt(3)</code></a></a></h2>
<p>Configure action when glibc detects memory error.</p>
<pre><code class="language-bash">export MALLOC_CHECK_=<N>
</code></pre>
<p>Useful values:</p>
<pre><code class="language-markdown">1 print detailed error & continue
3 print detailed error + stack trace + memory mappings & abort
7 print simple error message + stack trace + memory mappings & abort
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="gcc1"><a class="header" href="#gcc1">gcc(1)</a></h1>
<h2 id="cli-2"><a class="header" href="#cli-2">CLI</a></h2>
<h3 id="preprocessing"><a class="header" href="#preprocessing">Preprocessing</a></h3>
<p>While debugging can be helpful to just pre-process files.</p>
<pre><code class="language-bash">gcc -E [-dM] ...
</code></pre>
<ul>
<li><code>-E</code> run only preprocessor</li>
<li><code>-dM</code> list only <code>#define</code> statements</li>
<li><code>-###</code> dry-run, outputting exact compiler/linker invocations</li>
<li><code>-print-multi-lib</code> print available multilib configurations</li>
<li><code>--help=<class></code> print description of cmdline options for given class, eg
<code>warnings</code>, <code>optimizers</code>, <code>target</code>, <code>c</code>, <code>c++</code></li>
</ul>
<h3 id="target-options"><a class="header" href="#target-options">Target options</a></h3>
<pre><code class="language-bash"># List all target options with their description.
gcc --help=target
# Configure for current cpu arch and query (-Q) value of options.
gcc -march=native -Q --help=target
</code></pre>
<h3 id="warnings--optimizations"><a class="header" href="#warnings--optimizations">Warnings / optimizations</a></h3>
<pre><code class="language-bash"># List available warnings with short description.
gcc --help=warnings
# List available optimizations with short description.
gcc --help=optimizers
# Prepend --help with `-Q` to print wheter options are enabled or disabled
# instead showing their description.
</code></pre>
<h2 id="builtins"><a class="header" href="#builtins"><a href="https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html">Builtins</a></a></h2>
<h3 id="__builtin_expectexpr-cond"><a class="header" href="#__builtin_expectexpr-cond"><code>__builtin_expect(expr, cond)</code></a></h3>
<p>Give the compiler a hint which branch is hot, so it can lay out the code
accordingly to reduce number of jump instructions.
See on <a href="https://godbolt.org/z/MbTHAP">compiler explorer</a>.</p>
<p>The semantics of this hint are as follows, the compiler prioritises <code>expr == cond</code>. So <code>__builtin_expect(expr, 0)</code> means that we expect the <code>expr</code> to be <code>0</code>
most of the time.</p>
<pre><code class="language-bash">echo "
extern void foo();
extern void bar();
void run0(int x) {
if (__builtin_expect(x,0)) { foo(); }
else { bar(); }
}
void run1(int x) {
if (__builtin_expect(x,1)) { foo(); }
else { bar(); }
}
" | gcc -O2 -S -masm=intel -o /dev/stdout -xc -
</code></pre>
<p>Will generate something similar to the following.</p>
<ul>
<li><code>run0</code>: <code>bar</code> is on the path without branch</li>
<li><code>run1</code>: <code>foo</code> is on the path without branch</li>
</ul>
<pre><code class="language-x86asm">run0:
test edi, edi
jne .L4
xor eax, eax
jmp bar
.L4:
xor eax, eax
jmp foo
run1:
test edi, edi
je .L6
xor eax, eax
jmp foo
.L6:
xor eax, eax
jmp bar
</code></pre>
<h2 id="abi-linux"><a class="header" href="#abi-linux">ABI (Linux)</a></h2>
<ul>
<li>C ABI (x86_64) - <a href="https://gitlab.com/x86-psABIs/x86-64-ABI">SystemV ABI</a></li>
<li>C++ ABI - <a href="https://itanium-cxx-abi.github.io/cxx-abi">C++ Itanium ABI</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="cmake1"><a class="header" href="#cmake1">cmake(1)</a></h1>
<h2 id="private--public--interface"><a class="header" href="#private--public--interface"><code>PRIVATE</code> / <code>PUBLIC</code> / <code>INTERFACE</code></a></h2>
<p>These modifier control where properties for a given target are visible.</p>
<ul>
<li><code>PRIVATE</code>: Only for the target itself.</li>
<li><code>INTERFACE</code>: Only for anyone linking against the target.</li>
<li><code>PUBLIC</code>: For the target itself and anyone linking against it (effectively
<code>PRIVATE</code> + <code>INTERFACE</code>).</li>
</ul>
<p>The following gives an example for preprocessor definitions specified on a
library target. This behaves in the same way for other properties like for
example include directories.</p>
<pre><code class="language-cmake"># CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(moose)
# -- LIBRARY
add_library(liba STATIC liba.cc)
target_compile_definitions(liba PUBLIC DEF_PUBLIC)
target_compile_definitions(liba PRIVATE DEF_PRIVATE)
target_compile_definitions(liba INTERFACE DEF_INTERFACE)
# -- APPLICATION
add_executable(main main.cc)
target_link_libraries(main liba)
</code></pre>
<pre><code class="language-sh">> touch liba.cc; echo "int main() {}" > main.cc
> cmake -B build -S . -G Ninja
> ninja -C build -j1 --verbose
[1/4] /usr/bin/c++ -DDEF_PRIVATE -DDEF_PUBLIC [..] .../liba.cc
[2/4] [..]
[3/4] /usr/bin/c++ -DDEF_INTERFACE -DDEF_PUBLIC [..] .../main.cc
[4/4] [..]
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="make1"><a class="header" href="#make1">make(1)</a></h1>
<h2 id="anatomy-of-make-rules"><a class="header" href="#anatomy-of-make-rules">Anatomy of <code>make</code> rules</a></h2>
<pre><code class="language-make">target .. : prerequisite ..
recipe
..
</code></pre>
<ul>
<li><code>target</code>: an output generated by the rule</li>
<li><code>prerequisite</code>: an input that is used to generate the target</li>
<li><code>recipe</code>: list of actions to generate the output from the input</li>
</ul>
<blockquote>
<p>Use <code>make -p</code> to print all rules and variables (implicitly + explicitly defined).</p>
</blockquote>
<h2 id="pattern-rules--automatic-variables"><a class="header" href="#pattern-rules--automatic-variables">Pattern rules & Automatic variables</a></h2>
<h3 id="pattern-rules"><a class="header" href="#pattern-rules">Pattern rules</a></h3>
<p>A pattern rule contains the <code>%</code> char (exactly one of them) and look like this example:</p>
<pre><code class="language-make">%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
</code></pre>
<p>The target matches files of the pattern <code>%.o</code>, where <code>%</code> matches any none-empty
substring and other character match just them self.</p>
<p>The substring matched by <code>%</code> is called the <code>stem</code>.</p>
<p><code>%</code> in the prerequisite stands for the matched <code>stem</code> in the target.</p>
<h3 id="automatic-variables"><a class="header" href="#automatic-variables">Automatic variables</a></h3>
<p>As targets and prerequisites in pattern rules can't be spelled explicitly in
the recipe, make provides a set of automatic variables to work with:</p>
<ul>
<li><code>$@</code>: Name of the target that triggered the rule.</li>
<li><code>$<</code>: Name of the first prerequisite.</li>
<li><code>$^</code>: Names of all prerequisites (without duplicates).</li>
<li><code>$+</code>: Names of all prerequisites (with duplicates).</li>
<li><code>$*</code>: Stem of the pattern rule.</li>
</ul>
<pre><code class="language-make"># file: Makefile
all: foobar blabla
foo% bla%: aaa bbb bbb
@echo "@ = $@"
@echo "< = $<"
@echo "^ = $^"
@echo "+ = $+"
@echo "* = $*"
@echo "----"
aaa:
bbb:
</code></pre>
<p>Running above <code>Makefile</code> gives:</p>
<pre><code class="language-text">@ = foobar
< = aaa
^ = aaa bbb
+ = aaa bbb bbb
* = bar
----
@ = blabla
< = aaa
^ = aaa bbb
+ = aaa bbb bbb
* = bla
----
</code></pre>
<p>Variables related to filesystem paths:</p>
<ul>
<li><code>$(CURDIR)</code>: Path of current working dir after using <code>make -C path</code></li>
</ul>
<h2 id="arguments"><a class="header" href="#arguments">Arguments</a></h2>
<p>Arguments specified on the command line <em>override</em> ordinary variable
assignments in the makefile (<a href="https://www.gnu.org/software/make/manual/html_node/Overriding.html">overriding variables</a>).</p>
<pre><code class="language-make">VAR = abc
all:
@echo VAR=$(VAR)
</code></pre>
<pre><code class="language-text"># make
VAR=abc
# make VAR=123
VAR=123
</code></pre>
<h2 id="useful-functions"><a class="header" href="#useful-functions">Useful functions</a></h2>
<h3 id="substitution-references"><a class="header" href="#substitution-references">Substitution references</a></h3>
<p>Substitute strings matching pattern in a list.</p>
<pre><code class="language-make">in := a.o l.a c.o
out := $(in:.o=.c)
# => out = a.c l.a c.c
</code></pre>
<h3 id="patsubst-ref"><a class="header" href="#patsubst-ref"><code>patsubst</code> (<a href="https://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-patsubst-1">ref</a>)</a></h3>
<pre><code class="language-make">in := a.c b.c
out := $(patsubst %.c, build/%.o, $(in))
# => out = build/a.o build/b.o
# This is actually equivalent to $(in:%.c=build/%.o)
</code></pre>
<h3 id="filter"><a class="header" href="#filter"><code>filter</code></a></h3>
<p>Keep strings matching a pattern in a list.</p>
<pre><code class="language-make">in := a.a b.b c.c d.d
out := $(filter %.b %.c, $(in))
# => out = b.b c.c
</code></pre>
<h3 id="filter-out"><a class="header" href="#filter-out"><code>filter-out</code></a></h3>
<p>Remove strings matching a pattern from a list.</p>
<pre><code class="language-make">in := a.a b.b c.c d.d
out := $(filter-out %.b %.c, $(in))
# => out = a.a d.d
</code></pre>
<h3 id="abspath"><a class="header" href="#abspath"><code>abspath</code></a></h3>
<p>Resolve each file name as absolute path (don't resolve symlinks).</p>
<pre><code class="language-make">$(abspath fname1 fname2 ..)
### `realpath`
Resolve each file name as canonical path.
```make
$(realpath fname1 fname2 ..)
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="ldso8"><a class="header" href="#ldso8">ld.so(8)</a></h1>
<h2 id="environment-variables"><a class="header" href="#environment-variables">Environment Variables</a></h2>
<pre><code class="language-console"> LD_PRELOAD=<l_so> colon separated list of libso's to be pre loaded
LD_DEBUG=<opts> comma separated list of debug options
=help list available options
=libs show library search path
=files processing of input files
=symbols show search path for symbol lookup
=bindings show against which definition a symbol is bound
</code></pre>
<h3 id="ld_library_path-and-dlopen3"><a class="header" href="#ld_library_path-and-dlopen3">LD_LIBRARY_PATH and dlopen(3)</a></h3>
<p>When dynamically loading a shared library during program runtime with
<code>dlopen(3)</code>, only the <code>LD_LIBRARY_PATH</code> as it was during program startup is
evaluated.
Therefore the following is a code smell:</p>
<pre><code class="language-c">// at startup LD_LIBRARY_PATH=/moose
// Assume /foo/libbar.so
setenv("LD_LIBRARY_PATH", "/foo", true /* overwrite */);
// Will look in /moose and NOT in /foo.
dlopen("libbar.so", RTLD_LAZY);
</code></pre>
<h2 id="ld_preload-initialization-order-and-link-map"><a class="header" href="#ld_preload-initialization-order-and-link-map">LD_PRELOAD: Initialization Order and Link Map</a></h2>
<p>Libraries specified in <code>LD_PRELOAD</code> are loaded from <code>left-to-right</code> but
initialized from <code>right-to-left</code>.</p>
<pre><code class="language-markdown"> > ldd ./main
>> libc.so.6 => /usr/lib/libc.so.6
> LD_PRELOAD=liba.so:libb.so ./main
-->
preloaded in this order
<--
initialized in this order
</code></pre>
<p>The preload order determines:</p>
<ul>
<li>the order libraries are inserted into the <code>link map</code></li>
<li>the initialization order for libraries</li>
</ul>
<p>For the example listed above the resulting <code>link map</code> will look like the
following:</p>
<pre><code class="language-makrdown"> +------+ +------+ +------+ +------+
| main | -> | liba | -> | libb | -> | libc |
+------+ +------+ +------+ +------+
</code></pre>
<p>This can be seen when running with <code>LD_DEBUG=files</code>:</p>
<pre><code class="language-makrdown"> > LD_DEBUG=files LD_PRELOAD=liba.so:libb.so ./main
# load order (-> determines link map)
>> file=liba.so [0]; generating link map
>> file=libb.so [0]; generating link map
>> file=libc.so.6 [0]; generating link map
# init order
>> calling init: /usr/lib/libc.so.6
>> calling init: <path>/libb.so
>> calling init: <path>/liba.so
>> initialize program: ./main
</code></pre>
<p>To verify the <code>link map</code> order we let <code>ld.so</code> resolve the <code>memcpy(3)</code> libc
symbol (used in <em>main</em>) dynamically, while enabling <code>LD_DEBUG=symbols,bindings</code>
to see the resolving in action.</p>
<pre><code class="language-makrdown"> > LD_DEBUG=symbols,bindings LD_PRELOAD=liba.so:libb.so ./main
>> symbol=memcpy; lookup in file=./main [0]
>> symbol=memcpy; lookup in file=<path>/liba.so [0]
>> symbol=memcpy; lookup in file=<path>/libb.so [0]
>> symbol=memcpy; lookup in file=/usr/lib/libc.so.6 [0]
>> binding file ./main [0] to /usr/lib/libc.so.6 [0]: normal symbol `memcpy' [GLIBC_2.14]
</code></pre>
<h2 id="dynamic-linking-x86_64"><a class="header" href="#dynamic-linking-x86_64">Dynamic Linking (x86_64)</a></h2>
<p>Dynamic linking basically works via one indirect jump. It uses a combination of
function trampolines (<code>.plt</code> section) and a function pointer table (<code>.got.plt</code>
section).
On the first call the trampoline sets up some metadata and then jumps to the
<code>ld.so</code> runtime resolve function, which in turn patches the table with the
correct function pointer.</p>
<pre><code class="language-makrdown"> .plt ....... procedure linkage table, contains function trampolines, usually
located in code segment (rx permission)
.got.plt ... global offset table for .plt, holds the function pointer table
</code></pre>
<p>Using <code>radare2</code> we can analyze this in more detail:</p>
<pre><code class="language-makrdown"> [0x00401040]> pd 4 @ section..got.plt
;-- section..got.plt:
;-- .got.plt: ; [22] -rw- section size 32 named .got.plt
;-- _GLOBAL_OFFSET_TABLE_:
[0] 0x00404000 .qword 0x0000000000403e10 ; section..dynamic
[1] 0x00404008 .qword 0x0000000000000000
; CODE XREF from section..plt @ +0x6
[2] 0x00404010 .qword 0x0000000000000000
;-- reloc.puts:
; CODE XREF from sym.imp.puts @ 0x401030
[3] 0x00404018 .qword 0x0000000000401036 ; RELOC 64 puts
[0x00401040]> pd 6 @ section..plt
;-- section..plt:
;-- .plt: ; [12] -r-x section size 32 named .plt
┌─> 0x00401020 ff35e22f0000 push qword [0x00404008]
╎ 0x00401026 ff25e42f0000 jmp qword [0x00404010]
╎ 0x0040102c 0f1f4000 nop dword [rax]
┌ 6: int sym.imp.puts (const char *s);
└ ╎ 0x00401030 ff25e22f0000 jmp qword [reloc.puts]
╎ 0x00401036 6800000000 push 0
└─< 0x0040103b e9e0ffffff jmp sym..plt
</code></pre>
<ul>
<li>At address <code>0x00401030</code> in the <code>.plt</code> section we see the indirect jump for
<code>puts</code> using the function pointer in <code>_GLOBAL_OFFSET_TABLE_[3] (GOT)</code>.</li>
<li><code>GOT[3]</code> initially points to instruction after the <code>puts</code> trampoline
<code>0x00401036</code>.</li>
<li>This pushes the relocation index <code>0</code> and then jumps to the first trampoline
<code>0x00401020</code>.</li>
<li>The first trampoline jumps to <code>GOT[2]</code> which will be filled at program
startup by the <code>ld.so</code> with its resolve function.</li>
<li>The <code>ld.so</code> resolve function fixes the relocation referenced by the
relocation index pushed by the <code>puts</code> trampoline.</li>
<li>The relocation entry at index <code>0</code> tells the resolve function which symbol to
search for and where to put the function pointer:
<pre><code class="language-makrdown"> > readelf -r <main>
>> Relocation section '.rela.plt' at offset 0x4b8 contains 1 entry:
>> Offset Info Type Sym. Value Sym. Name + Addend
>> 000000404018 000200000007 R_X86_64_JUMP_SLO 0000000000000000 puts@GLIBC_2.2.5 + 0
</code></pre>
As we can see the offset from relocation at index <code>0</code> points to <code>GOT[3]</code>.</li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="elf-symbol-versioning"><a class="header" href="#elf-symbol-versioning">ELF Symbol Versioning</a></h1>
<p>The <a href="https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html">ELF symbol versioning</a> mechanism allows to attach version
information to symbols.
This can be used to express symbol version requirements or to provide certain
symbols multiple times in the same ELF file with different versions (eg for
backwards compatibility).</p>
<p>The <code>libpthread.so</code> library is an example which provides the
<code>pthread_cond_wait</code> symbol multiple times but in different versions.
With readelf the version of the symbol can be seen after the <code>@</code>.</p>
<pre><code class="language-bash">> readelf -W --dyn-syms /lib/libpthread.so
Symbol table '.dynsym' contains 342 entries:
Num: Value Size Type Bind Vis Ndx Name
...
141: 0000f080 696 FUNC GLOBAL DEFAULT 16 pthread_cond_wait@@GLIBC_2.3.2
142: 00010000 111 FUNC GLOBAL DEFAULT 16 pthread_cond_wait@GLIBC_2.2.5
</code></pre>
<p>The <code>@@</code> denotes the <strong>default symbol version</strong> which will be used during
static linking against the library.
The following dump shows that the <code>tmp</code> program linked against <code>lpthread</code> will
depend on the symbol version <code>GLIBC_2.3.2</code>, which is the default version.</p>
<pre><code class="language-bash">> echo "#include <pthread.h>
int main() {
return pthread_cond_wait(0,0);
}" | gcc -o tmp -xc - -lpthread;
readelf -W --dyn-syms tmp | grep pthread_cond_wait;
Symbol table '.dynsym' contains 7 entries:
Num: Value Size Type Bind Vis Ndx Name
...
2: 00000000 0 FUNC GLOBAL DEFAULT UND pthread_cond_wait@GLIBC_2.3.2 (2)
</code></pre>
<blockquote>
<p>Only <strong>one</strong> symbol can be annotated as the <code>@@</code> default version.</p>
</blockquote>
<p>Using the <code>--version-info</code> flag with readelf, more details on the symbol
version info compiled into the <code>tmp</code> ELF file can be obtained.</p>
<ul>
<li>The <code>.gnu.version</code> section contains the version definition for each symbol in
the <code>.dynsym</code> section. <code>pthread_cond_wait</code> is at index <code>2</code> in the <code>.dynsym</code>
section, the corresponding symbol version is at index <code>2</code> in the
<code>.gnu.version</code> section.</li>
<li>The <code>.gnu.version_r</code> section contains symbol version requirements per shared
library dependency (<code>DT_NEEDED</code> dynamic entry).</li>
</ul>
<pre><code class="language-bash">> readelf -W --version-info --dyn-syms tmp
Symbol table '.dynsym' contains 7 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTable
2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND pthread_cond_wait@GLIBC_2.3.2 (2)
3: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (3)
4: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
5: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable
6: 0000000000000000 0 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.2.5 (3)
Version symbols section '.gnu.version' contains 7 entries:
Addr: 0x0000000000000534 Offset: 0x000534 Link: 6 (.dynsym)
000: 0 (*local*) 0 (*local*) 2 (GLIBC_2.3.2) 3 (GLIBC_2.2.5)
004: 0 (*local*) 0 (*local*) 3 (GLIBC_2.2.5)
Version needs section '.gnu.version_r' contains 2 entries:
Addr: 0x0000000000000548 Offset: 0x000548 Link: 7 (.dynstr)
000000: Version: 1 File: libc.so.6 Cnt: 1
0x0010: Name: GLIBC_2.2.5 Flags: none Version: 3
0x0020: Version: 1 File: libpthread.so.0 Cnt: 1
0x0030: Name: GLIBC_2.3.2 Flags: none Version: 2
</code></pre>
<p>The gnu dynamic linker allows to inspect the version processing during runtime
by setting the <code>LD_DEBUG</code> environment variable accordingly.</p>
<pre><code class="language-text"># version: Display version dependencies.
> LD_DEBUG=versions ./tmp
717904: checking for version `GLIBC_2.2.5' in file /usr/lib/libc.so.6 [0] required by file ./tmp [0]
717904: checking for version `GLIBC_2.3.2' in file /usr/lib/libpthread.so.0 [0] required by file ./tmp [0]
...
# symbols : Display symbol table processing.
# bindings: Display information about symbol binding.
> LD_DEBUG=symbols,bindings ./tmp
...
718123: symbol=pthread_cond_wait; lookup in file=./tmp [0]
718123: symbol=pthread_cond_wait; lookup in file=/usr/lib/libpthread.so.0 [0]
718123: binding file ./tmp [0] to /usr/lib/libpthread.so.0 [0]: normal symbol `pthread_cond_wait' [GLIBC_2.3.2]
</code></pre>
<h2 id="example-version-script"><a class="header" href="#example-version-script">Example: version script</a></h2>
<p>The following shows an example C++ library <code>libfoo</code> which provides the same
symbol multiple times but in different versions.</p>
<pre><code class="language-cpp">// file: libfoo.cc
#include<stdio.h>
// Bind function symbols to version nodes.
//
// ..@ -> Is the unversioned symbol.
// ..@@.. -> Is the default symbol.
__asm__(".symver func_v0,func@");
__asm__(".symver func_v1,func@LIB_V1");
__asm__(".symver func_v2,func@@LIB_V2");
extern "C" {
void func_v0() { puts("func_v0"); }
void func_v1() { puts("func_v1"); }
void func_v2() { puts("func_v2"); }
}
__asm__(".symver _Z11func_cpp_v1i,_Z8func_cppi@LIB_V1");
__asm__(".symver _Z11func_cpp_v2i,_Z8func_cppi@@LIB_V2");
void func_cpp_v1(int) { puts("func_cpp_v1"); }
void func_cpp_v2(int) { puts("func_cpp_v2"); }
void func_cpp(int) { puts("func_cpp_v2"); }
</code></pre>
<p>Version script for <code>libfoo</code> which defines which symbols for which versions are
exported from the ELF file.</p>
<pre><code class="language-ld"># file: libfoo.ver
LIB_V1 {
global:
func;
extern "C++" {
"func_cpp(int)";
};
local:
*;
};
LIB_V2 {
global:
func;
extern "C++" {
"func_cpp(int)";
};
} LIB_V1;
</code></pre>
<blockquote>
<p>The <strong>local:</strong> section in <code>LIB_V1</code> is a catch all, that matches any symbol
not explicitly specified, and defines that the symbol is local and therefore
not exported from the ELF file.</p>
</blockquote>
<p>The library <code>libfoo</code> can be linked with the version definitions in <code>libfoo.ver</code>
by passing the version script to the linker with the <code>--version-script</code> flag.</p>
<pre><code class="language-bash">> g++ -shared -fPIC -o libfoo.so libfoo.cc -Wl,--version-script=libfoo.ver
> readelf -W --dyn-syms libfoo.so | c++filt
Symbol table '.dynsym' contains 14 entries:
Num: Value Size Type Bind Vis Ndx Name
...
6: 0000000000000000 0 OBJECT GLOBAL DEFAULT ABS LIB_V1
7: 000000000000114b 29 FUNC GLOBAL DEFAULT 13 func_cpp(int)@LIB_V1
8: 0000000000001168 29 FUNC GLOBAL DEFAULT 13 func_cpp(int)@@LIB_V2
9: 0000000000001185 29 FUNC GLOBAL DEFAULT 13 func_cpp(int)@@LIB_V1
10: 0000000000000000 0 OBJECT GLOBAL DEFAULT ABS LIB_V2
11: 0000000000001109 22 FUNC GLOBAL DEFAULT 13 func
12: 000000000000111f 22 FUNC GLOBAL DEFAULT 13 func@LIB_V1
13: 0000000000001135 22 FUNC GLOBAL DEFAULT 13 func@@LIB_V2
</code></pre>
<p>The following program demonstrates how to make use of the different versions:</p>
<pre><code class="language-cpp">// file: main.cc
#include <dlfcn.h>
#include <assert.h>
// Links against default symbol in the lib.so.
extern "C" void func();
int main() {
// Call the default version.
func();
#ifdef _GNU_SOURCE
typedef void (*fnptr)();
// Unversioned lookup.
fnptr fn_v0 = (fnptr)dlsym(RTLD_DEFAULT, "func");
// Version lookup.
fnptr fn_v1 = (fnptr)dlvsym(RTLD_DEFAULT, "func", "LIB_V1");
fnptr fn_v2 = (fnptr)dlvsym(RTLD_DEFAULT, "func", "LIB_V2");
assert(fn_v0 != 0);
assert(fn_v1 != 0);
assert(fn_v2 != 0);
fn_v0();
fn_v1();
fn_v2();
#endif
return 0;
}
</code></pre>
<p>Compiling and running results in:</p>
<pre><code class="language-bash">> g++ -o main main.cc -ldl ./libfoo.so && ./main
func_v2
func_v0
func_v1
func_v2
</code></pre>
<h2 id="references-3"><a class="header" href="#references-3">References</a></h2>
<ul>
<li><a href="https://akkadia.org/drepper/symbol-versioning">ELF Symbol Versioning</a></li>
<li><a href="https://sourceware.org/binutils/docs/ld/VERSION.html">Binutils ld: Symbol Versioning</a></li>
<li><a href="https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html">LSB: Symbol Versioning</a></li>
<li><a href="https://akkadia.org/drepper/dsohowto.pdf">How To Write Shared Libraries</a></li>
<li><a href="https://refspecs.linuxbase.org/elf/elf.pdf">LSB: ELF File Format</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="python"><a class="header" href="#python">python</a></h1>
<h2 id="decorator-run"><a class="header" href="#decorator-run">Decorator [<a href="https://www.online-python.com/IDdiE0gpYU">run</a>]</a></h2>
<p>Some decorator examples with type annotation.</p>
<pre><code class="language-python">from typing import Callable
def log(f: Callable[[int], None]) -> Callable[[int], None]:
def inner(x: int):
print(f"log::inner f={f.__name__} x={x}")
f(x)
return inner
@log
def some_fn(x: int):
print(f"some_fn x={x}")
def log_tag(tag: str) -> Callable[[Callable[[int], None]], Callable[[int], None]]:
def decorator(f: Callable[[int], None]) -> Callable[[int], None]:
def inner(x: int):
print(f"log_tag::inner f={f.__name__} tag={tag} x={x}")
f(x)
return inner
return decorator
@log_tag("some_tag")
def some_fn2(x: int):
print(f"some_fn2 x={x}")
</code></pre>
<h2 id="walrus-operator-run"><a class="header" href="#walrus-operator-run">Walrus operator [<a href="https://www.online-python.com/9T12PvmKVy">run</a>]</a></h2>
<p>Walrus operator <code>:=</code> added since <strong>python 3.8</strong>.</p>
<pre><code class="language-python">from typing import Optional
# Example 1: if let statements
def foo(ret: Optional[int]) -> Optional[int]:
return ret
if r := foo(None):
print(f"foo(None) -> {r}")
if r := foo(1337):
print(f"foo(1337) -> {r}")
# Example 2: while let statements
toks = iter(['a', 'b', 'c'])
while tok := next(toks, None):
print(f"{tok}")
# Example 3: list comprehension
print([tok for t in [" a", " ", " b "] if (tok := t.strip())])
</code></pre>
<h2 id="unittest-run"><a class="header" href="#unittest-run"><a href="https://docs.python.org/3/library/unittest.html">Unittest</a> [<a href="https://www.online-python.com/2fit4UcbzI">run</a>]</a></h2>
<p>Run unittests directly from the command line as <br />
<code>python3 -m unittest -v test</code></p>
<p>Optionally pass <code>-k <patter></code> to only run subset of tests.</p>
<pre><code class="language-python"># file: test.py
import unittest
class MyTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
# Tests need to start with the prefix 'test'.
def test_foo(self):
self.assertEqual(1 + 2, 3)
def test_bar(self):
with self.assertRaises(IndexError):
list()[0]
</code></pre>
<h2 id="doctest-run"><a class="header" href="#doctest-run"><a href="https://docs.python.org/3/library/doctest.html">Doctest</a> [<a href="https://www.online-python.com/LZst51UNIH">run</a>]</a></h2>
<p>Run doctests directly from the command line as <br />
<code>python -m doctest -v test.py</code></p>
<pre><code class="language-python"># file: test.py
def sum(a: int, b: int) -> int:
"""Sum a and b.
>>> sum(1, 2)
3
>>> sum(10, 20)
30
"""
return a + b
</code></pre>
<h2 id="timeit"><a class="header" href="#timeit"><a href="https://docs.python.org/3/library/timeit.html">timeit</a></a></h2>
<p>Micro benchmarking.</p>
<pre><code class="language-bash">python -m timeit '[x.strip() for x in ["a ", " b"]]'
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="gcov1"><a class="header" href="#gcov1">gcov(1)</a></h1>
<p>Generate code coverage reports in text format.</p>
<p>Compile the source files of interest and link the final binary with the
following flags:</p>
<ul>
<li><code>-fprofile-arcs</code> instruments the generated code such that it writes a <code>.gcda</code>
file when being executed with details about which branches are taken</li>
<li><code>-ftest-coverage</code> writes a <code>.gcno</code> notes file which is used by <code>gcov</code> during
generation of the coverage report</li>
</ul>
<p>Depending on the build environment one may also set <code>-fprofile-abs-path</code> to
generate absolute path names into the <code>.gcno</code> note files, this can ease setups
where compilations are done in different directories to the source directory.</p>
<blockquote>
<p><code>gcc</code> / <code>clang</code> also support an alias flag <code>--coverage</code> which during
compilation time is equivalent to <code>-fprofile-arcs -ftest-coverage</code> and during
link time <code>-lgcov</code>.</p>
</blockquote>
<p>After running the instrumented binary, the human readable report can then be
generated for a single file for example such as</p>
<pre><code class="language-shell">gcov <SRC FILE | OBJ FILE>
</code></pre>
<h2 id="example-6"><a class="header" href="#example-6">Example</a></h2>
<pre><code class="language-cpp">#include <cstdio>
void tell_me(int desc) {
if (desc & 1) {
std::puts("this");
} else {
std::puts("that");
}
}
int main(int argc, char *argv[]) {
tell_me(argc);
tell_me(argc);
return 0;
}
</code></pre>
<p>The <code>gcov</code> coverage report can be generated as follows for <code>gcc</code> or <code>clang</code>.</p>
<pre><code class="language-make">CXXFLAGS = -fprofile-arcs -ftest-coverage
# or the alias
#CXXFLAGS = --coverage
cov-gcc: clean
g++ $(CXXFLAGS) -c -o cov.o cov.cc
g++ $(CXXFLAGS) -o $@ cov.o
./$@
gcov --demangled-names cov.cc
cat cov.cc.gcov
.PHONY: cov-gcc
cov-clang: clean
clang++ $(CXXFLAGS) -c -o cov.o cov.cc
clang++ $(CXXFLAGS) -o $@ cov.o
./$@
llvm-cov gcov --demangled-names cov.cc
cat cov.cc.gcov
.PHONY: cov-clang
clean:
$(RM) *.gcov *.gcno *.gcda *.o cov-*
</code></pre>
<p>The will generate a report similar to the following.</p>
<pre><code class="language-text">cat cov.cc.gcov
-: 0:Source:cov.cc
-: 0:Graph:cov.gcno
-: 0:Data:cov.gcda
-: 0:Runs:1
-: 1:// Copyright (C) 2023 johannst
-: 2:
-: 3:#include <cstdio>
-: 4:
2: 5:void tell_me(int desc) {
2: 6: if (desc & 1) {
2: 7: std::puts("this");
-: 8: } else {
#####: 9: std::puts("that");
-: 10: }
2: 11:}
-: 12:
1: 13:int main(int argc, char *argv[]) {
1: 14: tell_me(argc);
1: 15: tell_me(argc);
1: 16: return 0;
-: 17:}
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="profile-guided-optimization-pgo"><a class="header" href="#profile-guided-optimization-pgo">Profile guided optimization (pgo)</a></h1>
<p><code>pgo</code> is an optimization technique to optimize a program for its usual
workload.</p>
<p>It is applied in two phases:</p>
<ol>
<li>Collect profiling data (best with representative benchmarks).</li>
<li>Optimize program based on collected profiling data.</li>
</ol>
<p>The following simple program is used as demonstrator.</p>
<pre><code class="language-c">#include <stdio.h>
#define NOINLINE __attribute__((noinline))
NOINLINE void foo() { puts("foo()"); }
NOINLINE void bar() { puts("bar()"); }
int main(int argc, char *argv[]) {
if (argc == 2) {
foo();
} else {
bar();
}
}
</code></pre>
<h2 id="clang"><a class="header" href="#clang">clang</a></h2>
<p>On the actual machine with <code>clang 15.0.7</code>, the following code is generated for
the <code>main()</code> function.</p>
<pre><code class="language-x86asm"># clang -o test test.c -O3
0000000000001160 <main>:
1160: 50 push rax
; Jump if argc != 2.
1161: 83 ff 02 cmp edi,0x2
1164: 75 09 jne 116f <main+0xf>
; foor() is on the hot path (fall-through).
1166: e8 d5 ff ff ff call 1140 <_Z3foov>
116b: 31 c0 xor eax,eax
116d: 59 pop rcx
116e: c3 ret
; bar() is on the cold path (branch).
116f: e8 dc ff ff ff call 1150 <_Z3barv>
1174: 31 c0 xor eax,eax
1176: 59 pop rcx
1177: c3 ret
</code></pre>
<p>The following shows how to compile with profiling instrumentation and how to
optimize the final program with the collected profiling data (<a href="https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization">llvm
pgo</a>).</p>
<p>The arguments to <code>./test</code> are chosen such that <code>9/10</code> runs call <code>bar()</code>, which
is currently on the <code>cold path</code>.</p>
<pre><code class="language-bash"># Compile test program with profiling instrumentation.
clang -o test test.cc -O3 -fprofile-instr-generate
# Collect profiling data from multiple runs.
for i in {0..10}; do
LLVM_PROFILE_FILE="prof.clang/%p.profraw" ./test $(seq 0 $i)
done
# Merge raw profiling data into single profile data.
llvm-profdata merge -o pgo.profdata prof.clang/*.profraw
# Optimize test program with profiling data.
clang -o test test.cc -O3 -fprofile-use=pgo.profdata
</code></pre>
<blockquote>
<p>NOTE: If <code>LLVM_PROFILE_FILE</code> is not given the profile data is written to
<code>default.profraw</code> which is re-written on each run. If the <code>LLVM_PROFILE_FILE</code>
contains a <code>%m</code> in the filename, a unique integer will be generated and
consecutive runs will update the same generated profraw file,
<code>LLVM_PROFILE_FILE</code> can specify a new file every time, however that requires
more storage in general.</p>
</blockquote>
<p>After optimizing the program with the profiling data, the <code>main()</code> function
looks as follows.</p>
<pre><code class="language-x86asm">0000000000001060 <main>:
1060: 50 push rax
; Jump if argc == 2.
1061: 83 ff 02 cmp edi,0x2
1064: 74 09 je 106f <main+0xf>
; bar() is on the hot path (fall-through).
1066: e8 e5 ff ff ff call 1050 <_Z3barv>
106b: 31 c0 xor eax,eax
106d: 59 pop rcx
106e: c3 ret
; foo() is on the cold path (branch).
106f: e8 cc ff ff ff call 1040 <_Z3foov>
1074: 31 c0 xor eax,eax
1076: 59 pop rcx
1077: c3 ret
</code></pre>
<h2 id="gcc"><a class="header" href="#gcc">gcc</a></h2>
<p>With <code>gcc 13.2.1</code> on the current machine, the optimizer puts <code>bar()</code> on the
<code>hot path</code> by default.</p>
<pre><code class="language-x86asm">0000000000001040 <main>:
1040: 48 83 ec 08 sub rsp,0x8
; Jump if argc == 2.
1044: 83 ff 02 cmp edi,0x2
1047: 74 0c je 1055 <main+0x15>
; bar () is on the hot path (fall-through).
1049: e8 22 01 00 00 call 1170 <_Z3barv>
104e: 31 c0 xor eax,eax
1050: 48 83 c4 08 add rsp,0x8
1054: c3 ret
; foo() is on the cold path (branch).
1055: e8 06 01 00 00 call 1160 <_Z3foov>
105a: eb f2 jmp 104e <main+0xe>
105c: 0f 1f 40 00 nop DWORD PTR [rax+0x0]
</code></pre>
<p>The following shows how to compile with profiling instrumentation and how to
optimize the final program with the collected profiling data.</p>
<p>The arguments to <code>./test</code> are chosen such that <code>2/3</code> runs call <code>foo()</code>, which
is currently on the <code>cold path</code>.</p>
<pre><code class="language-bash">gcc -o test test.cc -O3 -fprofile-generate
./test 1
./test 1
./test 2 2
gcc -o test test.cc -O3 -fprofile-use
</code></pre>
<blockquote>
<p>NOTE: Consecutive runs update the generated <code>test.gcda</code> profile data file
rather than re-write it.</p>
</blockquote>
<p>After optimizing the program with the profiling data, the <code>main()</code> function</p>
<pre><code class="language-x86asm">0000000000001040 <main.cold>:
; bar() is on the cold path (branch).
1040: e8 05 00 00 00 call 104a <_Z3barv>
1045: e9 25 00 00 00 jmp 106f <main+0xf>
0000000000001060 <main>:
1060: 51 push rcx
; Jump if argc != 2.
1061: 83 ff 02 cmp edi,0x2
1064: 0f 85 d6 ff ff ff jne 1040 <main.cold>
; for() is on the hot path (fall-through).
106a: e8 11 01 00 00 call 1180 <_Z3foov>
106f: 31 c0 xor eax,eax
1071: 5a pop rdx
1072: c3 ret
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="linux"><a class="header" href="#linux">Linux</a></h1>
<ul>
<li><a href="linux/./systemd.html">systemd</a></li>
<li><a href="linux/./coredump.html">coredump</a></li>
<li><a href="linux/./ptrace_scope.html">ptrace_scope</a></li>
<li><a href="linux/./cryptsetup.html">cryptsetup</a></li>
<li><a href="linux/./swap.html">swap</a></li>
<li><a href="linux/./input.html">input</a></li>
<li><a href="linux/./acl.html">acl</a></li>
<li><a href="linux/./zfs.html">zfs</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="systemd"><a class="header" href="#systemd">systemd</a></h1>
<h2 id="systemctl"><a class="header" href="#systemctl">systemctl</a></h2>
<p>Inspect units:</p>
<pre><code class="language-text">systemctl [opts] [cmd]
[opts]
--user
--type=TYPE List only given types eg, service, timer, socket (use --type=help for a list)
--state=STATE List only given states eg running, enabled (use --state=help for a list)
--failed List only failed services
[cmd]
list-units <pattern> List units in memory
status <unit> Show runtime status of unit
start <unit> Start a unit
stop <unit> Stop a unit
restart <unit> Restart a unit
reload <unit> Reload a unit
enable <unit> Enable a unit (persistent)
disable <unit> Disable a unit
cat <unit> Print unit file
show <unit> Show properties of unit
</code></pre>
<h3 id="example-list-failed-units"><a class="header" href="#example-list-failed-units">Example: List failed units</a></h3>
<pre><code class="language-bash"># List all system failed units.
systemctl --failed
# List all user failed units.
systemctl --user --failed
</code></pre>
<h3 id="example-trivial-user-unit"><a class="header" href="#example-trivial-user-unit">Example: Trivial user unit</a></h3>
<pre><code class="language-bash"># Generate unit
mkdir -p ~/.config/systemd/user
echo '[Unit]
Description=Test logger
[Service]
Type=oneshot
ExecStart=logger "Hello from test unit"' > ~/.config/systemd/user/test.service
# Run unit
systemctl --user start test
# See log message
journalctl --user -u test -n 5
</code></pre>
<h2 id="journalctl"><a class="header" href="#journalctl">journalctl</a></h2>
<p>Inspect journal logs:</p>
<pre><code class="language-text">journalctl [opts] [matches]
--user Current user journal (system by default)
-u <unit> Show logs for specified <unit>
-n <lines> Show only last <lines>
-f Follow journal
-g <pattern> Grep for <pattern>
</code></pre>
<p>Cleanup:</p>
<pre><code class="language-text">journalctl [opts]
--disk-usage Show current disk usage
--vacuum-size=<size> Reduce journal log to <size> (K/M/G)
</code></pre>
<h2 id="references-4"><a class="header" href="#references-4">References</a></h2>
<ul>
<li><a href="https://www.man7.org/linux/man-pages/man5/systemd.unit.5.html">man systemd.unit(5)</a></li>
<li><a href="https://www.man7.org/linux/man-pages/man5/systemd.service.5.html">man systemd.service(5)</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="core5"><a class="header" href="#core5">core(5)</a></h1>
<p>There are multiple requirements that must be satisfied that <code>coredumps</code> are
being generated, a full list can be found in <a href="https://man7.org/linux/man-pages/man5/core.5.html">core(5)</a>.</p>
<p>An important one is to configure the soft resource limit <code>RLMIT_CORE</code>
(typically as unlimited during debugging).
In a typical bash/zsh this can be done as</p>
<pre><code class="language-bash">ulimit -Sc unlimited
</code></pre>
<h2 id="naming-of-coredump-files"><a class="header" href="#naming-of-coredump-files">Naming of coredump files</a></h2>
<p>There are two important kernel configs to control the naming:</p>
<pre><code class="language-config">/proc/sys/kernel/core_pattern
<pattern> => Specifies a name pattern for the coredump file. This can
include certain FORMAT specifier.
|<cmdline> => Coredump is pipe through stdin to the user space process
specified by the cmdline, this can also contain FORMAT specifier.
FORMAT specifier (full list, see core(5)):
%E Pathname of the executable ('/' replaced by '!').
%p PID of the dumping process in its pid namespace.
%P PID of the dumping process in the initial pid namespace.
%u Real UID of dumping process.
%s Signal number causing the dump.
/proc/sys/kernel/core_uses_pid
1 => Append ".<pid>" suffic to the coredump file name
(pid of the dumping process).
0 => Do not append the suffix.
</code></pre>
<h2 id="control-which-segments-are-dumped"><a class="header" href="#control-which-segments-are-dumped">Control which segments are dumped</a></h2>
<p>Each process has a coredump filter defined in <code>/proc/<pid>/coredump_filter</code>
which specifies which memory segments are being dumped.
Filters are preseved across <code>fork/exec</code> calls and hence child processes inherit
the parents filters.</p>
<p>The filter is a bitmask where <code>1</code> indicates to dump the given type.</p>
<pre><code>From core(5):
bit 0 Dump anonymous private mappings.
bit 1 Dump anonymous shared mappings.
bit 2 Dump file-backed private mappings.
bit 3 Dump file-backed shared mappings.
bit 4 Dump ELF headers.
bit 5 Dump private huge pages.
bit 6 Dump shared huge pages.
bit 7 Dump private DAX pages.
bit 8 Dump shared DAX pages.
Default filter 0x33.
</code></pre>
<h1 id="some-examples-out-there"><a class="header" href="#some-examples-out-there">Some examples out there</a></h1>
<h2 id="coredumpctl-systemd"><a class="header" href="#coredumpctl-systemd">coredumpctl (systemd)</a></h2>
<pre><code class="language-bash"># List available coredumps.
coredumpctl list
TIME PID UID GID SIG COREFILE EXE SIZE
...
Fri 2022-03-11 12:10:48 CET 6363 1000 1000 SIGSEGV present /usr/bin/sleep 18.1K
# Get detailed info on specific coredump.
coredumpctl info 6363
# Debug specific coredump.
coredumpctl debug 6363
# Dump specific coredump to file.
coredumpctl dump 6363 -o <file>
</code></pre>
<h2 id="apport-ubuntu"><a class="header" href="#apport-ubuntu">apport (ubuntu)</a></h2>
<p>Known crash report locations:</p>
<ul>
<li><code>/var/crash</code></li>
</ul>
<p>To get to the raw coredump, crash reports can be unpacked as:</p>
<pre><code class="language-bash">apport-unpack <crash_repot> <dest_dir>
</code></pre>
<p>The coredump resides under <code><dest_dir>/CoreDump</code>.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="ptrace_scope"><a class="header" href="#ptrace_scope">ptrace_scope</a></h1>
<p>In case the kernel was compiled with the <code>yama</code> security module
(<code>CONFIG_SECURITY_YAMA</code>), tracing processes with <code>ptrace(2)</code> can be restricted.</p>
<pre><code class="language-config">/proc/sys/kernel/yama/ptrace_scope
0 => No restrictions.
1 => Restricted attach, only the following can attach
- A process in the parent hierarchy.
- A process with CAP_SYS_PTRACE.
- A process with the PID that the tracee allowed by via
PR_SET_PTRACER.
2 => Only processes with CAP_SYS_PTRACE in the user namespace of the tracee
can attach.
3 => No tracing allowed.
</code></pre>
<p>Further details in <a href="https://man7.org/linux/man-pages/man2/ptrace.2.html"><code>ptrace(2)</code></a>.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="cryptsetup8"><a class="header" href="#cryptsetup8"><a href="https://www.man7.org/linux/man-pages/man8/cryptsetup.8.html">cryptsetup(8)</a></a></h1>
<pre><code class="language-text">cryptsetup <action> [opts] <action args>
action:
open <dev> <name> --type <type> Open (decrypt) <dev> and map with <name>.
Mapped as /dev/mapper/<name>.
Type: {luks,plain,tcrypt,bitlk}
close <name> Close existing mapping <name>.
status <name> Print status for mapping <name>.
luksFormat <dev> Create new LUKS partition and set initial passphrase.
(Keyslot 0)
luksAddKey <dev> Add a new passphrase.
luksRemoveKey <dev> Remove existing passphrase.
luksChangeKey <dev> Change existing passphrase.
lusDump <dev> Dump LUKS header for device.
</code></pre>
<h2 id="example-create-luks-encrypted-disk"><a class="header" href="#example-create-luks-encrypted-disk">Example: Create <code>LUKS</code> encrypted disk.</a></h2>
<p>For this example we use a file as backing storage and set it up as
<a href="https://man7.org/linux/man-pages/man4/loop.4.html">loop(4)</a> device. The loop device can be replaced by any block
device file.</p>
<blockquote>
<p>Optional: Overwrite existing data on disk.<br />
<code>sudo dd if=/dev/urandom of=/dev/sdX bs=1M</code></p>
</blockquote>
<p>First create the backing file and setup the loop device.</p>
<pre><code class="language-sh"># Create 100MB file.
dd if=/dev/zero of=blkfile bs=1M count=100
# Attach file to first free (-f) loop device
sudo losetup -f ./blkfile
# List loop devices.
sudo losetup -l
# NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
# /dev/loop0 0 0 0 0 /home/johannst/blkfile 0 512
</code></pre>
<p>Create a new LUKS partition and format new filesystem.</p>
<pre><code class="language-sh"># Initialize LUKS partition and set initial passphrase.
sudo cryptsetup luksFormat /dev/loop0
file blkfile
# blkfile: LUKS encrypted file, ver 2 [, , sha256] UUID: 8...
# Open (decrypt) the LUKS device, it will be mapped under /dev/mapper/loop0.
sudo cryptsetup open --type luks /dev/loop0 loop0
# Format partition with new filesystem.
sudo mkfs.vfat /dev/mapper/loop0
lsblk -f
# NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
# loop0 crypto_LU 2 8...
# └─loop0 vfat FAT16 D... 83.8M 0% /home/johannst/mnt
# Close (re-encrypt) LUKS device.
sudo cryptsetup close loop0
</code></pre>
<h2 id="example-using-an-existing-luks-device"><a class="header" href="#example-using-an-existing-luks-device">Example: Using an existing LUKS device.</a></h2>
<pre><code class="language-sh"># Open (decrypt) the LUKS device, it will be mapped under /dev/mapper/loop0.
sudo cryptsetup open --type luks /dev/loop0 loop0
# Mount filesystem.
sudo mount /dev/mapper/loop0 <mntpoint>
# Use disk ...
# Unmount filesystem.
sudo umount <mntpoint>
# Close (re-encrypt) LUKS device.
sudo cryptsetup close loop0
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="swap"><a class="header" href="#swap">swap</a></h1>
<h2 id="list-active-swap-areas"><a class="header" href="#list-active-swap-areas">List active swap areas</a></h2>
<pre><code class="language-sh"># procfs
cat /proc/swaps
# cli tool
swapon --show
</code></pre>
<h2 id="manual-swapfile-setup"><a class="header" href="#manual-swapfile-setup">Manual swapfile setup</a></h2>
<pre><code class="language-sh"># One time:
# Create and initialize swapfile.
# mkswap will initialize swap area over full filesize by default.
sudo dd if=/dev/zero of=/swapfile bs=1G count=1
mkswap /swapfile
# Enable swap file (until next reboot).
swapon /swapfile
# Persistent setup of swap file.
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
# Disable swap file (until next reboot).
swapoff /swapfile
</code></pre>
<blockquote>
<p>Recommended file permissions <code>0600</code> and file owner <code>uid=0 (root)</code>.</p>
</blockquote>
<h2 id="using-dphys-swapfile-service"><a class="header" href="#using-dphys-swapfile-service">Using <code>dphys-swapfile</code> service.</a></h2>
<p>Dynamically computes size of swap file based on installed RAM.</p>
<pre><code class="language-sh"># Setup and enable swap based on /etc/dphys-swapfile.
dphys-swapfile setup
dphys-swapfile swapon
# Disable swap on configured file.
dphys-swapfile swapoff
</code></pre>
<blockquote>
<p>Usually comes with a script to be automatically run at system startup and shutdown.
For example as <code>systemd</code> service:</p>
<pre><code>systemctl status dphys-swapfile
</code></pre>
</blockquote>
<div style="break-before: page; page-break-before: always;"></div><h1 id="linux-input"><a class="header" href="#linux-input">Linux input</a></h1>
<p>Some notes on using <code>/dev/input/*</code> device driver files.</p>
<h2 id="mousex--mice"><a class="header" href="#mousex--mice">mouseX / mice</a></h2>
<p>These device files are created by the <a href="https://elixir.bootlin.com/linux/latest/source/drivers/input/mousedev.c#L842">mousedev</a> driver.</p>
<ul>
<li><code>/dev/input/mouseX</code> represents the input stream for a <em>SINGLE</em> mouse device.</li>
<li><code>/dev/input/mice</code> represents the merged input stream for <em>ALL</em> mouse devices.</li>
</ul>
<p>The data stream consists of <code>3 bytes</code> per <code>event</code>. An event is encoded as <code>(BTN, X, Y)</code>.</p>
<ul>
<li><code>BTN</code> button pressed</li>
<li><code>X</code> movement in x-direction <code>-1 -> left</code> and <code>1 -> right</code></li>
<li><code>Y</code> movement in y-direction <code>-1 -> down</code> and <code>1 -> up</code></li>
</ul>
<p>The raw data stream can be inspected as follows.</p>
<pre><code class="language-bash">sudo cat /dev/input/mice | od -tx1 -w3 -v
</code></pre>
<h2 id="eventx"><a class="header" href="#eventx">eventX</a></h2>
<p>These device files are created by the <a href="https://elixir.bootlin.com/linux/latest/source/drivers/input/evdev.c#L1337">evdev</a> driver.</p>
<ul>
<li><code>/dev/input/eventX</code> represents the generic input event interface a <em>SINGLE</em> input device.</li>
</ul>
<p>Input events are encoded as given by the <code>input_event</code> struct below. Reading
from the <code>eventX</code> device file will always yield whole number of input events.</p>
<pre><code class="language-c">struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;
};
</code></pre>
<p>On most 64bit machines the raw data stream can be inspected as follows.</p>
<pre><code class="language-bash">sudo cat /dev/input/event4 | od -tx1 -w24 -v
</code></pre>
<h2 id="identifying-device-files"><a class="header" href="#identifying-device-files">Identifying device files.</a></h2>
<p>To find out which device file is assigned to which input device the following
file <code>/proc/bus/input/devices</code> in the proc filesystem can be consulted.</p>
<p>This yields entries as follows and shows which <code>Handlers</code> are assigned to which
<code>Name</code>.</p>
<pre><code>I: Bus=0018 Vendor=04f3 Product=0033 Version=0000
N: Name="Elan Touchpad"
...
H: Handlers=event15 mouse0
...
</code></pre>
<h2 id="example-toying-with-devinputeventx"><a class="header" href="#example-toying-with-devinputeventx">Example: Toying with <code>/dev/input/eventX</code></a></h2>
<p>Once compiled, the example should be run as <code>sudo ./event /dev/input/eventX</code>.</p>
<pre><code class="language-c">#include <stdio.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <linux/input-event-codes.h>
struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;
};
const char* type(unsigned short t) {
static char buf[32];
const char* fmt = "0x%x";
switch (t) {
#define FMT(TYPE) case TYPE: fmt = #TYPE"(0x%x)"; break
FMT(EV_SYN);
FMT(EV_KEY);
FMT(EV_REL);
FMT(EV_ABS);
#undef FMT
}
snprintf(buf, sizeof(buf), fmt, t);
return buf;
}
const char* code(unsigned short c) {
static char buf[32];
const char* fmt = "0x%x";
switch (c) {
#define FMT(CODE) case CODE: fmt = #CODE"(0x%x)"; break
FMT(BTN_LEFT);
FMT(BTN_RIGHT);
FMT(BTN_MIDDLE);
FMT(REL_X);
FMT(REL_Y);
#undef FMT
}
snprintf(buf, sizeof(buf), fmt, c);
return buf;
}
const char* timefmt(const struct timeval* t) {
assert(t);
struct tm* lt = localtime(&t->tv_sec); // Returns pointer to static tm object.
static char buf[64];
strftime(buf, sizeof(buf), "%H:%M:%S", lt);
return buf;
}
int main(int argc, char* argv[]) {
assert(argc == 2);
int fd = open(argv[1], O_RDONLY);
assert(fd != -1);
struct input_event inp;
while (1) {
int ret = read(fd, &inp, sizeof(inp));
assert(ret == sizeof(inp));
printf("time: %s type: %s code: %s value: 0x%x\n",
timefmt(&inp.time), type(inp.type), code(inp.code), inp.value);
}
}
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="access-control-list-acl"><a class="header" href="#access-control-list-acl">access control list (acl)</a></h1>
<blockquote>
<p>This describes <code>POSIX</code> acl.</p>
</blockquote>
<p>The access control list provides a flexibel permission mechanism next to the
<code>UNIX</code> file permissions. This allows to specify fine grained permissions for
users/groups on filesystems.</p>
<p>Filesystems which support acl typically have an <code>acl</code> option, which must be
specified while mounting when it is not a default option.
Filesystems must be mounted with the <code>acl</code> option if not enabled as default
option.</p>
<p>Files or folder that have an <code>acl</code> defined, can be identified by the <code>+</code> sign
next to the UNIX permissions.</p>
<p>The following shows on example for a zfs filesystem.</p>
<pre><code class="language-bash"># mount | grep tank
tank on /tank type zfs (rw,xattr,noacl)
tank/foo on /tank/foo type zfs (rw,xattr,posixacl)
# ls -h /tank
drwxrwxr-x+ 2 root root 4 11. Jun 14:26 foo/
</code></pre>
<h2 id="show-acl-entries"><a class="header" href="#show-acl-entries">Show acl entries</a></h2>
<pre><code class="language-bash"># List current acl entries.
getfacl /tank/foo
</code></pre>
<h2 id="modify-acl-entries"><a class="header" href="#modify-acl-entries">Modify acl entries</a></h2>
<pre><code class="language-bash"># Add acl entry for user "user123".
setfacl -m "u:user123:rwx" /tank/foo
# Remove entry for user "user123".
setfacl -x "u:user123" /tank/foo
# Add acl entry for group "group456".
setfacl -m "g:group456:rx" /tank/foo
# Add acl entry for others.
setfacl -m "o:rx" /tank/foo
# Remove extended acl entries.
setfacl -b /tank/foo
</code></pre>
<h2 id="masking-of-acl-entries"><a class="header" href="#masking-of-acl-entries">Masking of acl entries</a></h2>
<p>The <code>mask</code> defines the maximum access rights that can be given to <strong>users</strong> and
<strong>groups</strong>.</p>
<pre><code class="language-bash"># Update the mask.
setfacl -m "m:rx" /tank/foo
# List acl entries.
getfacl /tank/foo
# file: tank/foo
# owner: root
# group: root
user::rwx
user:user123:rwx # effective:r-x
group::r-x
mask::r-x
other::rwx
</code></pre>
<h2 id="references-5"><a class="header" href="#references-5">References</a></h2>
<ul>
<li><a href="https://www.man7.org/linux/man-pages/man5/acl.5.html">acl(5)</a></li>
<li><a href="https://www.man7.org/linux/man-pages/man1/getfacl.1.html">getfacl(1)</a></li>
<li><a href="https://www.man7.org/linux/man-pages/man1/setfacl.1.html">setfacl(1)</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="zfs"><a class="header" href="#zfs">zfs</a></h1>
<p>Pools are managed with the <a href="https://openzfs.github.io/openzfs-docs/man/8/zpool.8.html"><code>zpool(8)</code></a> command and have the
following hierarchy:</p>
<ul>
<li><code>pool</code>: consists of one or more virtual devices (<code>vdev</code>)</li>
<li><code>vdev</code>: consists of one or more physical devices (<code>dev</code>) and come in
different kinds such as <a href="https://openzfs.github.io/openzfs-docs/man/7/zpoolconcepts.7.html#Virtual_Devices_(vdevs)"><code>disk</code>, <code>mirror</code>, <code>raidzX</code>, ...</a>
<ul>
<li><code>disk</code>: single physical disk (<code>vdev == dev</code>)</li>
<li><code>mirror</code>: data is identically replicated on all <code>devs</code> (requires at least 2
physical devices).</li>
</ul>
</li>
</ul>
<p>Data stored in a pool is distributed and stored across all <code>vdevs</code> by zfs.
Therefore a total failure of a single <code>vdev</code> can lead to total loss of a pool.</p>
<p>A <code>dataset</code> is a logical volume which can be created on top of a <code>pool</code>. Each
<code>dataset</code> can be configured with its own set of <code>properties</code> like
<a href="https://openzfs.github.io/openzfs-docs/man/7/zfsprops.7.html"><code>encryption</code>, <code>quota</code>, ...</a>.
Datasets are managed with the <a href="https://openzfs.github.io/openzfs-docs/man/8/zfs.8.html"><code>zfs(8)</code></a> command.</p>
<h2 id="zfs-pool-management"><a class="header" href="#zfs-pool-management">zfs pool management</a></h2>
<p>Pools are by default mounted at <code>/<POOL></code>.</p>
<h3 id="create-modify-and-destroy-zfs-pools"><a class="header" href="#create-modify-and-destroy-zfs-pools">Create, modify and destroy zfs pools</a></h3>
<pre><code class="language-bash"># Create a pool MOOSE with a two mirror vdevs.
zpool create moose mirror <dev1> <dev2> mirror <dev3> <dev4>..
# Add new raidz1 vdev to a pool.
zpool add moose raidz1 <devA> <devB> <devC>..
# Remove a vdev from a pool.
zpool remove moose <vdevX>
# Destroy a pool.
zpool destroy moose
</code></pre>
<blockquote>
<p>For stable device names in small home setups it is recommended to use names
from <a href="https://openzfs.github.io/openzfs-docs/Project%20and%20Community/FAQ.html#selecting-dev-names-when-creating-a-pool-linux"><code>/dev/disk/by-id</code></a>.</p>
</blockquote>
<h3 id="inspect-zfs-pools"><a class="header" href="#inspect-zfs-pools">Inspect zfs pools</a></h3>
<pre><code class="language-bash"># Show status of all pools or a single one.
zpool status [<pool>]
# Show information / statistics about pools or single one.
zpool list [<pool>]
# Show statistics for all devices.
zpool list -v
# Show command history for pools.
zpool history
</code></pre>
<h3 id="modify-vdevs"><a class="header" href="#modify-vdevs">Modify <code>vdevs</code></a></h3>
<pre><code class="language-bash"># vdev MIRROR-0 with two devs.
zpool status
NAME STATE READ WRITE CKSUM
moose ONLINE 0 0 0
mirror-0 ONLINE 0 0 0
virtio-200 ONLINE 0 0 0
virtio-300 ONLINE 0 0 0
# Attach new device to an existing vdev.
zpool attach moose virtio-200 virtio-400
# vdev MIRROR-0 with three devs.
zpool status
NAME STATE READ WRITE CKSUM
moose ONLINE 0 0 0
mirror-0 ONLINE 0 0 0
virtio-200 ONLINE 0 0 0
virtio-300 ONLINE 0 0 0
virtio-400 ONLINE 0 0 0
# Detach device from vdev.
zpool detach moose virtio-200
</code></pre>
<h3 id="replace-faulty-disk"><a class="header" href="#replace-faulty-disk">Replace faulty disk</a></h3>
<pre><code class="language-bash"># MIRROR-0 is degraded as one disk failed, but still intact.
zpool status
NAME STATE READ WRITE CKSUM
moose DEGRADED 0 0 0
mirror-0 DEGRADED 0 0 0
virtio-200 UNAVAIL 0 0 0 invalid label
virtio-300 ONLINE 0 0 0
# Replace faulty disk, in mirror.
# No data is lost since mirror still has one good disk.
zpool replace moose virtio-200 virtio-400
# MIRROR-0 back in ONLINE (good) state.
zpool status
NAME STATE READ WRITE CKSUM
moose ONLINE 0 0 0
mirror-0 ONLINE 0 0 0
virtio-400 ONLINE 0 0 0
virtio-300 ONLINE 0 0 0
</code></pre>
<h3 id="import-or-export-zfs-pools"><a class="header" href="#import-or-export-zfs-pools">Import or export zfs pools</a></h3>
<p>When moving pools between hosts, the pool must be <code>exported</code> on the currently
active host and <code>imported</code> on the new host.</p>
<pre><code class="language-bash"># Export a pool called MOOSE.
zpool export moose
# If datasets are busy, use lsof to check which processes keep it busy.
# lsof <mntpoint>
# List pools that can be imported using BY-ID deivce names (for example).
zpool import -d /dev/disk/by-id
# Import pool MOOSE using BY-ID device names (for example).
zpool import -d /dev/disk/by-id moose
</code></pre>
<blockquote>
<p>Device names used by an existing pool can be changed by <a href="https://openzfs.github.io/openzfs-docs/Project%20and%20Community/FAQ.html#changing-dev-names-on-an-existing-pool">exporting and
importing</a> a pool again.</p>
</blockquote>
<h2 id="zfs-dataset-management"><a class="header" href="#zfs-dataset-management">zfs dataset management</a></h2>
<p>Datasets are by default mounted at <code>/<POOL>/<DATASET></code>.</p>
<h3 id="create-and-destroy-zfs-datasets"><a class="header" href="#create-and-destroy-zfs-datasets">Create and destroy zfs datasets</a></h3>
<pre><code class="language-bash"># Create dataset FOO on pool MOOSE.
zfs create moose/foo
# Destroy dataset.
zfs destroy moose/foo
</code></pre>
<h3 id="list-all-zfs-datasets"><a class="header" href="#list-all-zfs-datasets">List all zfs datasets</a></h3>
<pre><code class="language-bash"># List all zfs datasets.
zfs list
</code></pre>
<h3 id="mount-zfs-datasets"><a class="header" href="#mount-zfs-datasets">Mount zfs datasets</a></h3>
<pre><code class="language-bash"># List currently mounted datasets.
zfs mount
# Mount dataset.
zfs mount moose/foo
# Unmount dataset.
zfs unmount moose/foo
</code></pre>
<h3 id="encrypted-datasets"><a class="header" href="#encrypted-datasets">Encrypted datasets</a></h3>
<p>Encryption is a readonly property, can only be set when creating a dataset.</p>
<pre><code class="language-bash"># Create encrypted dataset FOO on pool MOOSE.
zfs create -o encryption=on -o keyformat=passphrase moose/foo
# Mount encrypte dataset and load encryption key (if not loaded).
zfs mount -l moose/foo
# -l is equivalent to first loading the key via zfs load-key moose/foo.
# Unmount dataset and unload encryption key (unload is optional).
zfs umount -u moose/foo
</code></pre>
<h3 id="manage-zfs-encryption-keys"><a class="header" href="#manage-zfs-encryption-keys">Manage zfs encryption keys</a></h3>
<pre><code class="language-bash"># Preload encryption key for dataset.
zfs load-key moose/foo
# Preload encryption key for all datasets.
zfs load-key -a
# Change encryption key for dataset.
zfs change-key moose/foo
# Unload encryption key for dataset.
zfs unload-key moose/foo
</code></pre>
<h3 id="manage-dataset-properties"><a class="header" href="#manage-dataset-properties">Manage dataset properties</a></h3>
<pre><code class="language-bash"># Get all properties for dataset.
zfs get quota moose/foo
# Get single property for dataset.
zfs get all moose/foo
# Get single property for all datasets.
zfs get quota
# Set property on dataset.
zfs set quota=10G moose/foo
</code></pre>
<h3 id="snapshots"><a class="header" href="#snapshots">Snapshots</a></h3>
<pre><code class="language-bash"># Create snapshot called V2 for dataset moose/foo.
zfs snapshot moose/foo@v2
# List all snapshots.
zfs list -t snapshot
# Make .zfs direcotry visible in the root of the dataset.
zfs set snapdir=visible moose/foo
# Browse available snapshots in visible .zfs direcotry (readonly).
ls /moose/foo/.zfs/snapshot
v1/ v2/
# Create a new dataset based on the V1 snapshot
zfs clone moose/foo@v1 moose/foov1
# Destroy snapshot.
zfs destroy moose/foo@v1
</code></pre>
<h3 id="access-control-list"><a class="header" href="#access-control-list">Access control list</a></h3>
<p>Focus on <a href="linux/./acl.html">posix acl</a>.</p>
<pre><code class="language-bash"># Set the ACL type for the FOO dataset to POSIXACL.
zfs set acltype=posixacl moose/foo
# Get the ACL type of a given dataset.
zfs get acltype moose/foo
</code></pre>
<blockquote>
<p>For performance reasons it is recommended to also set <code>zfs set xattr=sa moose/foo</code> [<a href="https://github.com/openzfs/zfs/issues/170#issuecomment-27348094">ref</a>].</p>
</blockquote>
<h2 id="example-zfs-pool-import-during-startup-systemd"><a class="header" href="#example-zfs-pool-import-during-startup-systemd">Example: zfs pool import during startup (<code>systemd</code>)</a></h2>
<p>The default zpool cache file is <code>/etc/zfs/zpool.cache</code>. When pools are imported
the cache is updated.</p>
<p>Enable the following targets / services to automatically import pools from the
cache.</p>
<pre><code class="language-bash">systemctl list-dependencies
...
└─zfs.target
└─zfs-import.target
└─zfs-import-cache.service
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="network-1"><a class="header" href="#network-1">Network</a></h1>
<ul>
<li><a href="network/./tcpdump.html">tcpdump</a></li>
<li><a href="network/./tshark.html">tshark</a></li>
<li><a href="network/./firewall-cmd.html">firewall-cmd</a></li>
<li><a href="network/./nftables.html">nftables</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="tcpdump1"><a class="header" href="#tcpdump1">tcpdump(1)</a></h1>
<h1 id="cli-3"><a class="header" href="#cli-3">CLI</a></h1>
<pre><code class="language-markdown">tcpdump [opts] -i <if> [<filter>]
-n Don't covert host/port names.
-w <file|-> Write pcap trace to file or stdout (-).
-r <file> Read & parse pcap file.
</code></pre>
<p>Some useful filters, for the full syntax see <a href="https://www.tcpdump.org/manpages/pcap-filter.7.html">pcap-filter(7)</a>.</p>
<pre><code class="language-markdown">src <ip> Filter for source IP.
dst <ip> Filter for destination IP.
host <ip> Filter for IP (src + dst).
net <ip>/<range> Filter traffic on subnet.
[src/dst] port <port> Filter for port (optionally src/dst).
tcp/udp/icmp Filter for protocol.
</code></pre>
<blockquote>
<p>Use <code>and/or/not</code> and <code>()</code> to build filter expressions.</p>
</blockquote>
<h1 id="examples-11"><a class="header" href="#examples-11">Examples</a></h1>
<h2 id="capture-packets-from-remote-host"><a class="header" href="#capture-packets-from-remote-host">Capture packets from remote host</a></h2>
<pre><code class="language-makrdown"># -k: Start capturing immediately.
ssh <host> tcpdump -i any -w - | sudo wireshark -k -i -
</code></pre>
<blockquote>
<p>The <code>any</code> interface is a special keyword to capture traffic on all interfaces.</p>
</blockquote>
<div style="break-before: page; page-break-before: always;"></div><h1 id="tshark-1"><a class="header" href="#tshark-1">tshark (1)</a></h1>
<pre><code class="language-text">tshark [opts] -i <if>
--color Colorize output.
-w <file|-> Write pcap trace to file or stdout (-).
-r <file> Read & parse pcap file.
-f <filter> Apply capture filter (see pcap-filter(7) or tcpdump).
Only applicable during capturing.
-Y <filter> Apply display filter.
Only applicable during viewing capture.
-c <count> Stop capturing after COUNT packets (INF by default).
</code></pre>
<p>Some useful display filters.</p>
<pre><code class="language-text">ip.addr != 192.168.1.0/24 Filter out whole ip subnet (source + destination).
ip.dst == 192.168.1.42 Filter for destination ip address.
tcp.dstport == 80 Filter for tcp destinatio port.
!wg Filter out all wireguard traffic.
tcp/udp/ssh/wg/... Filter for protocol.
"and/or/not/!" and "()" can be used to build filter expressions.
</code></pre>
<blockquote>
<p>Use <code>tshak -G</code> to list all fields that can be used in display filters.</p>
</blockquote>
<h1 id="examples-12"><a class="header" href="#examples-12">Examples</a></h1>
<h2 id="capture-and-filter-packet-to-file"><a class="header" href="#capture-and-filter-packet-to-file">Capture and filter packet to file</a></h2>
<pre><code class="language-bash"># Capture TCP traffic with port 80 on interface eth0 to file.
sudo tshark -i eht0 -f 'tcp and port 80' -w tx.pcap
# View captured packets.
sudo tshark -r tx.pcap
# View captured packets and apply additionaly display filters.
sudo tshark -r tx.pcap -Y 'ip.addr != 192.168.1.42'
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="firewall-cmd1"><a class="header" href="#firewall-cmd1">firewall-cmd(1)</a></h1>
<p>Command line interface to the <a href="https://firewalld.org/documentation/man-pages/firewalld.html">firewalld(1)</a> daemon.</p>
<h2 id="list-current-status-of-the-firewall"><a class="header" href="#list-current-status-of-the-firewall">List current status of the firewall</a></h2>
<pre><code class="language-sh"># List all services and ports for all zones.
firewall-cmd --list-all
# List all services.
firewall-cmd --list-services
# List all ports.
firewall-cmd --list-ports
</code></pre>
<blockquote>
<p>Add <code>--zone <ZONE></code> to limit output to a given <code>ZONE</code>. Use <code>--get-zones</code> to
see all available zones.</p>
</blockquote>
<h2 id="add-entries"><a class="header" href="#add-entries">Add entries</a></h2>
<pre><code class="language-sh"># Add a service to the firewall, use `--get-services` to list all available
# service names.
firewall-cmd --add-service <SERVICE>
# Add a specific port.
firewall-cmd --add-port 8000/tcp
# Add a rich rule (eg port forwarding, dnat).
firewall-cmd --add-rich-rule 'rule family="ipv4" forward-port port="80" protocol="tcp" to-port="8080"'
</code></pre>
<h2 id="remove-entries"><a class="header" href="#remove-entries">Remove entries</a></h2>
<pre><code class="language-sh"># Remove service.
firewall-cmd --remove-service <SERVICE>
# Remove port.
firewall-cmd --remove-port 8000/tcp
# Remove rich rule.
firewall-cmd --remove-rich-rule 'rule family="ipv4" forward-port port="80" protocol="tcp" to-port="8080"'
</code></pre>
<h2 id="references-6"><a class="header" href="#references-6">References</a></h2>
<ul>
<li>man <a href="https://firewalld.org/documentation/man-pages/firewall-cmd.html">firewall-cmd(1)</a></li>
<li>man <a href="https://firewalld.org/documentation/man-pages/firewalld.html">firewalld(1)</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="nftables"><a class="header" href="#nftables">nftables</a></h1>
<p><a href="https://nftables.org/projects/nftables/index.html">Nftables</a> is a stateful Linux firewall which uses the
<a href="https://nftables.org">netfilter</a> kernel hooks.
It is used for stateless, stateful packet filtering and all sorts of NAT.</p>
<p>Nftables is the successor to <a href="https://nftables.org/projects/iptables/index.html">iptables</a>.</p>
<p>In nftables, <code>rules</code> are organized with <code>chains</code> and <code>tables</code>.</p>
<ul>
<li><code>chain</code>: Orders <code>rules</code>. Chains exist in two kinds:
<ul>
<li><code>base chain</code>: Entry point from netfilter hooks (network stack).</li>
<li><code>regular chain</code>: Can be used as jump target to group rules for better organization.</li>
</ul>
</li>
<li><code>table</code>: Groups chains together. Tables are defined by a <code>name</code> and an
<code>address family</code> (eg <code>inet</code>, <code>ip</code>, <code>ip6</code>, ..).</li>
</ul>
<h2 id="ruleset"><a class="header" href="#ruleset">Ruleset</a></h2>
<pre><code class="language-bash">nft list ruleset # List all tables/chains/rules (whole ruleset).
nft flush ruleset # Clear whole ruleset.
</code></pre>
<h2 id="examples-save-rules-to-files-and-re-apply"><a class="header" href="#examples-save-rules-to-files-and-re-apply">Examples: Save rules to files and re-apply</a></h2>
<pre><code class="language-bash">nft list ruleset > nft.rules
nft flush ruleset
nft -f nft.rules
</code></pre>
<h2 id="example-fully-trace-evaluation-of-nftables-rules"><a class="header" href="#example-fully-trace-evaluation-of-nftables-rules">Example: Fully trace evaluation of nftables rules</a></h2>
<pre><code class="language-text">table ip traceall {
chain filter_prerouting {
# Install chain with higher priority as the RAW standard priority.
type filter hook prerouting priority raw - 50; policy accept;
# Trace each and every packet (very verbose).
#meta nftrace set 1;
# Trace packet to port 80/81/8081 from localhost.
tcp dport { 80, 81, 8081 } ip saddr 127.0.0.1 meta nftrace set 1;
}
}
</code></pre>
<p>Use <code>nft monitor trace</code> to get trace output on tty.</p>
<h2 id="example-ipv4-port-forwarding"><a class="header" href="#example-ipv4-port-forwarding">Example: IPv4 port forwarding</a></h2>
<pre><code class="language-text">table ip fwd {
chain nat_preroute {
# Register this chain to the PREROUTE:NAT hook (stateful packet tracking via conntrack).
type nat hook prerouting priority dstnat + 10 ; policy accept;
meta nfproto ipv4 tcp dport 81 redirect to :8081
}
}
</code></pre>
<h2 id="example-base-vs-regular-chain"><a class="header" href="#example-base-vs-regular-chain">Example: Base vs regular chain</a></h2>
<pre><code class="language-text"># Table named 'playground' handling 'ip' (ipv4) address family.
table ip playground {
# Base chain.
chain filter_input_base {
# Register this chain to the INPUT:FILTER hook in the netfilter package flow.
# Specify a prioirty relative to the inbuilt 'filter' priority (smaller
# number means higher priority).
# Set the default policy to ACCEPT, to let every packet pass by default.
type filter hook input priority filter - 10; policy accept;
# Create a rule for tcp packets arriving on either port 8000 or 8100.
tcp dport { 8000, 8100 } jump input_reg_log;
# Create a rule for tcp packets arriving on port 8200.
tcp dport 8200 jump input_reg_log_all;
}
# Regular chain.
chain input_reg_log {
# Log every packet traversing this chain.
# Message lands in the kernel ring buffer.
log;
}
# Regular chain.
chain input_reg_log_all {
# Log every packet with all flags traversing this chain.
log flags all;
}
}
</code></pre>
<pre><code class="language-bash"># Load the nf rules.
sudo nft -f playground.rules
# Create test servers.
nc -lk 0.0.0.0 8000
nc -lk 0.0.0.0 8100
nc -lk 0.0.0.0 8200
# See the nftables logging in the kernel ring buffer.
sudo dmesg -w
# Make some client connections.
nc localhost 8000
nc localhost 8200
</code></pre>
<h2 id="mental-model-for-netfilter-packet-flow"><a class="header" href="#mental-model-for-netfilter-packet-flow">Mental model for netfilter packet flow</a></h2>
<p><img src="network/assets/nf_pkt_flow.png" alt="nf_pkt_flow.png" /></p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="web"><a class="header" href="#web">Web</a></h1>
<ul>
<li><a href="web/./html.html">html</a></li>
<li><a href="web/./css.html">css</a></li>
<li><a href="web/./chartjs.html">chartjs</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="html"><a class="header" href="#html">html</a></h1>
<h2 id="collapsible-element"><a class="header" href="#collapsible-element">Collapsible element</a></h2>
<p><a href="web/src/details.html">Rendered html</a></p>
<pre><code class="language-html"><details>
<summary>Some text goes here</summary>
... some more text goes here.
</details>
</code></pre>
<blockquote>
<p>With the <code>open</code> attribute <code><details open></code> the details are unfolded by default.</p>
</blockquote>
<h1 id="minimal-2-column-layout"><a class="header" href="#minimal-2-column-layout">Minimal 2 column layout</a></h1>
<p><a href="web/src/grid-2col.html">Rendered html</a></p>
<pre><code class="language-html"><style>
.grid-2col {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 1em
}
.col1 {
grid-column-start: 1;
background-color: red;
padding-left: 1em;
}
.col2 {
grid-column-start: 2;
background-color: green;
padding-left: 1em;
}
</style>
<div class="grid-2col">
<div class="col1">
<p>Some text in the first column.</p>
</div>
<div class="col2">
<p>Some text in the second column.</p>
</div>
</div>
</code></pre>
<h1 id="minimal-grid-area"><a class="header" href="#minimal-grid-area">Minimal grid area</a></h1>
<p><a href="web/src/grid-area.html">Rendered html</a></p>
<pre><code class="language-html"><style>
.page-grid {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-areas:
"h h"
"s m"
"f f";
gap: 1em;
}
.gh {
grid-area: h;
background-color: orange;
}
.gs {
grid-area: s;
background-color: green;
}
.gm {
grid-area: m;
background-color: gray;
}
.gf {
grid-area: f;
background-color: yellow;
}
.nav-items {
display: flex; /* flexbox model => flexible layout on row */
justify-content: space-around; /* align flex boxes horizontally with space around */
align-items: center; /* center flex items vertically */
list-style: none;
}
p {
margin: 1em;
}
</style>
<div class="page-grid">
<div class="gh">
<ul class="nav-items">
<li class="nav-item"><a href="">aa</a></li>
<li class="nav-item"><a href="">bb</a></li>
<li class="nav-item"><a href="">cc</a></li>
</ul>
</div>
<div class="gs">
<p>Some text in the second column.</p>
</div>
<div class="gm">
<p>Some text in the second column.</p>
</div>
<div class="gf">
<p>Some text in the second column.</p>
</div>
</div>
</code></pre>
<h1 id="minimal-tabs"><a class="header" href="#minimal-tabs">Minimal tabs</a></h1>
<p><a href="web/src/tabs.html">Rendered html</a></p>
<pre><code class="language-html"><script>
const showTab = (E, T) => {
const TABS = Array.from(document.getElementsByClassName("content"));
TABS.forEach(T => {
T.style.display = "none";
});
document.getElementById(T).style.display = "block";
};
window.onload = () => {
document.getElementById("bTab1").onclick = (E) => {
showTab(E, "tTab1");
};
document.getElementById("bTab2").onclick = (E) => {
showTab(E, "tTab2");
};
}
</script>
<button type="button" id="bTab1">Tab1</button>
<button type="button" id="bTab2">Tab2</button>
<div id="tTab1" class="content" style="display: block;">
<p>Some content goes here ...</p>
</div>
<div id="tTab2" class="content" style="display: none;">
<p>... and there.</p>
</div>
</code></pre>
<h1 id="minimal-tags-filter"><a class="header" href="#minimal-tags-filter">Minimal tags filter</a></h1>
<p><a href="web/src/tags.html">Rendered html</a></p>
<pre><code class="language-html"><script>
/// Map HTML elements to display kinds.
const elementToDisplay = (E) => {
switch (E.nodeName) {
case "LI":
return "list-item";
default:
return "block";
}
}
/// Display only elements with tag T.
const showTag = (T) => {
Array.from(document.getElementsByClassName("content")).forEach(E => {
E.style.display = "none";
});
Array.from(document.getElementsByClassName(T)).forEach(E => {
E.style.display = elementToDisplay(E);
});
};
/// Initialize buttons and callbacks.
window.onload = () => {
// Handle to the filter placeholder.
const filter = document.getElementById("filter");
// Create buttons for each tag T.
["arm", "x86", "clear"].forEach(T => {
const btn = document.createElement("button");
btn.innerHTML = T;
btn.onclick = T === "clear"
? (E) => {
showTag("content");
filter.innerHTML = "";
}
: (E) => {
showTag(T);
filter.innerHTML = `<p>filter: <mark>${T}</mark></p>`;
};
document.body.prepend(btn);
});
}
</script>
<div id = "filter"></div>
<ul>
<li class = "content arm">arm 1</li>
<li class = "content arm">arm 2</li>
<li class = "content arm">arm 3</li>
<li class = "content x86">x86 1</li>
<li class = "content x86">x86 2</li>
<li class = "content x86 arm">x86 + arm</li>
</ul>
<div class = "content arm">arm</div>
<div class = "content arm x86">arm x86</div>
<div class = "content x86">x86</div>
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="css"><a class="header" href="#css">css</a></h1>
<h2 id="selector"><a class="header" href="#selector">selector</a></h2>
<p><code>.moose</code> element with class</p>
<pre><code class="language-html"><div class = "moose"></div> // selected
<div class = "bar"></div> // NOT selected
</code></pre>
<p><code>.moose.bar</code> element with multiple classes</p>
<pre><code class="language-html"><div class = "moose bar"></div> // selected
<div class = "bar"></div> // NOT selected
</code></pre>
<p><code>.moose .bar</code> descendant element with classes</p>
<pre><code class="language-html"><div class = "moose">
<div class = "bar"></div> // selected
</div>
<div class = "bar"></div> // NOT selected
</code></pre>
<p><code>p</code> specific element</p>
<pre><code class="language-html"><p></p> // selected
<div></div> // NOT selected
</code></pre>
<p><code>p.bar</code> specific element with class</p>
<pre><code class="language-html"><p class = "bar"></p> // selected
<p class = "foo"></p> // NOT selected
</code></pre>
<p><code>p,div</code> any element</p>
<pre><code class="language-html"><p></p> // selected
<div></div> // selected
<a></a> // NOT selected
</code></pre>
<p><code>div p</code> descendant element of other element</p>
<pre><code class="language-html"><div><p></p></div> // selected
<div><ul><p></p></ul></div> // NOT selected
</code></pre>
<p><code>div > o</code> direct descendant element of other element</p>
<pre><code class="language-html"><div><p></p></div> // selected
<div><ul><p></p></ul></div> // NOT selected
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="chartjs"><a class="header" href="#chartjs">Chart.js</a></h1>
<h2 id="minimal-example-with-external-tooltips"><a class="header" href="#minimal-example-with-external-tooltips">Minimal example with <em>external</em> tooltips</a></h2>
<p><a href="web/src/chartjs-ext-tooltip.html">Rendered html</a></p>
<pre><code class="language-html"><canvas id="myChart" style="margin:5em;"></canvas>
<script>
const get_or_create_tooltip = (id) => {
if (tooltip = document.getElementById(id)) {
return tooltip;
} else {
// -- Create a new Tooltip element.
const tooltip = document.createElement('div');
tooltip.id = id;
document.body.appendChild(tooltip);
// -- Some minimal styling.
tooltip.style.background = 'rgba(0, 0, 0, 0.1)';
tooltip.style.position = 'absolute';
tooltip.style.transition = 'all .2s ease';
// -- Add a table element for the tooltip content.
const table = document.createElement('table');
tooltip.appendChild(table);
return tooltip
}
}
const render_tooltip = (context) => {
const {chart, tooltip} = context;
// -- Get Tooltip element.
const tooltip_elem = get_or_create_tooltip('myTooltip');
// -- Get data point values (only one data point).
const {label: x, formattedValue: y} = tooltip.dataPoints[0];
// -- Format new tooltip.
const link = document.createElement('a');
link.href = "https://github.com/johannst";
link.innerHTML = "X:" + x + " Y:" + y;
// -- Remove previous child element and add new one.
const table = tooltip_elem.querySelector('table');
table.innerHTML = "";
table.appendChild(link);
// -- Get absolute X/Y position of the top left corner of the canvas.
const {offsetLeft: canvas_x, offsetTop: canvas_y} = chart.canvas;
// -- Set position and minimal style for the tooltip.
tooltip_elem.style.left = canvas_x + tooltip.caretX + 'px';
tooltip_elem.style.top = canvas_y + tooltip.caretY + 'px';
tooltip_elem.style.font = tooltip.options.bodyFont.string;
// -- Place the tooltip (I) left or (II) right of the data point.
if (tooltip.xAlign === "right") {
tooltip_elem.style.transform = 'translate(-100%, 0)'; // (I)
} else if (tooltip.xAlign === "left") {
tooltip_elem.style.transform = 'translate(0%, 0)'; // (II)
}
}
// -- Render a chart with some dummy data on the canvas.
const chart = new Chart(
document.getElementById('myChart'),
{
data: {
datasets: [{
// -- A single dataset.
label: 'Just some values',
type: 'scatter',
data: [
{x: 4, y: 4},
{x: 5, y: 1},
{x: 7, y: 6},
{x: 10, y: 8},
{x: 10, y: 7},
{x: 10, y: 3},
],
backgroundColor: 'rgba(255, 99, 132, 0.5)',
borderColor: 'rgb(255, 99, 132)',
}],
},
options: {
scales: {
y: {
beginAtZero: true, // -- Start the Y-Axis at zero instead min(y) of dataset.
}
},
plugins: {
tooltip: {
enabled: false, // -- Disable builtin tooltips.
mode: 'nearest', // -- Get the item that is nearest to the mouse.
intersect: false, // -- 'mode' is active also when the mouse doesnt intersect with an item on the chart.
external: render_tooltip, // -- External tooltip handler, allows to create own HTML.
}
}
}
}
);
</script>
</code></pre>
<div style="break-before: page; page-break-before: always;"></div><h1 id="arch"><a class="header" href="#arch">Arch</a></h1>
<ul>
<li><a href="arch/./x86_64.html">x86_64</a></li>
<li><a href="arch/./arm64.html">arm64</a></li>
<li><a href="arch/./armv7.html">armv7</a></li>
<li><a href="arch/./riscv.html">riscv</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="x86_64"><a class="header" href="#x86_64">x86_64</a></h1>
<p>keywords: x86_64, x86, abi</p>
<ul>
<li>64bit synonyms: <code>x86_64</code>, <code>x64</code>, <code>amd64</code>, <code>intel 64</code></li>
<li>32bit synonyms: <code>x86</code>, <code>ia32</code>, <code>i386</code></li>
<li>ISA type: <code>CISC</code></li>
<li>Endianness: <code>little</code></li>
</ul>
<h2 id="registers"><a class="header" href="#registers">Registers</a></h2>
<h3 id="general-purpose-register"><a class="header" href="#general-purpose-register">General purpose register</a></h3>
<pre><code class="language-markdown">bytes
[7:0] [3:0] [1:0] [1] [0] desc
----------------------------------------------------------
rax eax ax ah al accumulator
rbx ebx bx bh bl base register
rcx ecx cx ch cl counter
rdx edx dx dh dl data register
rsi esi si - sil source index
rdi edi di - dil destination index
rbp ebp bp - bpl base pointer
rsp esp sp - spl stack pointer
r8-15 rNd rNw - rNb
</code></pre>
<h3 id="special-register"><a class="header" href="#special-register">Special register</a></h3>
<pre><code class="language-markdown">bytes
[7:0] [3:0] [1:0] desc
---------------------------------------------------
rflags eflags flags flags register
rip eip ip instruction pointer
</code></pre>
<h3 id="flags-register"><a class="header" href="#flags-register">FLAGS register</a></h3>
<pre><code class="language-markdown">rflags
bits desc instr comment
--------------------------------------------------------------------------------------------------------------
[21] ID identification ability to set/clear -> indicates support for CPUID instr
[18] AC alignment check alignment exception for PL 3 (user), requires CR0.AM
[13:12] IOPL io privilege level
[11] OF overflow flag
[10] DF direction flag cld/std increment (0) or decrement (1) registers in string operations
[9] IF interrupt enable cli/sti
[7] SF sign flag
[6] ZF zero flag
[4] AF auxiliary carry flag
[2] PF parity flag
[0] CF carry flag
</code></pre>
<p>Change flag bits with <code>pushf</code> / <code>popf</code> instructions:</p>
<pre><code class="language-x86asm">pushfd // push flags (4bytes) onto stack
or dword ptr [esp], (1 << 18) // enable AC flag
popfd // pop flags (4byte) from stack
</code></pre>
<blockquote>
<p>There is also <code>pushfq</code> / <code>popfq</code> to push and pop all 8 bytes of <code>rflags</code>.</p>
</blockquote>
<h3 id="model-specific-register-msr"><a class="header" href="#model-specific-register-msr">Model Specific Register (MSR)</a></h3>
<pre><code class="language-x86asm">rdmsr // Read MSR register, effectively does EDX:EAX <- MSR[ECX]
wrmsr // Write MSR register, effectively does MSR[ECX] <- EDX:EAX
</code></pre>
<h2 id="size-directives"><a class="header" href="#size-directives">Size directives</a></h2>
<p>Explicitly specify size of the operation.</p>
<pre><code class="language-x86asm">mov byte ptr [rax], 0xff // save 1 byte(s) at [rax]
mov word ptr [rax], 0xff // save 2 byte(s) at [rax]
mov dword ptr [rax], 0xff // save 4 byte(s) at [rax]
mov qword ptr [rax], 0xff // save 8 byte(s) at [rax]
</code></pre>
<h2 id="addressing"><a class="header" href="#addressing">Addressing</a></h2>
<pre><code class="language-x86asm">mov qword ptr [rax], rbx // save val in rbx at [rax]
mov qword ptr [imm], rbx // save val in rbx at [imm]
mov rax, qword ptr [rbx+4*rcx] // load val at [rbx+4*rcx] into rax
</code></pre>
<p><code>rip</code> relative addressing:</p>
<pre><code class="language-x86asm">lea rax, [rip+.my_str] // load addr of .my_str into rax
...
.my_str:
.asciz "Foo"
</code></pre>
<p>Load effective address:</p>
<pre><code class="language-x86asm">mov rax, 2
lea r11, [rax + 3] // r11 <- 5
</code></pre>
<h2 id="string-instructions"><a class="header" href="#string-instructions">String instructions</a></h2>
<p>The operand size of a string instruction is defined by the instruction suffix
<code>b | w | d | q</code>.</p>
<p>Source and destination registers are modified according to the <code>direction flag (DF)</code> in the <code>flags</code> register</p>
<ul>
<li><code>DF=0</code> increment src/dest registers</li>
<li><code>DF=1</code> decrement src/dest registers</li>
</ul>
<p>Following explanation assumes <code>byte</code> operands with <code>DF=0</code>:</p>
<pre><code class="language-x86asm">movsb // move data from string to string
// ES:[DI] <- DS:[SI]
// DI <- DI + 1
// SI <- SI + 1
lodsb // load string
// AL <- DS:[SI]
// SI <- SI + 1
stosb // store string
// ES:[DI] <- AL
// DI <- DI + 1
cmpsb // compare string operands
// DS:[SI] - ES:[DI] ; set status flag (eg ZF)
// SI <- SI + 1
// DI <- DI + 1
scasb // scan string
// AL - ES:[DI] ; set status flag (eg ZF)
// DI <- DI + 1
</code></pre>
<p>String operations can be repeated:</p>
<pre><code class="language-x86asm">rep // repeat until rcx = 0
repz // repeat until rcx = 0 or while ZF = 0
repnz // repeat until rcx = 0 or while ZF = 1
</code></pre>
<h3 id="example-simple-memset"><a class="header" href="#example-simple-memset">Example: Simple <code>memset</code></a></h3>
<pre><code class="language-x86asm">// memset (dest, 0xaa /* char */, 0x10 /* len */)
lea di, [dest]
mov al, 0xaa
mov cx, 0x10
rep stosb
</code></pre>
<h2 id="sysv-x86_64-abi"><a class="header" href="#sysv-x86_64-abi"><a href="https://gitlab.com/x86-psABIs/x86-64-ABI">SysV x86_64 ABI</a></a></h2>
<h3 id="passing-arguments-to-functions"><a class="header" href="#passing-arguments-to-functions">Passing arguments to functions</a></h3>
<ul>
<li>Integer/Pointer arguments
<pre><code class="language-markdown">reg arg
-----------
rdi 1
rsi 2
rdx 3
rcx 4
r8 5
r9 6
</code></pre>
</li>
<li>Floating point arguments
<pre><code class="language-markdown">reg arg
-----------
xmm0 1
.. ..
xmm7 8
</code></pre>
</li>
<li>Additional arguments are passed on the stack. Arguments are pushed
right-to-left (RTL), meaning next arguments are closer to current <code>rsp</code>.</li>
</ul>
<h3 id="return-values-from-functions"><a class="header" href="#return-values-from-functions">Return values from functions</a></h3>
<ul>
<li>Integer/Pointer return values
<pre><code class="language-markdown">reg size
-----------------
rax 64 bit
rax+rdx 128 bit
</code></pre>
</li>
<li>Floating point return values
<pre><code class="language-markdown">reg size
-------------------
xmm0 64 bit
xmm0+xmm1 128 bit
</code></pre>
</li>
</ul>
<h3 id="caller-saved-registers"><a class="header" href="#caller-saved-registers">Caller saved registers</a></h3>
<p>Caller must save these registers if they should be preserved across function
calls.</p>
<ul>
<li><code>rax</code></li>
<li><code>rcx</code></li>
<li><code>rdx</code></li>
<li><code>rsi</code></li>
<li><code>rdi</code></li>
<li><code>rsp</code></li>
<li><code>r8</code> - <code>r11</code></li>
</ul>
<h3 id="callee-saved-registers"><a class="header" href="#callee-saved-registers">Callee saved registers</a></h3>
<p>Caller can expect these registers to be preserved across function calls. Callee
must must save these registers in case they are used.</p>
<ul>
<li><code>rbx</code></li>
<li><code>rbp</code></li>
<li><code>r12</code> – <code>r15</code></li>
</ul>
<h3 id="stack"><a class="header" href="#stack">Stack</a></h3>
<ul>
<li>grows downwards</li>
<li>frames aligned on 16 byte boundary
<pre><code class="language-text">Hi ADDR
| +------------+
| | prev frame |
| +------------+ <--- 16 byte aligned (X & ~0xf)
| [rbp+8] | saved RIP |
| [rbp] | saved RBP |
| [rbp-8] | func stack |
| | ... |
v +------------+
Lo ADDR
</code></pre>
</li>
</ul>
<h3 id="function-prologue--epilogue"><a class="header" href="#function-prologue--epilogue">Function prologue & epilogue</a></h3>
<ul>
<li>prologue
<pre><code class="language-x86asm">push rbp // save caller base pointer
mov rbp, rsp // save caller stack pointer
</code></pre>
</li>
<li>epilogue
<pre><code class="language-x86asm">mov rsp, rbp // restore caller stack pointer
pop rbp // restore caller base pointer
</code></pre>
<blockquote>
<p>Equivalent to <code>leave</code> instruction.</p>
</blockquote>
</li>
</ul>
<h2 id="windows-x64-abi"><a class="header" href="#windows-x64-abi"><a href="https://learn.microsoft.com/en-us/cpp/build/x64-software-conventions">Windows x64 ABI</a></a></h2>
<h3 id="passing-arguments-to-functions-ref"><a class="header" href="#passing-arguments-to-functions-ref">Passing arguments to functions (<a href="https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention">ref</a>)</a></h3>
<blockquote>
<p>A single argument is never spread across multiple registers.</p>
</blockquote>
<ul>
<li>Integer/Pointer arguments
<pre><code class="language-markdown">reg arg
-----------
rcx 1
rdx 2
r8 3
r9 4
</code></pre>
</li>
<li>Floating point arguments
<pre><code class="language-markdown">reg arg
-----------
xmm0 1
.. ..
xmm3 4
</code></pre>
</li>
<li>Additional arguments are passed on the stack. Arguments are pushed
right-to-left (RTL), meaning next arguments are closer to current <code>rsp</code>.
<a href="https://godbolt.org/z/oT5Tjdf7Y">See example</a>.</li>
</ul>
<h3 id="return-values-from-functions-1"><a class="header" href="#return-values-from-functions-1">Return values from functions</a></h3>
<ul>
<li>Integer/Pointer return values
<pre><code class="language-markdown">reg size
-----------------
rax 64 bit
</code></pre>
</li>
<li>Floating point return values
<pre><code class="language-markdown">reg size
-------------------
xmm0 64 bit
</code></pre>
</li>
</ul>
<h3 id="caller-saved-registers-1"><a class="header" href="#caller-saved-registers-1">Caller saved registers</a></h3>
<p>Caller must save these registers if they should be preserved across function
calls.</p>
<ul>
<li><code>rax</code></li>
<li><code>rcx</code></li>
<li><code>rdx</code></li>
<li><code>r8</code> - <code>r11</code></li>
<li><code>xmm0</code> - <code>xmm5</code></li>
</ul>
<h3 id="callee-saved-registers-1"><a class="header" href="#callee-saved-registers-1">Callee saved registers</a></h3>
<p>Caller can expect these registers to be preserved across function calls. Callee
must must save these registers in case they are used.</p>
<ul>
<li><code>rbx</code></li>
<li><code>rbp</code></li>
<li><code>rdi</code></li>
<li><code>rsi</code></li>
<li><code>rsp</code></li>
<li><code>r12</code> - <code>r15</code></li>
<li><code>xmm6</code> - <code>xmm15</code></li>
</ul>
<h2 id="asm-skeleton"><a class="header" href="#asm-skeleton">ASM skeleton</a></h2>
<p>Small assembler skeleton, ready to use with following properties:</p>
<ul>
<li>use raw Linux syscalls (<code>man 2 syscall</code> for ABI)</li>
<li>no <code>C runtime (crt)</code></li>
<li>gnu assembler <a href="https://sourceware.org/binutils/docs/as"><code>gas</code></a></li>
<li>intel syntax</li>
</ul>
<pre><code class="language-x86asm"># file: greet.s
.intel_syntax noprefix
.section .text, "ax", @progbits
.global _start
_start:
mov rdi, 1 # fd
lea rsi, [rip + greeting] # buf
mov rdx, [rip + greeting_len] # count
mov rax, 1 # write(2) syscall nr
syscall
mov rdi, 0 # exit code
mov rax, 60 # exit(2) syscall nr
syscall
.section .rdonly, "a", @progbits
greeting:
.asciz "Hi ASM-World!\n"
greeting_len:
.int .-greeting
</code></pre>
<blockquote>
<p>Syscall numbers are defined in <code>/usr/include/asm/unistd.h</code>.</p>
</blockquote>
<p>To compile and run:</p>
<pre><code class="language-bash">> gcc -o greet greet.s -nostartfiles -nostdlib && ./greet
Hi ASM-World!
</code></pre>
<h2 id="references-7"><a class="header" href="#references-7">References</a></h2>
<ul>
<li><a href="https://gitlab.com/x86-psABIs/x86-64-ABI">SystemV AMD64 ABI</a></li>
<li><a href="https://www.amd.com/system/files/TechDocs/24592.pdf">AMD64 Vol1: Application Programming</a></li>
<li><a href="https://www.amd.com/system/files/TechDocs/24593.pdf">AMD64 Vol2: System Programming</a></li>
<li><a href="https://www.amd.com/system/files/TechDocs/24594.pdf">AMD64 Vol3: General-Purpose & System Instructions</a></li>
<li><a href="https://cs.brown.edu/courses/cs033/docs/guides/x64_cheatsheet.pdf">X86_64 Cheat-Sheet</a></li>
<li><a href="https://software.intel.com/content/www/us/en/develop/download/intel-64-and-ia-32-architectures-software-developers-manual-volume-1-basic-architecture.html">Intel 64 Vol1: Basic Architecture</a></li>
<li><a href="https://software.intel.com/content/www/us/en/develop/download/intel-64-and-ia-32-architectures-sdm-combined-volumes-2a-2b-2c-and-2d-instruction-set-reference-a-z.html">Intel 64 Vol2: Instruction Set Reference</a></li>
<li><a href="https://software.intel.com/content/www/us/en/develop/download/intel-64-and-ia-32-architectures-sdm-combined-volumes-3a-3b-3c-and-3d-system-programming-guide.html">Intel 64 Vol3: System Programming Guide</a></li>
<li><a href="https://sourceware.org/binutils/docs/as">GNU Assembler</a></li>
<li><a href="https://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops">GNU Assembler Directives</a></li>
<li><a href="https://sourceware.org/binutils/docs/as/i386_002dDependent.html">GNU Assembler <code>x86_64</code> dependent features</a></li>
<li><a href="https://github.com/johannst/juicebox-asm"><code>juicebox-asm</code> an <code>x86_64</code> jit assembler playground</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="arm64"><a class="header" href="#arm64">arm64</a></h1>
<p>keywords: arm64, aarch64, abi</p>
<ul>
<li>64bit synonyms: <code>arm64</code>, <code>aarch64</code></li>
<li>ISA type: <code>RISC</code></li>
<li>Endianness: <code>little</code>, <code>big</code></li>
</ul>
<h2 id="registers-1"><a class="header" href="#registers-1">Registers</a></h2>
<h3 id="general-purpose-registers"><a class="header" href="#general-purpose-registers">General purpose registers</a></h3>
<pre><code class="language-markdown">bytes
[7:0] [3:0] desc
---------------------------------------------
x0-x28 w0-w28 general purpose registers
x29 w29 frame pointer (FP)
x30 w30 link register (LR)
sp wsp stack pointer (SP)
pc program counter (PC)
xzr wzr zero register
</code></pre>
<blockquote>
<p>Write to <code>wN</code> register clears upper 32bit.</p>
</blockquote>
<h3 id="special-registers-per-el"><a class="header" href="#special-registers-per-el">Special registers per EL</a></h3>
<pre><code class="language-markdown">bytes
[7:0] desc
---------------------------------------------
sp_el0 stack pointer EL0
sp_el1 stack pointer EL1
elr_el1 exception link register EL1
spsr_el1 saved process status register EL1
sp_el2 stack pointer EL2
elr_el2 exception link register EL2
spsr_el2 saved process status register EL2
sp_el3 stack pointer EL3
elr_el3 exception link register EL3
spsr_el3 saved process status register EL3
</code></pre>
<h2 id="instructions-cheatsheet"><a class="header" href="#instructions-cheatsheet">Instructions cheatsheet</a></h2>
<h3 id="accessing-system-registers"><a class="header" href="#accessing-system-registers">Accessing system registers</a></h3>
<p>Reading from system registers:</p>
<pre><code class="language-armasm">mrs x0, vbar_el1 // move vbar_el1 into x0
</code></pre>
<p>Writing to system registers:</p>
<pre><code class="language-armasm">msr vbar_el1, x0 // move x0 into vbar_el1
</code></pre>
<h3 id="control-flow-1"><a class="header" href="#control-flow-1">Control Flow</a></h3>
<pre><code class="language-armasm">b <offset> // relative forward/back branch
br <Xn> // absolute branch to address in register Xn
// branch & link, store return address in X30 (LR)
bl <offset> // relative forward/back branch
blr <Xn> // absolute branch to address in register Xn
ret {Xn} // return to address in X30, or Xn if supplied
</code></pre>
<h2 id="addressing-1"><a class="header" href="#addressing-1">Addressing</a></h2>
<h3 id="offset"><a class="header" href="#offset">Offset</a></h3>
<pre><code class="language-armasm">ldr x0, [x1] // x0 = [x1]
ldr x0, [x1, 8] // x0 = [x1 + 8]
ldr x0, [x1, x2, lsl #3] // x0 = [x1 + (x2<<3)]
ldr x0, [x1, w2, stxw] // x0 = [x1 + sign_ext(w2)]
ldr x0, [x1, w2, stxw #3] // x0 = [x1 + (sign_ext(w2)<<3)]
</code></pre>
<blockquote>
<p>Shift amount can either be <code>0</code> or <code>log2(access_size_bytes)</code>. Eg for 8byte
access it can either be <code>{0, 3}</code>.</p>
</blockquote>
<h3 id="index"><a class="header" href="#index">Index</a></h3>
<pre><code class="language-armasm">ldr x0, [x1, 8]! // pre-inc : x1+=8; x0 = [x1]
ldr x0, [x1], 8 // post-inc: x0 = [x1]; x1+=8
</code></pre>
<h3 id="pair-access"><a class="header" href="#pair-access">Pair access</a></h3>
<pre><code class="language-armasm">ldp x1, x2, [x0] // x1 = [x0]; x2 = [x0 + 8]
stp x1, x2, [x0] // [x0] = x1; [x0 + 8] = x2
</code></pre>
<h2 id="procedure-call-standard-arm64-aapcs64"><a class="header" href="#procedure-call-standard-arm64-aapcs64">Procedure Call Standard ARM64 (<a href="https://github.com/ARM-software/abi-aa/blob/master/aapcs64/aapcs64.rst"><code>aapcs64</code></a>)</a></h2>
<h3 id="passing-arguments-to-functions-1"><a class="header" href="#passing-arguments-to-functions-1">Passing arguments to functions</a></h3>
<ul>
<li>Integer/Pointer arguments
<pre><code class="language-markdown">reg arg
-----------
x0 1
.. ..
x7 8
</code></pre>
</li>
<li>Additional arguments are passed on the stack. Arguments are pushed
<code>right-to-left (RTL)</code>, meaning next arguments are closer to current <code>sp</code>.
<pre><code class="language-markdown">void take(..., int a9, int a10);
| | | ... | Hi
| +-->| a10 | |
+---------->| a9 | <-SP |
+-----+ v
| ... | Lo
</code></pre>
</li>
</ul>
<h3 id="return-values-from-functions-2"><a class="header" href="#return-values-from-functions-2">Return values from functions</a></h3>
<ul>
<li>Integer/Pointer return values
<pre><code class="language-markdown">reg size
-----------------
x0 64 bit
</code></pre>
</li>
</ul>
<h3 id="callee-saved-registers-2"><a class="header" href="#callee-saved-registers-2">Callee saved registers</a></h3>
<ul>
<li><code>x19</code> - <code>x28</code></li>
<li><code>SP</code></li>
</ul>
<h3 id="stack-1"><a class="header" href="#stack-1">Stack</a></h3>
<ul>
<li>full descending
<ul>
<li>full: <code>sp</code> points to the last used location (valid item)</li>
<li>descending: stack grows downwards</li>
</ul>
</li>
<li><code>sp</code> must be 16byte aligned when used to access memory for r/w</li>
<li><code>sp</code> must be 16byte aligned on public interface interfaces</li>
</ul>
<h3 id="frame-chain"><a class="header" href="#frame-chain">Frame chain</a></h3>
<ul>
<li>linked list of stack-frames</li>
<li>each frame links to the frame of its caller by a <code>frame record</code>
<ul>
<li>a frame record is described as a <code>(FP,LR)</code> pair</li>
</ul>
</li>
<li><code>x29 (FP)</code> must point to the frame record of the current stack-frame
<pre><code class="language-markdown"> +------+ Hi
| 0 | frame0 |
+->| 0 | |
| | ... | |
| +------+ |
| | LR | frame1 |
+--| FP |<-+ |
| ... | | |
+------+ | |
| LR | | current |
x29 ->| FP |--+ frame v
| ... | Lo
</code></pre>
</li>
<li>end of the frame chain is indicated by following frame record <code>(0,-)</code></li>
<li>location of the frame record in the stack frame is not specified</li>
</ul>
<h3 id="function-prologue--epilogue-1"><a class="header" href="#function-prologue--epilogue-1">Function prologue & epilogue</a></h3>
<ul>
<li>prologue
<pre><code class="language-armasm">sub sp, sp, 16
stp x29, x30, [sp] // [sp] = x29; [sp + 8] = x30
mov x29, sp // FP points to frame record
</code></pre>
</li>
<li>epilogue
<pre><code class="language-armasm">ldp x29, x30, [sp] // x29 = [sp]; x30 = [sp + 8]
add sp, sp, 16
ret
</code></pre>
</li>
</ul>
<h2 id="asm-skeleton-1"><a class="header" href="#asm-skeleton-1">ASM skeleton</a></h2>
<p>Small assembler skeleton, ready to use with following properties:</p>
<ul>
<li>use raw Linux syscalls (<code>man 2 syscall</code> for ABI)</li>
<li>no <code>C runtime (crt)</code></li>
<li>gnu assembler <a href="https://sourceware.org/binutils/docs/as"><code>gas</code></a></li>
</ul>
<pre><code class="language-armasm">// file: greet.S
#include <asm/unistd.h> // syscall NRs
.arch armv8-a
.section .text, "ax", @progbits
.balign 4 // align code on 4byte boundary
.global _start
_start:
mov x0, 2 // fd
ldr x1, =greeting // buf
ldr x2, =greeting_len // &len
ldr x2, [x2] // len
mov w8, __NR_write // write(2) syscall
svc 0
mov x0, 0 // exit code
mov w8, __NR_exit // exit(2) syscall
svc 0
.balign 8 // align data on 8byte boundary
.section .rodata, "a", @progbits
greeting:
.asciz "Hi ASM-World!\n"
greeting_len:
.int .-greeting
</code></pre>
<blockquote>
<p>man gcc: <code>file.S</code> assembler code that must be preprocessed.</p>
</blockquote>
<p>To cross-compile and run:</p>
<pre><code class="language-bash">> aarch64-linux-gnu-g++ -o greet greet.S -nostartfiles -nostdlib \
-Wl,--dynamic-linker=/usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1 \
&& qemu-aarch64 ./greet
Hi ASM-World!
</code></pre>
<blockquote>
<p>Cross-compiling on <code>Ubuntu 20.04 (x86_64)</code>, paths might differ on other
distributions. Explicitly specifying the dynamic linker should not be
required when compiling natively on arm64.</p>
</blockquote>
<h2 id="references-8"><a class="header" href="#references-8">References</a></h2>
<ul>
<li><a href="https://github.com/ARM-software/abi-aa/blob/master/aapcs64/aapcs64.rst">Procedure Call Standard ARM64</a></li>
<li><a href="https://developer.arm.com/documentation/den0024/latest">ARMv8-A Programmer's Guide</a></li>
<li><a href="https://developer.arm.com/documentation/ddi0487/latest">ARMv8-A Architecture Reference Manual</a></li>
<li><a href="https://developer.arm.com/documentation/dai0527/latest">AppNote: ARMv8 Bare-metal boot code</a></li>
<li><a href="https://sourceware.org/binutils/docs/as">GNU Assembler</a></li>
<li><a href="https://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops">GNU Assembler Directives</a></li>
<li><a href="https://sourceware.org/binutils/docs/as/AArch64_002dDependent.html">GNU Assembler <code>AArch64</code> dependent features</a></li>
<li><a href="https://developer.arm.com/documentation/ddi0602/latest/Base-Instructions">ARM64-A Instruction Browser</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="armv7a"><a class="header" href="#armv7a">armv7a</a></h1>
<p>keywords: arm, armv7, abi</p>
<ul>
<li>ISA type: <code>RISC</code></li>
<li>Endianness: <code>little</code>, <code>big</code></li>
</ul>
<h2 id="registers-2"><a class="header" href="#registers-2">Registers</a></h2>
<h3 id="general-purpose-registers-1"><a class="header" href="#general-purpose-registers-1">General purpose registers</a></h3>
<pre><code class="language-markdown">bytes
[3:0] alt desc
---------------------------------------------
r0-r12 general purpose registers
r11 fp
r13 sp stack pointer
r14 lr link register
r15 pc program counter
</code></pre>
<h3 id="special-registers"><a class="header" href="#special-registers">Special registers</a></h3>
<pre><code class="language-markdown">bytes
[3:0] desc
---------------------------------------------
cpsr current program status register
</code></pre>
<h3 id="cpsr-register"><a class="header" href="#cpsr-register">CPSR register</a></h3>
<pre><code class="language-markdown">cpsr
bits desc
-----------------------------
[31] N negative flag
[30] Z zero flag
[29] C carry flag
[28] V overflow flag
[27] Q cummulative saturation (sticky)
[9] E load/store endianness
[8] A disable asynchronous aborts
[7] I disable IRQ
[6] F disable FIQ
[5] T indicate Thumb state
[4:0] M process mode (USR, FIQ, IRQ, SVC, ABT, UND, SYS)
</code></pre>
<h2 id="instructions-cheatsheet-1"><a class="header" href="#instructions-cheatsheet-1">Instructions cheatsheet</a></h2>
<h3 id="accessing-system-registers-1"><a class="header" href="#accessing-system-registers-1">Accessing system registers</a></h3>
<p>Reading from system registers:</p>
<pre><code class="language-armasm">mrs r0, cpsr // move cpsr into r0
</code></pre>
<p>Writing to system registers:</p>
<pre><code class="language-armasm">msr cpsr, r0 // move r0 into cpsr
</code></pre>
<h3 id="control-flow-2"><a class="header" href="#control-flow-2">Control Flow</a></h3>
<pre><code class="language-armasm">b <lable> // relative forward/back branch
bl <lable> // relative forward/back branch & link return addr in r14 (LR)
// branch & exchange (can change between ARM & Thumb instruction set)
// bit Rm[0] == 0 -> ARM
// bit Rm[0] == 1 -> Thumb
bx <Rm> // absolute branch to address in register Rm
blx <Rm> // absolute branch to address in register Rm &
// link return addr in r14 (LR)
</code></pre>
<h3 id="loadstore"><a class="header" href="#loadstore">Load/Store</a></h3>
<p>Different addressing modes.</p>
<pre><code class="language-armasm">ldr r1, [r0] // r1 = [r0]
ldr r1, [r0, #4] // r1 = [r0+4]
ldr r1, [r0, #4]! // pre-inc : r0+=4; r1 = [r0]
ldr r1, [r0], #4 // post-inc: [r0] = r1; r0+=4
ldr r0, [r1, r2, lsl #3] // r0 = [r1 + (r2<<3)]
</code></pre>
<p>Load/store multiple registers full-descending.</p>
<pre><code class="language-armasm">stmfd r0!, {r1-r2, r5} // r0-=4; [r0]=r5
// r0-=4; [r0]=r2
// r0-=4; [r0]=r1
ldmfd r0!, {r1-r2, r5} // r1=[r0]; r0+=4
// r2=[r0]; r0+=4
// r5=[r0]; r0+=4
</code></pre>
<blockquote>
<p><code>!</code> is optional but has the effect to update the base pointer register <code>r0</code> here.</p>
</blockquote>
<p>Push/Pop</p>
<pre><code class="language-armasm">push {r0-r2} // effectively stmfd sp!, {r0-r2}
pop {r0-r2} // effectively ldmfd sp!, {r0-r2}
</code></pre>
<h2 id="procedure-call-standard-arm-aapcs32"><a class="header" href="#procedure-call-standard-arm-aapcs32">Procedure Call Standard ARM (<a href="https://github.com/ARM-software/abi-aa/blob/master/aapcs32/aapcs32.rst"><code>aapcs32</code></a>)</a></h2>
<h3 id="passing-arguments-to-functions-2"><a class="header" href="#passing-arguments-to-functions-2">Passing arguments to functions</a></h3>
<ul>
<li>integer/pointer arguments
<pre><code class="language-markdown">reg arg
-----------
r0 1
.. ..
r3 4
</code></pre>
</li>
<li>a double word (64bit) is passed in two consecutive registers (eg <code>r1+r2</code>)</li>
<li>additional arguments are passed on the stack. Arguments are pushed
<code>right-to-left (RTL)</code>, meaning next arguments are closer to current <code>sp</code>.
<pre><code class="language-markdown">void take(..., int a5, int a6);
| | | ... | Hi
| +-->| a6 | |
+---------->| a5 | <-SP |
+-----+ v
| ... | Lo
</code></pre>
</li>
</ul>
<h3 id="return-values-from-functions-3"><a class="header" href="#return-values-from-functions-3">Return values from functions</a></h3>
<ul>
<li>integer/pointer return values
<pre><code class="language-markdown">reg size
-----------------
r0 32 bit
r0+r1 64 bit
</code></pre>
</li>
</ul>
<h3 id="callee-saved-registers-3"><a class="header" href="#callee-saved-registers-3">Callee saved registers</a></h3>
<ul>
<li><code>r4</code> - <code>r11</code></li>
<li><code>sp</code></li>
</ul>
<h3 id="stack-2"><a class="header" href="#stack-2">Stack</a></h3>
<ul>
<li>full descending
<ul>
<li>full: <code>sp</code> points to the last used location (valid item)</li>
<li>descending: stack grows downwards</li>
</ul>
</li>
<li><code>sp</code> must be 4byte aligned (word boundary) at all time</li>
<li><code>sp</code> must be 8byte aligned on public interface interfaces</li>
</ul>
<h3 id="frame-chain-1"><a class="header" href="#frame-chain-1">Frame chain</a></h3>
<ul>
<li>not strictly required by each platform</li>
<li>linked list of stack-frames</li>
<li>each frame links to the frame of its caller by a <code>frame record</code>
<ul>
<li>a frame record is described as a <code>(FP,LR)</code> pair (2x32bit)</li>
</ul>
</li>
<li><code>r11 (FP)</code> must point to the frame record of the current stack-frame
<pre><code class="language-markdown"> +------+ Hi
| 0 | frame0 |
+->| 0 | |
| | ... | |
| +------+ |
| | LR | frame1 |
+--| FP |<-+ |
| ... | | |
+------+ | |
| LR | | current |
r11 ->| FP |--+ frame v
| ... | Lo
</code></pre>
</li>
<li>end of the frame chain is indicated by following frame record <code>(0,-)</code></li>
<li>location of the frame record in the stack frame is not specified</li>
<li><code>r11</code> is not updated before the new frame record is fully constructed</li>
</ul>
<h3 id="function-prologue--epilogue-2"><a class="header" href="#function-prologue--epilogue-2">Function prologue & epilogue</a></h3>
<ul>
<li>prologue
<pre><code class="language-armasm">push {fp, lr}
mov fp, sp // FP points to frame record
</code></pre>
</li>
<li>epilogue
<pre><code class="language-armasm">pop {fp, pc} // pop LR directly into PC
</code></pre>
</li>
</ul>
<h2 id="asm-skeleton-2"><a class="header" href="#asm-skeleton-2">ASM skeleton</a></h2>
<p>Small assembler skeleton, ready to use with following properties:</p>
<ul>
<li>use raw Linux syscalls (<code>man 2 syscall</code> for ABI)</li>
<li>no <code>C runtime (crt)</code></li>
<li>gnu assembler <a href="https://sourceware.org/binutils/docs/as"><code>gas</code></a></li>
</ul>
<pre><code class="language-armasm">// file: greet.S
#include <asm/unistd.h> // syscall NRs
.arch armv7-a
.section .text, "ax"
.balign 4
// Emit `arm` instructions, same as `.arm` directive.
.code 32
.global _start
_start:
// Branch with link and exchange instruction set.
blx _do_greet
mov r0, #0 // exit code
mov r7, #__NR_exit // exit(2) syscall
swi 0x0
// Emit `thumb` instructions, same as `.thumb` directive.
.code 16
.thumb_func
_do_greet:
mov r0, #2 // fd
ldr r1, =greeting // buf
ldr r2, =greeting_len // &len
ldr r2, [r2] // len
mov r7, #__NR_write // write(2) syscall
swi 0x0
// Branch and exchange instruction set.
bx lr
.balign 8 // align data on 8byte boundary
.section .rodata, "a"
greeting:
.asciz "Hi ASM-World!\n"
greeting_len:
.int .-greeting
</code></pre>
<blockquote>
<p>man gcc: <code>file.S</code> assembler code that must be preprocessed.</p>
</blockquote>
<p>To cross-compile and run:</p>
<pre><code class="language-bash">> arm-linux-gnueabi-gcc -o greet greet.S -nostartfiles -nostdlib \
-Wl,--dynamic-linker=/usr/arm-linux-gnueabi/lib/ld-linux.so.3 \
&& qemu-arm ./greet
Hi ASM-World!
</code></pre>
<blockquote>
<p>Cross-compiling on <code>Ubuntu 20.04 (x86_64)</code>, paths might differ on other
distributions. Explicitly specifying the dynamic linker should not be
required when compiling natively on arm.</p>
</blockquote>
<h2 id="references-9"><a class="header" href="#references-9">References</a></h2>
<ul>
<li><a href="https://github.com/ARM-software/abi-aa/blob/master/aapcs32/aapcs32.rst">Procedure Call Standard ARM</a></li>
<li><a href="https://developer.arm.com/documentation/den0013/latest">ARMv7-A Programmer's Guide</a></li>
<li><a href="https://developer.arm.com/documentation/ddi0406/latest">ARMv7-A Architecture Reference Manual</a></li>
<li><a href="https://sourceware.org/binutils/docs/as">GNU Assembler</a></li>
<li><a href="https://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops">GNU Assembler Directives</a></li>
<li><a href="https://sourceware.org/binutils/docs/as/ARM_002dDependent.html">GNU Assembler <code>ARM</code> dependent features</a></li>
</ul>
<div style="break-before: page; page-break-before: always;"></div><h1 id="riscv"><a class="header" href="#riscv">riscv</a></h1>
<p>keywords: rv32, rv64</p>
<ul>
<li>ISA type: <code>RISC</code></li>
<li>Endianness: <code>little</code>, <code>big</code></li>
</ul>
<h2 id="registers-3"><a class="header" href="#registers-3">Registers</a></h2>
<ul>
<li>riscv32 => <code>XLEN=32</code></li>
<li>riscv64 => <code>XLEN=64</code></li>
</ul>
<h3 id="general-purpose-registers-2"><a class="header" href="#general-purpose-registers-2">General purpose registers</a></h3>
<pre><code class="language-markdown">[XLEN-1:0] abi name desc
---------------------------------------------
x0 zero zero register
x1 ra return addr
x2 sp stack ptr
x3 gp global ptr
x4 tp thread ptr
x5-x7 t0-t2 temp regs
x8-x9 s0-s1 saved regs
x10-x17 a0-a7 arg regs
x18-x27 s2-s11 saved regs
x28-x31 t3-t6 temp regs
</code></pre>
<h2 id="asm-skeleton-3"><a class="header" href="#asm-skeleton-3">ASM skeleton</a></h2>
<p>Small assembler skeleton, ready to use with following properties:</p>
<ul>
<li>use raw Linux syscalls (<code>man 2 syscall</code> for ABI)</li>
<li>no <code>C runtime (crt)</code></li>
<li>gnu assembler <a href="https://sourceware.org/binutils/docs/as"><code>gas</code></a></li>
</ul>
<pre><code class="language-mipsasm">// file: greet.S
#include <asm/unistd.h> // syscall NRs
.section .text, "ax", @progbits
.balign 4 // align code on 4byte boundary
.global _start
_start:
li a0, 2 // fd
la a1, greeting // buf
ld a2, (greeting_len) // &len
li a7, __NR_write // write(2) syscall
ecall
li a0, 42 // exit code
li a7, __NR_exit // exit(2) syscall
ecall
.balign 8 // align data on 8byte boundary
.section .rodata, "a", @progbits
greeting:
.asciz "Hi ASM-World!\n"
greeting_len:
.int .-greeting
</code></pre>
<blockquote>
<p>man gcc: <code>file.S</code> assembler code that must be preprocessed.</p>
</blockquote>
<p>To cross-compile and run:</p>
<pre><code class="language-bash">> riscv64-linux-gnu-gcc -o greet greet.S -nostartfiles -nostdlib \
-Wl,--dynamic-linker=/usr/riscv64-linux-gnu/lib/ld-linux-riscv64-lp64d.so.1 \
&& qemu-riscv64 ./greet
Hi ASM-World!
</code></pre>
<blockquote>
<p>Cross-compiling on <code>Ubuntu 20.04 (x86_64)</code>, paths might differ on other
distributions. Explicitly specifying the dynamic linker should not be
required when compiling natively on riscv.</p>
<p>Select dynamic linker according to abi used during compile & link.</p>
</blockquote>
<h2 id="references-10"><a class="header" href="#references-10">References</a></h2>
<ul>
<li><a href="https://sourceware.org/binutils/docs/as">GNU Assembler</a></li>
<li><a href="https://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops">GNU Assembler Directives</a></li>
<li><a href="https://github.com/johannst/rv64i-linux-user-no-std">rv64i-linux-user-no-std</a></li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js"></script>
<script src="mark.min.js"></script>
<script src="searcher.js"></script>
<script src="clipboard.min.js"></script>
<script src="highlight.js"></script>
<script src="book.js"></script>
<!-- Custom JS scripts -->
<script>
window.addEventListener('load', function() {
window.setTimeout(window.print, 100);
});
</script>
</div>
</body>
</html>