YourLabs.org


Django 1.7 Release !

Django 1.7 final has been released, and it’s awesome, check it out !



Mailr: new gmail-like open-source webmail

Mailr looks like a cool alternative to Roundcube. Try out the demo and let me know what you think !


Build beautiful books with Git and MarkDown with GitBook

GitBook looks promising, see how classy that Javascript book build with GitBook is !


Fun with debugging symbols

This article follows up with Basics of GDB debugging, focus on symbols.

List symbols with nm

Use the nm command to list symbols in a binary:

$ nm main
0000000000600920 B __bss_start
0000000000600920 b completed.6330
0000000000600910 D __data_start
0000000000600910 W data_start
0000000000400440 t deregister_tm_clones
00000000004004b0 t __do_global_dtors_aux
00000000006006f8 t __do_global_dtors_aux_fini_array_entry
0000000000600918 D __dso_handle
0000000000600708 d _DYNAMIC
0000000000600920 D _edata
0000000000600928 B _end
00000000004005a4 T _fini
00000000004004d0 t frame_dummy
00000000006006f0 t __frame_dummy_init_array_entry
00000000004006e8 r __FRAME_END__
00000000006008e0 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
00000000004003a8 T _init
00000000006006f8 t __init_array_end
00000000006006f0 t __init_array_start
00000000004005b0 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000600700 d __JCR_END__
0000000000600700 d __JCR_LIST__
                 w _Jv_RegisterClasses
00000000004005a0 T __libc_csu_fini
0000000000400530 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
00000000004004fd T main
                 U printf@@GLIBC_2.2.5
0000000000400470 t register_tm_clones
0000000000400410 T _start
0000000000600920 D __TMC_END__
0000000000600924 B YourGlobalVariable

Here’s a snippet from man nm which explains what we see here:

For each symbol, nm shows:

· The symbol value, in the radix selected by options (see below), or hexadecimal by default.

· The symbol type. At least the following types are used; others are, as well, depending on the object file format. If lowercase, the symbol is usually local; if uppercase, the symbol is global (external). There are however a few lowercase symbols that are shown for special global symbols (“u”, “v” and “w”).

"A" The symbol’s value is absolute, and will not be changed by further linking.

"B" "b" The symbol is in the uninitialized data section (known as BSS).

"C" The symbol is common. Common symbols are uninitialized data. When linking, multiple common symbols may appear with the same name. If the symbol is defined anywhere, the common symbols are treated as undefined references.

"D" "d" The symbol is in the initialized data section.

"G" "g" The symbol is in an initialized data section for small objects. Some object file formats permit more efficient access to small data objects, such as a global int variable as opposed to a large global array.

"I" The symbol is an indirect reference to another symbol.

"N" The symbol is a debugging symbol.

[…] snipped for readability

”?” The symbol type is unknown, or object file format specific.

· The symbol name.

List symbols with readelf -Ws

You can also use readelf -Ws to list symbols in an object:

$ readelf -Ws main

Symbol table '.dynsym' contains 4 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
     3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__

