3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
21 * A type representing an address.
28 DbgAddr(const QString
& aa
);
29 DbgAddr(const DbgAddr
& src
) : a(src
.a
), fnoffs(src
.fnoffs
) { }
30 void operator=(const QString
& aa
);
31 void operator=(const DbgAddr
& src
) { a
= src
.a
; fnoffs
= src
.fnoffs
; }
32 QString
asString() const;
33 bool isEmpty() const { return a
.isEmpty(); }
37 bool operator==(const DbgAddr
& a1
, const DbgAddr
& a2
);
38 bool operator>(const DbgAddr
& a1
, const DbgAddr
& a2
);
66 DCuntil
, /* line number is zero-based! */
69 DCbreakline
, /* line number is zero-based! */
70 DCtbreakline
, /* line number is zero-based! */
91 RDNstdin
= 0x1, /* redirect stdin to /dev/null */
92 RDNstdout
= 0x2, /* redirect stdout to /dev/null */
93 RDNstderr
= 0x4 /* redirect stderr to /dev/null */
97 * How the memory dump is formated. The lowest 4 bits define the size of
98 * the entities. The higher bits define how these are formatted. Note that
99 * not all combinations make sense.
101 enum MemoryDumpType
{
123 * Debugger commands are placed in a queue. Only one command at a time is
124 * sent down to the debugger. All other commands in the queue are retained
125 * until the sent command has been processed by gdb. The debugger tells us
126 * that it's done with the command by sending the prompt. The output of the
127 * debugger is parsed at that time. Then, if more commands are in the
128 * queue, the next one is sent to the debugger.
134 bool m_committed
; /* just a debugging aid */
135 // remember which expression when printing an expression
138 // remember file position
141 // whether command was emitted due to direct user request (only set when relevant)
144 CmdQueueItem(DbgCommand cmd
, const QString
& str
) :
156 * The information about a breakpoint that is parsed from the list of
161 int id
; /* gdb's number */
163 breakpoint
, watchpoint
168 DbgAddr address
; /* exact address of breakpoint */
169 QString condition
; /* condition as printed by gdb */
170 int ignoreCount
; /* ignore next that may hits */
171 int hitCount
; /* as reported by gdb */
172 // the following items repeat the location, but in a better usable way
174 int lineNo
; /* zero-based line number */
178 * Information about a stack frame.
183 int lineNo
; /* zero-based line number */
184 DbgAddr address
; /* exact address of PC */
188 * The information about a stack frame as parsed from the backtrace.
190 struct StackFrame
: FrameInfo
193 VarTree
* var
; /* more information if non-zero */
194 StackFrame() : var(0) { }
199 * The information about a thread as parsed from the threads list.
201 struct ThreadInfo
: FrameInfo
203 int id
; /* gdb's number */
204 QString threadName
; /* the SYSTAG */
205 QString function
; /* where thread is halted */
206 bool hasFocus
; /* the thread whose stack we are watching */
210 * Register information
216 QString cookedValue
; /* may be empty */
222 struct DisassembledCode
238 * This is an abstract base class for debugger process.
240 * This class represents the debugger program. It provides the low-level
241 * interface to the commandline debugger. As such it implements the
242 * commands and parses the output.
244 class DebuggerDriver
: public KProcess
249 virtual ~DebuggerDriver() = 0;
251 virtual QString
driverName() const = 0;
253 * Returns the default command string to invoke the debugger driver.
255 virtual QString
defaultInvocation() const = 0;
257 virtual bool startup(QString cmdStr
);
258 void dequeueCmdByVar(VarTree
* var
);
259 void setLogFileName(const QString
& fname
) { m_logFileName
= fname
; }
265 DSidle
, /* gdb waits for input */
266 DSinterrupted
, /* a command was interrupted */
267 DSrunningLow
, /* gdb is running a low-priority command */
268 DSrunning
, /* gdb waits for program */
269 DScommandSent
, /* command has been sent, we wait for wroteStdin signal */
270 DScommandSentLow
/* low-prioritycommand has been sent */
272 DebuggerState m_state
;
275 bool isIdle() const { return m_state
== DSidle
; }
277 * Tells whether a high prority command would be executed immediately.
279 bool canExecuteImmediately() const { return m_hipriCmdQueue
.isEmpty(); }
282 char* m_output
; /* normal gdb output */
283 int m_outputLen
; /* current accumulated output */
284 int m_outputAlloc
; /* space available in m_gdbOutput */
286 typedef QString DelayedStr
;
288 typedef QCString DelayedStr
;
290 QQueue
<DelayedStr
> m_delayedOutput
; /* output colleced while we have receivedOutput */
291 /* but before signal wroteStdin arrived */
295 * Enqueues a high-priority command. High-priority commands are
296 * executed before any low-priority commands. No user interaction is
297 * possible as long as there is a high-priority command in the queue.
299 virtual CmdQueueItem
* executeCmd(DbgCommand
,
300 bool clearLow
= false) = 0;
301 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg
,
302 bool clearLow
= false) = 0;
303 virtual CmdQueueItem
* executeCmd(DbgCommand
, int intArg
,
304 bool clearLow
= false) = 0;
305 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg
, int intArg
,
306 bool clearLow
= false) = 0;
307 virtual CmdQueueItem
* executeCmd(DbgCommand
, QString strArg1
, QString strArg2
,
308 bool clearLow
= false) = 0;
309 virtual CmdQueueItem
* executeCmd(DbgCommand
, int intArg1
, int intArg2
,
310 bool clearLow
= false) = 0;
313 QMnormal
, /* queues the command last */
314 QMoverride
, /* removes an already queued command */
315 QMoverrideMoreEqual
/* ditto, also puts the command first in the queue */
319 * Enqueues a low-priority command. Low-priority commands are executed
320 * after any high-priority commands.
322 virtual CmdQueueItem
* queueCmd(DbgCommand
,
324 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg
,
326 virtual CmdQueueItem
* queueCmd(DbgCommand
, int intArg
,
328 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg
, int intArg
,
330 virtual CmdQueueItem
* queueCmd(DbgCommand
, QString strArg1
, QString strArg2
,
334 * Flushes the command queues.
335 * @param hipriOnly if true, only the high priority queue is flushed.
337 virtual void flushCommands(bool hipriOnly
= false);
340 * Terminates the debugger process.
342 virtual void terminate() = 0;
345 * Terminates the debugger process, but also detaches any program that
346 * it has been attached to.
348 virtual void detachAndTerminate() = 0;
351 * Interrupts the debuggee.
353 virtual void interruptInferior() = 0;
356 * Parses the output as an array of QChars.
358 virtual VarTree
* parseQCharArray(const char* output
, bool wantErrorValue
) = 0;
361 * Parses a back-trace (the output of the DCbt command).
363 virtual void parseBackTrace(const char* output
, QList
<StackFrame
>& stack
) = 0;
366 * Parses the output of the DCframe command;
367 * @param frameNo Returns the frame number.
368 * @param file Returns the source file name.
369 * @param lineNo The zero-based line number.
370 * @param address Returns the exact address.
371 * @return false if the frame could not be parsed successfully. The
372 * output values are undefined in this case.
374 virtual bool parseFrameChange(const char* output
, int& frameNo
,
375 QString
& file
, int& lineNo
, DbgAddr
& address
) = 0;
378 * Parses a list of breakpoints.
379 * @param output The output of the debugger.
380 * @param brks The list of new #Breakpoint objects. The list
381 * must initially be empty.
382 * @return False if there was an error before the first breakpoint
383 * was found. Even if true is returned, #brks may be empty.
385 virtual bool parseBreakList(const char* output
, QList
<Breakpoint
>& brks
) = 0;
388 * Parses a list of threads.
389 * @param output The output of the debugger.
390 * @param threads The list of new #ThreadInfo objects. The list
391 * must initially be empty.
392 * @return False if there was an error before the first thread entry
393 * was found. Even if true is returned, #threads may be empty.
395 virtual bool parseThreadList(const char* output
, QList
<ThreadInfo
>& threads
) = 0;
398 * Parses the output when the program stops to see whether this it
399 * stopped due to a breakpoint.
400 * @param output The output of the debugger.
401 * @param id Returns the breakpoint id.
402 * @param file Returns the file name in which the breakpoint is.
403 * @param lineNo Returns he zero-based line number of the breakpoint.
404 * @return False if there was no breakpoint.
406 virtual bool parseBreakpoint(const char* output
, int& id
,
407 QString
& file
, int& lineNo
) = 0;
410 * Parses the output of the DCinfolocals command.
411 * @param output The output of the debugger.
412 * @param newVars Receives the parsed variable values. The values are
413 * simply append()ed to the supplied list.
415 virtual void parseLocals(const char* output
, QList
<VarTree
>& newVars
) = 0;
418 * Parses the output of a DCprint or DCprintStruct command.
419 * @param output The output of the debugger.
420 * @param wantErrorValue Specifies whether the error message should be
421 * provided as the value of a NKplain variable. If this is false,
422 * #var will be 0 if the printed value is an error message.
423 * @param var Returns the variable value. It is set to 0 if there was
424 * a parse error or if the output is an error message and wantErrorValue
425 * is false. If it is not 0, #var->text() will return junk and must be
426 * set using #var->setText().
427 * @return false if the output is an error message. Even if true is
428 * returned, #var might still be 0 (due to a parse error).
430 virtual bool parsePrintExpr(const char* output
, bool wantErrorValue
,
434 * Parses the output of the DCcd command.
435 * @return false if the message is an error message.
437 virtual bool parseChangeWD(const char* output
, QString
& message
) = 0;
440 * Parses the output of the DCexecutable command.
441 * @return false if an error occured.
443 virtual bool parseChangeExecutable(const char* output
, QString
& message
) = 0;
446 * Parses the output of the DCcorefile command.
447 * @return false if the core file was not loaded successfully.
449 virtual bool parseCoreFile(const char* output
) = 0;
452 SFrefreshSource
= 1, /* refresh of source code is needed */
453 SFrefreshBreak
= 2, /* refresh breakpoints */
454 SFrefreshThreads
= 4, /* refresh thread list */
455 SFprogramActive
= 128 /* program remains active */
458 * Parses the output of commands that execute (a piece of) the program.
459 * @return The inclusive OR of zero or more of the StopFlags.
461 virtual uint
parseProgramStopped(const char* output
, QString
& message
) = 0;
464 * Parses the output of the DCsharedlibs command.
466 virtual void parseSharedLibs(const char* output
, QStrList
& shlibs
) = 0;
469 * Parses the output of the DCfindType command.
470 * @return true if a type was found.
472 virtual bool parseFindType(const char* output
, QString
& type
) = 0;
475 * Parses the output of the DCinforegisters command.
477 virtual void parseRegisters(const char* output
, QList
<RegisterInfo
>& regs
) = 0;
480 * Parses the output of the DCinfoline command. Returns false if the
481 * two addresses could not be found.
483 virtual bool parseInfoLine(const char* output
,
484 QString
& addrFrom
, QString
& addrTo
) = 0;
487 * Parses the ouput of the DCdisassemble command.
489 virtual void parseDisassemble(const char* output
, QList
<DisassembledCode
>& code
) = 0;
492 * Parses a memory dump. Returns an empty string if no error was found;
493 * otherwise it contains an error message.
495 virtual QString
parseMemoryDump(const char* output
, QList
<MemoryDump
>& memdump
) = 0;
498 /** Removes all commands from the low-priority queue. */
499 void flushLoPriQueue();
500 /** Removes all commands from the high-priority queue. */
501 void flushHiPriQueue();
503 QQueue
<CmdQueueItem
> m_hipriCmdQueue
;
504 QList
<CmdQueueItem
> m_lopriCmdQueue
;
506 * The active command is kept separately from other pending commands.
508 CmdQueueItem
* m_activeCmd
;
510 * Helper function that queues the given command string in the
511 * low-priority queue.
513 CmdQueueItem
* queueCmdString(DbgCommand cmd
, QString cmdString
,
516 * Helper function that queues the given command string in the
517 * high-priority queue.
519 CmdQueueItem
* executeCmdString(DbgCommand cmd
, QString cmdString
,
522 virtual void commandFinished(CmdQueueItem
* cmd
) = 0;
526 virtual int commSetupDoneC();
530 char m_promptLastChar
;
533 QString m_logFileName
;
537 void slotReceiveOutput(KProcess
*, char* buffer
, int buflen
);
538 void slotCommandRead(KProcess
*);
539 void slotExited(KProcess
*);
543 * This signal is emitted when the output of a command has been fully
544 * collected and is ready to be interpreted.
546 void commandReceived(CmdQueueItem
* cmd
, const char* output
);
549 * This signal is emitted when the debugger recognizes that a specific
550 * location in a file ought to be displayed.
552 * Gdb's --fullname option supports this for the step, next, frame, and
553 * run commands (and possibly others).
555 * @param file specifies the file; this is not necessarily a full path
556 * name, and if it is relative, you won't know relative to what, you
558 * @param lineNo specifies the line number (0-based!) (this may be
559 * negative, in which case the file should be activated, but the line
560 * should NOT be changed).
561 * @param address specifies the exact address of the PC or is empty.
563 void activateFileLine(const QString
& file
, int lineNo
, const DbgAddr
& address
);
566 * This signal is emitted when a command that starts the inferior has
567 * been submitted to the debugger.
569 void inferiorRunning();
572 * This signal is emitted when all output from the debugger has been
573 * consumed and no more commands are in the queues.
575 void enterIdleState();
578 #endif // DBGDRIVER_H