Wednesday, May 6, 2009

Introducing pread

There are many situations when you just want to read some data from a file starting with a given offset. The usual way of doing this is:
lseek(fd, offset, SEEK_SET);
read(fd, buffer, LEN);

You can do this in one function call using pread(2) . The above code is equivalent with:

pread(fd, buffer, LEN, offset);

In a similar manner you can notice the existence of pwrite(2) which writes some data into a file at a given position.

Wednesday, March 11, 2009


Usage: ltrace [option ...] [command [arg ...]]

ltrace is a tool that simply runs the specified command until it exits. It intercepts and records the dynamic library calls which are called by the executed process.

dan@lemon:~/playground$ ltrace -n 8 -C date
__libc_start_main(0x8049780, 1, 0xbfb1e874, 0x8052c50, 0x8052c40
setlocale(6, "") = "en_US.UTF-8"
bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale"
textdomain("coreutils") = "coreutils"
__cxa_atexit(0x804cd70, 0, 0, 0xb7ee4ff4, 0xbfb1e7d8) = 0
getopt_long(1, 0xbfb1e874, "d:f:I::r:Rs:u", 0x8053ea0, NULL) = -1
nl_langinfo(131180, 0xbfb1e874, 0x8053e85, 0x8053ea0, 0) = 0xb7ef3915
clock_gettime(0, 0xbfb1e7b8, 0xb7dae8f5, 0xb7ee4ff4, 0) = 0
localtime(0xbfb1e6c8) = 0xb7ee8380
strftime(" Wed", 1024, " %a", 0xb7ee8380) = 4
fwrite("Wed", 3, 1, 0xb7ee54c0) = 1
fputc(' ', 0xb7ee54c0) = 32
strftime(" Mar", 1024, " %b", 0xb7ee8380) = 4
fwrite("Mar", 3, 1, 0xb7ee54c0) = 1
fputc(' ', 0xb7ee54c0) = 32
fwrite("11", 2, 1, 0xb7ee54c0) = 1
fputc(' ', 0xb7ee54c0) = 32
fputc('0', 0xb7ee54c0) = 48
fwrite("9", 1, 1, 0xb7ee54c0) = 1
fputc(':', 0xb7ee54c0) = 58
fwrite("38", 2, 1, 0xb7ee54c0) = 1
fputc(':', 0xb7ee54c0) = 58
fwrite("33", 2, 1, 0xb7ee54c0) = 1
fputc(' ', 0xb7ee54c0) = 32
strlen("EET") = 3
fwrite("EET", 3, 1, 0xb7ee54c0) = 1
fputc(' ', 0xb7ee54c0) = 32
fwrite("2009", 4, 1, 0xb7ee54c0) = 1
__overflow(0xb7ee54c0, 10, 0x1e08f70c, 0, 0x1e08f70cWed Mar 11 09:38:33 EET 2009
) = 10
__fpending(0xb7ee54c0, 0xb7f20ff4, 0x8048704, 0xb7ee4ff4, 0) = 0
fclose(0xb7ee54c0) = 0
__fpending(0xb7ee5560, 0xb7f20ff4, 0x8048704, 0xb7ee4ff4, 0) = 0
fclose(0xb7ee5560) = 0
+++ exited (status 0) +++

ltrace accepts a lot of options. You can trace also for system calls, count time , follow forks etc. A short list with options can be found below:

-a, --align=COLUMN align return values in a secific column.
-c count time and calls, and report a summary on exit.
-C, --demangle decode low-level symbol names into user-level names.
-d, --debug print debugging info.
-e expr modify which events to trace.
-f follow forks.
-h, --help display this help and exit.
-i print instruction pointer at time of library call.
-l, --library=FILE print library calls from this library only.
-L do NOT display library calls.
-n, --indent=NR indent output by NR spaces for each call level nesting.
-o, --output=FILE write the trace output to that file.
-p PID attach to the process with the process ID pid.
-r print relative timestamps.
-s STRLEN specify the maximum string size to print.
-S display system calls.
-t, -tt, -ttt print absolute timestamps.
-T show the time spent inside each call.
-u USERNAME run command with the userid, groupid of username.
-V, --version output version information and exit.
-x NAME treat the global NAME like a library subroutine.

Tuesday, March 3, 2009


dmidecode is a tool for dumping a computer's DMI table contents in a human readable format. This table contains information regarding system's hardware components as described in BIOS.

More information you can find here or in dmidecode(8) manpage.

Sunday, February 22, 2009

Compilation without words

Click on the image to have a better view.

Friday, February 13, 2009


Lindent is a script that changes the appearance of a C program according to linux kernel coding style. It can be found in linux/scripts/ and it makes use of indent.(see indent(1) manual page).

For a debian based distribution running Lindent is straightforward:

#install indent
apt-get install indent
#run Lindent over a file
linux/scripts/Lindent file.c

preferred coding style used in linux kernel is described in linux/Documentation/CodingStyle.