Symbol table '.symtab' contains 73 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000400200     0 SECTION LOCAL  DEFAULT    1 
     2: 000000000040021c     0 SECTION LOCAL  DEFAULT    2 
     3: 000000000040023c     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000400260     0 SECTION LOCAL  DEFAULT    4 
     5: 0000000000400280     0 SECTION LOCAL  DEFAULT    5 
     6: 00000000004002e0     0 SECTION LOCAL  DEFAULT    6 
     7: 0000000000400320     0 SECTION LOCAL  DEFAULT    7 
     8: 0000000000400328     0 SECTION LOCAL  DEFAULT    8 
     9: 0000000000400348     0 SECTION LOCAL  DEFAULT    9 
    10: 0000000000400360     0 SECTION LOCAL  DEFAULT   10 
    11: 00000000004003a8     0 SECTION LOCAL  DEFAULT   11 
    12: 00000000004003d0     0 SECTION LOCAL  DEFAULT   12 
    13: 0000000000400410     0 SECTION LOCAL  DEFAULT   13 
    14: 00000000004005a4     0 SECTION LOCAL  DEFAULT   14 
    15: 00000000004005b0     0 SECTION LOCAL  DEFAULT   15 
    16: 00000000004005c4     0 SECTION LOCAL  DEFAULT   16 
    17: 00000000004005f8     0 SECTION LOCAL  DEFAULT   17 
    18: 00000000006006f0     0 SECTION LOCAL  DEFAULT   18 
    19: 00000000006006f8     0 SECTION LOCAL  DEFAULT   19 
    20: 0000000000600700     0 SECTION LOCAL  DEFAULT   20 
    21: 0000000000600708     0 SECTION LOCAL  DEFAULT   21 
    22: 00000000006008d8     0 SECTION LOCAL  DEFAULT   22 
    23: 00000000006008e0     0 SECTION LOCAL  DEFAULT   23 
    24: 0000000000600910     0 SECTION LOCAL  DEFAULT   24 
    25: 0000000000600920     0 SECTION LOCAL  DEFAULT   25 
    26: 0000000000000000     0 SECTION LOCAL  DEFAULT   26 
    27: 0000000000000000     0 SECTION LOCAL  DEFAULT   27 
    28: 0000000000000000     0 SECTION LOCAL  DEFAULT   28 
    29: 0000000000000000     0 SECTION LOCAL  DEFAULT   29 
    30: 0000000000000000     0 SECTION LOCAL  DEFAULT   30 
    31: 0000000000000000     0 SECTION LOCAL  DEFAULT   31 
    32: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS init.c
    33: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
    34: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    35: 0000000000600700     0 OBJECT  LOCAL  DEFAULT   20 __JCR_LIST__
    36: 0000000000400440     0 FUNC    LOCAL  DEFAULT   13 deregister_tm_clones
    37: 0000000000400470     0 FUNC    LOCAL  DEFAULT   13 register_tm_clones
    38: 00000000004004b0     0 FUNC    LOCAL  DEFAULT   13 __do_global_dtors_aux
    39: 0000000000600920     1 OBJECT  LOCAL  DEFAULT   25 completed.6330
    40: 00000000006006f8     0 OBJECT  LOCAL  DEFAULT   19 __do_global_dtors_aux_fini_array_entry
    41: 00000000004004d0     0 FUNC    LOCAL  DEFAULT   13 frame_dummy
    42: 00000000006006f0     0 OBJECT  LOCAL  DEFAULT   18 __frame_dummy_init_array_entry
    43: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS main.c
    44: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    45: 00000000004006e8     0 OBJECT  LOCAL  DEFAULT   17 __FRAME_END__
    46: 0000000000600700     0 OBJECT  LOCAL  DEFAULT   20 __JCR_END__
    47: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
    48: 00000000006006f8     0 NOTYPE  LOCAL  DEFAULT   18 __init_array_end
    49: 0000000000600708     0 OBJECT  LOCAL  DEFAULT   21 _DYNAMIC
    50: 00000000006006f0     0 NOTYPE  LOCAL  DEFAULT   18 __init_array_start
    51: 00000000006008e0     0 OBJECT  LOCAL  DEFAULT   23 _GLOBAL_OFFSET_TABLE_
    52: 00000000004005a0     2 FUNC    GLOBAL DEFAULT   13 __libc_csu_fini
    53: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTable
    54: 0000000000600910     0 NOTYPE  WEAK   DEFAULT   24 data_start
    55: 0000000000600920     0 NOTYPE  GLOBAL DEFAULT   24 _edata
    56: 00000000004005a4     0 FUNC    GLOBAL DEFAULT   14 _fini
    57: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@@GLIBC_2.2.5
    58: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_2.2.5
    59: 0000000000600910     0 NOTYPE  GLOBAL DEFAULT   24 __data_start
    60: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    61: 0000000000600918     0 OBJECT  GLOBAL HIDDEN    24 __dso_handle
    62: 0000000000600924     4 OBJECT  GLOBAL DEFAULT   25 YourGlobalVariable
    63: 00000000004005b0     4 OBJECT  GLOBAL DEFAULT   15 _IO_stdin_used
    64: 0000000000400530   101 FUNC    GLOBAL DEFAULT   13 __libc_csu_init
    65: 0000000000600928     0 NOTYPE  GLOBAL DEFAULT   25 _end
    66: 0000000000400410     0 FUNC    GLOBAL DEFAULT   13 _start
    67: 0000000000600920     0 NOTYPE  GLOBAL DEFAULT   25 __bss_start
    68: 00000000004004fd    42 FUNC    GLOBAL DEFAULT   13 main
    69: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
    70: 0000000000600920     0 OBJECT  GLOBAL HIDDEN    24 __TMC_END__
    71: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
    72: 00000000004003a8     0 FUNC    GLOBAL DEFAULT   11 _init

