Use GTK programming 4 together (use GDB to debug the Program)

Source: Internet
Author: User
Tags gtk
Use gtk programming 4 together (use GDB to debug the Program) Reprinted please indicate the source: http://lvjinhua.cublog.cn
Author: lvjinhua at gmail dot com
2006.09.23

  • 6. Use GDB to debug the program

Let's talk about how to use Makefile to organize the source code. This will briefly introduce how to use GDB to debug our program. Other applications about GDB will be further explored in subsequent chapters.

First, you must be certain that GDB is a debugging tool with exceptionally powerful functions. It can only run in character mode, but currently many GUI-based debugger/IDE, most of Free Software and commercial software use GDB as their backend (but these GUI-based debuggers are not stable ), therefore, GDB is the best choice (I recommend the GUI Debugger: insight and ddd ).

Here we use hello_gdb.c as an example. If you have seen it all the time before, you will not be unfamiliar with this program. hello_gdb.c mainly adds several integer and string variables based on hello_dubuntu2.c, to demonstrate some basic functions of gdb:
Run:

Hello_gdb.c
/* The main purpose of this example is to display a button in the window,
* Click the button to exit the program and construct several variables to demonstrate the gdb function.
*/
# Include <GTK/GTK. h>

Void
Cb_button (GtkWidget * widget, gpointer data)
{// Callback function of the button "button"
Gint I = 5;
Gint j = ++ I;
Gtk_main_quit ();
}

// This function is used to demonstrate the function that GDB directly calls the program to be debugged.
Gint
Gdb_test (gint arg)
{
G_print ("arg = % d/n", arg );
Arg ++;
Return arg;
}

Int
Main (int argc, char * argv [])
{
GtkWidget * main_window; // Main Window object
GtkWidget * button; // button object to be placed in the main window

// Construct two variables to demonstrate the GDB Function
Gint a = 5;
Gchar * name = "Dubuntu-6.06 ";
//
Gtk_init (& argc, & argv );

Main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL );
Gtk_window_set_title (GTK_WINDOW (main_window), "Hello, Dubuntu2! ");
// Set the default window size (200 in width and 50 in height)
Gtk_window_set_default_size (GTK_WINDOW (main_window );

Button = gtk_button_new_with_label ("Exit program ");
Gtk_container_add (GTK_CONTAINER (main_window), button );
// Connect the callback function to be called by clicking event for "button"
G_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cb_button), NULL );

Gtk_widget_show (button );
Gtk_widget_show (main_window );
// The above two sentences can be combined into gtk_widget_show_all (window)

G_signal_connect (G_OBJECT (main_window), "destroy", G_CALLBACK (cb_button), NULL );

Gtk_main ();
Return 0;
}
 

Compile: gcc-g-wall-O hello_gdb hello_gdb.c 'pkg-config -- cflags -- libs GTK +-2.0'
Note: The-G parameter is used to generate debugging information for executable files;
-Wall is used to print all warning information during program compilation.

Well, the program is the above (hello_gdb.c), we use the following command to compile it:

Gcc-g-wall-O hello_gdb hello_gdb.c 'pkg-config -- cflags -- libs GTK +-2.0'

After compilation, if there are no errors, the hello_gdb executable file will be generated. This executable file will carry the debugging information required for gdb debugging. With these debugging messages, you can view the function name, variable name, and source code when debugging the program.

Now, start debugging:
1) run the following command to load the newly compiled hello_gdb program:

GDB./hello_gdb

After loading, you will receive the following prompt and enter the command line mode of gdb:

Dubuntu @ dubuntu :~ /Desktop/gnome-gtk-prog/hello_gtk/src $ gdb./hello_gdb
GNU gdb 6.5.50.20060605-cvs
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
Welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"... Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1 ".

(Gdb)

2) Well, now we have successfully started gdb and loaded the executable program hello_gdb. Most of the subsequent commands compete for the hello_gdb program. Below we will list several of the most common commands:

Tip: In the gdb command, you only need to enter the first few characters of the command or parameter, and then press the "TAB" key on the disk, then gdb will automatically complete the command or parameter, if there are multiple candidates, gdb lists them.

  • List: l is used to list the source code of a program. By default, 10 lines are displayed each time.
  • List row number: displays the first 10 lines of code centered on the current file, such as list 12.
  • List function name: displays the source code of the function where the "function name" is located, for example, list main
  • List: output the following content of the last list Command without parameters.
