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
;
32 struct DisassembledCode
;
38 class KDebugger
: public QObject
42 KDebugger(QWidget
* parent
, /* will be used as the parent for dialogs */
49 * This function starts to debug the specified executable using the
50 * specified driver. If a program is currently being debugged, it is
51 * terminated first. Ownership of driver is taken if and only if
54 * @return false if an error occurs.
56 bool debugProgram(const QString
& executable
,
57 DebuggerDriver
* driver
);
60 * Uses the specified core to debug the active program.
61 * @param batch tells whether the core file was given on the
64 void useCoreFile(QString corefile
, bool batch
);
67 * Attaches to the specified process and debugs it.
69 void attachProgram(const QString
& pid
);
72 * Returns the file name of the per-program config file for the
75 static QString
getConfigForExe(const QString
& exe
);
78 * The driver name entry in the per-program config file.
80 static const char DriverNameEntry
[];
84 * Runs the program or continues it if it is stopped at a breakpoint.
89 * Restarts the debuggee.
91 void programRunAgain();
94 * Performs a single-step, possibly stepping into a function call.
95 * If byInsn is true, a step by instruction is performed.
100 * Performs a single-step, stepping over a function call.
101 * If byInsn is true, a step by instruction is performed.
106 * Performs a single-step by instruction, possibly stepping into a
112 * Performs a single-step by instruction, stepping over a function
118 * Runs the program until it returns from the current function.
120 void programFinish();
123 * Kills the program (removes it from memory).
128 * Interrupts the program if it is currently running.
133 * Moves the program counter to the specified line.
134 * If an address is given, it is moved to the address.
136 void setProgramCounter(const QString
&, int, const DbgAddr
&);
140 * Queries the user for program arguments.
142 void programArgs(QWidget
* parent
);
145 * Queries the user for program settings: Debugger command, terminal
148 void programSettings(QWidget
* parent
);
151 * Setup remote debugging device
153 void setRemoteDevice(const QString
& remoteDevice
) { m_remoteDevice
= remoteDevice
; }
156 * Run the debuggee until the specified line in the specified file is
159 * @return false if the command was not executed, e.g. because the
160 * debuggee is running at the moment.
162 bool runUntil(const QString
& fileName
, int lineNo
);
167 * @param fileName The source file in which to set the breakpoint.
168 * @param lineNo The zero-based line number.
169 * @param address The exact address of the breakpoint.
170 * @param temporary Specifies whether this is a temporary breakpoint
171 * @return false if the command was not executed, e.g. because the
172 * debuggee is running at the moment.
174 bool setBreakpoint(QString fileName
, int lineNo
,
175 const DbgAddr
& address
, bool temporary
);
180 * @param bp Describes the breakpoint.
182 void setBreakpoint(Breakpoint
* bp
);
185 * Enable or disable a breakpoint at the specified location.
187 * @param fileName The source file in which the breakpoint is.
188 * @param lineNo The zero-based line number.
189 * @param address The exact address of the breakpoint.
190 * @return false if the command was not executed, e.g. because the
191 * debuggee is running at the moment.
193 bool enableDisableBreakpoint(QString fileName
, int lineNo
,
194 const DbgAddr
& address
);
197 * Enables or disables the specified breakpoint.
199 * @return false if the command was not executed, e.g. because the
200 * debuggee is running at the moment.
202 bool enableDisableBreakpoint(Breakpoint
* bp
);
205 * Removes the specified breakpoint.
207 * @return false if the command was not executed, e.g. because the
208 * debuggee is running at the moment.
210 bool deleteBreakpoint(Breakpoint
* bp
);
213 * Changes the specified breakpoint's condition and ignore count.
215 * @return false if the command was not executed, e.g. because the
216 * debuggee is running at the moment.
218 bool conditionalBreakpoint(Breakpoint
* bp
,
219 const QString
& condition
,
223 * Tells whether one of the single stepping commands can be invoked
224 * (step, next, finish, until, also run).
226 bool canSingleStep();
229 * Tells whether a breakpoints can be set, deleted, enabled, or disabled.
231 bool canChangeBreakpoints();
234 * Tells whether the debuggee can be changed.
236 bool canUseCoreFile() { return isReady() && !m_programActive
; }
239 * Add a watch expression.
241 void addWatch(const QString
& expr
);
244 * Retrieves the current status message.
246 const QString
& statusMessage() const { return m_statusMessage
; }
249 * Is the debugger ready to receive another high-priority command?
251 bool isReady() const;
254 * Is the debuggee running (not just active)?
256 bool isProgramRunning() { return m_haveExecutable
&& m_programRunning
; }
259 * Do we have an executable set?
261 bool haveExecutable() { return m_haveExecutable
; }
264 * Is the debuggee active, i.e. was it started by the debugger?
266 bool isProgramActive() { return m_programActive
; }
269 * Is the debugger driver idle?
273 /** The list of breakpoints. */
274 int numBreakpoints() const { return m_brkpts
.size(); }
275 const Breakpoint
* breakpoint(int i
) const { return m_brkpts
[i
]; }
278 * Returns the breakpoint with the specified \a id.
280 Breakpoint
* breakpointById(int id
);
282 const QString
& executable() const { return m_executable
; }
285 * Terminal emulation level.
288 ttyNone
= 0, /* ignore output, input triggers EOF */
289 ttySimpleOutputOnly
= 1, /* minmal output emulation, input triggers EOF */
290 ttyFull
= 7 /* program needs full emulation */
294 * Returns the level of terminal emulation requested by the inferior.
296 TTYLevel
ttyLevel() const { return m_ttyLevel
; }
298 /** Sets the terminal that is to be used by the debugger. */
299 void setTerminal(const QString
& term
) { m_inferiorTerminal
= term
; }
301 /** Returns the debugger driver. */
302 DebuggerDriver
* driver() { return m_d
; }
304 /** Returns the pid that the debugger is currently attached to. */
305 const QString
& attachedPid() const { return m_attachedPid
; }
308 * The memory at that the expression evaluates to is watched. Can be
309 * empty. Triggers a redisplay even if the expression did not change.
311 void setMemoryExpression(const QString
& memexpr
);
314 * Sets how the watched memory location is displayed.
315 * Call setMemoryExpression() to force a redisplay.
317 void setMemoryFormat(unsigned format
) { m_memoryFormat
= format
; }
320 void saveSettings(KConfig
*);
321 void restoreSettings(KConfig
*);
324 QString m_inferiorTerminal
;
325 QString m_debuggerCmd
; /* per-program setting */
326 TTYLevel m_ttyLevel
; /* level of terminal emulation */
331 QList
<VarTree
> m_watchEvalExpr
; /* exprs to evaluate for watch windows */
332 QPtrVector
<Breakpoint
> m_brkpts
;
333 QString m_memoryExpression
; /* memory location to watch */
334 unsigned m_memoryFormat
; /* how that output should look */
337 void parse(CmdQueueItem
* cmd
, const char* output
);
339 VarTree
* parseExpr(const char* output
, bool wantErrorValue
);
340 void handleRunCommands(const char* output
);
341 void updateAllExprs();
342 void updateProgEnvironment(const QString
& args
, const QString
& wd
,
343 const QDict
<EnvVar
>& newVars
,
344 const QStringList
& newOptions
);
345 void parseLocals(const char* output
, QList
<VarTree
>& newVars
);
346 void handleLocals(const char* output
);
347 bool handlePrint(CmdQueueItem
* cmd
, const char* output
);
348 void handleBacktrace(const char* output
);
349 void handleFrameChange(const char* output
);
350 void handleFindType(CmdQueueItem
* cmd
, const char* output
);
351 void handlePrintStruct(CmdQueueItem
* cmd
, const char* output
);
352 void handleSharedLibs(const char* output
);
353 void handleRegisters(const char* output
);
354 void handleMemoryDump(const char* output
);
355 void handleInfoLine(CmdQueueItem
* cmd
, const char* output
);
356 void handleDisassemble(CmdQueueItem
* cmd
, const char* output
);
357 void handleThreadList(const char* output
);
358 void handleSetPC(const char* output
);
359 void evalExpressions();
360 void evalInitialStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
);
361 void evalStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
);
362 void exprExpandingHelper(ExprWnd
* wnd
, KTreeViewItem
* item
, bool& allow
);
363 void dereferencePointer(ExprWnd
* wnd
, VarTree
* var
, bool immediate
);
364 void determineType(ExprWnd
* wnd
, VarTree
* var
);
365 void removeExpr(ExprWnd
* wnd
, VarTree
* var
);
366 void queueMemoryDump(bool immediate
);
367 CmdQueueItem
* loadCoreFile();
368 void openProgramConfig(const QString
& name
);
370 Breakpoint
* breakpointByFilePos(QString file
, int lineNo
,
371 const DbgAddr
& address
);
372 void newBreakpoint(CmdQueueItem
* cmd
, const char* output
);
373 void updateBreakList(const char* output
);
374 bool stopMayChangeBreakList() const;
375 void saveBreakpoints(KSimpleConfig
* config
);
376 void restoreBreakpoints(KSimpleConfig
* config
);
378 bool m_haveExecutable
; /* has an executable been specified */
379 bool m_programActive
; /* is the program active (possibly halting in a brkpt)? */
380 bool m_programRunning
; /* is the program executing (not stopped)? */
381 bool m_sharedLibsListed
; /* do we know the shared libraries loaded by the prog? */
382 QString m_executable
;
384 QString m_attachedPid
; /* user input of attaching to pid */
385 QString m_programArgs
;
386 QString m_remoteDevice
;
387 QString m_programWD
; /* working directory of gdb */
388 QDict
<EnvVar
> m_envVars
; /* environment variables set by user */
389 QStringList m_boolOptions
; /* boolean options */
390 QStrList m_sharedLibs
; /* shared libraries used by program */
391 ProgramTypeTable
* m_typeTable
; /* known types used by the program */
392 KSimpleConfig
* m_programConfig
; /* program-specific settings (brkpts etc) */
393 void saveProgramSettings();
394 void restoreProgramSettings();
398 bool m_explicitKill
; /* whether we are killing gdb ourselves */
400 QString m_statusMessage
;
403 void gdbExited(KProcess
*);
404 void slotInferiorRunning();
405 void backgroundUpdate();
407 void slotLocalsExpanding(KTreeViewItem
*, bool&);
408 void slotWatchExpanding(KTreeViewItem
*, bool&);
409 void slotUpdateAnimation();
410 void slotDeleteWatch();
411 void slotValuePopup(const QString
&);
412 void slotDisassemble(const QString
&, int);
419 * This signal is emitted before the debugger is started. The slot is
420 * supposed to set up m_inferiorTerminal.
422 void debuggerStarting();
425 * This signal is emitted whenever a part of the debugger needs to
426 * highlight the specfied source code line (e.g. when the program
429 * @param file specifies the file; this is not necessarily a full path
430 * name, and if it is relative, you won't know relative to what, you
432 * @param lineNo specifies the line number (0-based!) (this may be
433 * negative, in which case the file should be activated, but the line
434 * should NOT be changed).
435 * @param address specifies the exact address of the PC or is empty.
437 void activateFileLine(const QString
& file
, int lineNo
, const DbgAddr
& address
);
440 * This signal indicates that the program counter has changed.
442 * @param filename specifies the filename where the program stopped
443 * @param lineNo specifies the line number (zero-based); it can be -1
445 * @param address specifies the address that the instruction pointer
447 * @param frameNo specifies the frame number: 0 is the innermost frame,
448 * positive numbers are frames somewhere up the stack (indicates points
449 * where a function was called); the latter cases should be indicated
450 * differently in the source window.
452 void updatePC(const QString
& filename
, int lineNo
,
453 const DbgAddr
& address
, int frameNo
);
456 * This signal is emitted when gdb detects that the executable has been
457 * updated, e.g. recompiled. (You usually need not handle this signal
458 * if you are the editor which changed the executable.)
460 void executableUpdated();
463 * This signal is emitted when the animated icon should advance to the
466 void animationTimeout();
469 * Indicates that a new status message is available.
471 void updateStatusMessage();
474 * Indicates that the internal state of the debugger has changed, and
475 * that this will very likely have an impact on the UI.
480 * Indicates that the list of breakpoints has possibly changed.
482 void breakpointsChanged();
485 * Indicates that the register values have possibly changed.
487 void registersChanged(QList
<RegisterInfo
>&);
490 * Indicates that the list of threads has possibly changed.
492 void threadsChanged(QList
<ThreadInfo
>&);
495 * Indicates that the value for a value popup is ready.
497 void valuePopup(const QString
&);
500 * Provides the disassembled code of the location given by file and
501 * line number (zero-based).
503 void disassembled(const QString
& file
, int line
, const QList
<DisassembledCode
>& code
);
506 * Indicates that the program has stopped for any reason: by a
507 * breakpoint, by a signal that the debugger driver caught, by a single
510 void programStopped();
513 * Indicates that a new memory dump output is ready.
514 * @param msg is an error message or empty
515 * @param memdump is the memory dump
517 void memoryDumpChanged(const QString
&, QList
<MemoryDump
>&);
520 * Gives other objects a chance to save program specific settings.
522 void saveProgramSpecific(KSimpleConfig
* config
);
525 * Gives other objects a chance to restore program specific settings.
527 void restoreProgramSpecific(KSimpleConfig
* config
);
530 ExprWnd
& m_localVariables
;
531 ExprWnd
& m_watchVariables
;
532 QListBox
& m_btWindow
;
535 QTimer m_animationTimer
;
536 int m_animationInterval
;
538 // implementation helpers
540 void startAnimation(bool fast
);
541 void stopAnimation();
543 QWidget
* parentWidget() { return static_cast<QWidget
*>(parent()); }
545 friend class BreakpointTable
;