... in conjunction with '-i' filter for protocol in state -U ......... show unix domain sockets ('@' indicates abstract sock name, see unix(7)) file flags: R/W/RW ..... read/write/read-write CR ......... create AP ......... append TR ......... truncate -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","breadcrumbs":"Resource analysis & monitor » lsof(8)","id":"166","title":"lsof(8)"},"167":{"body":"","breadcrumbs":"Resource analysis & monitor » Examples","id":"167","title":"Examples"},"168":{"body":"Show open files with file flags for process: lsof +fg -p ","breadcrumbs":"Resource analysis & monitor » File flags","id":"168","title":"File flags"},"169":{"body":"Show open tcp connections for $USER: lsof -a -u $USER -i TCP Note : -a ands the results. If -a is not given all open files matching $USER and all tcp connections are listed ( ored ).","breadcrumbs":"Resource analysis & monitor » Open TCP connections","id":"169","title":"Open TCP connections"},"17":{"body":"# generate sequence from n to m\n{n..m}\n# generate sequence from n to m step by s\n{n..m..s} # expand cartesian product\n{a,b}{c,d}","breadcrumbs":"Tools » Generator","id":"17","title":"Generator"},"170":{"body":"Show open connections to localhost for $USER: lsof -a -u $USER -i @localhost","breadcrumbs":"Resource analysis & monitor » Open connection to specific host","id":"170","title":"Open connection to specific host"},"171":{"body":"Show open connections to port :1234 for $USER: lsof -a -u $USER -i :1234","breadcrumbs":"Resource analysis & monitor » Open connection to specific port","id":"171","title":"Open connection to specific port"},"172":{"body":"lsof -i 4TCP -s TCP:ESTABLISHED","breadcrumbs":"Resource analysis & monitor » IPv4 TCP connections in ESTABLISHED state","id":"172","title":"IPv4 TCP connections in ESTABLISHED state"},"173":{"body":"ss [option] [filter] [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 [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","breadcrumbs":"Resource analysis & monitor » ss(8)","id":"173","title":"ss(8)"},"174":{"body":"Show all tcp IPv4 sockets connecting to port 443: ss -4 'dport 443' Show all tcp IPv4 sockets that don't connect to port 443 or connect to address 1.2.3.4. ss -4 'dport != 443 or dst 1.2.3.4'","breadcrumbs":"Resource analysis & monitor » Examples","id":"174","title":"Examples"},"175":{"body":"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","breadcrumbs":"Resource analysis & monitor » pidstat(1)","id":"175","title":"pidstat(1)"},"176":{"body":"pidstat -r -p [interval] [count] 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).","breadcrumbs":"Resource analysis & monitor » Page fault and memory utilization","id":"176","title":"Page fault and memory utilization"},"177":{"body":"pidstat -d -p [interval] [count]","breadcrumbs":"Resource analysis & monitor » I/O statistics","id":"177","title":"I/O statistics"},"178":{"body":"pgrep [opts] -n only list newest matching process -u only show matching for user -l additionally list command -a additionally list command + arguments","breadcrumbs":"Resource analysis & monitor » pgrep(1)","id":"178","title":"pgrep(1)"},"179":{"body":"For example attach gdb to newest zsh process from $USER. gdb -p $(pgrep -n -u $USER zsh)","breadcrumbs":"Resource analysis & monitor » Debug newest process","id":"179","title":"Debug newest process"},"18":{"body":"# default value\nbar=${foo:-some_val} # if $foo set, then bar=$foo else bar=some_val # alternate value\nbar=${foo:+bla $foo} # if $foo set, then bar=\"bla $foo\" else bar=\"\" # check param set\nbar=${foo:?msg} # if $foo set, then bar=$foo else exit and print msg # indirect\nFOO=foo\nBAR=FOO\nbar=${!BAR} # deref value of BAR -> bar=$FOO # prefix\n${foo#prefix} # remove prefix when expanding $foo\n# suffix\n${foo%suffix} # remove suffix when expanding $foo # substitute\n${foo/pattern/string} # replace pattern with string when expanding foo\n# pattern starts with\n# '/' replace all occurences of pattern\n# '#' pattern match at beginning\n# '%' pattern match at end Note: prefix/suffix/pattern are expanded as pathnames .","breadcrumbs":"Tools » Parameter","id":"18","title":"Parameter"},"180":{"body":"pmap Dump virtual memory map of process. Compared to /proc//maps it shows the size of the mappings.","breadcrumbs":"Resource analysis & monitor » pmap(1)","id":"180","title":"pmap(1)"},"181":{"body":"pstack Dump stack for all threads of process.","breadcrumbs":"Resource analysis & monitor » pstack(1)","id":"181","title":"pstack(1)"},"182":{"body":"strace ltrace perf OProfile time","breadcrumbs":"Trace and Profile","id":"182","title":"Trace and Profile"},"183":{"body":"strace [opts] [prg] -f .......... follow child processes on fork(2) -p .... attach to running process -s ... max string size, truncate of longer (default: 32) -e ... expression for trace filtering -o ... log output into -c .......... dump syscall statitics at the end -k .......... dump stack trace for each syscall -P ... only trace syscall accesing path -y .......... print paths for FDs -tt ......... print absolute timestamp (with us precision) -r .......... print relative timestamp : 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","breadcrumbs":"Trace and Profile » strace(1)","id":"183","title":"strace(1)"},"184":{"body":"Trace open(2) & socket(2) syscalls for a running process + child processes: strace -f -e trace=open,socket -p Trace signals delivered to a running process: strace -e signal -e 'trace=!all' -p ","breadcrumbs":"Trace and Profile » Examples","id":"184","title":"Examples"},"185":{"body":"ltrace [opts] [prg] -f .......... follow child processes on fork(2) -p .... attach to running process -o ... log output into -l . show who calls into lib matched by -C .......... demangle","breadcrumbs":"Trace and Profile » ltrace(1)","id":"185","title":"ltrace(1)"},"186":{"body":"List which program/libs call into libstdc++: ltrace -l '*libstdc++*' -C -o ltrace.log ./main","breadcrumbs":"Trace and Profile » Example","id":"186","title":"Example"},"187":{"body":"perf list show supported hw/sw events perf stat -p .. show stats for running process -I ... show stats periodically over interval -e ... filter for events perf top -p .. show stats for running process -F ... sampling frequency -K ........ hide kernel threads perf record -p ............... record stats for running process -F ................ sampling frequency --call-graph .. [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 ................ filter for events perf report -n .................... annotate symbols with nr of samples --stdio ............... report to stdio, if not presen tui mode -g graph,0.5,caller ... show caller based call chains with value >0.5 Useful : page-faults minor-faults major-faults cpu-cycles` task-clock","breadcrumbs":"Trace and Profile » perf(1)","id":"187","title":"perf(1)"},"188":{"body":"","breadcrumbs":"Trace and Profile » Flamegraph","id":"188","title":"Flamegraph"},"189":{"body":"perf record -g -e cpu-cycles -p \nperf script | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > cycles-flamegraph.svg","breadcrumbs":"Trace and Profile » Flamegraph with single event trace","id":"189","title":"Flamegraph with single event trace"},"19":{"body":"* match any string\n? match any single char\n\\\\ match backslash\n[abc] match any char of 'a' 'b' 'c'\n[a-z] match any char between 'a' - 'z'\n[^ab] negate, match all not 'a' 'b'\n[:class:] match any char in class, available: alnum,alpha,ascii,blank,cntrl,digit,graph,lower, print,punct,space,upper,word,xdigit Wit extglob shell option enabled it is possible to have more powerful patterns. In the following pattern-list is one ore more patterns separated by | char. ?(pattern-list) matches zero or one occurrence of the given patterns\n*(pattern-list) matches zero or more occurrences of the given patterns\n+(pattern-list) matches one or more occurrences of the given patterns\n@(pattern-list) matches one of the given patterns\n!(pattern-list) matches anything except one of the given patterns Note: shopt -s extglob/shopt -u extglob to enable/disable extglob option.","breadcrumbs":"Tools » Pathname","id":"19","title":"Pathname"},"190":{"body":"perf record -g -e cpu-cycles,page-faults -p \nperf script --per-event-dump\n# fold & generate as above","breadcrumbs":"Trace and Profile » Flamegraph with multiple event traces","id":"190","title":"Flamegraph with multiple event traces"},"191":{"body":"operf -g -p -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","breadcrumbs":"Trace and Profile » OProfile","id":"191","title":"OProfile"},"192":{"body":"# statistics of process run\n/usr/bin/time -v ","breadcrumbs":"Trace and Profile » /usr/bin/time(1)","id":"192","title":"/usr/bin/time(1)"},"193":{"body":"od xxd readelf objdump nm","breadcrumbs":"Binary","id":"193","title":"Binary"},"194":{"body":"od [opts] -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 skip bytes from (hex if start with 0x) -N dump bytes (hex of start with 0x)","breadcrumbs":"Binary » od(1)","id":"194","title":"od(1)"},"195":{"body":"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","breadcrumbs":"Binary » ASCII to hex string","id":"195","title":"ASCII to hex string"},"196":{"body":"For example .rodata section from an elf file. We can use readelf to get the offset into the file where the .rodata section starts. 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 With the offset of -j 0x0009c0 we can dump -N 0x30 bytes from the beginning of the .rodata section as follows: od -j 0x0009c0 -N 0x30 -tx4 -w4 foo >> 0004700 00020001 >> 0004704 00000000 >> * >> 0004740 00000001 >> 0004744 00000002 >> 0004750 00000003 >> 0004754 00000004 Note : Numbers starting with 0x will be interpreted as hex by od.","breadcrumbs":"Binary » Extract parts of file","id":"196","title":"Extract parts of file"},"197":{"body":"xxd [opts] -p dump continuous hexdump -r convert hexdump into binary ('revert') -e dump as little endian mode -i output as C array","breadcrumbs":"Binary » xxd(1)","id":"197","title":"xxd(1)"},"198":{"body":"echo -n 'aabb' | xxd -p >> 61616262","breadcrumbs":"Binary » ASCII to hex stream","id":"198","title":"ASCII to hex stream"},"199":{"body":"echo -n '61616262' | xxd -p -r >> aabb","breadcrumbs":"Binary » Hex to binary stream","id":"199","title":"Hex to binary stream"},"2":{"body":"","breadcrumbs":"Tools » zsh(1)","id":"2","title":"zsh(1)"},"20":{"body":"Note: The trick with bash I/O redirection is to interpret from left-to-right. # stdout & stderr to file\ncommand >file 2>&1\n# equivalent\ncommand &>file # stderr to stdout & stdout to file\ncommand 2>&1 >file","breadcrumbs":"Tools » I/O redirection","id":"20","title":"I/O redirection"},"200":{"body":"echo -n '\\x7fELF' | xxd -p | xxd -p -r | file -p - >> ELF","breadcrumbs":"Binary » ASCII to binary","id":"200","title":"ASCII to binary"},"201":{"body":"xxd -i <(echo -n '\\x7fELF') >> unsigned char _proc_self_fd_11[] = { >> 0x7f, 0x45, 0x4c, 0x46 >> }; >> unsigned int _proc_self_fd_11_len = 4;","breadcrumbs":"Binary » ASCII to C array (hex encoded)","id":"201","title":"ASCII to C array (hex encoded)"},"202":{"body":"readelf [opts] -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.*)","breadcrumbs":"Binary » readelf(1)","id":"202","title":"readelf(1)"},"203":{"body":"objdump [opts] -M intel use intil syntax -d disassemble text section -D disassemble all sections -S mix disassembly with source code -C demangle -j display info for section --[no-]show-raw-insn [dont] show object code next to disassembly","breadcrumbs":"Binary » objdump(1)","id":"203","title":"objdump(1)"},"204":{"body":"For example .plt section: objdump -j .plt -d ","breadcrumbs":"Binary » Disassemble section","id":"204","title":"Disassemble section"},"205":{"body":"nm [opts] -C demangle -u undefined only","breadcrumbs":"Binary » nm(1)","id":"205","title":"nm(1)"},"206":{"body":"c++filt c++ glibc gcc [make] (./make.md) ld.so symbol versioning python","breadcrumbs":"Development","id":"206","title":"Development"},"207":{"body":"","breadcrumbs":"Development » c++filt(1)","id":"207","title":"c++filt(1)"},"208":{"body":"c++-filt ","breadcrumbs":"Development » Demangle symbol","id":"208","title":"Demangle symbol"},"209":{"body":"For example dynamic symbol table: readelf -W --dyn-syms | c++filt","breadcrumbs":"Development » Demangle stream","id":"209","title":"Demangle stream"},"21":{"body":"j>&i Duplicate fd i to fd j, making j a copy of i. See dup2(2) . Example: command 2>&1 >file duplicate fd 1 to fd 2, effectively redirecting stderr to stdout redirect stdout to file","breadcrumbs":"Tools » Explanation","id":"21","title":"Explanation"},"210":{"body":"","breadcrumbs":"Development » c++","id":"210","title":"c++"},"211":{"body":"Force compile error to see what auto is deduced to. auto foo = bar(); // force compile error\ntypename decltype(foo)::_;","breadcrumbs":"Development » Type deduction","id":"211","title":"Type deduction"},"212":{"body":"","breadcrumbs":"Development » glibc","id":"212","title":"glibc"},"213":{"body":"Trace memory allocation and de-allocation to detect memory leaks. Need to call mtrace(3) to install the tracing hooks. If we can't modify the binary to call mtrace we can create a small shared library and pre-load it. // libmtrace.c\n#include \n__attribute__((constructor)) static void init_mtrace() { mtrace(); } Compile as: gcc -shared -fPIC -o libmtrace.so libmtrace.c To generate the trace file run: export MALLOC_TRACE=\nLD_PRELOAD=./libmtrace.so Note : If MALLOC_TRACE is not set mtrace won't install tracing hooks. To get the results of the trace file: mtrace $MALLOC_TRACE","breadcrumbs":"Development » malloc tracer mtrace(3)","id":"213","title":"malloc tracer mtrace(3)"},"214":{"body":"Configure action when glibc detects memory error. export MALLOC_CHECK_= Useful values: 1 print detailed error & continue\n3 print detailed error + stack trace + memory mappings & abort\n7 print simple error message + stack trace + memory mappings & abort","breadcrumbs":"Development » malloc check mallopt(3)","id":"214","title":"malloc check mallopt(3)"},"215":{"body":"","breadcrumbs":"Development » gcc(1)","id":"215","title":"gcc(1)"},"216":{"body":"","breadcrumbs":"Development » CLI","id":"216","title":"CLI"},"217":{"body":"While debugging can be helpful to just pre-process files. gcc -E [-dM] ... -E run only preprocessor -dM list only #define statements -### dry-run, outputting exact compiler/linker invocations -print-multi-lib print available multilib configurations","breadcrumbs":"Development » Preprocessing","id":"217","title":"Preprocessing"},"218":{"body":"# List all target options with their description.\ngcc --help=target # Configure for current cpu arch and query (-Q) value of options.\ngcc -march=native -Q --help=target","breadcrumbs":"Development » Target options","id":"218","title":"Target options"},"219":{"body":"","breadcrumbs":"Development » Builtins","id":"219","title":"Builtins"},"22":{"body":"The getopts builtin uses following global variables: OPTARG, value of last option argument OPTIND, index of the next argument to process (user must reset) OPTERR, display errors if set to 1 getopts [] specifies the names of supported options, eg f:c f: means -f option with an argument c means -c option without an argument specifies a variable name which getopts fills with the last parsed option argument optionally specify argument string to parse, by default getopts parses $@","breadcrumbs":"Tools » Argument parsing with getopts","id":"22","title":"Argument parsing with getopts"},"220":{"body":"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 compiler explorer . echo \"\nextern void foo();\nextern void bar();\nvoid run0(int x) { if (__builtin_expect(x,0)) { foo(); } else { bar(); }\n}\nvoid run1(int x) { if (__builtin_expect(x,1)) { foo(); } else { bar(); }\n}\n\" | gcc -O2 -S -masm=intel -o /dev/stdout -xc - Will generate something similar to the following. run0: bar is on the path without branch run1: foo is on the path without branch run0: test edi, edi jne .L4 xor eax, eax jmp bar\n.L4: xor eax, eax jmp foo\nrun1: test edi, edi je .L6 xor eax, eax jmp foo\n.L6: xor eax, eax jmp bar","breadcrumbs":"Development » __builtin_expect(expr, cond)","id":"220","title":"__builtin_expect(expr, cond)"},"221":{"body":"C ABI - SystemV ABI C++ ABI - C++ Itanium ABI","breadcrumbs":"Development » ABI (Linux)","id":"221","title":"ABI (Linux)"},"222":{"body":"","breadcrumbs":"Development » make(1)","id":"222","title":"make(1)"},"223":{"body":"target .. : prerequisite .. recipe .. target: an output generated by the rule prerequisite: an input that is used to generate the target recipe: list of actions to generate the output from the input Use make -p to print all rules and variables (implicitly + explicitly defined).","breadcrumbs":"Development » Anatomy of make rules","id":"223","title":"Anatomy of make rules"},"224":{"body":"","breadcrumbs":"Development » Pattern rules & Automatic variables","id":"224","title":"Pattern rules & Automatic variables"},"225":{"body":"A pattern rule contains the % char (exactly one of them) and look like this example: %.o : %.c $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ The target matches files of the pattern %.o, where % matches any none-empty substring and other character match just them self. The substring matched by % is called the stem. % in the prerequisite stands for the matched stem in the target.","breadcrumbs":"Development » Pattern rules","id":"225","title":"Pattern rules"},"226":{"body":"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: $@: Name of the target that triggered the rule. $<: Name of the first prerequisite. $^: Names of all prerequisites (without duplicates). $+: Names of all prerequisites (with duplicates). $*: Stem of the pattern rule. # file: Makefile all: foobar blabla foo% bla%: aaa bbb bbb @echo \"@ = $@\" @echo \"< = $<\" @echo \"^ = $^\" @echo \"+ = $+\" @echo \"* = $*\" @echo \"----\" aaa:\nbbb: Running above Makefile gives: @ = foobar\n< = aaa\n^ = aaa bbb\n+ = aaa bbb bbb\n* = bar\n----\n@ = blabla\n< = aaa\n^ = aaa bbb\n+ = aaa bbb bbb\n* = bla\n---- Variables related to filesystem paths: $(CURDIR): Path of current working dir after using make -C path","breadcrumbs":"Development » Automatic variables","id":"226","title":"Automatic variables"},"227":{"body":"","breadcrumbs":"Development » Useful functions","id":"227","title":"Useful functions"},"228":{"body":"Substitute strings matching pattern in a list. in := a.o l.a c.o\nout := $(in:.o=.c)\n# => out = a.c l.a c.c","breadcrumbs":"Development » Substitution references","id":"228","title":"Substitution references"},"229":{"body":"Keep strings matching a pattern in a list. in := a.a b.b c.c d.d\nout := $(filter %.b %.c, $(in))\n# => out = b.b c.c","breadcrumbs":"Development » filter","id":"229","title":"filter"},"23":{"body":"#!/bin/bash\nfunction 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\n} parse_args -f xxx -c\nparse_args -f yyy","breadcrumbs":"Tools » Example","id":"23","title":"Example"},"230":{"body":"Remove strings matching a pattern from a list. in := a.a b.b c.c d.d\nout := $(filter-out %.b %.c, $(in))\n# => out = a.a d.d","breadcrumbs":"Development » filter-out","id":"230","title":"filter-out"},"231":{"body":"Resolve each file name as absolute path (don't resolve symlinks). $(abspath fname1 fname2 ..) ### `realpath`\nResolve each file name as canonical path.\n```make\n$(realpath fname1 fname2 ..)","breadcrumbs":"Development » abspath","id":"231","title":"abspath"},"232":{"body":"","breadcrumbs":"Development » ld.so(8)","id":"232","title":"ld.so(8)"},"233":{"body":"LD_PRELOAD= colon separated list of libso's to be pre loaded LD_DEBUG= 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","breadcrumbs":"Development » Environment Variables","id":"233","title":"Environment Variables"},"234":{"body":"Libraries specified in LD_PRELOAD are loaded from left-to-right but initialized from right-to-left. > 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 The preload order determines: the order libraries are inserted into the link map the initialization order for libraries For the example listed above the resulting link map will look like the following: +------+ +------+ +------+ +------+ | main | -> | liba | -> | libb | -> | libc | +------+ +------+ +------+ +------+ This can be seen when running with LD_DEBUG=files: > 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: /libb.so >> calling init: /liba.so >> initialize program: ./main To verify the link map order we let ld.so resolve the memcpy(3) libc symbol (used in main ) dynamically, while enabling LD_DEBUG=symbols,bindings to see the resolving in action. > LD_DEBUG=symbols,bindings LD_PRELOAD=liba.so:libb.so ./main >> symbol=memcpy; lookup in file=./main [0] >> symbol=memcpy; lookup in file=/liba.so [0] >> symbol=memcpy; lookup in file=/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]","breadcrumbs":"Development » LD_PRELOAD: Initialization Order and Link Map","id":"234","title":"LD_PRELOAD: Initialization Order and Link Map"},"235":{"body":"Dynamic linking basically works via one indirect jump. It uses a combination of function trampolines (.plt section) and a function pointer table (.got.plt section). On the first call the trampoline sets up some metadata and then jumps to the ld.so runtime resolve function, which in turn patches the table with the correct function pointer. .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 Using radare2 we can analyze this in more detail: [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 At address 0x00401030 in the .plt section we see the indirect jump for puts using the function pointer in _GLOBAL_OFFSET_TABLE_[3] (GOT). GOT[3] initially points to instruction after the puts trampoline 0x00401036. This pushes the relocation index 0 and then jumps to the first trampoline 0x00401020. The first trampoline jumps to GOT[2] which will be filled at program startup by the ld.so with its resolve function. The ld.so resolve function fixes the relocation referenced by the relocation index pushed by the puts trampoline. The relocation entry at index 0 tells the resolve function which symbol to search for and where to put the function pointer: > readelf -r >> 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 As we can see the offset from relocation at index 0 points to GOT[3].","breadcrumbs":"Development » Dynamic Linking (x86_64)","id":"235","title":"Dynamic Linking (x86_64)"},"236":{"body":"The ELF symbol versioning 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). The libpthread.so library is an example which provides the pthread_cond_wait symbol multiple times but in different versions. With readelf the version of the symbol can be seen after the @. > 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 The @@ denotes the default symbol version which will be used during static linking against the library. The following dump shows that the tmp program linked against lpthread will depend on the symbol version GLIBC_2.3.2, which is the default version. > echo \"#include 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) Only one symbol can be annotated as the @@ default version. Using the --version-info flag with readelf, more details on the symbol version info compiled into the tmp ELF file can be obtained. The .gnu.version section contains the version definition for each symbol in the .dynsym section. pthread_cond_wait is at index 2 in the .dynsym section, the corresponding symbol version is at index 2 in the .gnu.version section. The .gnu.version_r section contains symbol version requirements per shared library dependency (DT_NEEDED dynamic entry). > 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 The gnu dynamic linker allows to inspect the version processing during runtime by setting the LD_DEBUG environment variable accordingly. # version: Display version dependencies.\n> 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.\n# bindings: Display information about symbol binding.\n> 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]","breadcrumbs":"Development » ELF Symbol Versioning","id":"236","title":"ELF Symbol Versioning"},"237":{"body":"The following shows an example C++ library libfoo which provides the same symbol multiple times but in different versions. // file: libfoo.cc\n#include // Bind function symbols to version nodes.\n//\n// ..@ -> Is the unversioned symbol.\n// ..@@.. -> Is the default symbol. __asm__(\".symver func_v0,func@\");\n__asm__(\".symver func_v1,func@LIB_V1\");\n__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\"); }\n} __asm__(\".symver _Z11func_cpp_v1i,_Z8func_cppi@LIB_V1\");\n__asm__(\".symver _Z11func_cpp_v2i,_Z8func_cppi@@LIB_V2\"); void func_cpp_v1(int) { puts(\"func_cpp_v1\"); }\nvoid func_cpp_v2(int) { puts(\"func_cpp_v2\"); } void func_cpp(int) { puts(\"func_cpp_v2\"); } Version script for libfoo which defines which symbols for which versions are exported from the ELF file. # file: libfoo.ver\nLIB_V1 { global: func; extern \"C++\" { \"func_cpp(int)\"; }; local: *;\n}; LIB_V2 { global: func; extern \"C++\" { \"func_cpp(int)\"; };\n} LIB_V1; The local: section in LIB_V1 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. The library libfoo can be linked with the version definitions in libfoo.ver by passing the version script to the linker with the --version-script flag. > g++ -shared -fPIC -o libfoo.so libfoo.cc -Wl,--version-script=libfoo.ver\n> 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 The following program demonstrates how to make use of the different versions: // file: main.cc\n#include \n#include // Links against default symbol in the lib.so.\nextern \"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();\n#endif return 0;\n} Compiling and running results in: > g++ -o main main.cc -ldl ./libfoo.so && ./main\nfunc_v2\nfunc_v0\nfunc_v1\nfunc_v2","breadcrumbs":"Development » Example: version script","id":"237","title":"Example: version script"},"238":{"body":"ELF Symbol Versioning Binutils ld: Symbol Versioning LSB: Symbol Versioning How To Write Shared Libraries","breadcrumbs":"Development » References","id":"238","title":"References"},"239":{"body":"","breadcrumbs":"Development » python","id":"239","title":"python"},"24":{"body":"Bash supports regular expression matching with the binary operator =~. The match results can be accessed via the $BASH_REMATCH variable: ${BASH_REMATCH[0]} contains the full match ${BASH_REMATCH[1]} contains match of the first capture group INPUT='title foo : 1234'\nREGEX='^title (.+) : ([0-9]+)$'\nif [[ $INPUT =~ $REGEX ]]; then echo \"${BASH_REMATCH[0]}\" # title foo : 1234 echo \"${BASH_REMATCH[1]}\" # foo echo \"${BASH_REMATCH[2]}\" # 1234\nfi Caution : When specifying a regex in the [[ ]] block directly, quotes will be treated as part of the pattern. [[ $INPUT =~ \"foo\" ]] will match against \"foo\" not foo!","breadcrumbs":"Tools » Regular Expressions","id":"24","title":"Regular Expressions"},"240":{"body":"Some decorator examples with type annotation. 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\ndef 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\")\ndef some_fn2(x: int): print(f\"some_fn2 x={x}\")","breadcrumbs":"Development » Decorator [ run ]","id":"240","title":"Decorator [ run ]"},"241":{"body":"Walrus operator := added since python 3.8 . 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'])\nwhile tok := next(toks, None): print(f\"{tok}\") # Example 3: list comprehension print([tok for t in [\" a\", \" \", \" b \"] if (tok := t.strip())])","breadcrumbs":"Development » Walrus operator [ run ]","id":"241","title":"Walrus operator [ run ]"},"242":{"body":"Run unittests directly from the command line as python3 -m unittest -v test Optionally pass -k to only run subset of tests. # 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]","breadcrumbs":"Development » Unittest [ run ]","id":"242","title":"Unittest [ run ]"},"243":{"body":"Run doctests directly from the command line as python -m doctest -v test.py # 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","breadcrumbs":"Development » Doctest [ run ]","id":"243","title":"Doctest [ run ]"},"244":{"body":"Micro benchmarking. python -m timeit '[x.strip() for x in [\"a \", \" b\"]]'","breadcrumbs":"Development » timeit","id":"244","title":"timeit"},"245":{"body":"systemd coredump ptrace_scope","breadcrumbs":"Linux","id":"245","title":"Linux"},"246":{"body":"","breadcrumbs":"Linux » systemd","id":"246","title":"systemd"},"247":{"body":"Inspect units: systemctl [opts] [cmd]\n[opts] --user [cmd] list-units List units in memory status Show runtime status of unit start Start a unit stop Stop a unit restart Restart a unit reload