Implemented orphaned breakpoints to simplify debuggin of dynamic modules.
[kdbg.git] / kdbg / dbgdriver.h
blob040b0e0d08985f2ffbda955565fde214a8efd0f8
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #ifndef DBGDRIVER_H
7 #define DBGDRIVER_H
9 #include <qqueue.h>
10 #include <qlist.h>
11 #include <qfile.h>
12 #include <qregexp.h>
13 #include <kprocess.h>
16 class VarTree;
17 class ExprWnd;
18 class KDebugger;
19 class QStrList;
20 class QStringList;
23 /**
24 * A type representing an address.
26 struct DbgAddr
28 QString a;
29 QString fnoffs;
30 DbgAddr() { }
31 DbgAddr(const QString& aa);
32 DbgAddr(const DbgAddr& src) : a(src.a), fnoffs(src.fnoffs) { }
33 void operator=(const QString& aa);
34 void operator=(const DbgAddr& src) { a = src.a; fnoffs = src.fnoffs; }
35 QString asString() const;
36 bool isEmpty() const { return a.isEmpty(); }
37 protected:
38 void cleanAddr();
40 bool operator==(const DbgAddr& a1, const DbgAddr& a2);
41 bool operator>(const DbgAddr& a1, const DbgAddr& a2);
44 enum DbgCommand {
45 DCinitialize,
46 DCtty,
47 DCexecutable,
48 DCtargetremote,
49 DCcorefile,
50 DCattach,
51 DCinfolinemain,
52 DCinfolocals,
53 DCinforegisters,
54 DCexamine,
55 DCinfoline,
56 DCdisassemble,
57 DCsetargs,
58 DCsetenv,
59 DCunsetenv,
60 DCsetoption, /* debugger options */
61 DCcd,
62 DCbt,
63 DCrun,
64 DCcont,
65 DCstep,
66 DCstepi,
67 DCnext,
68 DCnexti,
69 DCfinish,
70 DCuntil, /* line number is zero-based! */
71 DCkill,
72 DCbreaktext,
73 DCbreakline, /* line number is zero-based! */
74 DCtbreakline, /* line number is zero-based! */
75 DCbreakaddr,
76 DCtbreakaddr,
77 DCwatchpoint,
78 DCdelete,
79 DCenable,
80 DCdisable,
81 DCprint,
82 DCprintStruct,
83 DCprintQStringStruct,
84 DCframe,
85 DCfindType,
86 DCinfosharedlib,
87 DCthread,
88 DCinfothreads,
89 DCinfobreak,
90 DCcondition,
91 DCsetpc,
92 DCignore
95 enum RunDevNull {
96 RDNstdin = 0x1, /* redirect stdin to /dev/null */
97 RDNstdout = 0x2, /* redirect stdout to /dev/null */
98 RDNstderr = 0x4 /* redirect stderr to /dev/null */
102 * How the memory dump is formated. The lowest 4 bits define the size of
103 * the entities. The higher bits define how these are formatted. Note that
104 * not all combinations make sense.
106 enum MemoryDumpType {
107 // sizes
108 MDTbyte = 0x1,
109 MDThalfword = 0x2,
110 MDTword = 0x3,
111 MDTgiantword = 0x4,
112 MDTsizemask = 0xf,
113 // formats
114 MDThex = 0x10,
115 MDTsigned = 0x20,
116 MDTunsigned = 0x30,
117 MDToctal = 0x40,
118 MDTbinary = 0x50,
119 MDTaddress = 0x60,
120 MDTchar = 0x70,
121 MDTfloat = 0x80,
122 MDTstring = 0x90,
123 MDTinsn = 0xa0,
124 MDTformatmask = 0xf0
127 struct Breakpoint;
130 * Debugger commands are placed in a queue. Only one command at a time is
131 * sent down to the debugger. All other commands in the queue are retained
132 * until the sent command has been processed by gdb. The debugger tells us
133 * that it's done with the command by sending the prompt. The output of the
134 * debugger is parsed at that time. Then, if more commands are in the
135 * queue, the next one is sent to the debugger.
137 struct CmdQueueItem
139 DbgCommand m_cmd;
140 QString m_cmdString;
141 bool m_committed; /* just a debugging aid */
142 // remember which expression when printing an expression
143 VarTree* m_expr;
144 ExprWnd* m_exprWnd;
145 // remember file position
146 QString m_fileName;
147 int m_lineNo;
148 // the breakpoint info
149 Breakpoint* m_brkpt;
150 // whether command was emitted due to direct user request (only set when relevant)
151 bool m_byUser;
153 CmdQueueItem(DbgCommand cmd, const QString& str) :
154 m_cmd(cmd),
155 m_cmdString(str),
156 m_committed(false),
157 m_expr(0),
158 m_exprWnd(0),
159 m_lineNo(0),
160 m_brkpt(0),
161 m_byUser(false)
166 * The information about a breakpoint that is parsed from the list of
167 * breakpoints.
169 struct Breakpoint
171 int id; /* gdb's number */
172 enum Type {
173 breakpoint, watchpoint
174 } type;
175 bool temporary;
176 bool enabled;
177 QString location;
178 QString text; /* text if set using DCbreaktext */
179 DbgAddr address; /* exact address of breakpoint */
180 QString condition; /* condition as printed by gdb */
181 int ignoreCount; /* ignore next that may hits */
182 int hitCount; /* as reported by gdb */
183 // the following items repeat the location, but in a better usable way
184 QString fileName;
185 int lineNo; /* zero-based line number */
186 Breakpoint();
187 bool isOrphaned() const { return id < 0; }
191 * Information about a stack frame.
193 struct FrameInfo
195 QString fileName;
196 int lineNo; /* zero-based line number */
197 DbgAddr address; /* exact address of PC */
201 * The information about a stack frame as parsed from the backtrace.
203 struct StackFrame : FrameInfo
205 int frameNo;
206 VarTree* var; /* more information if non-zero */
207 StackFrame() : var(0) { }
208 ~StackFrame();
212 * The information about a thread as parsed from the threads list.
214 struct ThreadInfo : FrameInfo
216 int id; /* gdb's number */
217 QString threadName; /* the SYSTAG */
218 QString function; /* where thread is halted */
219 bool hasFocus; /* the thread whose stack we are watching */
223 * Register information
225 struct RegisterInfo
227 QString regName;
228 QString rawValue;
229 QString cookedValue; /* may be empty */
230 QString type; /* of vector register if not empty */
234 * Disassembled code
236 struct DisassembledCode
238 DbgAddr address;
239 QString code;
243 * Memory contents
245 struct MemoryDump
247 DbgAddr address;
248 QString dump;
252 * This is an abstract base class for debugger process.
254 * This class represents the debugger program. It provides the low-level
255 * interface to the commandline debugger. As such it implements the
256 * commands and parses the output.
258 class DebuggerDriver : public KProcess
260 Q_OBJECT
261 public:
262 DebuggerDriver();
263 virtual ~DebuggerDriver() = 0;
265 virtual QString driverName() const = 0;
267 * Returns the default command string to invoke the debugger driver.
269 virtual QString defaultInvocation() const = 0;
272 * Returns a list of options that can be turned on and off.
274 virtual QStringList boolOptionList() const = 0;
276 virtual bool startup(QString cmdStr);
277 void dequeueCmdByVar(VarTree* var);
278 void setLogFileName(const QString& fname) { m_logFileName = fname; }
280 protected:
281 QString m_runCmd;
283 enum DebuggerState {
284 DSidle, /* gdb waits for input */
285 DSinterrupted, /* a command was interrupted */
286 DSrunningLow, /* gdb is running a low-priority command */
287 DSrunning, /* gdb waits for program */
288 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
289 DScommandSentLow /* low-prioritycommand has been sent */
291 DebuggerState m_state;
293 public:
294 bool isIdle() const { return m_state == DSidle; }
296 * Tells whether a high prority command would be executed immediately.
298 bool canExecuteImmediately() const { return m_hipriCmdQueue.isEmpty(); }
300 protected:
301 char* m_output; /* normal gdb output */
302 size_t m_outputLen; /* amount of data so far accumulated in m_output */
303 size_t m_outputAlloc; /* space available in m_output */
304 typedef QCString DelayedStr;
305 QQueue<DelayedStr> m_delayedOutput; /* output colleced while we have receivedOutput */
306 /* but before signal wroteStdin arrived */
308 public:
310 * Enqueues a high-priority command. High-priority commands are
311 * executed before any low-priority commands. No user interaction is
312 * possible as long as there is a high-priority command in the queue.
314 virtual CmdQueueItem* executeCmd(DbgCommand,
315 bool clearLow = false) = 0;
316 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
317 bool clearLow = false) = 0;
318 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
319 bool clearLow = false) = 0;
320 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
321 bool clearLow = false) = 0;
322 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
323 bool clearLow = false) = 0;
324 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
325 bool clearLow = false) = 0;
327 enum QueueMode {
328 QMnormal, /* queues the command last */
329 QMoverride, /* removes an already queued command */
330 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
334 * Enqueues a low-priority command. Low-priority commands are executed
335 * after any high-priority commands.
337 virtual CmdQueueItem* queueCmd(DbgCommand,
338 QueueMode mode) = 0;
339 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
340 QueueMode mode) = 0;
341 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
342 QueueMode mode) = 0;
343 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
344 QueueMode mode) = 0;
345 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
346 QueueMode mode) = 0;
349 * Flushes the command queues.
350 * @param hipriOnly if true, only the high priority queue is flushed.
352 virtual void flushCommands(bool hipriOnly = false);
355 * Terminates the debugger process.
357 virtual void terminate() = 0;
360 * Terminates the debugger process, but also detaches any program that
361 * it has been attached to.
363 virtual void detachAndTerminate() = 0;
366 * Interrupts the debuggee.
368 virtual void interruptInferior() = 0;
371 * Parses the output as an array of QChars.
373 virtual VarTree* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
376 * Parses a back-trace (the output of the DCbt command).
378 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
381 * Parses the output of the DCframe command;
382 * @param frameNo Returns the frame number.
383 * @param file Returns the source file name.
384 * @param lineNo The zero-based line number.
385 * @param address Returns the exact address.
386 * @return false if the frame could not be parsed successfully. The
387 * output values are undefined in this case.
389 virtual bool parseFrameChange(const char* output, int& frameNo,
390 QString& file, int& lineNo, DbgAddr& address) = 0;
393 * Parses a list of breakpoints.
394 * @param output The output of the debugger.
395 * @param brks The list of new #Breakpoint objects. The list
396 * must initially be empty.
397 * @return False if there was an error before the first breakpoint
398 * was found. Even if true is returned, #brks may be empty.
400 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
403 * Parses a list of threads.
404 * @param output The output of the debugger.
405 * @param threads The list of new #ThreadInfo objects. The list
406 * must initially be empty.
407 * @return False if there was an error before the first thread entry
408 * was found. Even if true is returned, #threads may be empty.
410 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
413 * Parses the output when the program stops to see whether this it
414 * stopped due to a breakpoint.
415 * @param output The output of the debugger.
416 * @param id Returns the breakpoint id.
417 * @param file Returns the file name in which the breakpoint is.
418 * @param lineNo Returns the zero-based line number of the breakpoint.
419 * @param address Returns the address of the breakpoint.
420 * @return False if there was no breakpoint.
422 virtual bool parseBreakpoint(const char* output, int& id,
423 QString& file, int& lineNo, QString& address) = 0;
426 * Parses the output of the DCinfolocals command.
427 * @param output The output of the debugger.
428 * @param newVars Receives the parsed variable values. The values are
429 * simply append()ed to the supplied list.
431 virtual void parseLocals(const char* output, QList<VarTree>& newVars) = 0;
434 * Parses the output of a DCprint or DCprintStruct command.
435 * @param output The output of the debugger.
436 * @param wantErrorValue Specifies whether the error message should be
437 * provided as the value of a NKplain variable. If this is false,
438 * #var will be 0 if the printed value is an error message.
439 * @param var Returns the variable value. It is set to 0 if there was
440 * a parse error or if the output is an error message and wantErrorValue
441 * is false. If it is not 0, #var->text() will return junk and must be
442 * set using #var->setText().
443 * @return false if the output is an error message. Even if true is
444 * returned, #var might still be 0 (due to a parse error).
446 virtual bool parsePrintExpr(const char* output, bool wantErrorValue,
447 VarTree*& var) = 0;
450 * Parses the output of the DCcd command.
451 * @return false if the message is an error message.
453 virtual bool parseChangeWD(const char* output, QString& message) = 0;
456 * Parses the output of the DCexecutable command.
457 * @return false if an error occured.
459 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
462 * Parses the output of the DCcorefile command.
463 * @return false if the core file was not loaded successfully.
465 virtual bool parseCoreFile(const char* output) = 0;
467 enum StopFlags {
468 SFrefreshSource = 1, /* refresh of source code is needed */
469 SFrefreshBreak = 2, /* refresh breakpoints */
470 SFrefreshThreads = 4, /* refresh thread list */
471 SFprogramActive = 128 /* program remains active */
474 * Parses the output of commands that execute (a piece of) the program.
475 * @return The inclusive OR of zero or more of the StopFlags.
477 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
480 * Parses the output of the DCsharedlibs command.
482 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
485 * Parses the output of the DCfindType command.
486 * @return true if a type was found.
488 virtual bool parseFindType(const char* output, QString& type) = 0;
491 * Parses the output of the DCinforegisters command.
493 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
496 * Parses the output of the DCinfoline command. Returns false if the
497 * two addresses could not be found.
499 virtual bool parseInfoLine(const char* output,
500 QString& addrFrom, QString& addrTo) = 0;
503 * Parses the ouput of the DCdisassemble command.
505 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
508 * Parses a memory dump. Returns an empty string if no error was found;
509 * otherwise it contains an error message.
511 virtual QString parseMemoryDump(const char* output, QList<MemoryDump>& memdump) = 0;
513 protected:
514 /** Removes all commands from the low-priority queue. */
515 void flushLoPriQueue();
516 /** Removes all commands from the high-priority queue. */
517 void flushHiPriQueue();
519 QQueue<CmdQueueItem> m_hipriCmdQueue;
520 QList<CmdQueueItem> m_lopriCmdQueue;
522 * The active command is kept separately from other pending commands.
524 CmdQueueItem* m_activeCmd;
526 * Helper function that queues the given command string in the
527 * low-priority queue.
529 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
530 QueueMode mode);
532 * Helper function that queues the given command string in the
533 * high-priority queue.
535 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
536 bool clearLow);
537 void writeCommand();
538 virtual void commandFinished(CmdQueueItem* cmd) = 0;
540 protected:
541 /** @internal */
542 virtual int commSetupDoneC();
544 char m_prompt[10];
545 size_t m_promptMinLen;
546 char m_promptLastChar;
547 QRegExp m_promptRE;
549 // log file
550 QString m_logFileName;
551 QFile m_logFile;
553 protected slots:
554 virtual void slotReceiveOutput(KProcess*, char* buffer, int buflen);
555 virtual void slotCommandRead(KProcess*);
556 virtual void slotExited(KProcess*);
558 signals:
560 * This signal is emitted when the output of a command has been fully
561 * collected and is ready to be interpreted.
563 void commandReceived(CmdQueueItem* cmd, const char* output);
566 * This signal is emitted when the debugger recognizes that a specific
567 * location in a file ought to be displayed.
569 * Gdb's --fullname option supports this for the step, next, frame, and
570 * run commands (and possibly others).
572 * @param file specifies the file; this is not necessarily a full path
573 * name, and if it is relative, you won't know relative to what, you
574 * can only guess.
575 * @param lineNo specifies the line number (0-based!) (this may be
576 * negative, in which case the file should be activated, but the line
577 * should NOT be changed).
578 * @param address specifies the exact address of the PC or is empty.
580 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
583 * This signal is emitted when a command that starts the inferior has
584 * been submitted to the debugger.
586 void inferiorRunning();
589 * This signal is emitted when all output from the debugger has been
590 * consumed and no more commands are in the queues.
592 void enterIdleState();
595 #endif // DBGDRIVER_H