Wednesday 6 September 2017

How to find structure member offset in gdb


(gdb) print (int)&((struct A*)0)->a

Above command can be used to find structure member offset 

For example your structure is 

struct node {
  int a;
  char b;
  short c;
};

If you want to find relative offset of char b then you need to run

(gdb) print (int)&((struct A*)0)->b
$2 = 4

Tuesday 25 July 2017

Linux kernel module debugging using gdb

Choose appropriate function from stack which has failed. Suppose
function failed is "function_foo()" then you will have entry like

kernel: [<ffffffff814ec2ba>] ? function_foo+198

in stack trace. Pass corresponding debug .ko to gdb 
[root@pritam-pc]# gdb mymodule.ko.debug

(gdb) l* function_foo+198

This will give you .c file which caused failure with line number.
Suppose you got details like .c file is foo.c and line number is 122
then you can do more debugging with following command

(gdb) info line  foo.c:122