Pretty printing json in Bash + gzipped response with curl

To decompress response in curl add: --compressed to curl command.

To pretty print JSON response, pipe the result to: python -m json.tool

All in one line:

$ curl --compressed "http://someUrl"  | python -m json.tool
Advertisements

PostgreSQL faster dump & restore

For PostgreSQL it takes a lot of time to import a text-based dump.

It turns out that binary dumps are imported much faster.
To create a binary dump run:

pg_dump -Fc dbname > /tmp/database.bak

Creating a database upon dump import should work, but for me it didn’t on PostgreSQL 9.4, so I recommend first creating the database and then running:

pg_restore -Fc /tmp/database.bak -d dbname

Debugging C/C++ programs with GDB

Starting

Compiling

To add debugging info to an executable generated by gcc:

gcc -g source.c

Amount of debugging info added by gcc can be controlled.
For minimal debug info use: -g1, for maximal: -g3

Running

gdb a.out

To skip intro message add -q flag.

To attach gdb to a running process:

gdb a.out pid 

Debugging

breakpoints

Setting a breakpoint

Set a breakpoint at specific line:

break line_number

If you have multiple source files you can specify one:

break source.c:line_number

Set a breakpoint on a function call:

break function_name
break source.c:function_name

You can make a breakpoint conditional by adding if condition to breakpoint definition. E.g.:

break 12 if x > 5

Removing a breakpoint

clear line_number
clear function_name

Versions with source_file_name work as well.

running and traversing

To start debugging an app run:

run

If you have set breakpoints, the app will stop at first of them.

command description
n go to the next line of program
s make a single step in execution, i.e. go into the function if another function is called at our current line or go to the next line.
c continue execution (i.e. go to the next breakpoint or finish

printing out variables

To print value of a specific variable:

print variable_name

To print array:

print *array_name@length

To print all function arguments with their values:

info args

To print all local variables with their values:

info locals

To print all global and static variables with their values:

info variables

trace/frames

To print out function calls that lead to current place:

bt

To print frames: frame
To step into specific frame (e.g. to print out callers variables):

frame frame_number

To switch between frames one can also use: up and down

Extras

Quitting

q

calling a function

While debugging a program one can call a function. To do it use:

call function_name(function_params)

E.g.: call print("some text\n")

heap consistency checking

To enable controlling of mallocated memory one can call:

mcheck(0)

The function has to be called before the first malloc.
To call it from gdb use:

call mcheck(0)

More info about mcheck: http://www.gnu.org/software/libc/manual/html_node/Heap-Consistency-Checking.html

For more info about gdb visit: http://www.yolinux.com/TUTORIALS/GDB-Commands.html

/sbin/ip route add … RTNETLINK answers: File exists

When starting opevpn I encountered strange issue. VPN was connecting but I could not access services behind it.
What I found in its log was:

Thu Oct 16 12:15:50 2014 /sbin/ip route add 10.1.1.0/24 via 10.1.1.1
RTNETLINK answers: File exists

It turned out the newly added route was conflicting with other route (a leftover from messing up vpn-things).

First, I removed the conflicting rule:

sudo route del -net 10.1.1.0 netmask 255.255.255.0

Then I manually added the route the vpn was trying to add (but I think it’s optional and simply restarting the VPN would do the thing)

/sbin/ip route add 10.1.1.0/24 via 10.1.1.1