title: How to include C backtrace in a kernel module code?
body: <p>So I am trying to find out what kernel processes are calling some functions in a block driver. I thought including backtrace() in the C library would make it easy. But I am having trouble to load the backtrace. </p>
  
  <p>I copied this example function to show the backtrace:</p>
  
  <p><a href="http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/063/6391/6391l1.html" rel="noreferrer">http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/063/6391/6391l1.html</a></p>
  
  <p>All attempts to compile have error in one place or another that a file cannot be found or that the functions are not defined. </p>
  
  <p>Here is what comes closest. </p>
  
  <p>In the Makefile I put the compiler directives:</p>
  
  <pre><code> -rdynamic -I/usr/include 
  </code></pre>
  
  <p>If I leave out  the second one, -I/usr/include, then the compiler reports it cannot find the required header execinfo.h.</p>
  
  <p>Next, in the code where I want to do the backtrace I have copied the function from the example:</p>
  
  <pre><code>//trying to include the c backtrace capability
  #include &lt;execinfo.h&gt;
  
  void show_stackframe() {
  void *trace[16];
  char **messages = (char **)NULL;
  int i, trace_size = 0;
  
  trace_size = backtrace(trace, 16);
  messages = backtrace_symbols(trace, trace_size);
  printk(KERN_ERR "[bt] Execution path:\n");
  for (i=0; i&lt;trace_size; ++i)
      printk(KERN_ERR "[bt] %s\n", messages[i]);
  }
  //backtrace function
  </code></pre>
  
  <p>I have put the call to this function later on, in a block driver function where the first sign of the error happens. Simply:</p>
  
  <pre><code>show_stackframe();
  </code></pre>
  
  <p>So when I compile it, the following errors:</p>
  
  <pre><code>user@slinux:~/2.6-32$ make -s
  Invoking make againt the kernel at /lib/modules/2.6.32-5-686/build
  In file included from /usr/include/features.h:346,
          from /usr/include/execinfo.h:22,
          from /home/linux/2.6-32/block/block26.c:49:
  /usr/include/sys/cdefs.h:287:1: warning: "__always_inline" redefined
  In file included from /usr/src/linux-headers-2.6.32-5-common/include/linux/compiler-gcc.h:86,
          from /usr/src/linux-headers-2.6.32-5-common/include/linux/compiler.h:40,
          from /usr/src/linux-headers-2.6.32-5-common/include/linux/stddef.h:4,
          from /usr/src/linux-headers-2.6.32-5-common/include/linux/list.h:4,
          from /usr/src/linux-headers-2.6.32-5-common/include/linux/module.h:9,
          from /home/linux/2.6-32/inc/linux_ver.h:40,
          from /home/linux/2.6-32/block/block26.c:32:
  /usr/src/linux-headers-2.6.32-5-common/include/linux/compiler-gcc4.h:15:1: warning: this is the location of the previous definition
      /home/linux/2.6-32/block/block26.c:50: warning: function declaration isn’t a prototype
  WARNING: "backtrace" [/home/linux/2.6-32/ndas_block.ko] undefined!
  WARNING: "backtrace_symbols" [/home/linux/2.6-32/ndas_block.ko] undefined!
  </code></pre>
  
  <p>Note: block26.c is the file I am hoping to get the backtrace from.</p>
  
  <p>Is there an obvious reason why the backtrace and backtrace_symbols remain undefined when it is compiled into the .ko modules?</p>
  
  <p>I am guessing it because I use the compiler include execinfo.h which is residing on the computer and not being loaded to the module. </p>
  
  <p>It is my uneducated guess to say the least. </p>
  
  <p>Can anyone offer a help to get the backtrace functions loading up in the module?</p>
  
  <p>Thanks for looking at this inquiry. </p>
  
  <p>I am working on debian. When I take out the function and such, the module compiles fine and almost works perfectly. </p>
  
  <p>From ndasusers</p>