3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
24 * A type representing an address.
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(); }
40 bool operator==(const DbgAddr
& a1
, const DbgAddr
& a2
);
41 bool operator>(const DbgAddr
& a1
, const DbgAddr
& a2
);
60 DCsetoption
, /* debugger options */
70 DCuntil
, /* line number is zero-based! */
73 DCbreakline
, /* line number is zero-based! */
74 DCtbreakline
, /* line number is zero-based! */
95 RDNstdin
= 0x1, /* redirect stdin to /dev/null */
96 RDNstdout
= 0x2, /* redirect stdout to /dev/null */
97 RDNstderr
= 0x4 /* redirect stderr to /dev/null */
101 * How the memory dump is formated. The lowest 4 bits define the size of
102 * the entities. The higher bits define how these are formatted. Note that
103 * not all combinations make sense.
105 enum MemoryDumpType
{
127 * Debugger commands are placed in a queue. Only one command at a time is
128 * sent down to the debugger. All other commands in the queue are retained
129 * until the sent command has been processed by gdb. The debugger tells us
130 * that it's done with the command by sending the prompt. The output of the
131 * debugger is parsed at that time. Then, if more commands are in the
132 * queue, the next one is sent to the debugger.
138 bool m_committed
; /* just a debugging aid */
139 // remember which expression when printing an expression
142 // remember file position
145 // whether command was emitted due to direct user request (only set when relevant)
148 CmdQueueItem(DbgCommand cmd
, const QString
& str
) :
160 * The information about a breakpoint that is parsed from the list of
165 int id
; /* gdb's number */
167 breakpoint
, watchpoint
172 DbgAddr address
; /* exact address of breakpoint */
173 QString condition
; /* condition as printed by gdb */
174 int ignoreCount
; /* ignore next that may hits */
175 int hitCount
; /* as reported by gdb */
176 // the following items repeat the location, but in a better usable way
178 int lineNo
; /* zero-based line number */
182 * Information about a stack frame.
187 int lineNo
; /* zero-based line number */
188 DbgAddr address
; /* exact address of PC */
192 * The information about a stack frame as parsed from the backtrace.
194 struct StackFrame
: FrameInfo
197 VarTree
* var
; /* more information if non-zero */
198 StackFrame() : var(0) { }
203 * The information about a thread as parsed from the threads list.
205 struct ThreadInfo
: FrameInfo
207 int id
; /* gdb's number */
208 QString threadName
; /* the SYSTAG */
209 QString function
; /* where thread is halted */
210 bool hasFocus
; /* the thread whose stack we are watching */
214 * Register information
220 QString cookedValue
; /* may be empty */
226 struct DisassembledCode
242 * This is an abstract base class for debugger process.
244 * This class represents the debugger program. It provides the low-level
245 * interface to the commandline debugger. As such it implements the
246 * commands and parses the output.
248 class DebuggerDriver
: public KProcess
253 virtual ~DebuggerDriver() = 0;
255 virtual QString
driverName() const = 0;
257 * Returns the default command string to invoke the debugger driver.
259 virtual QString
defaultInvocation() const = 0;
262 * Returns a list of options that can be turned on and off.
264 virtual QStringList
boolOptionList() const = 0;
266 virtual bool startup(QString cmdStr
);
267 void dequeueCmdByVar(VarTree
* var
);
268 void setLogFileName(const QString
& fname
) { m_logFileName
= fname
; }
274 DSidle
, /* gdb waits for input */
275 DSinterrupted
, /* a command was interrupted */
276 DSrunningLow
, /* gdb is running a low-priority command */
277 DSrunning
, /* gdb waits for program */
278 DScommandSent
, /* command has been sent, we wait for wroteStdin signal */
279 DScommandSentLow
/* low-prioritycommand has been sent */
281 DebuggerState m_state
;
284 bool isIdle() const { return m_state
== DSidle
; }
286 * Tells whether a high prority command would be executed immediately.
288 bool canExecuteImmediately() const { return m_hipriCmdQueue
.isEmpty(); }
291 char* m_output
; /* normal gdb output */
292 size_t m_outputLen
; /* amount of data so far accumulated in m_output */
293 size_t m_outputAlloc
; /* space available in m_output */
294 typedef QCString DelayedStr
;
295 QQueue
<DelayedStr
> m_delayedOutput
; /* output colleced while we have receivedOutput */
296 /* but before signal wroteStdin arrived */
300 * Enqueues a high-priority command. High-priority commands are
301 * executed before any low-priority commands. No user interaction is
302 * possible as long as there is a high-priority command in the queue.
304 virtual CmdQueueItem
* executeCmd(DbgCommand
,
305 bool clearLow
= false) = 0;
306 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg
,
307 bool clearLow
= false) = 0;
308 virtual CmdQueueItem
* executeCmd(DbgCommand
, int intArg
,
309 bool clearLow
= false) = 0;
310 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg
, int intArg
,
311 bool clearLow
= false) = 0;
312 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg1
, QString strArg2
,
313 bool clearLow
= false) = 0;
314 virtual CmdQueueItem
* executeCmd(DbgCommand
, int intArg1
, int intArg2
,
315 bool clearLow
= false) = 0;
318 QMnormal
, /* queues the command last */
319 QMoverride
, /* removes an already queued command */
320 QMoverrideMoreEqual
/* ditto, also puts the command first in the queue */
324 * Enqueues a low-priority command. Low-priority commands are executed
325 * after any high-priority commands.
327 virtual CmdQueueItem
* queueCmd(DbgCommand
,
329 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg
,
331 virtual CmdQueueItem
* queueCmd(DbgCommand
, int intArg
,
333 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg
, int intArg
,
335 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg1
, QString strArg2
,
339 * Flushes the command queues.
340 * @param hipriOnly if true, only the high priority queue is flushed.
342 virtual void flushCommands(bool hipriOnly
= false);
345 * Terminates the debugger process.
347 virtual void terminate() = 0;
350 * Terminates the debugger process, but also detaches any program that
351 * it has been attached to.
353 virtual void detachAndTerminate() = 0;
356 * Interrupts the debuggee.
358 virtual void interruptInferior() = 0;
361 * Parses the output as an array of QChars.
363 virtual VarTree
* parseQCharArray(const char* output
, bool wantErrorValue
, bool qt3like
) = 0;
366 * Parses a back-trace (the output of the DCbt command).
368 virtual void parseBackTrace(const char* output
, QList
<StackFrame
>& stack
) = 0;
371 * Parses the output of the DCframe command;
372 * @param frameNo Returns the frame number.
373 * @param file Returns the source file name.
374 * @param lineNo The zero-based line number.
375 * @param address Returns the exact address.
376 * @return false if the frame could not be parsed successfully. The
377 * output values are undefined in this case.
379 virtual bool parseFrameChange(const char* output
, int& frameNo
,
380 QString
& file
, int& lineNo
, DbgAddr
& address
) = 0;
383 * Parses a list of breakpoints.
384 * @param output The output of the debugger.
385 * @param brks The list of new #Breakpoint objects. The list
386 * must initially be empty.
387 * @return False if there was an error before the first breakpoint
388 * was found. Even if true is returned, #brks may be empty.
390 virtual bool parseBreakList(const char* output
, QList
<Breakpoint
>& brks
) = 0;
393 * Parses a list of threads.
394 * @param output The output of the debugger.
395 * @param threads The list of new #ThreadInfo objects. The list
396 * must initially be empty.
397 * @return False if there was an error before the first thread entry
398 * was found. Even if true is returned, #threads may be empty.
400 virtual bool parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
) = 0;
403 * Parses the output when the program stops to see whether this it
404 * stopped due to a breakpoint.
405 * @param output The output of the debugger.
406 * @param id Returns the breakpoint id.
407 * @param file Returns the file name in which the breakpoint is.
408 * @param lineNo Returns he zero-based line number of the breakpoint.
409 * @return False if there was no breakpoint.
411 virtual bool parseBreakpoint(const char* output
, int& id
,
412 QString
& file
, int& lineNo
) = 0;
415 * Parses the output of the DCinfolocals command.
416 * @param output The output of the debugger.
417 * @param newVars Receives the parsed variable values. The values are
418 * simply append()ed to the supplied list.
420 virtual void parseLocals(const char* output
, QList
<VarTree
>& newVars
) = 0;
423 * Parses the output of a DCprint or DCprintStruct command.
424 * @param output The output of the debugger.
425 * @param wantErrorValue Specifies whether the error message should be
426 * provided as the value of a NKplain variable. If this is false,
427 * #var will be 0 if the printed value is an error message.
428 * @param var Returns the variable value. It is set to 0 if there was
429 * a parse error or if the output is an error message and wantErrorValue
430 * is false. If it is not 0, #var->text() will return junk and must be
431 * set using #var->setText().
432 * @return false if the output is an error message. Even if true is
433 * returned, #var might still be 0 (due to a parse error).
435 virtual bool parsePrintExpr(const char* output
, bool wantErrorValue
,
439 * Parses the output of the DCcd command.
440 * @return false if the message is an error message.
442 virtual bool parseChangeWD(const char* output
, QString
& message
) = 0;
445 * Parses the output of the DCexecutable command.
446 * @return false if an error occured.
448 virtual bool parseChangeExecutable(const char* output
, QString
& message
) = 0;
451 * Parses the output of the DCcorefile command.
452 * @return false if the core file was not loaded successfully.
454 virtual bool parseCoreFile(const char* output
) = 0;
457 SFrefreshSource
= 1, /* refresh of source code is needed */
458 SFrefreshBreak
= 2, /* refresh breakpoints */
459 SFrefreshThreads
= 4, /* refresh thread list */
460 SFprogramActive
= 128 /* program remains active */
463 * Parses the output of commands that execute (a piece of) the program.
464 * @return The inclusive OR of zero or more of the StopFlags.
466 virtual uint
parseProgramStopped(const char* output
, QString
& message
) = 0;
469 * Parses the output of the DCsharedlibs command.
471 virtual void parseSharedLibs(const char* output
, QStrList
& shlibs
) = 0;
474 * Parses the output of the DCfindType command.
475 * @return true if a type was found.
477 virtual bool parseFindType(const char* output
, QString
& type
) = 0;
480 * Parses the output of the DCinforegisters command.
482 virtual void parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
) = 0;
485 * Parses the output of the DCinfoline command. Returns false if the
486 * two addresses could not be found.
488 virtual bool parseInfoLine(const char* output
,
489 QString
& addrFrom
, QString
& addrTo
) = 0;
492 * Parses the ouput of the DCdisassemble command.
494 virtual void parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
) = 0;
497 * Parses a memory dump. Returns an empty string if no error was found;
498 * otherwise it contains an error message.
500 virtual QString
parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
) = 0;
503 /** Removes all commands from the low-priority queue. */
504 void flushLoPriQueue();
505 /** Removes all commands from the high-priority queue. */
506 void flushHiPriQueue();
508 QQueue
<CmdQueueItem
> m_hipriCmdQueue
;
509 QList
<CmdQueueItem
> m_lopriCmdQueue
;
511 * The active command is kept separately from other pending commands.
513 CmdQueueItem
* m_activeCmd
;
515 * Helper function that queues the given command string in the
516 * low-priority queue.
518 CmdQueueItem
* queueCmdString(DbgCommand cmd
, QString cmdString
,
521 * Helper function that queues the given command string in the
522 * high-priority queue.
524 CmdQueueItem
* executeCmdString(DbgCommand cmd
, QString cmdString
,
527 virtual void commandFinished(CmdQueueItem
* cmd
) = 0;
531 virtual int commSetupDoneC();
534 size_t m_promptMinLen
;
535 char m_promptLastChar
;
539 QString m_logFileName
;
543 virtual void slotReceiveOutput(KProcess
*, char* buffer
, int buflen
);
544 virtual void slotCommandRead(KProcess
*);
545 virtual void slotExited(KProcess
*);
549 * This signal is emitted when the output of a command has been fully
550 * collected and is ready to be interpreted.
552 void commandReceived(CmdQueueItem
* cmd
, const char* output
);
555 * This signal is emitted when the debugger recognizes that a specific
556 * location in a file ought to be displayed.
558 * Gdb's --fullname option supports this for the step, next, frame, and
559 * run commands (and possibly others).
561 * @param file specifies the file; this is not necessarily a full path
562 * name, and if it is relative, you won't know relative to what, you
564 * @param lineNo specifies the line number (0-based!) (this may be
565 * negative, in which case the file should be activated, but the line
566 * should NOT be changed).
567 * @param address specifies the exact address of the PC or is empty.
569 void activateFileLine(const QString
& file
, int lineNo
, const DbgAddr
& address
);
572 * This signal is emitted when a command that starts the inferior has
573 * been submitted to the debugger.
575 void inferiorRunning();
578 * This signal is emitted when all output from the debugger has been
579 * consumed and no more commands are in the queues.
581 void enterIdleState();
584 #endif // DBGDRIVER_H