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 * Uses the specified pid to attach to the active program.
70 void setAttachPid(const QString
& pid
);
73 * Attaches to the specified process and debugs it.
75 void attachProgram(const QString
& pid
);
78 * Returns the file name of the per-program config file for the
81 static QString
getConfigForExe(const QString
& exe
);
84 * The driver name entry in the per-program config file.
86 static const char DriverNameEntry
[];
90 * Runs the program or continues it if it is stopped at a breakpoint.
95 * Restarts the debuggee.
97 void programRunAgain();
100 * Performs a single-step, possibly stepping into a function call.
101 * If byInsn is true, a step by instruction is performed.
106 * Performs a single-step, stepping over a function call.
107 * If byInsn is true, a step by instruction is performed.
112 * Performs a single-step by instruction, possibly stepping into a
118 * Performs a single-step by instruction, stepping over a function
124 * Runs the program until it returns from the current function.
126 void programFinish();
129 * Kills the program (removes it from memory).
134 * Interrupts the program if it is currently running.
139 * Moves the program counter to the specified line.
140 * If an address is given, it is moved to the address.
142 void setProgramCounter(const QString
&, int, const DbgAddr
&);
146 * Queries the user for program arguments.
148 void programArgs(QWidget
* parent
);
151 * Queries the user for program settings: Debugger command, terminal
154 void programSettings(QWidget
* parent
);
157 * Setup remote debugging device
159 void setRemoteDevice(const QString
& remoteDevice
) { m_remoteDevice
= remoteDevice
; }
162 * Run the debuggee until the specified line in the specified file is
165 * @return false if the command was not executed, e.g. because the
166 * debuggee is running at the moment.
168 bool runUntil(const QString
& fileName
, int lineNo
);
173 * @param fileName The source file in which to set the breakpoint.
174 * @param lineNo The zero-based line number.
175 * @param address The exact address of the breakpoint.
176 * @param temporary Specifies whether this is a temporary breakpoint
177 * @return false if the command was not executed, e.g. because the
178 * debuggee is running at the moment.
180 bool setBreakpoint(QString fileName
, int lineNo
,
181 const DbgAddr
& address
, bool temporary
);
186 * @param bp Describes the breakpoint.
187 * @param queueOnly If false, the breakpoint is set using a high-priority command.
189 void setBreakpoint(Breakpoint
* bp
, bool queueOnly
);
192 * Enable or disable a breakpoint at the specified location.
194 * @param fileName The source file in which the breakpoint is.
195 * @param lineNo The zero-based line number.
196 * @param address The exact address of the breakpoint.
197 * @return false if the command was not executed, e.g. because the
198 * debuggee is running at the moment.
200 bool enableDisableBreakpoint(QString fileName
, int lineNo
,
201 const DbgAddr
& address
);
204 * Enables or disables the specified breakpoint.
206 * @return false if the command was not executed, e.g. because the
207 * debuggee is running at the moment.
209 bool enableDisableBreakpoint(Breakpoint
* bp
);
212 * Removes the specified breakpoint. Note that if bp is an orphaned
213 * breakpoint, then bp is an invalid pointer if (and only if) this
214 * function returns true.
216 * @return false if the command was not executed, e.g. because the
217 * debuggee is running at the moment.
219 bool deleteBreakpoint(Breakpoint
* bp
);
222 * Changes the specified breakpoint's condition and ignore count.
224 * @return false if the command was not executed, e.g. because the
225 * debuggee is running at the moment.
227 bool conditionalBreakpoint(Breakpoint
* bp
,
228 const QString
& condition
,
232 * Tells whether one of the single stepping commands can be invoked
233 * (step, next, finish, until, also run).
235 bool canSingleStep();
238 * Tells whether a breakpoints can be set, deleted, enabled, or disabled.
240 bool canChangeBreakpoints();
243 * Tells whether the debuggee can be changed.
245 bool canUseCoreFile() { return isReady() && !m_programActive
; }
248 * Add a watch expression.
250 void addWatch(const QString
& expr
);
253 * Retrieves the current status message.
255 const QString
& statusMessage() const { return m_statusMessage
; }
258 * Is the debugger ready to receive another high-priority command?
260 bool isReady() const;
263 * Is the debuggee running (not just active)?
265 bool isProgramRunning() { return m_haveExecutable
&& m_programRunning
; }
268 * Do we have an executable set?
270 bool haveExecutable() { return m_haveExecutable
; }
273 * Is the debuggee active, i.e. was it started by the debugger?
275 bool isProgramActive() { return m_programActive
; }
278 * Is the debugger driver idle?
282 /** The list of breakpoints. */
283 int numBreakpoints() const { return m_brkpts
.size(); }
284 const Breakpoint
* breakpoint(int i
) const { return m_brkpts
[i
]; }
287 * Returns the breakpoint with the specified \a id.
289 Breakpoint
* breakpointById(int id
);
291 const QString
& executable() const { return m_executable
; }
294 * Terminal emulation level.
297 ttyNone
= 0, /* ignore output, input triggers EOF */
298 ttySimpleOutputOnly
= 1, /* minmal output emulation, input triggers EOF */
299 ttyFull
= 7 /* program needs full emulation */
303 * Returns the level of terminal emulation requested by the inferior.
305 TTYLevel
ttyLevel() const { return m_ttyLevel
; }
307 /** Sets the terminal that is to be used by the debugger. */
308 void setTerminal(const QString
& term
) { m_inferiorTerminal
= term
; }
310 /** Returns the debugger driver. */
311 DebuggerDriver
* driver() { return m_d
; }
313 /** Returns the pid that the debugger is currently attached to. */
314 const QString
& attachedPid() const { return m_attachedPid
; }
317 * The memory at that the expression evaluates to is watched. Can be
318 * empty. Triggers a redisplay even if the expression did not change.
320 void setMemoryExpression(const QString
& memexpr
);
323 * Sets how the watched memory location is displayed.
324 * Call setMemoryExpression() to force a redisplay.
326 void setMemoryFormat(unsigned format
) { m_memoryFormat
= format
; }
329 void saveSettings(KConfig
*);
330 void restoreSettings(KConfig
*);
333 QString m_inferiorTerminal
;
334 QString m_debuggerCmd
; /* per-program setting */
335 TTYLevel m_ttyLevel
; /* level of terminal emulation */
340 QList
<VarTree
> m_watchEvalExpr
; /* exprs to evaluate for watch windows */
341 QPtrVector
<Breakpoint
> m_brkpts
;
342 QString m_memoryExpression
; /* memory location to watch */
343 unsigned m_memoryFormat
; /* how that output should look */
346 void parse(CmdQueueItem
* cmd
, const char* output
);
348 VarTree
* parseExpr(const char* output
, bool wantErrorValue
);
349 void handleRunCommands(const char* output
);
350 void updateAllExprs();
351 void updateProgEnvironment(const QString
& args
, const QString
& wd
,
352 const QDict
<EnvVar
>& newVars
,
353 const QStringList
& newOptions
);
354 void parseLocals(const char* output
, QList
<VarTree
>& newVars
);
355 void handleLocals(const char* output
);
356 bool handlePrint(CmdQueueItem
* cmd
, const char* output
);
357 bool handlePrintDeref(CmdQueueItem
* cmd
, const char* output
);
358 void handleBacktrace(const char* output
);
359 void handleFrameChange(const char* output
);
360 void handleFindType(CmdQueueItem
* cmd
, const char* output
);
361 void handlePrintStruct(CmdQueueItem
* cmd
, const char* output
);
362 void handleSharedLibs(const char* output
);
363 void handleRegisters(const char* output
);
364 void handleMemoryDump(const char* output
);
365 void handleInfoLine(CmdQueueItem
* cmd
, const char* output
);
366 void handleDisassemble(CmdQueueItem
* cmd
, const char* output
);
367 void handleThreadList(const char* output
);
368 void handleSetPC(const char* output
);
369 void handleSetVariable(CmdQueueItem
* cmd
, const char* output
);
370 void evalExpressions();
371 void evalInitialStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
);
372 void evalStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
);
373 void exprExpandingHelper(ExprWnd
* wnd
, KTreeViewItem
* item
, bool& allow
);
374 void dereferencePointer(ExprWnd
* wnd
, VarTree
* var
, bool immediate
);
375 void determineType(ExprWnd
* wnd
, VarTree
* var
);
376 void removeExpr(ExprWnd
* wnd
, VarTree
* var
);
377 void queueMemoryDump(bool immediate
);
378 CmdQueueItem
* loadCoreFile();
379 void openProgramConfig(const QString
& name
);
381 Breakpoint
* breakpointByFilePos(QString file
, int lineNo
,
382 const DbgAddr
& address
);
383 void newBreakpoint(CmdQueueItem
* cmd
, const char* output
);
384 void updateBreakList(const char* output
);
385 bool stopMayChangeBreakList() const;
386 void saveBreakpoints(ProgramConfig
* config
);
387 void restoreBreakpoints(ProgramConfig
* config
);
389 bool m_haveExecutable
; /* has an executable been specified */
390 bool m_programActive
; /* is the program active (possibly halting in a brkpt)? */
391 bool m_programRunning
; /* is the program executing (not stopped)? */
392 bool m_sharedLibsListed
; /* do we know the shared libraries loaded by the prog? */
393 QString m_executable
;
395 QString m_attachedPid
; /* user input of attaching to pid */
396 QString m_programArgs
;
397 QString m_remoteDevice
;
398 QString m_programWD
; /* working directory of gdb */
399 QDict
<EnvVar
> m_envVars
; /* environment variables set by user */
400 QStringList m_boolOptions
; /* boolean options */
401 QStrList m_sharedLibs
; /* shared libraries used by program */
402 ProgramTypeTable
* m_typeTable
; /* known types used by the program */
403 ProgramConfig
* m_programConfig
; /* program-specific settings (brkpts etc) */
404 void saveProgramSettings();
405 void restoreProgramSettings();
406 QString
readDebuggerCmd();
410 bool m_explicitKill
; /* whether we are killing gdb ourselves */
412 QString m_statusMessage
;
415 void gdbExited(KProcess
*);
416 void slotInferiorRunning();
417 void backgroundUpdate();
419 void slotLocalsExpanding(KTreeViewItem
*, bool&);
420 void slotWatchExpanding(KTreeViewItem
*, bool&);
421 void slotDeleteWatch();
422 void slotValuePopup(const QString
&);
423 void slotDisassemble(const QString
&, int);
424 void slotValueEdited(int, const QString
&);
431 * This signal is emitted before the debugger is started. The slot is
432 * supposed to set up m_inferiorTerminal.
434 void debuggerStarting();
437 * This signal is emitted whenever a part of the debugger needs to
438 * highlight the specfied source code line (e.g. when the program
441 * @param file specifies the file; this is not necessarily a full path
442 * name, and if it is relative, you won't know relative to what, you
444 * @param lineNo specifies the line number (0-based!) (this may be
445 * negative, in which case the file should be activated, but the line
446 * should NOT be changed).
447 * @param address specifies the exact address of the PC or is empty.
449 void activateFileLine(const QString
& file
, int lineNo
, const DbgAddr
& address
);
452 * This signal indicates that the program counter has changed.
454 * @param filename specifies the filename where the program stopped
455 * @param lineNo specifies the line number (zero-based); it can be -1
457 * @param address specifies the address that the instruction pointer
459 * @param frameNo specifies the frame number: 0 is the innermost frame,
460 * positive numbers are frames somewhere up the stack (indicates points
461 * where a function was called); the latter cases should be indicated
462 * differently in the source window.
464 void updatePC(const QString
& filename
, int lineNo
,
465 const DbgAddr
& address
, int frameNo
);
468 * This signal is emitted when gdb detects that the executable has been
469 * updated, e.g. recompiled. (You usually need not handle this signal
470 * if you are the editor which changed the executable.)
472 void executableUpdated();
475 * Indicates that a new status message is available.
477 void updateStatusMessage();
480 * Indicates that the internal state of the debugger has changed, and
481 * that this will very likely have an impact on the UI.
486 * Indicates that the list of breakpoints has possibly changed.
488 void breakpointsChanged();
491 * Indicates that the register values have possibly changed.
493 void registersChanged(QList
<RegisterInfo
>&);
496 * Indicates that the list of threads has possibly changed.
498 void threadsChanged(QList
<ThreadInfo
>&);
501 * Indicates that the value for a value popup is ready.
503 void valuePopup(const QString
&);
506 * Provides the disassembled code of the location given by file and
507 * line number (zero-based).
509 void disassembled(const QString
& file
, int line
, const QList
<DisassembledCode
>& code
);
512 * Indicates that the program has stopped for any reason: by a
513 * breakpoint, by a signal that the debugger driver caught, by a single
516 void programStopped();
519 * Indicates that a new memory dump output is ready.
520 * @param msg is an error message or empty
521 * @param memdump is the memory dump
523 void memoryDumpChanged(const QString
&, QList
<MemoryDump
>&);
526 * Gives other objects a chance to save program specific settings.
528 void saveProgramSpecific(KConfigBase
* config
);
531 * Gives other objects a chance to restore program specific settings.
533 void restoreProgramSpecific(KConfigBase
* config
);
536 ExprWnd
& m_localVariables
;
537 ExprWnd
& m_watchVariables
;
538 QListBox
& m_btWindow
;
540 // implementation helpers
542 QWidget
* parentWidget() { return static_cast<QWidget
*>(parent()); }
544 friend class BreakpointTable
;