Note: If you run the list command to get a print similar to the following, it is because the-g option is not added during program Compilation:
(Gdb) list
1./sysdeps/i386/elf/start. S: No such file or directory.
In ../sysdeps/i386/Elf/start. s

  • Run: r, which is used to run a program. When a breakpoint is reached, the program stops running at the breakpoint and waits for the user to enter the next command.
  • Set args: set the command line parameters when running the program, such as: set args 33 55
  • Show args: Display command line parameters
  • Continue: the message is c, which is used to continue running programs interrupted by breakpoints.
  • Break: Set a breakpoint for the program.
  • Break row number: Set a breakpoint at the "row number" of the current file, for example, break 33
  • Break function name: Set a breakpoint in the User-Defined Function "function name", for example, break cb_button
  • Info breakpoints: displays the breakpoint settings of the current program.
  • Disable breakpoints Num: disable the breakpoint "Num" to make it invalid. "Num" is the corresponding value displayed in info breakpoints.
  • Enable breakpoints num: Open the breakpoint "num" to make it take effect again
  • Step: Simple Description: "S". It is a single-step tracking program. When a function is called, it enters the function body (generally only the User-Defined Function ).
  • Next: the short note is n, a single-step tracking program. When a function is called, it does not enter the function body. The main difference between this command and step is that, when a user-defined function is encountered in a step, the step will be stepped into the function to run, and next will directly call the function and will not enter the function body.
  • Until: When you get tired of tracking a single step in a loop body, this command can run the program until you exit the loop body.
  • Finish: run the program until the current function finishes returning, and print the stack address, return value, and parameter value when the function returns.
  • Stepi or nexti: One-Step tracking of some machine commands.
  • Print expression: P, where "expression" can be a valid expression of any program being tested, for example, a program that is currently debugging C language, the "expression" can be a valid expression in any C language, including numbers, variables, and even function calls.
  • Print A: The value of integer a is displayed.
  • Print ++ A: adds the value of a to 1 and displays it.
  • Print name: the value of string name is displayed.
  • Print gdb_test (22): Call the gdb_test () function with an integer 22 as the parameter.
  • Print gdb_test (a): Call the gdb_test () function using variable A as the parameter.
  • BT: displays the function call stack of the current program.
  • Display expression: it is very useful when running a single step. After setting an expression using the display command, It outputs the set expression and value after each single step command. For example, display
  • Watch expression: sets a monitoring point. Once the value of the monitored "expression" changes, GDB forcibly terminates the program being debugged. For example, watch
  • Kill: the program being debugged will be forcibly terminated.
  • Help Command: The HELP command displays the commonly used help information of the "command ".
  • Call function (parameter): Call the "function" and pass the "parameter", for example, call gdb_test (55)
  • Layout: used to split the window. You can view the code and test it:
  • Layout SRC: displays the source code window.
  • Layout ASM: display the Disassembly Window
  • Layout regs: displays the source code/disassembly and CPU register windows.
  • Layout split: displays the source code and Disassembly window.
  • CTRL + L: refresh the window
  • Quit: Enter Q to exit GDB.

Of course, gdb has far more functions than this, including multi-process, multi-thread, signal, remote debugging, and other functions. Readers who need it can refer to other information, the subsequent sections of this article will also involve some content.

Recommended: debug the program with gdb

3) debugging instance:

GDB./hello_gdb # Start GDB and load the program to be debugged
List gdb_test # display the code of the gdb_test Function
Break cb_button # Set a breakpoint at the cb_button Function
Info breakpoints # display all breakpoint Information
Enable breakpoints # Start all breakpoints
Run # start running the program
# Note: The program is running normally now. When you click the "Exit program" button, it stops at the "cb_button" function because a breakpoint is set here.
Bt # display the current function call stack
Display j # monitor variable j
Next # Run the next command
Print j # display j value
Print gdb_test (j) # Call the gdb_test function and use j as the parameter.
Call gdb_test (++ j) # Same as above
Next
Finish # exit function cb_button
Continue # continue running the program
Quit # exit gdb

Next episode notice: Next section describes how to use vbox/hbox for window layout. This will solve the problem that the current button occupies the entire form.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.