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 */
22 class ProgramTypeTable
;
33 struct DisassembledCode
;
39 class KDebugger
: public QObject
43 KDebugger(QWidget
* parent
, /* will be used as the parent for dialogs */
50 * This function starts to debug the specified executable using the
51 * specified driver. If a program is currently being debugged, it is
52 * terminated first. Ownership of driver is taken if and only if
55 * @return false if an error occurs.
57 bool debugProgram(const QString
& executable
,
58 DebuggerDriver
* driver
);
61 * Uses the specified core to debug the active program.
62 * @param batch tells whether the core file was given on the
65 void useCoreFile(QString corefile
, bool batch
);
68 * Attaches to the specified process and debugs it.
70 void attachProgram(const QString
& pid
);
73 * Returns the file name of the per-program config file for the
76 static QString
getConfigForExe(const QString
& exe
);
79 * The driver name entry in the per-program config file.
81 static const char DriverNameEntry
[];
85 * Runs the program or continues it if it is stopped at a breakpoint.
90 * Restarts the debuggee.
92 void programRunAgain();
95 * Performs a single-step, possibly stepping into a function call.
96 * If byInsn is true, a step by instruction is performed.
101 * Performs a single-step, stepping over a function call.
102 * If byInsn is true, a step by instruction is performed.
107 * Performs a single-step by instruction, possibly stepping into a
113 * Performs a single-step by instruction, stepping over a function
119 * Runs the program until it returns from the current function.
121 void programFinish();
124 * Kills the program (removes it from memory).
129 * Interrupts the program if it is currently running.
134 * Moves the program counter to the specified line.
135 * If an address is given, it is moved to the address.
137 void setProgramCounter(const QString
&, int, const DbgAddr
&);
141 * Queries the user for program arguments.
143 void programArgs(QWidget
* parent
);
146 * Queries the user for program settings: Debugger command, terminal
149 void programSettings(QWidget
* parent
);
152 * Setup remote debugging device
154 void setRemoteDevice(const QString
& remoteDevice
) { m_remoteDevice
= remoteDevice
; }
157 * Run the debuggee until the specified line in the specified file is
160 * @return false if the command was not executed, e.g. because the
161 * debuggee is running at the moment.
163 bool runUntil(const QString
& fileName
, int lineNo
);
168 * @param fileName The source file in which to set the breakpoint.
169 * @param lineNo The zero-based line number.
170 * @param address The exact address of the breakpoint.
171 * @param temporary Specifies whether this is a temporary breakpoint
172 * @return false if the command was not executed, e.g. because the
173 * debuggee is running at the moment.
175 bool setBreakpoint(QString fileName
, int lineNo
,
176 const DbgAddr
& address
, bool temporary
);
181 * @param bp Describes the breakpoint.
182 * @param queueOnly If false, the breakpoint is set using a high-priority command.
184 void setBreakpoint(Breakpoint
* bp
, bool queueOnly
);
187 * Enable or disable a breakpoint at the specified location.
189 * @param fileName The source file in which the breakpoint is.
190 * @param lineNo The zero-based line number.
191 * @param address The exact address of the breakpoint.
192 * @return false if the command was not executed, e.g. because the
193 * debuggee is running at the moment.
195 bool enableDisableBreakpoint(QString fileName
, int lineNo
,
196 const DbgAddr
& address
);
199 * Enables or disables the specified breakpoint.
201 * @return false if the command was not executed, e.g. because the
202 * debuggee is running at the moment.
204 bool enableDisableBreakpoint(Breakpoint
* bp
);
207 * Removes the specified breakpoint. Note that if bp is an orphaned
208 * breakpoint, then bp is an invalid pointer if (and only if) this
209 * function returns true.
211 * @return false if the command was not executed, e.g. because the
212 * debuggee is running at the moment.
214 bool deleteBreakpoint(Breakpoint
* bp
);
217 * Changes the specified breakpoint's condition and ignore count.
219 * @return false if the command was not executed, e.g. because the
220 * debuggee is running at the moment.
222 bool conditionalBreakpoint(Breakpoint
* bp
,
223 const QString
& condition
,
227 * Tells whether one of the single stepping commands can be invoked
228 * (step, next, finish, until, also run).
230 bool canSingleStep();
233 * Tells whether a breakpoints can be set, deleted, enabled, or disabled.
235 bool canChangeBreakpoints();
238 * Tells whether the debuggee can be changed.
240 bool canUseCoreFile() { return isReady() && !m_programActive
; }
243 * Add a watch expression.
245 void addWatch(const QString
& expr
);
248 * Retrieves the current status message.
250 const QString
& statusMessage() const { return m_statusMessage
; }
253 * Is the debugger ready to receive another high-priority command?
255 bool isReady() const;
258 * Is the debuggee running (not just active)?
260 bool isProgramRunning() { return m_haveExecutable
&& m_programRunning
; }
263 * Do we have an executable set?
265 bool haveExecutable() { return m_haveExecutable
; }
268 * Is the debuggee active, i.e. was it started by the debugger?
270 bool isProgramActive() { return m_programActive
; }
273 * Is the debugger driver idle?
277 /** The list of breakpoints. */
278 int numBreakpoints() const { return m_brkpts
.size(); }
279 const Breakpoint
* breakpoint(int i
) const { return m_brkpts
[i
]; }
282 * Returns the breakpoint with the specified \a id.
284 Breakpoint
* breakpointById(int id
);
286 const QString
& executable() const { return m_executable
; }
289 * Terminal emulation level.
292 ttyNone
= 0, /* ignore output, input triggers EOF */
293 ttySimpleOutputOnly
= 1, /* minmal output emulation, input triggers EOF */
294 ttyFull
= 7 /* program needs full emulation */
298 * Returns the level of terminal emulation requested by the inferior.
300 TTYLevel
ttyLevel() const { return m_ttyLevel
; }
302 /** Sets the terminal that is to be used by the debugger. */
303 void setTerminal(const QString
& term
) { m_inferiorTerminal
= term
; }
305 /** Returns the debugger driver. */
306 DebuggerDriver
* driver() { return m_d
; }
308 /** Returns the pid that the debugger is currently attached to. */
309 const QString
& attachedPid() const { return m_attachedPid
; }
312 * The memory at that the expression evaluates to is watched. Can be
313 * empty. Triggers a redisplay even if the expression did not change.
315 void setMemoryExpression(const QString
& memexpr
);
318 * Sets how the watched memory location is displayed.
319 * Call setMemoryExpression() to force a redisplay.
321 void setMemoryFormat(unsigned format
) { m_memoryFormat
= format
; }
324 void saveSettings(KConfig
*);
325 void restoreSettings(KConfig
*);
328 QString m_inferiorTerminal
;
329 QString m_debuggerCmd
; /* per-program setting */
330 TTYLevel m_ttyLevel
; /* level of terminal emulation */
335 QList
<VarTree
> m_watchEvalExpr
; /* exprs to evaluate for watch windows */
336 QPtrVector
<Breakpoint
> m_brkpts
;
337 QString m_memoryExpression
; /* memory location to watch */
338 unsigned m_memoryFormat
; /* how that output should look */
341 void parse(CmdQueueItem
* cmd
, const char* output
);
343 VarTree
* parseExpr(const char* output
, bool wantErrorValue
);
344 void handleRunCommands(const char* output
);
345 void updateAllExprs();
346 void updateProgEnvironment(const QString
& args
, const QString
& wd
,
347 const QDict
<EnvVar
>& newVars
,
348 const QStringList
& newOptions
);
349 void parseLocals(const char* output
, QList
<VarTree
>& newVars
);
350 void handleLocals(const char* output
);
351 bool handlePrint(CmdQueueItem
* cmd
, const char* output
);
352 void handleBacktrace(const char* output
);
353 void handleFrameChange(const char* output
);
354 void handleFindType(CmdQueueItem
* cmd
, const char* output
);
355 void handlePrintStruct(CmdQueueItem
* cmd
, const char* output
);
356 void handleSharedLibs(const char* output
);
357 void handleRegisters(const char* output
);
358 void handleMemoryDump(const char* output
);
359 void handleInfoLine(CmdQueueItem
* cmd
, const char* output
);
360 void handleDisassemble(CmdQueueItem
* cmd
, const char* output
);
361 void handleThreadList(const char* output
);
362 void handleSetPC(const char* output
);
363 void evalExpressions();
364 void evalInitialStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
);
365 void evalStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
);
366 void exprExpandingHelper(ExprWnd
* wnd
, KTreeViewItem
* item
, bool& allow
);
367 void dereferencePointer(ExprWnd
* wnd
, VarTree
* var
, bool immediate
);
368 void determineType(ExprWnd
* wnd
, VarTree
* var
);
369 void removeExpr(ExprWnd
* wnd
, VarTree
* var
);
370 void queueMemoryDump(bool immediate
);
371 CmdQueueItem
* loadCoreFile();
372 void openProgramConfig(const QString
& name
);
374 Breakpoint
* breakpointByFilePos(QString file
, int lineNo
,
375 const DbgAddr
& address
);
376 void newBreakpoint(CmdQueueItem
* cmd
, const char* output
);
377 void updateBreakList(const char* output
);
378 bool stopMayChangeBreakList() const;
379 void saveBreakpoints(ProgramConfig
* config
);
380 void restoreBreakpoints(ProgramConfig
* config
);
382 bool m_haveExecutable
; /* has an executable been specified */
383 bool m_programActive
; /* is the program active (possibly halting in a brkpt)? */
384 bool m_programRunning
; /* is the program executing (not stopped)? */
385 bool m_sharedLibsListed
; /* do we know the shared libraries loaded by the prog? */
386 QString m_executable
;
388 QString m_attachedPid
; /* user input of attaching to pid */
389 QString m_programArgs
;
390 QString m_remoteDevice
;
391 QString m_programWD
; /* working directory of gdb */
392 QDict
<EnvVar
> m_envVars
; /* environment variables set by user */
393 QStringList m_boolOptions
; /* boolean options */
394 QStrList m_sharedLibs
; /* shared libraries used by program */
395 ProgramTypeTable
* m_typeTable
; /* known types used by the program */
396 ProgramConfig
* m_programConfig
; /* program-specific settings (brkpts etc) */
397 void saveProgramSettings();
398 void restoreProgramSettings();
399 QString
readDebuggerCmd();
403 bool m_explicitKill
; /* whether we are killing gdb ourselves */
405 QString m_statusMessage
;
408 void gdbExited(KProcess
*);
409 void slotInferiorRunning();
410 void backgroundUpdate();
412 void slotLocalsExpanding(KTreeViewItem
*, bool&);
413 void slotWatchExpanding(KTreeViewItem
*, bool&);
414 void slotUpdateAnimation();
415 void slotDeleteWatch();
416 void slotValuePopup(const QString
&);
417 void slotDisassemble(const QString
&, int);
424 * This signal is emitted before the debugger is started. The slot is
425 * supposed to set up m_inferiorTerminal.
427 void debuggerStarting();
430 * This signal is emitted whenever a part of the debugger needs to
431 * highlight the specfied source code line (e.g. when the program
434 * @param file specifies the file; this is not necessarily a full path
435 * name, and if it is relative, you won't know relative to what, you
437 * @param lineNo specifies the line number (0-based!) (this may be
438 * negative, in which case the file should be activated, but the line
439 * should NOT be changed).
440 * @param address specifies the exact address of the PC or is empty.
442 void activateFileLine(const QString
& file
, int lineNo
, const DbgAddr
& address
);
445 * This signal indicates that the program counter has changed.
447 * @param filename specifies the filename where the program stopped
448 * @param lineNo specifies the line number (zero-based); it can be -1
450 * @param address specifies the address that the instruction pointer
452 * @param frameNo specifies the frame number: 0 is the innermost frame,
453 * positive numbers are frames somewhere up the stack (indicates points
454 * where a function was called); the latter cases should be indicated
455 * differently in the source window.
457 void updatePC(const QString
& filename
, int lineNo
,
458 const DbgAddr
& address
, int frameNo
);
461 * This signal is emitted when gdb detects that the executable has been
462 * updated, e.g. recompiled. (You usually need not handle this signal
463 * if you are the editor which changed the executable.)
465 void executableUpdated();
468 * This signal is emitted when the animated icon should advance to the
471 void animationTimeout();
474 * Indicates that a new status message is available.
476 void updateStatusMessage();
479 * Indicates that the internal state of the debugger has changed, and
480 * that this will very likely have an impact on the UI.
485 * Indicates that the list of breakpoints has possibly changed.
487 void breakpointsChanged();
490 * Indicates that the register values have possibly changed.
492 void registersChanged(QList
<RegisterInfo
>&);
495 * Indicates that the list of threads has possibly changed.
497 void threadsChanged(QList
<ThreadInfo
>&);
500 * Indicates that the value for a value popup is ready.
502 void valuePopup(const QString
&);
505 * Provides the disassembled code of the location given by file and
506 * line number (zero-based).
508 void disassembled(const QString
& file
, int line
, const QList
<DisassembledCode
>& code
);
511 * Indicates that the program has stopped for any reason: by a
512 * breakpoint, by a signal that the debugger driver caught, by a single
515 void programStopped();
518 * Indicates that a new memory dump output is ready.
519 * @param msg is an error message or empty
520 * @param memdump is the memory dump
522 void memoryDumpChanged(const QString
&, QList
<MemoryDump
>&);
525 * Gives other objects a chance to save program specific settings.
527 void saveProgramSpecific(KConfigBase
* config
);
530 * Gives other objects a chance to restore program specific settings.
532 void restoreProgramSpecific(KConfigBase
* config
);
535 ExprWnd
& m_localVariables
;
536 ExprWnd
& m_watchVariables
;
537 QListBox
& m_btWindow
;
540 QTimer m_animationTimer
;
541 int m_animationInterval
;
543 // implementation helpers
545 void startAnimation(bool fast
);
546 void stopAnimation();
548 QWidget
* parentWidget() { return static_cast<QWidget
*>(parent()); }
550 friend class BreakpointTable
;