3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
11 #include <qptrvector.h>
12 #include <qstringlist.h>
14 #include "exprwnd.h" /* some compilers require this */
23 class ProgramTypeTable
;
34 struct DisassembledCode
;
40 class KDebugger
: public QObject
44 KDebugger(QWidget
* parent
, /* will be used as the parent for dialogs */
51 * This function starts to debug the specified executable using the
52 * specified driver. If a program is currently being debugged, it is
53 * terminated first. Ownership of driver is taken if and only if
56 * @return false if an error occurs.
58 bool debugProgram(const QString
& executable
,
59 DebuggerDriver
* driver
);
62 * Uses the specified core to debug the active program.
63 * @param batch tells whether the core file was given on the
66 void useCoreFile(QString corefile
, bool batch
);
69 * Overrides the program argument in the per-program config
72 void overrideProgramArguments(const QString
& args
);
76 * Uses the specified pid to attach to the active program.
78 void setAttachPid(const QString
& pid
);
81 * Attaches to the specified process and debugs it.
83 void attachProgram(const QString
& pid
);
86 * Returns the file name of the per-program config file for the
89 static QString
getConfigForExe(const QString
& exe
);
92 * The driver name entry in the per-program config file.
94 static const char DriverNameEntry
[];
98 * Runs the program or continues it if it is stopped at a breakpoint.
103 * Restarts the debuggee.
105 void programRunAgain();
108 * Performs a single-step, possibly stepping into a function call.
109 * If byInsn is true, a step by instruction is performed.
114 * Performs a single-step, stepping over a function call.
115 * If byInsn is true, a step by instruction is performed.
120 * Performs a single-step by instruction, possibly stepping into a
126 * Performs a single-step by instruction, stepping over a function
132 * Runs the program until it returns from the current function.
134 void programFinish();
137 * Kills the program (removes it from memory).
142 * Interrupts the program if it is currently running.
147 * Moves the program counter to the specified line.
148 * If an address is given, it is moved to the address.
150 void setProgramCounter(const QString
&, int, const DbgAddr
&);
154 * Queries the user for program arguments.
156 void programArgs(QWidget
* parent
);
159 * Queries the user for program settings: Debugger command, terminal
162 void programSettings(QWidget
* parent
);
165 * Setup remote debugging device
167 void setRemoteDevice(const QString
& remoteDevice
) { m_remoteDevice
= remoteDevice
; }
170 * Run the debuggee until the specified line in the specified file is
173 * @return false if the command was not executed, e.g. because the
174 * debuggee is running at the moment.
176 bool runUntil(const QString
& fileName
, int lineNo
);
181 * @param fileName The source file in which to set the breakpoint.
182 * @param lineNo The zero-based line number.
183 * @param address The exact address of the breakpoint.
184 * @param temporary Specifies whether this is a temporary breakpoint
185 * @return false if the command was not executed, e.g. because the
186 * debuggee is running at the moment.
188 bool setBreakpoint(QString fileName
, int lineNo
,
189 const DbgAddr
& address
, bool temporary
);
194 * @param bp Describes the breakpoint.
195 * @param queueOnly If false, the breakpoint is set using a high-priority command.
197 void setBreakpoint(Breakpoint
* bp
, bool queueOnly
);
200 * Enable or disable a breakpoint at the specified location.
202 * @param fileName The source file in which the breakpoint is.
203 * @param lineNo The zero-based line number.
204 * @param address The exact address of the breakpoint.
205 * @return false if the command was not executed, e.g. because the
206 * debuggee is running at the moment.
208 bool enableDisableBreakpoint(QString fileName
, int lineNo
,
209 const DbgAddr
& address
);
212 * Enables or disables the specified breakpoint.
214 * @return false if the command was not executed, e.g. because the
215 * debuggee is running at the moment.
217 bool enableDisableBreakpoint(Breakpoint
* bp
);
220 * Removes the specified breakpoint. Note that if bp is an orphaned
221 * breakpoint, then bp is an invalid pointer if (and only if) this
222 * function returns true.
224 * @return false if the command was not executed, e.g. because the
225 * debuggee is running at the moment.
227 bool deleteBreakpoint(Breakpoint
* bp
);
230 * Changes the specified breakpoint's condition and ignore count.
232 * @return false if the command was not executed, e.g. because the
233 * debuggee is running at the moment.
235 bool conditionalBreakpoint(Breakpoint
* bp
,
236 const QString
& condition
,
240 * Tells whether one of the single stepping commands can be invoked
241 * (step, next, finish, until, also run).
243 bool canSingleStep();
246 * Tells whether a breakpoints can be set, deleted, enabled, or disabled.
248 bool canChangeBreakpoints();
251 * Tells whether a the program is loaded, but not active.
256 * Add a watch expression.
258 void addWatch(const QString
& expr
);
261 * Retrieves the current status message.
263 const QString
& statusMessage() const { return m_statusMessage
; }
266 * Is the debugger ready to receive another high-priority command?
268 bool isReady() const;
271 * Is the debuggee running (not just active)?
273 bool isProgramRunning() { return m_haveExecutable
&& m_programRunning
; }
276 * Do we have an executable set?
278 bool haveExecutable() { return m_haveExecutable
; }
281 * Is the debuggee active, i.e. was it started by the debugger?
283 bool isProgramActive() { return m_programActive
; }
286 * Is the debugger driver idle?
290 /** The list of breakpoints. */
291 int numBreakpoints() const { return m_brkpts
.size(); }
292 const Breakpoint
* breakpoint(int i
) const { return m_brkpts
[i
]; }
295 * Returns the breakpoint with the specified \a id.
297 Breakpoint
* breakpointById(int id
);
299 const QString
& executable() const { return m_executable
; }
302 * Terminal emulation level.
305 ttyNone
= 0, /* ignore output, input triggers EOF */
306 ttySimpleOutputOnly
= 1, /* minmal output emulation, input triggers EOF */
307 ttyFull
= 7 /* program needs full emulation */
311 * Returns the level of terminal emulation requested by the inferior.
313 TTYLevel
ttyLevel() const { return m_ttyLevel
; }
315 /** Sets the terminal that is to be used by the debugger. */
316 void setTerminal(const QString
& term
) { m_inferiorTerminal
= term
; }
318 /** Returns the debugger driver. */
319 DebuggerDriver
* driver() { return m_d
; }
321 /** Returns the pid that the debugger is currently attached to. */
322 const QString
& attachedPid() const { return m_attachedPid
; }
325 * The memory at that the expression evaluates to is watched. Can be
326 * empty. Triggers a redisplay even if the expression did not change.
328 void setMemoryExpression(const QString
& memexpr
);
331 * Sets how the watched memory location is displayed.
332 * Call setMemoryExpression() to force a redisplay.
334 void setMemoryFormat(unsigned format
) { m_memoryFormat
= format
; }
337 void saveSettings(KConfig
*);
338 void restoreSettings(KConfig
*);
341 QString m_inferiorTerminal
;
342 QString m_debuggerCmd
; /* per-program setting */
343 TTYLevel m_ttyLevel
; /* level of terminal emulation */
348 QStringList m_watchEvalExpr
; /* exprs to evaluate for watch window */
349 QPtrVector
<Breakpoint
> m_brkpts
;
350 QString m_memoryExpression
; /* memory location to watch */
351 unsigned m_memoryFormat
; /* how that output should look */
354 void parse(CmdQueueItem
* cmd
, const char* output
);
356 void handleRunCommands(const char* output
);
357 void updateAllExprs();
358 void updateProgEnvironment(const QString
& args
, const QString
& wd
,
359 const QDict
<EnvVar
>& newVars
,
360 const QStringList
& newOptions
);
361 void parseLocals(const char* output
, QList
<ExprValue
>& newVars
);
362 void handleLocals(const char* output
);
363 bool handlePrint(CmdQueueItem
* cmd
, const char* output
);
364 bool handlePrintDeref(CmdQueueItem
* cmd
, const char* output
);
365 void handleBacktrace(const char* output
);
366 void handleFrameChange(const char* output
);
367 void handleFindType(CmdQueueItem
* cmd
, const char* output
);
368 void handlePrintStruct(CmdQueueItem
* cmd
, const char* output
);
369 void handleSharedLibs(const char* output
);
370 void handleRegisters(const char* output
);
371 void handleMemoryDump(const char* output
);
372 void handleInfoLine(CmdQueueItem
* cmd
, const char* output
);
373 void handleDisassemble(CmdQueueItem
* cmd
, const char* output
);
374 void handleThreadList(const char* output
);
375 void handleSetPC(const char* output
);
376 void handleSetVariable(CmdQueueItem
* cmd
, const char* output
);
377 void evalExpressions();
378 void evalInitialStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
);
379 void evalStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
);
380 void dereferencePointer(ExprWnd
* wnd
, VarTree
* var
, bool immediate
);
381 void determineType(ExprWnd
* wnd
, VarTree
* var
);
382 void queueMemoryDump(bool immediate
);
383 CmdQueueItem
* loadCoreFile();
384 void openProgramConfig(const QString
& name
);
386 Breakpoint
* breakpointByFilePos(QString file
, int lineNo
,
387 const DbgAddr
& address
);
388 void newBreakpoint(CmdQueueItem
* cmd
, const char* output
);
389 void updateBreakList(const char* output
);
390 bool stopMayChangeBreakList() const;
391 void saveBreakpoints(ProgramConfig
* config
);
392 void restoreBreakpoints(ProgramConfig
* config
);
394 bool m_haveExecutable
; /* has an executable been specified */
395 bool m_programActive
; /* is the program active (possibly halting in a brkpt)? */
396 bool m_programRunning
; /* is the program executing (not stopped)? */
397 bool m_sharedLibsListed
; /* do we know the shared libraries loaded by the prog? */
398 QString m_executable
;
400 QString m_attachedPid
; /* user input of attaching to pid */
401 QString m_programArgs
;
402 QString m_remoteDevice
;
403 QString m_programWD
; /* working directory of gdb */
404 QDict
<EnvVar
> m_envVars
; /* environment variables set by user */
405 QStringList m_boolOptions
; /* boolean options */
406 QStrList m_sharedLibs
; /* shared libraries used by program */
407 ProgramTypeTable
* m_typeTable
; /* known types used by the program */
408 ProgramConfig
* m_programConfig
; /* program-specific settings (brkpts etc) */
409 void saveProgramSettings();
410 void restoreProgramSettings();
411 QString
readDebuggerCmd();
415 bool m_explicitKill
; /* whether we are killing gdb ourselves */
417 QString m_statusMessage
;
420 void gdbExited(KProcess
*);
421 void slotInferiorRunning();
422 void backgroundUpdate();
424 void slotExpanding(QListViewItem
*);
425 void slotDeleteWatch();
426 void slotValuePopup(const QString
&);
427 void slotDisassemble(const QString
&, int);
428 void slotValueEdited(VarTree
*, const QString
&);
435 * This signal is emitted before the debugger is started. The slot is
436 * supposed to set up m_inferiorTerminal.
438 void debuggerStarting();
441 * This signal is emitted whenever a part of the debugger needs to
442 * highlight the specfied source code line (e.g. when the program
445 * @param file specifies the file; this is not necessarily a full path
446 * name, and if it is relative, you won't know relative to what, you
448 * @param lineNo specifies the line number (0-based!) (this may be
449 * negative, in which case the file should be activated, but the line
450 * should NOT be changed).
451 * @param address specifies the exact address of the PC or is empty.
453 void activateFileLine(const QString
& file
, int lineNo
, const DbgAddr
& address
);
456 * This signal indicates that the program counter has changed.
458 * @param filename specifies the filename where the program stopped
459 * @param lineNo specifies the line number (zero-based); it can be -1
461 * @param address specifies the address that the instruction pointer
463 * @param frameNo specifies the frame number: 0 is the innermost frame,
464 * positive numbers are frames somewhere up the stack (indicates points
465 * where a function was called); the latter cases should be indicated
466 * differently in the source window.
468 void updatePC(const QString
& filename
, int lineNo
,
469 const DbgAddr
& address
, int frameNo
);
472 * This signal is emitted when gdb detects that the executable has been
473 * updated, e.g. recompiled. (You usually need not handle this signal
474 * if you are the editor which changed the executable.)
476 void executableUpdated();
479 * Indicates that a new status message is available.
481 void updateStatusMessage();
484 * Indicates that the internal state of the debugger has changed, and
485 * that this will very likely have an impact on the UI.
490 * Indicates that the list of breakpoints has possibly changed.
492 void breakpointsChanged();
495 * Indicates that the register values have possibly changed.
497 void registersChanged(QList
<RegisterInfo
>&);
500 * Indicates that the list of threads has possibly changed.
502 void threadsChanged(QList
<ThreadInfo
>&);
505 * Indicates that the value for a value popup is ready.
507 void valuePopup(const QString
&);
510 * Provides the disassembled code of the location given by file and
511 * line number (zero-based).
513 void disassembled(const QString
& file
, int line
, const QList
<DisassembledCode
>& code
);
516 * Indicates that the program has stopped for any reason: by a
517 * breakpoint, by a signal that the debugger driver caught, by a single
520 void programStopped();
523 * Indicates that a new memory dump output is ready.
524 * @param msg is an error message or empty
525 * @param memdump is the memory dump
527 void memoryDumpChanged(const QString
&, QList
<MemoryDump
>&);
530 * Gives other objects a chance to save program specific settings.
532 void saveProgramSpecific(KConfigBase
* config
);
535 * Gives other objects a chance to restore program specific settings.
537 void restoreProgramSpecific(KConfigBase
* config
);
540 ExprWnd
& m_localVariables
;
541 ExprWnd
& m_watchVariables
;
542 QListBox
& m_btWindow
;
544 // implementation helpers
546 QWidget
* parentWidget() { return static_cast<QWidget
*>(parent()); }
548 friend class BreakpointTable
;