From f7f091a2e6d3167fed4e1063dea266cdfff3ab55 Mon Sep 17 00:00:00 2001 From: johannst Date: Thu, 19 Mar 2020 21:21:17 +0000 Subject: deploy: 6259d4ee6c06cc6ae4ce07484b75f7d327a6a52a --- print.html | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 115 insertions(+), 11 deletions(-) (limited to 'print.html') diff --git a/print.html b/print.html index e4da796..de3e0e1 100644 --- a/print.html +++ b/print.html @@ -83,7 +83,7 @@ @@ -309,7 +309,10 @@ As we can see the offset from relocation at index 0 points to +

File history

+
  git log -p <file> ......... show commit history + diffs for <file>
+  git log --oneline <file> .. show commit history for <file> in compact format
 

Patching

  git format-patch <opt> <since>/<revision range>
@@ -321,7 +324,7 @@ As we can see the offset from relocation at index 0 points to 0 points to 

Inspection

  git ls-tree [-r] <ref> .... show git tree for <ref>, -r to recursively ls sub-trees
@@ -733,10 +738,18 @@ executed. To workaround that bug one can create a wrapper function which calls
 

emacs(1)

help

-
  C-h f                 describe function
-  C-h b                 list buffer available keymaps
-  <kseq> C-h            list possible keymaps with <kseq>
-                        eg C-x C-h -> list keymaps beginning with C-x
+
  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
+
+

package manager

+
  package-refresh-contents    refresh package list
+  package-list-packages       list available/installed packages
 

window

  C-x 0         kill focused window
@@ -744,6 +757,13 @@ executed. To workaround that bug one can create a wrapper function which calls
   C-x 2         split horizontal
   C-x 3         split vertical
 
+

yank/paste

+
  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
+

block/rect

  C-x <SPC>                     activate rectangle-mark-mode
   M-x string-rectangle <RET>    insert text in marked rect
@@ -887,10 +907,6 @@ major_pagefault: Happens when the page needed is NOT in memory, the kernel
 
pstack <pid>
     Dump stack for all threads of process.
 
-

pstack(1)

-
pstack <pid>
-    Dump stack for all threads of process.
-

perf(1)

perf list      show supported hw/sw events
 
@@ -1053,6 +1069,94 @@ the .rodata section as follows:

Demangle stream

For example dynamic symbol table:

  readelf -W --dyn-syms <elf> | c++filt
+
+

c++

+

Type deduction

+

Force compile error to see what auto is deduced to.

+
auto foo = bar();
+
+// force compile error
+typename decltype(foo)::_;
+
+

glibc

+

malloc tracer mtrace(3)

+

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
+#include <mcheck.h>
+__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=<file>
+LD_PRELOAD=./libmtrace.so <binary>
+
+

Note: If MALLOC_TRACE is not set mtrace won't install tracing hooks.

+

To get the results of the trace file:

+
mtrace <binary> $MALLOC_TRACE
+
+

malloc check mallopt(3)

+

Configure action when glibc detects memory error.

+
export MALLOC_CHECK_=<N>
+
+

Useful values:

+
1   print detailed error & continue
+3   print detailed error + stack trace + memory mappings & abort
+7   print simple error message + stack trace + memory mappings & abort
+
+

gcc(1)

+

CLI

+

Preprocessing

+

While debugging can be helpful to just pre-process files.

+
gcc -E [-dM] ...
+
+
    +
  • -E run only preprocessor
  • +
  • -dM list only #define statements
  • +
+

Builtins

+

__builtin_expect(expr, cond)

+

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 "
+extern void foo();
+extern void bar();
+void run0(int x) {
+  if (__builtin_expect(x,0)) { foo(); }
+  else { bar(); }
+}
+void run1(int x) {
+  if (__builtin_expect(x,1)) { foo(); }
+  else { bar(); }
+}
+" | gcc -O2 -S -masm=intel -o /dev/stdout -xc -
+
+

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
+.L4:
+        xor     eax, eax
+        jmp     foo
+run1:
+        test    edi, edi
+        je      .L6
+        xor     eax, eax
+        jmp     foo
+.L6:
+        xor     eax, eax
+        jmp     bar
 
-- cgit v1.2.3