Extract symbols from a binary with objcopy --only-keep-debug

objcopy --only-keep-debug main symbols
nm symbols # print list of symbols

Stripping debug symbols from an object with objcopy -S

Create another executable without symbols ie.:

$ objcopy -S main main_without_symbols 

The new object, main_without_symbols will have harder to debug:

$ readelf -Ws  main_without_symbols 

Symbol table '.dynsym' contains 4 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
     3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__

Strip symbols off a binary with --strip-unneeded

strip --strip-debug --strip-unneeded main

Symbols are not in the binary anymore:

$ nm main
nm: main: no symbols

Load symbols from file with (gdb) symbols-file

Reading symbols from main...(no debugging symbols found)...done.
(gdb) info variables
All defined variables:
(gdb) symbol-file debug_symbols
Reading symbols from debug_symbols...done.
(gdb) info variables
All defined variables:

File main.c:
int YourGlobalVariable;

Non-debugging symbols:
0x00000000004005b0  _IO_stdin_used
0x00000000004006e8  __FRAME_END__
0x00000000006006f0  __frame_dummy_init_array_entry
0x00000000006006f0  __init_array_start
0x00000000006006f8  __do_global_dtors_aux_fini_array_entry
0x00000000006006f8  __init_array_end
0x0000000000600700  __JCR_END__
0x0000000000600700  __JCR_LIST__
0x0000000000600708  _DYNAMIC
0x00000000006008e0  _GLOBAL_OFFSET_TABLE_
0x0000000000600910  __data_start
0x0000000000600910  data_start
0x0000000000600918  __dso_handle
0x0000000000600920  __TMC_END__
0x0000000000600920  __bss_start
0x0000000000600920  _edata
0x0000000000600920  completed
0x0000000000600928  _end

Add symbols back to a binary with objcopy --add-gnu-debuglink=symbols_file

objcopy --add-gnu-debuglink=debug_symbols main

Read symbols from a file with nm

Note that the above won’t let nm find symbols in main, but it will be able to list them from debug_symbols:

$ nm debug_symbols 
0000000000600920 B __bss_start
0000000000600920 b completed.6330
[...] snipped for readability

GDB debugging basics

Introduction

GDB is a debugger for executables “objects” on Linux. It will use source code and “debug symbols” if available:

  • source code to display the code ie. corresponding to a specific frame at runtime,
  • debug symbols to display variables and functions names like in the code.

What’s the relation between security and GDB ? Well it will help finding security bugs which may be vulnerabilities to buffer overflow.

Debugging with sources and debug symbols

Consider the following C source code example for hello world:

#include<stdio.h>

int YourGlobalVariable;

main()
{
    int YourLocalVariable;

    printf("Hello World\n");

    return 0;
}

Compile with debug symbols with -ggdb:

gcc main.c -ggdb -o main

Now you can easely debug with gdb:

$ gdb main
GNU gdb (GDB) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...done.

List source files with (gdb) info sources

(gdb) info sources
Source files for which symbols have been read in:

/home/jpic/main.c

Source files for which symbols will be read in on demand:

Show variables with (gdb) info variables

(gdb) info variables
All defined variables:

File main.c:
int YourGlobalVariable;

