Update backtrace window from deprecated Q3ListBox widget to QListWidget.
[kdbg.git] / kdbg / debugger.h
blob9bf4b5ae1034057c0f6073dcd3b88e357e9cc92d
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #ifndef DEBUGGER_H
8 #define DEBUGGER_H
10 #include <Q3Dict>
11 #include <QStringList>
12 #include "envvar.h"
13 #include "exprwnd.h" /* some compilers require this */
15 class ExprWnd;
16 class VarTree;
17 struct ExprValue;
18 class ProgramTypeTable;
19 class KTreeViewItem;
20 class KConfig;
21 class KConfigBase;
22 class KConfigGroup;
23 class QListWidget;
24 class RegisterInfo;
25 class ThreadInfo;
26 class DebuggerDriver;
27 class CmdQueueItem;
28 class Breakpoint;
29 struct DisassembledCode;
30 struct MemoryDump;
31 struct DbgAddr;
32 class K3Process;
35 class KDebugger : public QObject
37 Q_OBJECT
38 public:
39 KDebugger(QWidget* parent, /* will be used as the parent for dialogs */
40 ExprWnd* localVars,
41 ExprWnd* watchVars,
42 QListWidget* backtrace);
43 ~KDebugger();
45 /**
46 * This function starts to debug the specified executable using the
47 * specified driver. If a program is currently being debugged, it is
48 * terminated first. Ownership of driver is taken if and only if
49 * true is returned.
51 * @return false if an error occurs.
53 bool debugProgram(const QString& executable,
54 DebuggerDriver* driver);
56 /**
57 * Uses the specified core to debug the active program.
58 * @param batch tells whether the core file was given on the
59 * command line.
61 void useCoreFile(QString corefile, bool batch);
63 /**
64 * Overrides the program argument in the per-program config
65 * with a new value.
67 void overrideProgramArguments(const QString& args);
70 /**
71 * Uses the specified pid to attach to the active program.
73 void setAttachPid(const QString& pid);
75 /**
76 * Attaches to the specified process and debugs it.
78 void attachProgram(const QString& pid);
80 /**
81 * Returns the file name of the per-program config file for the
82 * specified program.
84 static QString getConfigForExe(const QString& exe);
86 /**
87 * The driver name entry in the per-program config file.
89 static const char DriverNameEntry[];
91 public slots:
92 /**
93 * Runs the program or continues it if it is stopped at a breakpoint.
95 void programRun();
97 /**
98 * Restarts the debuggee.
100 void programRunAgain();
103 * Performs a single-step, possibly stepping into a function call.
104 * If byInsn is true, a step by instruction is performed.
106 void programStep();
109 * Performs a single-step, stepping over a function call.
110 * If byInsn is true, a step by instruction is performed.
112 void programNext();
115 * Performs a single-step by instruction, possibly stepping into a
116 * function call.
118 void programStepi();
121 * Performs a single-step by instruction, stepping over a function
122 * call.
124 void programNexti();
127 * Runs the program until it returns from the current function.
129 void programFinish();
132 * Kills the program (removes it from memory).
134 void programKill();
137 * Interrupts the program if it is currently running.
139 void programBreak();
142 * Moves the program counter to the specified line.
143 * If an address is given, it is moved to the address.
145 void setProgramCounter(const QString&, int, const DbgAddr&);
147 public:
149 * Queries the user for program arguments.
151 void programArgs(QWidget* parent);
154 * Queries the user for program settings: Debugger command, terminal
155 * emulator.
157 void programSettings(QWidget* parent);
160 * Setup remote debugging device
162 void setRemoteDevice(const QString& remoteDevice) { m_remoteDevice = remoteDevice; }
165 * Run the debuggee until the specified line in the specified file is
166 * reached.
168 * @return false if the command was not executed, e.g. because the
169 * debuggee is running at the moment.
171 bool runUntil(const QString& fileName, int lineNo);
174 * Ask debugger for information about the specified line in the specified file.
176 * If \a addr is given, then it is used as a hint to position
177 * to position the cursor in the source window at that address
178 * if it belongs to the specified line.
179 * @param fileName The source file in which to set the breakpoint.
180 * @param lineNo The zero-based line number.
181 * @param addr An address that belongs to the name; can be empty.
182 * @return false if the command was not executed, e.g. because the
183 * debuggee is running at the moment.
185 bool infoLine(QString fileName, int lineNo, const DbgAddr& addr);
187 * Set a breakpoint.
189 * @param fileName The source file in which to set the breakpoint.
190 * @param lineNo The zero-based line number.
191 * @param address The exact address of the breakpoint.
192 * @param temporary Specifies whether this is a temporary breakpoint
193 * @return false if the command was not executed, e.g. because the
194 * debuggee is running at the moment.
196 bool setBreakpoint(QString fileName, int lineNo,
197 const DbgAddr& address, bool temporary);
200 * Set a breakpoint.
202 * @param bp Describes the breakpoint.
203 * @param queueOnly If false, the breakpoint is set using a high-priority command.
205 void setBreakpoint(Breakpoint* bp, bool queueOnly);
208 * Enable or disable a breakpoint at the specified location.
210 * @param fileName The source file in which the breakpoint is.
211 * @param lineNo The zero-based line number.
212 * @param address The exact address of the breakpoint.
213 * @return false if the command was not executed, e.g. because the
214 * debuggee is running at the moment.
216 bool enableDisableBreakpoint(QString fileName, int lineNo,
217 const DbgAddr& address);
220 * Enables or disables the specified breakpoint.
222 * @return false if the command was not executed, e.g. because the
223 * debuggee is running at the moment.
225 bool enableDisableBreakpoint(int id)
226 { return enableDisableBreakpoint(breakpointById(id)); }
229 * Removes the specified breakpoint. Note that if bp is an orphaned
230 * breakpoint, then bp is an invalid pointer if (and only if) this
231 * function returns true.
233 * @return false if the command was not executed, e.g. because the
234 * debuggee is running at the moment.
236 bool deleteBreakpoint(int id)
237 { return deleteBreakpoint(breakpointById(id)); }
240 * Changes the specified breakpoint's condition and ignore count.
242 * @return false if the command was not executed, e.g. because the
243 * debuggee is running at the moment.
245 bool conditionalBreakpoint(int id,
246 const QString& condition,
247 int ignoreCount)
248 { return conditionalBreakpoint(breakpointById(id), condition, ignoreCount); }
251 * Tells whether one of the single stepping commands can be invoked
252 * (step, next, finish, until, also run).
254 bool canSingleStep();
257 * Tells whether a breakpoints can be set, deleted, enabled, or disabled.
259 bool canChangeBreakpoints();
262 * Tells whether a the program is loaded, but not active.
264 bool canStart();
267 * Add a watch expression.
269 void addWatch(const QString& expr);
272 * Retrieves the current status message.
274 const QString& statusMessage() const { return m_statusMessage; }
277 * Is the debugger ready to receive another high-priority command?
279 bool isReady() const;
282 * Is the debuggee running (not just active)?
284 bool isProgramRunning() { return m_haveExecutable && m_programRunning; }
287 * Do we have an executable set?
289 bool haveExecutable() { return m_haveExecutable; }
292 * Is the debuggee active, i.e. was it started by the debugger?
294 bool isProgramActive() { return m_programActive; }
297 * Is the debugger driver idle?
299 bool isIdle() const;
301 /* The list of breakpoints. */
302 typedef std::list<Breakpoint>::const_iterator BrkptROIterator;
303 BrkptROIterator breakpointsBegin() const { return m_brkpts.begin(); }
304 BrkptROIterator breakpointsEnd() const { return m_brkpts.end(); }
306 const QString& executable() const { return m_executable; }
309 * Terminal emulation level.
311 enum TTYLevel {
312 ttyNone = 0, /* ignore output, input triggers EOF */
313 ttySimpleOutputOnly = 1, /* minmal output emulation, input triggers EOF */
314 ttyFull = 7 /* program needs full emulation */
318 * Returns the level of terminal emulation requested by the inferior.
320 TTYLevel ttyLevel() const { return m_ttyLevel; }
322 /** Sets the terminal that is to be used by the debugger. */
323 void setTerminal(const QString& term) { m_inferiorTerminal = term; }
325 /** Returns the debugger driver. */
326 DebuggerDriver* driver() { return m_d; }
328 /** Returns the pid that the debugger is currently attached to. */
329 const QString& attachedPid() const { return m_attachedPid; }
332 * The memory at that the expression evaluates to is watched. Can be
333 * empty. Triggers a redisplay even if the expression did not change.
335 void setMemoryExpression(const QString& memexpr);
338 * Sets how the watched memory location is displayed.
339 * Call setMemoryExpression() to force a redisplay.
341 void setMemoryFormat(unsigned format) { m_memoryFormat = format; }
343 // settings
344 void saveSettings(KConfig*);
345 void restoreSettings(KConfig*);
347 protected:
348 QString m_inferiorTerminal;
349 QString m_debuggerCmd; /* per-program setting */
350 TTYLevel m_ttyLevel; /* level of terminal emulation */
351 bool startDriver();
352 void stopDriver();
353 void writeCommand();
355 QStringList m_watchEvalExpr; /* exprs to evaluate for watch window */
356 std::list<Breakpoint> m_brkpts;
357 QString m_memoryExpression; /* memory location to watch */
358 unsigned m_memoryFormat; /* how that output should look */
360 protected slots:
361 void parse(CmdQueueItem* cmd, const char* output);
362 protected:
363 void handleRunCommands(const char* output);
364 void updateAllExprs();
365 void updateProgEnvironment(const QString& args, const QString& wd,
366 const Q3Dict<EnvVar>& newVars,
367 const QStringList& newOptions);
368 void parseLocals(const char* output, std::list<ExprValue*>& newVars);
369 void handleLocals(const char* output);
370 bool handlePrint(CmdQueueItem* cmd, const char* output);
371 bool handlePrintDeref(CmdQueueItem* cmd, const char* output);
372 void handleBacktrace(const char* output);
373 void handleFrameChange(const char* output);
374 void handleFindType(CmdQueueItem* cmd, const char* output);
375 void handlePrintStruct(CmdQueueItem* cmd, const char* output);
376 void handleSharedLibs(const char* output);
377 void handleRegisters(const char* output);
378 void handleMemoryDump(const char* output);
379 void handleInfoLine(CmdQueueItem* cmd, const char* output);
380 void handleDisassemble(CmdQueueItem* cmd, const char* output);
381 void handleThreadList(const char* output);
382 void handleSetPC(const char* output);
383 void handleSetVariable(CmdQueueItem* cmd, const char* output);
384 void evalExpressions();
385 void evalInitialStructExpression(VarTree* var, ExprWnd* wnd, bool immediate);
386 void evalStructExpression(VarTree* var, ExprWnd* wnd, bool immediate);
387 void dereferencePointer(ExprWnd* wnd, VarTree* var, bool immediate);
388 void determineType(ExprWnd* wnd, VarTree* var);
389 void queueMemoryDump(bool immediate);
390 CmdQueueItem* loadCoreFile();
391 void openProgramConfig(const QString& name);
393 typedef std::list<Breakpoint>::iterator BrkptIterator;
394 BrkptIterator breakpointByFilePos(QString file, int lineNo,
395 const DbgAddr& address);
396 BrkptIterator breakpointById(int id);
397 CmdQueueItem* executeBreakpoint(const Breakpoint* bp, bool queueOnly);
398 void newBreakpoint(CmdQueueItem* cmd, const char* output);
399 void updateBreakList(const char* output);
400 bool stopMayChangeBreakList() const;
401 void saveBreakpoints(KConfig* config);
402 void restoreBreakpoints(KConfig* config);
403 bool enableDisableBreakpoint(BrkptIterator bp);
404 bool deleteBreakpoint(BrkptIterator bp);
405 bool conditionalBreakpoint(BrkptIterator bp,
406 const QString& condition,
407 int ignoreCount);
409 bool m_haveExecutable; /* has an executable been specified */
410 bool m_programActive; /* is the program active (possibly halting in a brkpt)? */
411 bool m_programRunning; /* is the program executing (not stopped)? */
412 bool m_sharedLibsListed; /* do we know the shared libraries loaded by the prog? */
413 QString m_executable;
414 QString m_corefile;
415 QString m_attachedPid; /* user input of attaching to pid */
416 QString m_programArgs;
417 QString m_remoteDevice;
418 QString m_programWD; /* working directory of gdb */
419 Q3Dict<EnvVar> m_envVars; /* environment variables set by user */
420 QStringList m_boolOptions; /* boolean options */
421 QStringList m_sharedLibs; /* shared libraries used by program */
422 ProgramTypeTable* m_typeTable; /* known types used by the program */
423 KConfig* m_programConfig; /* program-specific settings (brkpts etc) */
424 void saveProgramSettings();
425 void restoreProgramSettings();
426 QString readDebuggerCmd(const KConfigGroup& g);
428 // debugger process
429 DebuggerDriver* m_d;
430 bool m_explicitKill; /* whether we are killing gdb ourselves */
432 QString m_statusMessage;
434 protected slots:
435 void gdbExited(K3Process*);
436 void slotInferiorRunning();
437 void backgroundUpdate();
438 void gotoFrame(int);
439 void slotExpanding(Q3ListViewItem*);
440 void slotDeleteWatch();
441 void slotValuePopup(const QString&);
442 void slotDisassemble(const QString&, int);
443 void slotValueEdited(VarTree*, const QString&);
444 public slots:
445 void setThread(int);
446 void shutdown();
448 signals:
450 * This signal is emitted before the debugger is started. The slot is
451 * supposed to set up m_inferiorTerminal.
453 void debuggerStarting();
456 * This signal is emitted whenever a part of the debugger needs to
457 * highlight the specfied source code line (e.g. when the program
458 * stops).
460 * @param file specifies the file; this is not necessarily a full path
461 * name, and if it is relative, you won't know relative to what, you
462 * can only guess.
463 * @param lineNo specifies the line number (0-based!) (this may be
464 * negative, in which case the file should be activated, but the line
465 * should NOT be changed).
466 * @param address specifies the exact address of the PC or is empty.
468 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
471 * This signal indicates that the program counter has changed.
473 * @param filename specifies the filename where the program stopped
474 * @param lineNo specifies the line number (zero-based); it can be -1
475 * if it is unknown
476 * @param address specifies the address that the instruction pointer
477 * points to.
478 * @param frameNo specifies the frame number: 0 is the innermost frame,
479 * positive numbers are frames somewhere up the stack (indicates points
480 * where a function was called); the latter cases should be indicated
481 * differently in the source window.
483 void updatePC(const QString& filename, int lineNo,
484 const DbgAddr& address, int frameNo);
487 * This signal is emitted when gdb detects that the executable has been
488 * updated, e.g. recompiled. (You usually need not handle this signal
489 * if you are the editor which changed the executable.)
491 void executableUpdated();
494 * Indicates that a new status message is available.
496 void updateStatusMessage();
499 * Indicates that the internal state of the debugger has changed, and
500 * that this will very likely have an impact on the UI.
502 void updateUI();
505 * Indicates that the list of breakpoints has possibly changed.
507 void breakpointsChanged();
510 * Indicates that the register values have possibly changed.
512 void registersChanged(const std::list<RegisterInfo>&);
515 * Indicates that the list of threads has possibly changed.
517 void threadsChanged(const std::list<ThreadInfo>&);
520 * Indicates that the value for a value popup is ready.
522 void valuePopup(const QString&);
525 * Provides the disassembled code of the location given by file and
526 * line number (zero-based).
528 void disassembled(const QString& file, int line, const std::list<DisassembledCode>& code);
531 * Indicates that the program has stopped for any reason: by a
532 * breakpoint, by a signal that the debugger driver caught, by a single
533 * step instruction.
535 void programStopped();
538 * Indicates that a new memory dump output is ready.
539 * @param msg is an error message or empty
540 * @param memdump is the memory dump
542 void memoryDumpChanged(const QString&, const std::list<MemoryDump>&);
545 * Gives other objects a chance to save program specific settings.
547 void saveProgramSpecific(KConfigBase* config);
550 * Gives other objects a chance to restore program specific settings.
552 void restoreProgramSpecific(KConfigBase* config);
554 protected:
555 ExprWnd& m_localVariables;
556 ExprWnd& m_watchVariables;
557 QListWidget& m_btWindow;
559 // implementation helpers
560 protected:
561 QWidget* parentWidget() { return static_cast<QWidget*>(parent()); }
564 #endif // DEBUGGER_H