Fix a crash when a value is edited in a floating variable display.
[kdbg.git] / kdbg / dbgdriver.h
blob34fcc93b62ede6ceeefd9ee99e5fb9fa55f03b1f
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 <qptrqueue.h>
10 #include <qptrlist.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 DCprintDeref,
83 DCprintStruct,
84 DCprintQStringStruct,
85 DCframe,
86 DCfindType,
87 DCinfosharedlib,
88 DCthread,
89 DCinfothreads,
90 DCinfobreak,
91 DCcondition,
92 DCsetpc,
93 DCignore,
94 DCsetvariable
97 enum RunDevNull {
98 RDNstdin = 0x1, /* redirect stdin to /dev/null */
99 RDNstdout = 0x2, /* redirect stdout to /dev/null */
100 RDNstderr = 0x4 /* redirect stderr to /dev/null */
104 * How the memory dump is formated. The lowest 4 bits define the size of
105 * the entities. The higher bits define how these are formatted. Note that
106 * not all combinations make sense.
108 enum MemoryDumpType {
109 // sizes
110 MDTbyte = 0x1,
111 MDThalfword = 0x2,
112 MDTword = 0x3,
113 MDTgiantword = 0x4,
114 MDTsizemask = 0xf,
115 // formats
116 MDThex = 0x10,
117 MDTsigned = 0x20,
118 MDTunsigned = 0x30,
119 MDToctal = 0x40,
120 MDTbinary = 0x50,
121 MDTaddress = 0x60,
122 MDTchar = 0x70,
123 MDTfloat = 0x80,
124 MDTstring = 0x90,
125 MDTinsn = 0xa0,
126 MDTformatmask = 0xf0
129 struct Breakpoint;
132 * Debugger commands are placed in a queue. Only one command at a time is
133 * sent down to the debugger. All other commands in the queue are retained
134 * until the sent command has been processed by gdb. The debugger tells us
135 * that it's done with the command by sending the prompt. The output of the
136 * debugger is parsed at that time. Then, if more commands are in the
137 * queue, the next one is sent to the debugger.
139 struct CmdQueueItem
141 DbgCommand m_cmd;
142 QString m_cmdString;
143 bool m_committed; /* just a debugging aid */
144 // remember which expression when printing an expression
145 VarTree* m_expr;
146 ExprWnd* m_exprWnd;
147 // remember file position
148 QString m_fileName;
149 int m_lineNo;
150 // the breakpoint info
151 Breakpoint* m_brkpt;
152 // whether command was emitted due to direct user request (only set when relevant)
153 bool m_byUser;
155 CmdQueueItem(DbgCommand cmd, const QString& str) :
156 m_cmd(cmd),
157 m_cmdString(str),
158 m_committed(false),
159 m_expr(0),
160 m_exprWnd(0),
161 m_lineNo(0),
162 m_brkpt(0),
163 m_byUser(false)
168 * The information about a breakpoint that is parsed from the list of
169 * breakpoints.
171 struct Breakpoint
173 int id; /* gdb's number */
174 enum Type {
175 breakpoint, watchpoint
176 } type;
177 bool temporary;
178 bool enabled;
179 QString location;
180 QString text; /* text if set using DCbreaktext */
181 DbgAddr address; /* exact address of breakpoint */
182 QString condition; /* condition as printed by gdb */
183 int ignoreCount; /* ignore next that may hits */
184 int hitCount; /* as reported by gdb */
185 // the following items repeat the location, but in a better usable way
186 QString fileName;
187 int lineNo; /* zero-based line number */
188 Breakpoint();
189 bool isOrphaned() const { return id < 0; }
193 * Information about a stack frame.
195 struct FrameInfo
197 QString fileName;
198 int lineNo; /* zero-based line number */
199 DbgAddr address; /* exact address of PC */
203 * The information about a stack frame as parsed from the backtrace.
205 struct StackFrame : FrameInfo
207 int frameNo;
208 VarTree* var; /* more information if non-zero */
209 StackFrame() : var(0) { }
210 ~StackFrame();
214 * The information about a thread as parsed from the threads list.
216 struct ThreadInfo : FrameInfo
218 int id; /* gdb's number */
219 QString threadName; /* the SYSTAG */
220 QString function; /* where thread is halted */
221 bool hasFocus; /* the thread whose stack we are watching */
225 * Register information
227 struct RegisterInfo
229 QString regName;
230 QString rawValue;
231 QString cookedValue; /* may be empty */
232 QString type; /* of vector register if not empty */
236 * Disassembled code
238 struct DisassembledCode
240 DbgAddr address;
241 QString code;
245 * Memory contents
247 struct MemoryDump
249 DbgAddr address;
250 QString dump;
254 * This is an abstract base class for debugger process.
256 * This class represents the debugger program. It provides the low-level
257 * interface to the commandline debugger. As such it implements the
258 * commands and parses the output.
260 class DebuggerDriver : public KProcess
262 Q_OBJECT
263 public:
264 DebuggerDriver();
265 virtual ~DebuggerDriver() = 0;
267 virtual QString driverName() const = 0;
269 * Returns the default command string to invoke the debugger driver.
271 virtual QString defaultInvocation() const = 0;
274 * Returns a list of options that can be turned on and off.
276 virtual QStringList boolOptionList() const = 0;
278 virtual bool startup(QString cmdStr);
279 void setLogFileName(const QString& fname) { m_logFileName = fname; }
281 protected:
282 QString m_runCmd;
284 enum DebuggerState {
285 DSidle, /* gdb waits for input */
286 DSinterrupted, /* a command was interrupted */
287 DSrunningLow, /* gdb is running a low-priority command */
288 DSrunning, /* gdb waits for program */
289 DScommandSent, /* command has been sent, we wait for wroteStdin signal */
290 DScommandSentLow /* low-prioritycommand has been sent */
292 DebuggerState m_state;
294 public:
295 bool isIdle() const { return m_state == DSidle; }
297 * Tells whether a high prority command would be executed immediately.
299 bool canExecuteImmediately() const { return m_hipriCmdQueue.isEmpty(); }
301 protected:
302 char* m_output; /* normal gdb output */
303 size_t m_outputLen; /* amount of data so far accumulated in m_output */
304 size_t m_outputAlloc; /* space available in m_output */
305 typedef QCString DelayedStr;
306 QQueue<DelayedStr> m_delayedOutput; /* output colleced while we have receivedOutput */
307 /* but before signal wroteStdin arrived */
309 public:
311 * Enqueues a high-priority command. High-priority commands are
312 * executed before any low-priority commands. No user interaction is
313 * possible as long as there is a high-priority command in the queue.
315 virtual CmdQueueItem* executeCmd(DbgCommand,
316 bool clearLow = false) = 0;
317 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg,
318 bool clearLow = false) = 0;
319 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg,
320 bool clearLow = false) = 0;
321 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg, int intArg,
322 bool clearLow = false) = 0;
323 virtual CmdQueueItem* executeCmd(DbgCommand, QString strArg1, QString strArg2,
324 bool clearLow = false) = 0;
325 virtual CmdQueueItem* executeCmd(DbgCommand, int intArg1, int intArg2,
326 bool clearLow = false) = 0;
328 enum QueueMode {
329 QMnormal, /* queues the command last */
330 QMoverride, /* removes an already queued command */
331 QMoverrideMoreEqual /* ditto, also puts the command first in the queue */
335 * Enqueues a low-priority command. Low-priority commands are executed
336 * after any high-priority commands.
338 virtual CmdQueueItem* queueCmd(DbgCommand,
339 QueueMode mode) = 0;
340 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg,
341 QueueMode mode) = 0;
342 virtual CmdQueueItem* queueCmd(DbgCommand, int intArg,
343 QueueMode mode) = 0;
344 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg, int intArg,
345 QueueMode mode) = 0;
346 virtual CmdQueueItem* queueCmd(DbgCommand, QString strArg1, QString strArg2,
347 QueueMode mode) = 0;
350 * Flushes the command queues.
351 * @param hipriOnly if true, only the high priority queue is flushed.
353 virtual void flushCommands(bool hipriOnly = false);
356 * Terminates the debugger process.
358 virtual void terminate() = 0;
361 * Terminates the debugger process, but also detaches any program that
362 * it has been attached to.
364 virtual void detachAndTerminate() = 0;
367 * Interrupts the debuggee.
369 virtual void interruptInferior() = 0;
372 * Specifies the command that prints the QString data.
374 virtual void setPrintQStringDataCmd(const char* cmd) = 0;
377 * Parses the output as an array of QChars.
379 virtual VarTree* parseQCharArray(const char* output, bool wantErrorValue, bool qt3like) = 0;
382 * Parses a back-trace (the output of the DCbt command).
384 virtual void parseBackTrace(const char* output, QList<StackFrame>& stack) = 0;
387 * Parses the output of the DCframe command;
388 * @param frameNo Returns the frame number.
389 * @param file Returns the source file name.
390 * @param lineNo The zero-based line number.
391 * @param address Returns the exact address.
392 * @return false if the frame could not be parsed successfully. The
393 * output values are undefined in this case.
395 virtual bool parseFrameChange(const char* output, int& frameNo,
396 QString& file, int& lineNo, DbgAddr& address) = 0;
399 * Parses a list of breakpoints.
400 * @param output The output of the debugger.
401 * @param brks The list of new #Breakpoint objects. The list
402 * must initially be empty.
403 * @return False if there was an error before the first breakpoint
404 * was found. Even if true is returned, #brks may be empty.
406 virtual bool parseBreakList(const char* output, QList<Breakpoint>& brks) = 0;
409 * Parses a list of threads.
410 * @param output The output of the debugger.
411 * @param threads The list of new #ThreadInfo objects. The list
412 * must initially be empty.
413 * @return False if there was an error before the first thread entry
414 * was found. Even if true is returned, #threads may be empty.
416 virtual bool parseThreadList(const char* output, QList<ThreadInfo>& threads) = 0;
419 * Parses the output when the program stops to see whether this it
420 * stopped due to a breakpoint.
421 * @param output The output of the debugger.
422 * @param id Returns the breakpoint id.
423 * @param file Returns the file name in which the breakpoint is.
424 * @param lineNo Returns the zero-based line number of the breakpoint.
425 * @param address Returns the address of the breakpoint.
426 * @return False if there was no breakpoint.
428 virtual bool parseBreakpoint(const char* output, int& id,
429 QString& file, int& lineNo, QString& address) = 0;
432 * Parses the output of the DCinfolocals command.
433 * @param output The output of the debugger.
434 * @param newVars Receives the parsed variable values. The values are
435 * simply append()ed to the supplied list.
437 virtual void parseLocals(const char* output, QList<VarTree>& newVars) = 0;
440 * Parses the output of a DCprint or DCprintStruct command.
441 * @param output The output of the debugger.
442 * @param wantErrorValue Specifies whether the error message should be
443 * provided as the value of a NKplain variable. If this is false,
444 * #var will be 0 if the printed value is an error message.
445 * @param var Returns the variable value. It is set to 0 if there was
446 * a parse error or if the output is an error message and wantErrorValue
447 * is false. If it is not 0, #var->text() will return junk and must be
448 * set using #var->setText().
449 * @return false if the output is an error message. Even if true is
450 * returned, #var might still be 0 (due to a parse error).
452 virtual bool parsePrintExpr(const char* output, bool wantErrorValue,
453 VarTree*& var) = 0;
456 * Parses the output of the DCcd command.
457 * @return false if the message is an error message.
459 virtual bool parseChangeWD(const char* output, QString& message) = 0;
462 * Parses the output of the DCexecutable command.
463 * @return false if an error occured.
465 virtual bool parseChangeExecutable(const char* output, QString& message) = 0;
468 * Parses the output of the DCcorefile command.
469 * @return false if the core file was not loaded successfully.
471 virtual bool parseCoreFile(const char* output) = 0;
473 enum StopFlags {
474 SFrefreshSource = 1, /* refresh of source code is needed */
475 SFrefreshBreak = 2, /* refresh breakpoints */
476 SFrefreshThreads = 4, /* refresh thread list */
477 SFprogramActive = 128 /* program remains active */
480 * Parses the output of commands that execute (a piece of) the program.
481 * @return The inclusive OR of zero or more of the StopFlags.
483 virtual uint parseProgramStopped(const char* output, QString& message) = 0;
486 * Parses the output of the DCsharedlibs command.
488 virtual void parseSharedLibs(const char* output, QStrList& shlibs) = 0;
491 * Parses the output of the DCfindType command.
492 * @return true if a type was found.
494 virtual bool parseFindType(const char* output, QString& type) = 0;
497 * Parses the output of the DCinforegisters command.
499 virtual void parseRegisters(const char* output, QList<RegisterInfo>& regs) = 0;
502 * Parses the output of the DCinfoline command. Returns false if the
503 * two addresses could not be found.
505 virtual bool parseInfoLine(const char* output,
506 QString& addrFrom, QString& addrTo) = 0;
509 * Parses the ouput of the DCdisassemble command.
511 virtual void parseDisassemble(const char* output, QList<DisassembledCode>& code) = 0;
514 * Parses a memory dump. Returns an empty string if no error was found;
515 * otherwise it contains an error message.
517 virtual QString parseMemoryDump(const char* output, QList<MemoryDump>& memdump) = 0;
520 * Parses the output of the DCsetvariable command. Returns an empty
521 * string if no error was found; otherwise it contains an error
522 * message.
524 virtual QString parseSetVariable(const char* output) = 0;
527 * Returns a value that the user can edit.
529 virtual QString editableValue(VarTree* value);
531 protected:
532 /** Removes all commands from the low-priority queue. */
533 void flushLoPriQueue();
534 /** Removes all commands from the high-priority queue. */
535 void flushHiPriQueue();
537 QQueue<CmdQueueItem> m_hipriCmdQueue;
538 QList<CmdQueueItem> m_lopriCmdQueue;
540 * The active command is kept separately from other pending commands.
542 CmdQueueItem* m_activeCmd;
544 * Helper function that queues the given command string in the
545 * low-priority queue.
547 CmdQueueItem* queueCmdString(DbgCommand cmd, QString cmdString,
548 QueueMode mode);
550 * Helper function that queues the given command string in the
551 * high-priority queue.
553 CmdQueueItem* executeCmdString(DbgCommand cmd, QString cmdString,
554 bool clearLow);
555 void writeCommand();
556 virtual void commandFinished(CmdQueueItem* cmd) = 0;
558 protected:
559 /** @internal */
560 virtual int commSetupDoneC();
562 char m_prompt[10];
563 size_t m_promptMinLen;
564 char m_promptLastChar;
565 QRegExp m_promptRE;
567 // log file
568 QString m_logFileName;
569 QFile m_logFile;
571 public slots:
572 void dequeueCmdByVar(VarTree* var);
574 protected slots:
575 virtual void slotReceiveOutput(KProcess*, char* buffer, int buflen);
576 virtual void slotCommandRead(KProcess*);
577 virtual void slotExited(KProcess*);
579 signals:
581 * This signal is emitted when the output of a command has been fully
582 * collected and is ready to be interpreted.
584 void commandReceived(CmdQueueItem* cmd, const char* output);
587 * This signal is emitted when the debugger recognizes that a specific
588 * location in a file ought to be displayed.
590 * Gdb's --fullname option supports this for the step, next, frame, and
591 * run commands (and possibly others).
593 * @param file specifies the file; this is not necessarily a full path
594 * name, and if it is relative, you won't know relative to what, you
595 * can only guess.
596 * @param lineNo specifies the line number (0-based!) (this may be
597 * negative, in which case the file should be activated, but the line
598 * should NOT be changed).
599 * @param address specifies the exact address of the PC or is empty.
601 void activateFileLine(const QString& file, int lineNo, const DbgAddr& address);
604 * This signal is emitted when a command that starts the inferior has
605 * been submitted to the debugger.
607 void inferiorRunning();
610 * This signal is emitted when all output from the debugger has been
611 * consumed and no more commands are in the queues.
613 void enterIdleState();
616 #endif // DBGDRIVER_H