Non-debugging symbols:
0x00000000004005b0  _IO_stdin_used
0x00000000004006e8  __FRAME_END__
0x00000000006006f0  __frame_dummy_init_array_entry
0x00000000006006f0  __init_array_start
0x00000000006006f8  __do_global_dtors_aux_fini_array_entry
0x00000000006006f8  __init_array_end
0x0000000000600700  __JCR_END__
0x0000000000600700  __JCR_LIST__
0x0000000000600708  _DYNAMIC
0x00000000006008e0  _GLOBAL_OFFSET_TABLE_
0x0000000000600910  __data_start
0x0000000000600910  data_start
0x0000000000600918  __dso_handle
0x0000000000600920  __TMC_END__
0x0000000000600920  __bss_start
0x0000000000600920  _edata
0x0000000000600920  completed
0x0000000000600928  _end

Show functions with (gdb) info functions

(gdb) info functions
All defined functions:

File main.c:
int main();

Non-debugging symbols:
0x00000000004003a8  _init
0x00000000004003e0  printf@plt
0x00000000004003f0  __libc_start_main@plt
0x0000000000400400  __gmon_start__@plt
0x0000000000400410  _start
0x0000000000400440  deregister_tm_clones
0x0000000000400470  register_tm_clones
0x00000000004004b0  __do_global_dtors_aux
0x00000000004004d0  frame_dummy
0x0000000000400530  __libc_csu_init
0x00000000004005a0  __libc_csu_fini
0x00000000004005a4  _fini

Show local variables with (gdb) info scope

(gdb) info scope main
Scope for main:
Symbol YourLocalVariable is a complex DWARF expression:
     0: DW_OP_fbreg -20
, length 4.

Break on a function with (gdb) b

(gdb) b main
Breakpoint 1 at 0x400505: file main.c, line 7.

Run the object in gdb with (gdb) run

Run the object, it will break on main() (see above):

(gdb) run
Starting program: /home/jpic/main 
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?

Breakpoint 1, main () at main.c:7
7       int YourLocalVariable=6;

Continue to next line on break with (gdb) n

(gdb) n
9       printf("Hello %i World\n", YourLocalVariable);

Print a variable with (gdb) print

(gdb) print YourLocalVariable
$1 = 6

Continuing normal execution with (gdb) c

(gdb) c
Continuing.
Hello 6 World
[Inferior 1 (process 17984) exited normally]
(gdb) 

Debugging without debugging symbols

Without debugging symbols, gcc does not know where a function is defined:

(gdb) info functions
All defined functions:

Non-debugging symbols:
0x00000000004003a8  _init
0x00000000004003e0  puts@plt
0x00000000004003f0  __libc_start_main@plt
0x0000000000400400  __gmon_start__@plt
0x0000000000400410  _start
0x0000000000400440  deregister_tm_clones
0x0000000000400470  register_tm_clones
0x00000000004004b0  __do_global_dtors_aux
0x00000000004004d0  frame_dummy
0x00000000004004fd  main
0x0000000000400520  __libc_csu_init
0x0000000000400590  __libc_csu_fini
0x0000000000400594  _fini

Debugging without source code

Listing the source code does not work:

(gdb) list
No symbol table is loaded.  Use the "file" command.

Further reading


Strace basics

This is the first of a series of articles on security and exploiting. For starters we’ll cover basic debugging tools before we get into actual exploiting because the first step to exploiting is reverse-engineering most of the time.

The series targets experienced developers and tries to go straight to the point for them. Every article in this series is tagged “security”.

Introduction

strace is a tool that prints system calls like open, connect, etc … and signals like INT, KILL, etc … of a process. It is very useful to know what a program is doing.

strace as a wrapper

You can start a program using strace as a wrapper as such:

