Find the answer to your Linux question:
Results 1 to 3 of 3
Hello , I wanted to ask how to debug a source code for eg. kernel source code using gdb, also how to compile one entire directory in linux. Please help ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    1

    debugging source code


    Hello ,
    I wanted to ask how to debug a source code for eg. kernel source code using gdb, also how to compile one entire directory in linux.
    Please help me out with this as soon as possible...

  2. #2
    Just Joined!
    Join Date
    Mar 2009
    Location
    Norway
    Posts
    67
    If you want to debug kernel code, you should look at kgdb. However, 'debugging kernels' is a tricky thing, it's not like you can start the kernel via gdb and step through the instructions and set breakpoints etc.

    As to 'compile one entire directory' - what exactly do you mean?

    If your application contains a lot of components, you'd normally use make, so I'd start looking for a makefile (or Makefile, ymmv).

  3. #3
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    To compile all file in a directory, using the command line, you simply use the "glob" operator, wihch is an asterisk (*) charachter. So lets say your program directory contains the files "one.c", "two.c" and "three.c". If you first change directories to your program directory:
    Code:
    cd /path/to/my/program/dir
    Then the you can use the shell's glob function to pass all files to the compiler at once
    Code:
    gcc -o myApp *.c
    # this is exactly the same as typing:
    gcc -o myApp one.c two.c three.c
    As for debugging, you use "gdb". It is pretty simple, you just ask GDB to run your program for you. In a command line, GDB gives lets you control it using interactive commands.
    Code:
    gdb --args myApp --flag1 --flag2 --etc
    Once you are in the GDB command line, type "b main", this will set a "breakpoint" on the main function of your program. A break point is a place where GDB will pause running the program and ask you what you want to do next. So, to tell GDB to start line-by-line debugging from the very start (or "main") function of your program, "b main". You can, of course, start debugging from any function you like, it does not need to be "main".

    Once you set your break point, type "run" to execute the program, and it will pause as soon as it executes the line of code where your break point was set. If you typed "b main", it will pause right at the beginning of your program.

    When GDB pauses, you can use one-letter commands to control what it does next, like "l", "s", or "n". You can then enter "s" to tell gdb to execute one line of code in your program. Keep entering "s" until you get to where you want.

    If you come to a line of code with a function call like "printf" and you enter "s", it will take you through each line of code of "printf", which may not be C code, it may be assembler. You probably don't care what "printf" is doing, so if you want to execute "printf" without seeing the code inside of it, enter "n" in the GDB command line when GDB pauses on "printf()" or any other standard library function call.

    If you want to know where you are in your program, enter "l" (lowercase "L") in the command line, and you will see 10 lines of code from your program around the current line GDB is executing.

    That is the simplest introduction to GDB you can get. Play around with it until you get stuck. If you want to learn more, you need to Google for "GDB tutorials".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •