Do not insert new values in reversed order.
[kdbg.git] / kdbg / debugger.h
blob7390549ffa91cbe03c73b3d063b4df14ebb926b2
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #ifndef DEBUGGER_H
7 #define DEBUGGER_H
9 #include <qtimer.h>
10 #include <qdict.h>
11 #include <qptrvector.h>
12 #include <qstringlist.h>
13 #include "envvar.h"
14 #include "exprwnd.h" /* some compilers require this */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 class ExprWnd;
21 class VarTree;
22 struct ExprValue;
23 class ProgramTypeTable;
24 class KTreeViewItem;
25 class KConfig;
26 class KConfigBase;
27 class ProgramConfig;
28 class QListBox;
29 class RegisterInfo;
30 class ThreadInfo;
31 class DebuggerDriver;
32 class CmdQueueItem;
33 class Breakpoint;
34 struct DisassembledCode;
35 struct MemoryDump;
36 struct DbgAddr;
37 class KProcess;
40 class KDebugger : public QObject
42 Q_OBJECT
43 public:
44 KDebugger(QWidget* parent, /* will be used as the parent for dialogs */
45 ExprWnd* localVars,
46 ExprWnd* watchVars,
47 QListBox* backtrace);
48 ~KDebugger();
50 /**
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
54 * true is returned.
56 * @return false if an error occurs.
58 bool debugProgram(const QString& executable,
59 DebuggerDriver* driver);
61 /**
62 * Uses the specified core to debug the active program.
63 * @param batch tells whether the core file was given on the
64 * command line.
66 void useCoreFile(QString corefile, bool batch);
68 /**
69 * Uses the specified pid to attach to the active program.
71 void setAttachPid(const QString& pid);
73 /**
74 * Attaches to the specified process and debugs it.
76 void attachProgram(const QString& pid);
78 /**
79 * Returns the file name of the per-program config file for the
80 * specified program.
82 static QString getConfigForExe(const QString& exe);
84 /**
85 * The driver name entry in the per-program config file.
87 static const char DriverNameEntry[];
89 public slots:
90 /**
91 * Runs the program or continues it if it is stopped at a breakpoint.
93 void programRun();
95 /**
96 * Restarts the debuggee.
98 void programRunAgain();
101 * Performs a single-step, possibly stepping into a function call.
102 * If byInsn is true, a step by instruction is performed.
104 void programStep();
107 * Performs a single-step, stepping over a function call.
108 * If byInsn is true, a step by instruction is performed.
110 void programNext();
113 * Performs a single-step by instruction, possibly stepping into a
114 * function call.
116 void programStepi();
119 * Performs a single-step by instruction, stepping over a function
120 * call.
122 void programNexti();
125 * Runs the program until it returns from the current function.
127 void programFinish();
130 * Kills the program (removes it from memory).
132 void programKill();
135 * Interrupts the program if it is currently running.
137 void programBreak();
140 * Moves the program counter to the specified line.
141 * If an address is given, it is moved to the address.
143 void setProgramCounter(const QString&, int, const DbgAddr&);
145 public:
147 * Queries the user for program arguments.
149 void programArgs(QWidget* parent);
152 * Queries the user for program settings: Debugger command, terminal
153 * emulator.
155 void programSettings(QWidget* parent);
158 * Setup remote debugging device
160 void setRemoteDevice(const QString& remoteDevice) { m_remoteDevice = remoteDevice; }
163 * Run the debuggee until the specified line in the specified file is
164 * reached.
166 * @return false if the command was not executed, e.g. because the
167 * debuggee is running at the moment.
169 bool runUntil(const QString& fileName, int lineNo);
172 * Set a breakpoint.
174 * @param fileName The source file in which to set the breakpoint.
175 * @param lineNo The zero-based line number.
176 * @param address The exact address of the breakpoint.
177 * @param temporary Specifies whether this is a temporary breakpoint
178 * @return false if the command was not executed, e.g. because the
179 * debuggee is running at the moment.
181 bool setBreakpoint(QString fileName, int lineNo,
182 const DbgAddr& address, bool temporary);
185 * Set a breakpoint.
187 * @param bp Describes the breakpoint.
188 * @param queueOnly If false, the breakpoint is set using a high-priority command.
190 void setBreakpoint(Breakpoint* bp, bool queueOnly);
193 * Enable or disable a breakpoint at the specified location.
195 * @param fileName The source file in which the breakpoint is.
196 * @param lineNo The zero-based line number.
197 * @param address The exact address of the breakpoint.
198 * @return false if the command was not executed, e.g. because the
199 * debuggee is running at the moment.
201 bool enableDisableBreakpoint(QString fileName, int lineNo,
202 const DbgAddr& address);
205 * Enables or disables 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 enableDisableBreakpoint(Breakpoint* bp);
213 * Removes the specified breakpoint. Note that if bp is an orphaned
214 * breakpoint, then bp is an invalid pointer if (and only if) this
215 * function returns true.
217 * @return false if the command was not executed, e.g. because the
218 * debuggee is running at the moment.
220 bool deleteBreakpoint(Breakpoint* bp);
223 * Changes the specified breakpoint's condition and ignore count.
225 * @return false if the command was not executed, e.g. because the
226 * debuggee is running at the moment.
228 bool conditionalBreakpoint(Breakpoint* bp,
229 const QString& condition,
230 int ignoreCount);
233 * Tells whether one of the single stepping commands can be invoked
234 * (step, next, finish, until, also run).
236 bool canSingleStep();
239 * Tells whether a breakpoints can be set, deleted, enabled, or disabled.
241 bool canChangeBreakpoints();
244 * Tells whether a the program is loaded, but not active.
246 bool canStart();
249 * Add a watch expression.
251 void addWatch(const QString& expr);
254 * Retrieves the current status message.
256 const QString& statusMessage() const { return m_statusMessage; }
259 * Is the debugger ready to receive another high-priority command?
261 bool isReady() const;
264 * Is the debuggee running (not just active)?
266 bool isProgramRunning() { return m_haveExecutable && m_programRunning; }
269 * Do we have an executable set?
271 bool haveExecutable() { return m_haveExecutable; }
274 * Is the debuggee active, i.e. was it started by the debugger?
276 bool isProgramActive() { return m_programActive; }
279 * Is the debugger driver idle?
281 bool isIdle() const;
283 /** The list of breakpoints. */
284 int numBreakpoints() const { return m_brkpts.size(); }
285 const Breakpoint* breakpoint(int i) const { return m_brkpts[i]; }
288 * Returns the breakpoint with the specified \a id.
290 Breakpoint* breakpointById(int id);
292 const QString& executable() const { return m_executable; }
295 * Terminal emulation level.
297 enum TTYLevel {
298 ttyNone = 0, /* ignore output, input triggers EOF */
299 ttySimpleOutputOnly = 1, /* minmal output emulation, input triggers EOF */
300 ttyFull = 7 /* program needs full emulation */
304 * Returns the level of terminal emulation requested by the inferior.
306 TTYLevel ttyLevel() const { return m_ttyLevel; }
308 /** Sets the terminal that is to be used by the debugger. */
309 void setTerminal(const QString& term) { m_inferiorTerminal = term; }
311 /** Returns the debugger driver. */
312 DebuggerDriver* driver() { return m_d; }
314 /** Returns the pid that the debugger is currently attached to. */
315 const QString& attachedPid() const { return m_attachedPid; }
318 * The memory at that the expression evaluates to is watched. Can be
319 * empty. Triggers a redisplay even if the expression did not change.
321 void setMemoryExpression(const QString& memexpr);
324 * Sets how the watched memory location is displayed.
325 * Call setMemoryExpression() to force a redisplay.
327 void setMemoryFormat(unsigned format) { m_memoryFormat = format; }
329 // settings
330 void saveSettings(KConfig*);
331 void restoreSettings(KConfig*);
333 protected:
334 QString m_inferiorTerminal;
335 QString m_debuggerCmd; /* per-program setting */
336 TTYLevel m_ttyLevel; /* level of terminal emulation */
337 bool startDriver();
338 void stopDriver();
339 void writeCommand();
341 QStringList m_watchEvalExpr; /* exprs to evaluate for watch window */
342 QPtrVector<Breakpoint> m_brkpts;
343 QString m_memoryExpression; /* memory location to watch */
344 unsigned m_memoryFormat; /* how that output should look */
346 protected slots:
347 void parse(CmdQueueItem* cmd, const char* output);
348 protected:
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<ExprValue>& 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 dereferencePointer(ExprWnd* wnd, VarTree* var, bool immediate);
374 void determineType(ExprWnd* wnd, VarTree* var);
375 void queueMemoryDump(bool immediate);
376 CmdQueueItem* loadCoreFile();
377 void openProgramConfig(const QString& name);
379 Breakpoint* breakpointByFilePos(QString file, int lineNo,
380 const DbgAddr& address);
381 void newBreakpoint(CmdQueueItem* cmd, const char* output);
382 void updateBreakList(const char* output);
383 bool stopMayChangeBreakList() const;
384 void saveBreakpoints(ProgramConfig* config);
385 void restoreBreakpoints(ProgramConfig* config);
387 bool m_haveExecutable; /* has an executable been specified */
388 bool m_programActive; /* is the program active (possibly halting in a brkpt)? */
389 bool m_programRunning; /* is the program executing (not stopped)? */
390 bool m_sharedLibsListed; /* do we know the shared libraries loaded by the prog? */
391 QString m_executable;
392 QString m_corefile;
393 QString m_attachedPid; /* user input of attaching to pid */
394 QString m_programArgs;
395 QString m_remoteDevice;
396 QString m_programWD; /* working directory of gdb */
397 QDict<EnvVar> m_envVars; /* environment variables set by user */
398 QStringList m_boolOptions; /* boolean options */
399 QStrList m_sharedLibs; /* shared libraries used by program */
400 ProgramTypeTable* m_typeTable; /* known types used by the program */
401 ProgramConfig* m_programConfig; /* program-specific settings (brkpts etc) */
402 void saveProgramSettings();
403 void restoreProgramSettings();
404 QString readDebuggerCmd();
406 // debugger process
407 DebuggerDriver* m_d;
408 bool m_explicitKill; /* whether we are killing gdb ourselves */
410 QString m_statusMessage;
412 protected slots:
413 void gdbExited(KProcess*);
414 void slotInferiorRunning();
415 void backgroundUpdate();
416 void gotoFrame(int);
417 void slotExpanding(QListViewItem*);
418 void slotDeleteWatch();
419 void slotValuePopup(const QString&);
420 void slotDisassemble(const QString&, int);
421 void slotValueEdited(VarTree*, const QString&);
422 public slots:
423 void setThread(int);
424 void shutdown();
426 signals:
428 * This signal is emitted before the debugger is started. The slot is
429 * supposed to set up m_inferiorTerminal.
431 void debuggerStarting();
434 * This signal is emitted whenever a part of the debugger needs to
435 * highlight the specfied source code line (e.g. when the program
436 * stops).
438 * @param file specifies the file; this is not necessarily a full path
439 * name, and if it is relative, you won't know relative to what, you
440 * can only guess.
441 * @param lineNo specifies the line number (0-based!) (this may be
442 * negative, in which case the file should be activated, but the line
443 * should NOT be changed).
444 * @param address specifies the exact address of the PC or is empty.
446 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
449 * This signal indicates that the program counter has changed.
451 * @param filename specifies the filename where the program stopped
452 * @param lineNo specifies the line number (zero-based); it can be -1
453 * if it is unknown
454 * @param address specifies the address that the instruction pointer
455 * points to.
456 * @param frameNo specifies the frame number: 0 is the innermost frame,
457 * positive numbers are frames somewhere up the stack (indicates points
458 * where a function was called); the latter cases should be indicated
459 * differently in the source window.
461 void updatePC(const QString& filename, int lineNo,
462 const DbgAddr& address, int frameNo);
465 * This signal is emitted when gdb detects that the executable has been
466 * updated, e.g. recompiled. (You usually need not handle this signal
467 * if you are the editor which changed the executable.)
469 void executableUpdated();
472 * Indicates that a new status message is available.
474 void updateStatusMessage();
477 * Indicates that the internal state of the debugger has changed, and
478 * that this will very likely have an impact on the UI.
480 void updateUI();
483 * Indicates that the list of breakpoints has possibly changed.
485 void breakpointsChanged();
488 * Indicates that the register values have possibly changed.
490 void registersChanged(QList<RegisterInfo>&);
493 * Indicates that the list of threads has possibly changed.
495 void threadsChanged(QList<ThreadInfo>&);
498 * Indicates that the value for a value popup is ready.
500 void valuePopup(const QString&);
503 * Provides the disassembled code of the location given by file and
504 * line number (zero-based).
506 void disassembled(const QString& file, int line, const QList<DisassembledCode>& code);
509 * Indicates that the program has stopped for any reason: by a
510 * breakpoint, by a signal that the debugger driver caught, by a single
511 * step instruction.
513 void programStopped();
516 * Indicates that a new memory dump output is ready.
517 * @param msg is an error message or empty
518 * @param memdump is the memory dump
520 void memoryDumpChanged(const QString&, QList<MemoryDump>&);
523 * Gives other objects a chance to save program specific settings.
525 void saveProgramSpecific(KConfigBase* config);
528 * Gives other objects a chance to restore program specific settings.
530 void restoreProgramSpecific(KConfigBase* config);
532 protected:
533 ExprWnd& m_localVariables;
534 ExprWnd& m_watchVariables;
535 QListBox& m_btWindow;
537 // implementation helpers
538 protected:
539 QWidget* parentWidget() { return static_cast<QWidget*>(parent()); }
541 friend class BreakpointTable;
544 #endif // DEBUGGER_H