$ strace ./main 
execve("./main", ["./main"], [/* 43 vars */]) = 0
brk(0)                                  = 0x1b7a000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=169727, ...}) = 0
mmap(NULL, 169727, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff0770cd000
close(3)                                = 0
open("/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\34\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2022349, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff0770cc000
mmap(NULL, 3832336, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ff076b2f000
mprotect(0x7ff076ccd000, 2097152, PROT_NONE) = 0
mmap(0x7ff076ecd000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19e000) = 0x7ff076ecd000
mmap(0x7ff076ed3000, 14864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7ff076ed3000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff0770cb000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff0770ca000
arch_prctl(ARCH_SET_FS, 0x7ff0770cb700) = 0
mprotect(0x7ff076ecd000, 16384, PROT_READ) = 0
mprotect(0x7ff0770f7000, 4096, PROT_READ) = 0
munmap(0x7ff0770cd000, 169727)          = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 5), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff0770f6000
write(1, "Hello 6 World\n", 14Hello 6 World
)         = 14
exit_group(0)                           = ?
+++ exited with 0 +++

Filtering output with -e

Strace will probably output much more than you need. You could use the -e option to filter calls:

$ strace -e open,close,read,write ./main
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3)                                = 0
open("/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\34\2\0\0\0\0\0"..., 832) = 832
close(3)                                = 0
write(1, "Hello 6 World\n", 14Hello 6 World
)         = 14
+++ exited with 0 +++

Strace output to a file with -o

You’ll note that strace outputs quite an amount of data for our little main program, imagine how much it will output for a bigger program … Way to much.

Use the -o option to get strace to output to a file:

strace -o /tmp/main.strace ./main 

Attach to a running process with -p

Use something like ps aux or pgrep to find a process ID. Then you can attach to it with -p:

strace -o /tmp/postgresql -p 1234

You might have to do that as root:

sudo strace -o /tmp/postgresql -p 1234

Else, you might have this error:

strace: attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted

Timestamps with -t or -r

Use -t for time:

$ strace -t ./main
18:15:28 execve("./main", ["./main"], [/* 43 vars */]) = 0
18:15:28 brk(0)                         = 0x24ac000
18:15:28 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
18:15:28 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
18:15:28 fstat(3, {st_mode=S_IFREG|0644, st_size=169727, ...}) = 0
18:15:28 mmap(NULL, 169727, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f8d7b716000
18:15:28 close(3)                       = 0
18:15:28 open("/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
18:15:28 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\34\2\0\0\0\0\0"..., 832) = 832
18:15:28 fstat(3, {st_mode=S_IFREG|0755, st_size=2022349, ...}) = 0
18:15:28 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8d7b715000
18:15:28 mmap(NULL, 3832336, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f8d7b178000
18:15:28 mprotect(0x7f8d7b316000, 2097152, PROT_NONE) = 0
18:15:28 mmap(0x7f8d7b516000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19e000) = 0x7f8d7b516000
18:15:28 mmap(0x7f8d7b51c000, 14864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f8d7b51c000
18:15:28 close(3)                       = 0
18:15:28 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8d7b714000
18:15:28 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8d7b713000
18:15:28 arch_prctl(ARCH_SET_FS, 0x7f8d7b714700) = 0
18:15:28 mprotect(0x7f8d7b516000, 16384, PROT_READ) = 0
18:15:28 mprotect(0x7f8d7b740000, 4096, PROT_READ) = 0
18:15:28 munmap(0x7f8d7b716000, 169727) = 0
18:15:28 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0
18:15:28 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8d7b73f000
18:15:28 write(1, "Hello 6 World\n", 14Hello 6 World
) = 14
18:15:28 exit_group(0)                  = ?
18:15:28 +++ exited with 0 +++

And -r for relative time:

$ strace -r ./main
     0.000000 execve("./main", ["./main"], [/* 43 vars */]) = 0
     0.000307 brk(0)                    = 0x1392000
     0.000108 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
     0.000075 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
     0.000056 fstat(3, {st_mode=S_IFREG|0644, st_size=169727, ...}) = 0
     0.000083 mmap(NULL, 169727, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f971f25d000
     0.000061 close(3)                  = 0
     0.000071 open("/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
     0.000078 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\34\2\0\0\0\0\0"..., 832) = 832
     0.000091 fstat(3, {st_mode=S_IFREG|0755, st_size=2022349, ...}) = 0
     0.000050 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f971f25c000
     0.000044 mmap(NULL, 3832336, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f971ecbf000
     0.000034 mprotect(0x7f971ee5d000, 2097152, PROT_NONE) = 0
     0.000055 mmap(0x7f971f05d000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19e000) = 0x7f971f05d000
     0.000053 mmap(0x7f971f063000, 14864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f971f063000
     0.000064 close(3)                  = 0
     0.000053 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f971f25b000
     0.000031 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f971f25a000
     0.000032 arch_prctl(ARCH_SET_FS, 0x7f971f25b700) = 0
     0.000097 mprotect(0x7f971f05d000, 16384, PROT_READ) = 0
     0.000036 mprotect(0x7f971f287000, 4096, PROT_READ) = 0
     0.000027 munmap(0x7f971f25d000, 169727) = 0
     0.000069 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0
     0.000053 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f971f286000
     0.000058 write(1, "Hello 6 World\n", 14Hello 6 World
) = 14
     0.000058 exit_group(0)             = ?
     0.000063 +++ exited with 0 +++

Prevent strace from truncating strings with -s 1024

Sometimes, strace will truncate strings to print which we might want to read. Use the -s option:

   -s strsize  Specify  the  maximum string  size to print (the default is  32).
               Note  that  filenames are  not   considered strings    and    are
               always   printed   in full.

Get statistics with -c

$ strace -c ./main
Hello 6 World
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  0.00    0.000000           0         1           read
  0.00    0.000000           0         1           write
  0.00    0.000000           0         2           open
  0.00    0.000000           0         2           close
  0.00    0.000000           0         3           fstat
  0.00    0.000000           0         8           mmap
  0.00    0.000000           0         3           mprotect
  0.00    0.000000           0         1           munmap
  0.00    0.000000           0         1           brk
  0.00    0.000000           0         1         1 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         1           arch_prctl
------ ----------- ----------- --------- --------- ----------------
100.00    0.000000                    25         1 total

Better manual reader with most: the slang-pager

The default pager in most linux distributions is more. But it’s not very colorful. This article presents a more fun alternative: most.

Your manual probably looks like this:

Not very awesome, wouldn’t it be nicer to have it with colors ? like this:

Then go ahead and install most, ie.:

sudo apt-get install most

Try it out:

MANPAGER=most man man

If you like it, set something like that in your .bashrc:

export PAGER=most
export MANPAGER=$PAGER

django-cities-light 2.3.1 released

django-cities-light 2.3.1 was released:

  • #42 added FAQ: Some data fail to import or don’t import like I want, how to skip them ?
  • #45 django-rest-framework support update by @almalki.
  • #49 Added country_items_pre_import and region_items_pre_import by @mauricioabreu.
  • #51 Slug should be used in unique_together along with name.
  • #51 Skip duplicate cities with no regions
  • #52 Added FAQ on MySQL and UTF-8
  • #53 Country phone prefix support by @csarcom
  • Forms update.

It is backward compatible and has migrations: don’t forget to run:

./manage.py migrate cities_light
./manage.py cities_light  # for phone column to fill up

Bash interactive scripting basics

A variable looks like this:

    export FOO=bar

To get a variable in your interactive shell, source the script that contains it as such:

    source script_that_contains_FOO
    echo $FOO

A function looks like this:

    function foo() {
        echo foo
    }

To run a function in your interactive shell, source the script and call the function like this:

    source script_that_contains_foo
    foo

To debug something that’s wrapped in a bash function or script, set the -x option. To de-activate debugging, set +x. Example:

    [env] 04/02 2014 02:17:26 jpic@etta /home/jpic 
    $ source script_that_contains_foo 

    [env] 04/02 2014 02:17:29 jpic@etta /home/jpic 
    $ set -x; foo; set +x
    + foo
    + echo bar
    bar
    + set +x

Lines prefixed with a + sign are those that are executed by bash. Lines without + prefix correspond to output.

Ok, now you can use shell scripting like an interactive framework.