6/27/12

How to print a stack trace in your user space linux application

When debugging some code I wanted to print out the stack trace of code allocating/deallocating memory without using gdb. I run over the eminent article at: http://www.linuxjournal.com/article/6391 that explained how to use the functions backtrace() and backtrace_symbols() in glibc. Read the article at Linuxjournal if you want more information, copy the code from this page if you only want a function that you can use.

A short summary:

Compile the program with:
gcc -g -rdynamic stack_trace.c -o stack_trace

Test program:
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>

void stack_trace(){
  void *trace[16];
  char **messages = (char **)NULL;
  int i, trace_size = 0;

  trace_size = backtrace(trace, 16);
  messages = backtrace_symbols(trace, trace_size);
  printf("[stack trace]>>>\n");
  for (i=0; i < trace_size; i++)
    printf("%s\n", messages[i]);
  printf("<<<[stack trace]\n");

  free(messages);
}

void main(){
  stack_trace();
}


When running it produces the following output:
[stack trace]>>>
./stack_trace(stack_trace+0x27) [0x804861b]
./stack_trace(main+0xb) [0x8048680]
/lib/libc.so.6(__libc_start_main+0xe7) [0xbeece7]
./stack_trace() [0x8048561]
<<<[stack trace]

If you want to find the exact location of the adress last on stack:[0x804861b].
Use addr2line:
~/code_examples$ addr2line 0x804861b -e stack_trace
/home/kungjohan/code_examples/stack_trace.c:13

11/30/10

SVN username with svn+ssh, the correct way

If you use svn+ssh and want to connect to an external server with a different username than the one you are currently logged in with on your local computer you can try to use svn+ssh://username@host. This will work until you have externals in your repository, svn will in some way forget your username. So a better way is to specify your username in the ssh configfile.

So add a file if it not exists:
.ssh/config

Host svn.host
User yourusername

9/15/10

Oneliner for removing svn and git metainfo

If you receive a code tree that someone has to forgot to remove version control system metainfo from it here is a one liner for removing it.

.svn files:
find . -type d -name ".svn" | xargs rm -rf

.git files:
find . -type d -name ".git" | xargs rm -rf

9/16/09

Enable core dumps

To enable core dumps to persistent storage mounted on /mnt on your embedded device with the filename appended to the core name, also make applications started as root to dump core.

ulimit -c unlimited
echo "/mnt/core.%e" > /proc/sys/kernel/core_pattern
echo "1" > /proc/sys/fs/suid_dumpable

7/13/09

How to use etags for emacs

1. Create tag files
a. In linux kernel tree just execute:
cd linux 2.6.27
make ARCH=arm TAGS

b. Else make your own tag files for your project
etags *.c *.h --output=TAGS

c. Quick command line to make tags in a code tree is:
find . -regex ".*\.[cChH]\(pp\)?" -print | etags -

2. To use tags in emacs
Load TAGS file with M-x visit tag table

Place cursor on a variable and press M-.

Emacs questions you which tag that you want to find, default the one that you have your cursor on. Press enter if correct.

Emacs questions which tag file you want to use, default TAGS, if correct press enter once more

3. Emacs jumps to the definition

If you want to jump to next occurence, press C-u M-.

4. Name completion

To get completion on a long function/variable name type the first letters and press M-tab

6/2/09

How to avoid "The host key has changed!" when reinstalling systems with the same ip

If you have troubles when reinstallning systems with the same ip you get different SSH host keys Then you can disable strict checking of host keys on a certain IP-range by adding the following setting to your ~/.ssh/config file.

Host 10.0.0.*
UserKnownHostsFile /dev/null
StrictHostKeyChecking no

5/8/09

How to store an executable script in SVN

If you want that subversion to mark a file as executable when you check it out, you can tell it by setting a property in SVN: (Link)

$ svn propset svn:executable ON somescript
property 'svn:executable' set on 'somescript'