diff options
author | johannst <johannst@users.noreply.github.com> | 2020-04-28 09:11:18 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2020-04-28 09:11:18 +0000 |
commit | 15230bbb9b1f69def9b0e1b41a097638c0fda734 (patch) | |
tree | 2b490b2f0b8d6f3eab0af91c7ba5cc557b540af6 /print.html | |
parent | ce1a0eaa58f7ad07c5389996a90c4b02bdf573fc (diff) | |
download | notes-15230bbb9b1f69def9b0e1b41a097638c0fda734.tar.gz notes-15230bbb9b1f69def9b0e1b41a097638c0fda734.zip |
deploy: fef4d6ff2ad9f48e6dccde0f061453e6a3ac624e
Diffstat (limited to 'print.html')
-rw-r--r-- | print.html | 835 |
1 files changed, 428 insertions, 407 deletions
@@ -83,7 +83,7 @@ <nav id="sidebar" class="sidebar" aria-label="Table of contents"> <div id="sidebar-scrollbox" class="sidebar-scrollbox"> - <ol class="chapter"><li class="expanded "><a href="ld.so.html"><strong aria-hidden="true">1.</strong> ld.so</a></li><li class="expanded "><a href="git.html"><strong aria-hidden="true">2.</strong> git</a></li><li class="expanded "><a href="bash.html"><strong aria-hidden="true">3.</strong> bash</a></li><li class="expanded "><a href="zsh.html"><strong aria-hidden="true">4.</strong> zsh</a></li><li class="expanded "><a href="tmux.html"><strong aria-hidden="true">5.</strong> tmux</a></li><li class="expanded "><a href="awk.html"><strong aria-hidden="true">6.</strong> awk</a></li><li class="expanded "><a href="gdb.html"><strong aria-hidden="true">7.</strong> gdb</a></li><li class="expanded "><a href="radare2.html"><strong aria-hidden="true">8.</strong> radare2</a></li><li class="expanded "><a href="emacs.html"><strong aria-hidden="true">9.</strong> emacs</a></li><li class="expanded "><a href="fish.html"><strong aria-hidden="true">10.</strong> fish</a></li><li class="expanded "><a href="strace.html"><strong aria-hidden="true">11.</strong> strace</a></li><li class="expanded "><a href="lsof.html"><strong aria-hidden="true">12.</strong> lsof</a></li><li class="expanded "><a href="pidstat.html"><strong aria-hidden="true">13.</strong> pidstat</a></li><li class="expanded "><a href="time.html"><strong aria-hidden="true">14.</strong> time</a></li><li class="expanded "><a href="pgrep.html"><strong aria-hidden="true">15.</strong> pgrep</a></li><li class="expanded "><a href="pstack.html"><strong aria-hidden="true">16.</strong> pstack</a></li><li class="expanded "><a href="perf.html"><strong aria-hidden="true">17.</strong> perf</a></li><li class="expanded "><a href="oprofile.html"><strong aria-hidden="true">18.</strong> OProfile</a></li><li class="expanded "><a href="od.html"><strong aria-hidden="true">19.</strong> od</a></li><li class="expanded "><a href="xxd.html"><strong aria-hidden="true">20.</strong> xxd</a></li><li class="expanded "><a href="readelf.html"><strong aria-hidden="true">21.</strong> readelf</a></li><li class="expanded "><a href="objdump.html"><strong aria-hidden="true">22.</strong> objdump</a></li><li class="expanded "><a href="nm.html"><strong aria-hidden="true">23.</strong> nm</a></li><li class="expanded "><a href="c++filt.html"><strong aria-hidden="true">24.</strong> c++filt</a></li><li class="expanded "><a href="c++.html"><strong aria-hidden="true">25.</strong> c++</a></li><li class="expanded "><a href="glibc.html"><strong aria-hidden="true">26.</strong> glibc</a></li><li class="expanded "><a href="gcc.html"><strong aria-hidden="true">27.</strong> gcc</a></li></ol> + <ol class="chapter"><li class="expanded "><a href="misc/index.html"><strong aria-hidden="true">1.</strong> Misc</a></li><li><ol class="section"><li class="expanded "><a href="misc/zsh.html"><strong aria-hidden="true">1.1.</strong> zsh</a></li><li class="expanded "><a href="misc/bash.html"><strong aria-hidden="true">1.2.</strong> bash</a></li><li class="expanded "><a href="misc/fish.html"><strong aria-hidden="true">1.3.</strong> fish</a></li><li class="expanded "><a href="misc/tmux.html"><strong aria-hidden="true">1.4.</strong> tmux</a></li><li class="expanded "><a href="misc/git.html"><strong aria-hidden="true">1.5.</strong> git</a></li><li class="expanded "><a href="misc/awk.html"><strong aria-hidden="true">1.6.</strong> awk</a></li><li class="expanded "><a href="misc/emacs.html"><strong aria-hidden="true">1.7.</strong> emacs</a></li><li class="expanded "><a href="misc/gdb.html"><strong aria-hidden="true">1.8.</strong> gdb</a></li><li class="expanded "><a href="misc/radare2.html"><strong aria-hidden="true">1.9.</strong> radare2</a></li></ol></li><li class="expanded "><a href="monitor/index.html"><strong aria-hidden="true">2.</strong> Resource analysis & monitor</a></li><li><ol class="section"><li class="expanded "><a href="monitor/lsof.html"><strong aria-hidden="true">2.1.</strong> lsof</a></li><li class="expanded "><a href="monitor/pidstat.html"><strong aria-hidden="true">2.2.</strong> pidstat</a></li><li class="expanded "><a href="monitor/pgrep.html"><strong aria-hidden="true">2.3.</strong> pgrep</a></li><li class="expanded "><a href="monitor/pmap.html"><strong aria-hidden="true">2.4.</strong> pmap</a></li><li class="expanded "><a href="monitor/pstack.html"><strong aria-hidden="true">2.5.</strong> pstack</a></li></ol></li><li class="expanded "><a href="trace_profile/index.html"><strong aria-hidden="true">3.</strong> Trace and Profile</a></li><li><ol class="section"><li class="expanded "><a href="trace_profile/strace.html"><strong aria-hidden="true">3.1.</strong> strace</a></li><li class="expanded "><a href="trace_profile/ltrace.html"><strong aria-hidden="true">3.2.</strong> ltrace</a></li><li class="expanded "><a href="trace_profile/perf.html"><strong aria-hidden="true">3.3.</strong> perf</a></li><li class="expanded "><a href="trace_profile/oprofile.html"><strong aria-hidden="true">3.4.</strong> OProfile</a></li><li class="expanded "><a href="trace_profile/time.html"><strong aria-hidden="true">3.5.</strong> time</a></li></ol></li><li class="expanded "><a href="binary/index.html"><strong aria-hidden="true">4.</strong> Binary</a></li><li><ol class="section"><li class="expanded "><a href="binary/od.html"><strong aria-hidden="true">4.1.</strong> od</a></li><li class="expanded "><a href="binary/xxd.html"><strong aria-hidden="true">4.2.</strong> xxd</a></li><li class="expanded "><a href="binary/readelf.html"><strong aria-hidden="true">4.3.</strong> readelf</a></li><li class="expanded "><a href="binary/objdump.html"><strong aria-hidden="true">4.4.</strong> objdump</a></li><li class="expanded "><a href="binary/nm.html"><strong aria-hidden="true">4.5.</strong> nm</a></li></ol></li><li class="expanded "><a href="development/index.html"><strong aria-hidden="true">5.</strong> Development</a></li><li><ol class="section"><li class="expanded "><a href="development/c++filt.html"><strong aria-hidden="true">5.1.</strong> c++filt</a></li><li class="expanded "><a href="development/c++.html"><strong aria-hidden="true">5.2.</strong> c++</a></li><li class="expanded "><a href="development/glibc.html"><strong aria-hidden="true">5.3.</strong> glibc</a></li><li class="expanded "><a href="development/gcc.html"><strong aria-hidden="true">5.4.</strong> gcc</a></li><li class="expanded "><a href="development/ld.so.html"><strong aria-hidden="true">5.5.</strong> ld.so</a></li></ol></li></ol> </div> <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div> </nav> @@ -150,221 +150,106 @@ <div id="content" class="content"> <main> - <h1><a class="header" href="#ldso8" id="ldso8">ld.so(8)</a></h1> -<h2><a class="header" href="#environment-variables" id="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 + <h1><a class="header" href="#zsh1" id="zsh1">zsh(1)</a></h1> +<h2><a class="header" href="#keybindings" id="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> -<h2><a class="header" href="#ld_preload-initialization-order-and-link-map" id="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 +<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` - > 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 | - +------+ +------+ +------+ +------+ +# 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>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 +<h2><a class="header" href="#completion" id="completion">Completion</a></h2> +<h3><a class="header" href="#installation" id="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 - # 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><a class="header" href="#dynamic-linking-x86_64" id="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 +function _foo() { + ... +} </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 +<p>Alternatively one can install a completion function explicitly by calling <code>compdef <FUNC> <CMD></code>.</p> +<h3><a class="header" href="#completion-variables" id="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><a class="header" href="#completion-functions" id="completion-functions">Completion Functions</a></h3> <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> +<li><code>_describe</code> simple completion, just words + description</li> +<li><code>_arguments</code> sophisticated completion, allow to specify actions</li> </ul> -<h1><a class="header" href="#git1" id="git1">git(1)</a></h1> -<h2><a class="header" href="#staging" id="staging">staging</a></h2> -<pre><code class="language-markdown"> git add -p [<file>] ............ partial staging (interactive) -</code></pre> -<h2><a class="header" href="#remote" id="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><a class="header" href="#branching" id="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 branch with name <bname> - git checkout <bname> ........... switch to branch with name <bname> - git push -u origin <rbname> .... push branch to origin (or other remote), and - setup <rbname> as tracking branch -</code></pre> -<h2><a class="header" href="#resetting" id="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 +<h4><a class="header" href="#completion-with-a-hrefhttpzshsourceforgenetdocreleasecompletion-systemhtmlcompletion-functions_describea" id="completion-with-a-hrefhttpzshsourceforgenetdocreleasecompletion-systemhtmlcompletion-functions_describea">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> -<h2><a class="header" href="#tags" id="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 +<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> -<h2><a class="header" href="#diff" id="diff">Diff</a></h2> -<pre><code class="language-markdown"> git diff HEAD:<fname> origin/HEAD:<fname> ... diff files for different refs - git diff -U$(wc -l <fname>) <fname> ......... shows complete file with diffs - instead of usual diff snippets +<h4><a class="header" href="#completion-with-a-hrefhttpzshsourceforgenetdocreleasecompletion-systemhtmlcompletion-functions_argumentsa" id="completion-with-a-hrefhttpzshsourceforgenetdocreleasecompletion-systemhtmlcompletion-functions_argumentsa">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> -<h2><a class="header" href="#log" id="log">Log</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 +<p>where <code>SPEC</code> can have one of the following forms:</p> +<ul> +<li><code>OPT[DESC]:MSG:ACTION</code></li> +<li><code>N:MSG:ACTION</code></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> -<h2><a class="header" href="#file-history" id="file-history">File history</a></h2> -<pre><code class="language-markdown"> git log -p <file> ......... show commit history + diffs for <file> - git log --oneline <file> .. show commit history for <file> in compact format +<h3><a class="header" href="#example" id="example">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> -<h2><a class="header" href="#patching" id="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 +<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 - 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 <PATCH> .......... apply the patch locally without creating a commit +function _foo_color() { + local colors=() + colors+=('green:green color') + colors+=('red:red color') + colors+=('blue:blue color') + _describe "color" colors +} - # eg: generate patches for each commit from initial commit on - git format-patch -N $(git rev-list --max-parents=0 HEAD) +function _foo() { + _arguments \ + "-c[define color]:color:->s_color" \ + "-s[select sound]:color:(low high)" \ + "-f[select file]:file:_files" \ + "-d[select dir]:fir:_files -/" \ + "-h[help]" - # generate single patch file from a certain commit/ref - git format-patch <COMMIT/REF> --stdout > my-patch.patch -</code></pre> -<h2><a class="header" href="#submodules" id="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><a class="header" href="#inspection" id="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><a class="header" href="#revision-specifier" id="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 + case $state in + s_color) _foo_color;; + esac +} </code></pre> <h1><a class="header" href="#bash1" id="bash1">bash(1)</a></h1> <h2><a class="header" href="#expansion" id="expansion">Expansion</a></h2> @@ -402,7 +287,7 @@ ${foo/pattern/string} # replace pattern with string when expanding foo # '%' pattern match at end </code></pre> <blockquote> -<p>Note: <code>prefix</code>/<code>suffix</code>/<code>pattern</code> are expanded as <a href="bash.html#pathname">pathnames</a>.</p> +<p>Note: <code>prefix</code>/<code>suffix</code>/<code>pattern</code> are expanded as <a href="misc/bash.html#pathname">pathnames</a>.</p> </blockquote> <h3><a class="header" href="#pathname" id="pathname">Pathname</a></h3> <pre><code class="language-bash">* match any string @@ -451,7 +336,7 @@ command 2>&1 >file <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><a class="header" href="#completion" id="completion">Completion</a></h2> +<h2><a class="header" href="#completion-1" id="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> @@ -486,7 +371,7 @@ compgen -W "foo foobar bar" "f" # compare "hom" against file/dir names and generate matches compgen -d -f "hom" </code></pre> -<h3><a class="header" href="#example" id="example">Example</a></h3> +<h3><a class="header" href="#example-1" id="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-bash">foo -c green|red|blue -s low|high -f <file> -h @@ -507,106 +392,17 @@ compgen -d -f "hom" complete -F _foo foo </code></pre> -<h1><a class="header" href="#zsh1" id="zsh1">zsh(1)</a></h1> -<h2><a class="header" href="#keybindings" id="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> -<h2><a class="header" href="#completion-1" id="completion-1">Completion</a></h2> -<h3><a class="header" href="#installation" id="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><a class="header" href="#completion-variables" id="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><a class="header" href="#completion-functions" id="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><a class="header" href="#completion-with-a-hrefhttpzshsourceforgenetdocreleasecompletion-systemhtmlcompletion-functions_describea" id="completion-with-a-hrefhttpzshsourceforgenetdocreleasecompletion-systemhtmlcompletion-functions_describea">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><a class="header" href="#completion-with-a-hrefhttpzshsourceforgenetdocreleasecompletion-systemhtmlcompletion-functions_argumentsa" id="completion-with-a-hrefhttpzshsourceforgenetdocreleasecompletion-systemhtmlcompletion-functions_argumentsa">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></li> -<li><code>N:MSG:ACTION</code></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><a class="header" href="#example-1" id="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 +<h1><a class="header" href="#fish1" id="fish1">fish(1)</a></h1> +<h2><a class="header" href="#keymaps" id="keymaps">keymaps</a></h2> +<pre><code class="language-markdown"> 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 </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]:color:(low high)" \ - "-f[select file]:file:_files" \ - "-d[select dir]:fir:_files -/" \ - "-h[help]" - - case $state in - s_color) _foo_color;; - esac -} +<h2><a class="header" href="#debug" id="debug">debug</a></h2> +<pre><code class="language-markdown"> 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> <h1><a class="header" href="#tmux1" id="tmux1">tmux(1)</a></h1> <p>Terminology:</p> @@ -705,6 +501,111 @@ y yank selected text <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> +<h1><a class="header" href="#git1" id="git1">git(1)</a></h1> +<h2><a class="header" href="#staging" id="staging">staging</a></h2> +<pre><code class="language-markdown"> git add -p [<file>] ............ partial staging (interactive) +</code></pre> +<h2><a class="header" href="#remote" id="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><a class="header" href="#branching" id="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 branch with name <bname> + git checkout <bname> ........... switch to branch with name <bname> + git push -u origin <rbname> .... push branch to origin (or other remote), and + setup <rbname> as tracking branch +</code></pre> +<h2><a class="header" href="#resetting" id="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><a class="header" href="#tags" id="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 +</code></pre> +<h2><a class="header" href="#diff" id="diff">Diff</a></h2> +<pre><code class="language-markdown"> git diff HEAD:<fname> origin/HEAD:<fname> ... diff files for different refs + git diff -U$(wc -l <fname>) <fname> ......... shows complete file with diffs + instead of usual diff snippets +</code></pre> +<h2><a class="header" href="#log" id="log">Log</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 +</code></pre> +<h2><a class="header" href="#file-history" id="file-history">File history</a></h2> +<pre><code class="language-markdown"> 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><a class="header" href="#patching" id="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 <PATCH> .......... apply the patch locally without creating a commit + + # 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><a class="header" href="#submodules" id="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><a class="header" href="#inspection" id="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><a class="header" href="#revision-specifier" id="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> <h1><a class="header" href="#awk1" id="awk1">awk(1)</a></h1> <pre><code class="language-markdown">awk [opt] program [input] -F <sepstr> field separator string (can be regex) @@ -820,6 +721,70 @@ done | sort -k2 -n </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> +<h1><a class="header" href="#emacs1" id="emacs1">emacs(1)</a></h1> +<h2><a class="header" href="#help" id="help">help</a></h2> +<pre><code class="language-markdown"> C-h ? list available help modes + C-h f describe function + C-h v describe variable + 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><a class="header" href="#package-manager" id="package-manager">package manager</a></h2> +<pre><code class="language-markdown"> package-refresh-contents refresh package list + package-list-packages list available/installed packages +</code></pre> +<h2><a class="header" href="#window" id="window">window</a></h2> +<pre><code class="language-markdown"> C-x 0 kill focused window + C-x 1 kill all other windows + C-x 2 split horizontal + C-x 3 split vertical +</code></pre> +<h2><a class="header" href="#yankpaste" id="yankpaste">yank/paste</a></h2> +<pre><code class="language-markdown"> C-<SPACE> set start mark to select text + M-w copy selected text + C-w kill selected text + C-y paste selected text + M-y cycle through kill-ring +</code></pre> +<h2><a class="header" href="#blockrect" id="blockrect">block/rect</a></h2> +<pre><code class="language-markdown"> C-x <SPC> activate rectangle-mark-mode + M-x string-rectangle <RET> insert text in marked rect +</code></pre> +<h2><a class="header" href="#mass-edit" id="mass-edit">mass edit</a></h2> +<pre><code class="language-makrdown"> C-x h mark whole buffer (mark-whole-buffer) + M-x delete-matching-line <RET> delete lines matching regex + M-x % search & replace region (query-replace) + C-M-x % search & replace regex (query-replace-regexp) +</code></pre> +<h2><a class="header" href="#grep" id="grep">grep</a></h2> +<pre><code class="language-markdown"> M-x find-grep <RET> run find-grep result in *grep* buffer + n/p navigate next/previous match in *grep* buffer +</code></pre> +<h2><a class="header" href="#lisp-mode" id="lisp-mode">lisp mode</a></h2> +<pre><code class="language-markdown"> M-x lisp-interaction-mode activate lisp mode + C-M-x evaluate top expr under cursor + C-x C-e eval-last-sexp + C-u C-x C-e eval-last-sexp and prints result in current buffer +</code></pre> +<h2><a class="header" href="#narrow" id="narrow">narrow</a></h2> +<pre><code class="language-markdown"> C-x n n show only focused region (narrow) + C-x n w show whole buffer (wide) +</code></pre> +<h2><a class="header" href="#org" id="org">org</a></h2> +<pre><code class="language-markdown"> 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><a class="header" href="#org-source" id="org-source">org source</a></h3> +<pre><code class="language-markdown"> <s TAB generate a source block + C-c ' edit source block (in lang specific buffer) + C-c C-c eval source block +</code></pre> <h1><a class="header" href="#gdb1" id="gdb1">gdb(1)</a></h1> <h1><a class="header" href="#cli" id="cli">CLI</a></h1> <pre><code class="language-markdown"> gdb [opts] [prg [-c coredump | -p pid]] @@ -970,7 +935,7 @@ executed. To workaround that bug one can create a wrapper function which calls fs <fs> # select flag-space <fs> f # print flags of selected flag-space </code></pre> -<h2><a class="header" href="#help" id="help">help</a></h2> +<h2><a class="header" href="#help-1" id="help-1">help</a></h2> <pre><code class="language-markdown"> ?*~<kw> # '?*' list all commands and '~' grep for <kw> ?*~... # '..' less mode /'...' interactive search </code></pre> @@ -978,105 +943,7 @@ executed. To workaround that bug one can create a wrapper function which calls <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><a class="header" href="#emacs1" id="emacs1">emacs(1)</a></h1> -<h2><a class="header" href="#help-1" id="help-1">help</a></h2> -<pre><code class="language-markdown"> C-h ? list available help modes - C-h f describe function - C-h v describe variable - 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><a class="header" href="#package-manager" id="package-manager">package manager</a></h2> -<pre><code class="language-markdown"> package-refresh-contents refresh package list - package-list-packages list available/installed packages -</code></pre> -<h2><a class="header" href="#window" id="window">window</a></h2> -<pre><code class="language-markdown"> C-x 0 kill focused window - C-x 1 kill all other windows - C-x 2 split horizontal - C-x 3 split vertical -</code></pre> -<h2><a class="header" href="#yankpaste" id="yankpaste">yank/paste</a></h2> -<pre><code class="language-markdown"> C-<SPACE> set start mark to select text - M-w copy selected text - C-w kill selected text - C-y paste selected text - M-y cycle through kill-ring -</code></pre> -<h2><a class="header" href="#blockrect" id="blockrect">block/rect</a></h2> -<pre><code class="language-markdown"> C-x <SPC> activate rectangle-mark-mode - M-x string-rectangle <RET> insert text in marked rect -</code></pre> -<h2><a class="header" href="#mass-edit" id="mass-edit">mass edit</a></h2> -<pre><code class="language-makrdown"> C-x h mark whole buffer (mark-whole-buffer) - M-x delete-matching-line <RET> delete lines matching regex - M-x % search & replace region (query-replace) - C-M-x % search & replace regex (query-replace-regexp) -</code></pre> -<h2><a class="header" href="#grep" id="grep">grep</a></h2> -<pre><code class="language-markdown"> M-x find-grep <RET> run find-grep result in *grep* buffer - n/p navigate next/previous match in *grep* buffer -</code></pre> -<h2><a class="header" href="#lisp-mode" id="lisp-mode">lisp mode</a></h2> -<pre><code class="language-markdown"> M-x lisp-interaction-mode activate lisp mode - C-M-x evaluate top expr under cursor - C-x C-e eval-last-sexp - C-u C-x C-e eval-last-sexp and prints result in current buffer -</code></pre> -<h2><a class="header" href="#narrow" id="narrow">narrow</a></h2> -<pre><code class="language-markdown"> C-x n n show only focused region (narrow) - C-x n w show whole buffer (wide) -</code></pre> -<h2><a class="header" href="#org" id="org">org</a></h2> -<pre><code class="language-markdown"> 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><a class="header" href="#org-source" id="org-source">org source</a></h3> -<pre><code class="language-markdown"> <s TAB generate a source block - C-c ' edit source block (in lang specific buffer) - C-c C-c eval source block -</code></pre> -<h1><a class="header" href="#fish1" id="fish1">fish(1)</a></h1> -<h2><a class="header" href="#keymaps" id="keymaps">keymaps</a></h2> -<pre><code class="language-markdown"> 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 -</code></pre> -<h2><a class="header" href="#debug" id="debug">debug</a></h2> -<pre><code class="language-markdown"> 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> -<h1><a class="header" href="#strace1" id="strace1">strace(1)</a></h1> -<pre><code class="language-markdown">strace [opts] [prg] - -f .......... follow child processes on fork(2) - -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 -</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><a class="header" href="#examples-2" id="examples-2">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 -f -e signal -p <pid> -</code></pre> +<h1><a class="header" href="#resource-analysis--monitor" id="resource-analysis--monitor">Resource analysis & monitor</a></h1> <h1><a class="header" href="#lsof8" id="lsof8">lsof(8)</a></h1> <pre><code class="language-markdown">lsof -a ......... AND slection filters instead ORing (OR: default) @@ -1092,7 +959,7 @@ executed. To workaround that bug one can create a wrapper function which calls AP ......... append TR ......... truncate </code></pre> -<h1><a class="header" href="#examples-3" id="examples-3">Examples</a></h1> +<h1><a class="header" href="#examples-2" id="examples-2">Examples</a></h1> <h2><a class="header" href="#file-flags" id="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> @@ -1130,10 +997,6 @@ major_pagefault: Happens when the page needed is NOT in memory, the kernel <h1><a class="header" href="#io-statistics" id="io-statistics">I/O statistics</a></h1> <pre><code class="language-markdown">pidstat -d -p <pid> [interval] [count] </code></pre> -<h1><a class="header" href="#usrbintime1" id="usrbintime1">/usr/bin/time(1)</a></h1> -<pre><code class="language-markdown"># statistics of process run -/usr/bin/time -v <cmd> -</code></pre> <h1><a class="header" href="#pgrep1" id="pgrep1">pgrep(1)</a></h1> <pre><code class="language-markdown">pgrep [opts] <pattern> -n only list newest matching process @@ -1145,10 +1008,51 @@ major_pagefault: Happens when the page needed is NOT in memory, the kernel <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> +<h1><a class="header" href="#pmap1" id="pmap1">pmap(1)</a></h1> +<pre><code class="language-markdown">pmap <pid> + Dump virtual memory map of process. + Compared to /proc/<pid>/maps it shows the size of the mappings. +</code></pre> <h1><a class="header" href="#pstack1" id="pstack1">pstack(1)</a></h1> <pre><code class="language-markdown">pstack <pid> Dump stack for all threads of process. </code></pre> +<h1><a class="header" href="#trace-and-profile" id="trace-and-profile">Trace and Profile</a></h1> +<h1><a class="header" href="#strace1" id="strace1">strace(1)</a></h1> +<pre><code class="language-markdown">strace [opts] [prg] + -f .......... follow child processes on fork(2) + -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 +</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><a class="header" href="#examples-3" id="examples-3">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 -f -e signal -p <pid> +</code></pre> +<h1><a class="header" href="#ltrace1" id="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><a class="header" href="#example-2" id="example-2">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> <h1><a class="header" href="#perf1" id="perf1">perf(1)</a></h1> <pre><code class="language-markdown">perf list show supported hw/sw events @@ -1207,6 +1111,11 @@ opreport [opt] FILE ophelp show supported hw/sw events </code></pre> +<h1><a class="header" href="#usrbintime1" id="usrbintime1">/usr/bin/time(1)</a></h1> +<pre><code class="language-markdown"># statistics of process run +/usr/bin/time -v <cmd> +</code></pre> +<h1><a class="header" href="#binary" id="binary">Binary</a></h1> <h1><a class="header" href="#od1" id="od1">od(1)</a></h1> <pre><code class="language-markdown"> od [opts] <file> -An don't print addr info @@ -1304,6 +1213,7 @@ the <code>.rodata</code> section as follows:</p> -C demangle -u undefined only </code></pre> +<h1><a class="header" href="#development" id="development">Development</a></h1> <h1><a class="header" href="#cfilt1" id="cfilt1">c++filt(1)</a></h1> <h2><a class="header" href="#demangle-symbol" id="demangle-symbol">Demangle symbol</a></h2> <pre><code class="language-markdown"> c++-filt <symbol_str> @@ -1400,6 +1310,117 @@ run1: xor eax, eax jmp bar </code></pre> +<h1><a class="header" href="#ldso8" id="ldso8">ld.so(8)</a></h1> +<h2><a class="header" href="#environment-variables" id="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> +<h2><a class="header" href="#ld_preload-initialization-order-and-link-map" id="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><a class="header" href="#dynamic-linking-x86_64" id="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> </main> |