Introducción a Patrones de Diseño en C++ con Qt4/C.3.2 Inicio rápido en gdb

Versión para imprimir esta pagina

C.3.2. gdb Quickstart

[ fromfile: debugging.xml id: gdb ]

Imagine you are running a program and, for some mysterious reason, it crashes.

[lazarus] app> ./playlistmgr Segmentation fault [lazarus] app>

When your app aborts, or crashes, it is helpful to know (as quickly as possible) exactly where it happened. We can use gdb to locate the trouble spot quickly and easily.

[lazarus] app> gdb playlistmgr GNU gdb 6.3-debian Copyright 2004 Free Software Foundation, Inc. This GDB was configured as "i386-linux"...Using host libthread_db library "/lib/tls/".

(gdb) r [69] Starting program: ftgui/app/playlistmgr [Thread debugging using libthread_db enabled] [New Thread -1227622176 (LWP 17021)] Qt: gdb: -nograb added to command-line options.

        Use the -dograb option to enforce grabbing.

This is a debug message

Program received signal SIGSEGV, Segmentation fault. [Switching to Thread -1227622176 (LWP 17021)] 0xb7f03320 in FormDialog::createActions (this=0x80ae2a0) at formdialog.cpp:53 53 delete m_OkAction; (gdb)

gdb shows you not only the filename, and line number, but also the corresponding line in the source code. However, we still might want to get some context for this error. The command list shows you the surrounding source code for the current file:

(gdb) list 51 void FormDialog::createActions() { 52 53 delete m_OkAction; 54 delete m_CancelAction; 55 m_OkAction = new OkAction(m_Model, m_View); 56 m_CancelAction = new CancelAction(m_Model, m_View); 57 QHBoxLayout *buttons = new QHBoxLayout(0); (gdb)

The command where shows you the stack trace, or how we got there.

(gdb) where

  1. 0 0xb7f03320 in FormDialog::createActions (this=0x80ae2a0) at formdialog.cpp:53
  2. 1 0xb7f03058 in FormDialog::setModel (this=0x80ae2a0, fmodel=0x80c80d0)
   at formdialog.cpp:34
  1. 2 0x080664bd in SettingsDialog (this=0x80ae2a0, parent=0x0) at settingsdialog.cpp:14
  2. 3 0x0805f313 in MainWindow (this=0xbfffdec8) at mainwindow.cpp:42
  3. 4 0x08066f14 in Controller (this=0xbfffdec0, argc=1, argv=0xbfffdfe4) at controller.cpp:25
  4. 5 0x0805a8a4 in main (argc=1, argv=0xbfffdfe4) at main.cpp:7


Most open source IDEs use gdb under the hood. They each offer a user interface that makes certain features easier to learn and use. Other open-source tools that provide a front-end for gdb are: Eclipse, kdevelop, kdbg, and ddd. Some work may be required to get it to properly display the values of QString variables. [Tip] Viewing QStrings inside the debugger

QStrings are hard to see inside some debuggers because they are indirect pointers to Unicode data. The debugger needs to know extra things about a QString in order to display it properly.

Download kde-devel-gdb, some Qt 4 helper macros from the KDE subversion repository, and put this in your ~/.gdbinit:

source /path/to/kde/kde-devel-gdb define pqs

   printq4string $arg0


Now you should be able to print QStrings with the pqs macro.

[69] r is the command for "run"

Versión para imprimir esta pagina