Store the queue of pending watch expressions as strings.
[kdbg.git] / kdbg / debugger.cpp
blobcd0ff69d3f6ed8f0520a30f03654fcbd93df6ec6
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include "debugger.h"
7 #include "dbgdriver.h"
8 #include "pgmargs.h"
9 #include "typetable.h"
10 #include "exprwnd.h"
11 #include "pgmsettings.h"
12 #include "programconfig.h"
13 #include "valarray.h"
14 #include <qregexp.h>
15 #include <qfileinfo.h>
16 #include <qlistbox.h>
17 #include <qstringlist.h>
18 #include <kapp.h>
19 #include <kconfig.h>
20 #include <klocale.h> /* i18n */
21 #include <kmessagebox.h>
22 #include <ctype.h>
23 #include <stdlib.h> /* strtol, atoi */
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h> /* sleep(3) */
26 #endif
27 #include "mydebug.h"
30 KDebugger::KDebugger(QWidget* parent,
31 ExprWnd* localVars,
32 ExprWnd* watchVars,
33 QListBox* backtrace) :
34 QObject(parent, "debugger"),
35 m_ttyLevel(ttyFull),
36 m_memoryFormat(MDTword | MDThex),
37 m_haveExecutable(false),
38 m_programActive(false),
39 m_programRunning(false),
40 m_sharedLibsListed(false),
41 m_typeTable(0),
42 m_programConfig(0),
43 m_d(0),
44 m_localVariables(*localVars),
45 m_watchVariables(*watchVars),
46 m_btWindow(*backtrace)
48 m_envVars.setAutoDelete(true);
49 m_brkpts.setAutoDelete(true);
51 connect(&m_localVariables, SIGNAL(expanding(KTreeViewItem*,bool&)),
52 SLOT(slotLocalsExpanding(KTreeViewItem*,bool&)));
53 connect(&m_watchVariables, SIGNAL(expanding(KTreeViewItem*,bool&)),
54 SLOT(slotWatchExpanding(KTreeViewItem*,bool&)));
55 connect(&m_localVariables, SIGNAL(editValueCommitted(int, const QString&)),
56 SLOT(slotValueEdited(int, const QString&)));
57 connect(&m_watchVariables, SIGNAL(editValueCommitted(int, const QString&)),
58 SLOT(slotValueEdited(int, const QString&)));
60 connect(&m_btWindow, SIGNAL(highlighted(int)), SLOT(gotoFrame(int)));
62 emit updateUI();
65 KDebugger::~KDebugger()
67 if (m_programConfig != 0) {
68 saveProgramSettings();
69 m_programConfig->sync();
70 delete m_programConfig;
73 delete m_typeTable;
77 void KDebugger::saveSettings(KConfig* /*config*/)
81 void KDebugger::restoreSettings(KConfig* /*config*/)
86 //////////////////////////////////////////////////////////////////////
87 // external interface
89 const char GeneralGroup[] = "General";
90 const char DebuggerCmdStr[] = "DebuggerCmdStr";
91 const char TTYLevelEntry[] = "TTYLevel";
92 const char KDebugger::DriverNameEntry[] = "DriverName";
94 bool KDebugger::debugProgram(const QString& name,
95 DebuggerDriver* driver)
97 if (m_d != 0 && m_d->isRunning())
99 QApplication::setOverrideCursor(waitCursor);
101 stopDriver();
103 QApplication::restoreOverrideCursor();
105 if (m_d->isRunning() || m_haveExecutable) {
106 /* timed out! We can't really do anything useful now */
107 TRACE("timed out while waiting for gdb to die!");
108 return false;
110 delete m_d;
111 m_d = 0;
114 // wire up the driver
115 connect(driver, SIGNAL(activateFileLine(const QString&,int,const DbgAddr&)),
116 this, SIGNAL(activateFileLine(const QString&,int,const DbgAddr&)));
117 connect(driver, SIGNAL(processExited(KProcess*)), SLOT(gdbExited(KProcess*)));
118 connect(driver, SIGNAL(commandReceived(CmdQueueItem*,const char*)),
119 SLOT(parse(CmdQueueItem*,const char*)));
120 connect(driver, SIGNAL(wroteStdin(KProcess*)), SIGNAL(updateUI()));
121 connect(driver, SIGNAL(inferiorRunning()), SLOT(slotInferiorRunning()));
122 connect(driver, SIGNAL(enterIdleState()), SLOT(backgroundUpdate()));
123 connect(driver, SIGNAL(enterIdleState()), SIGNAL(updateUI()));
124 connect(&m_localVariables, SIGNAL(removingItem(VarTree*)),
125 driver, SLOT(dequeueCmdByVar(VarTree*)));
126 connect(&m_watchVariables, SIGNAL(removingItem(VarTree*)),
127 driver, SLOT(dequeueCmdByVar(VarTree*)));
129 // create the program settings object
130 openProgramConfig(name);
132 // get debugger command from per-program settings
133 if (m_programConfig != 0) {
134 m_programConfig->setGroup(GeneralGroup);
135 m_debuggerCmd = readDebuggerCmd();
136 // get terminal emulation level
137 m_ttyLevel = TTYLevel(m_programConfig->readNumEntry(TTYLevelEntry, ttyFull));
139 // the rest is read in later in the handler of DCexecutable
141 m_d = driver;
143 if (!startDriver()) {
144 TRACE("startDriver failed");
145 m_d = 0;
146 return false;
149 TRACE("before file cmd");
150 m_d->executeCmd(DCexecutable, name);
151 m_executable = name;
153 // set remote target
154 if (!m_remoteDevice.isEmpty()) {
155 m_d->executeCmd(DCtargetremote, m_remoteDevice);
156 m_d->queueCmd(DCbt, DebuggerDriver::QMoverride);
157 m_d->queueCmd(DCframe, 0, DebuggerDriver::QMnormal);
158 m_programActive = true;
159 m_haveExecutable = true;
162 // create a type table
163 m_typeTable = new ProgramTypeTable;
164 m_sharedLibsListed = false;
166 emit updateUI();
168 return true;
171 void KDebugger::shutdown()
173 // shut down debugger driver
174 if (m_d != 0 && m_d->isRunning())
176 stopDriver();
180 void KDebugger::useCoreFile(QString corefile, bool batch)
182 m_corefile = corefile;
183 if (!batch) {
184 CmdQueueItem* cmd = loadCoreFile();
185 cmd->m_byUser = true;
189 void KDebugger::setAttachPid(const QString& pid)
191 m_attachedPid = pid;
194 void KDebugger::programRun()
196 if (!isReady())
197 return;
199 // when program is active, but not a core file, continue
200 // otherwise run the program
201 if (m_programActive && m_corefile.isEmpty()) {
202 // gdb command: continue
203 m_d->executeCmd(DCcont, true);
204 } else {
205 // gdb command: run
206 m_d->executeCmd(DCrun, true);
207 m_corefile = QString();
208 m_programActive = true;
210 m_programRunning = true;
213 void KDebugger::attachProgram(const QString& pid)
215 if (!isReady())
216 return;
218 m_attachedPid = pid;
219 TRACE("Attaching to " + m_attachedPid);
220 m_d->executeCmd(DCattach, m_attachedPid);
221 m_programActive = true;
222 m_programRunning = true;
225 void KDebugger::programRunAgain()
227 if (canSingleStep()) {
228 m_d->executeCmd(DCrun, true);
229 m_corefile = QString();
230 m_programRunning = true;
234 void KDebugger::programStep()
236 if (canSingleStep()) {
237 m_d->executeCmd(DCstep, true);
238 m_programRunning = true;
242 void KDebugger::programNext()
244 if (canSingleStep()) {
245 m_d->executeCmd(DCnext, true);
246 m_programRunning = true;
250 void KDebugger::programStepi()
252 if (canSingleStep()) {
253 m_d->executeCmd(DCstepi, true);
254 m_programRunning = true;
258 void KDebugger::programNexti()
260 if (canSingleStep()) {
261 m_d->executeCmd(DCnexti, true);
262 m_programRunning = true;
266 void KDebugger::programFinish()
268 if (canSingleStep()) {
269 m_d->executeCmd(DCfinish, true);
270 m_programRunning = true;
274 void KDebugger::programKill()
276 if (haveExecutable() && isProgramActive()) {
277 if (m_programRunning) {
278 m_d->interruptInferior();
280 // this is an emergency command; flush queues
281 m_d->flushCommands(true);
282 m_d->executeCmd(DCkill, true);
286 bool KDebugger::runUntil(const QString& fileName, int lineNo)
288 if (isReady() && m_programActive && !m_programRunning) {
289 // strip off directory part of file name
290 QString file = fileName;
291 int offset = file.findRev("/");
292 if (offset >= 0) {
293 file.remove(0, offset+1);
295 m_d->executeCmd(DCuntil, file, lineNo, true);
296 m_programRunning = true;
297 return true;
298 } else {
299 return false;
303 void KDebugger::programBreak()
305 if (m_haveExecutable && m_programRunning) {
306 m_d->interruptInferior();
310 void KDebugger::programArgs(QWidget* parent)
312 if (m_haveExecutable) {
313 QStringList allOptions = m_d->boolOptionList();
314 PgmArgs dlg(parent, m_executable, m_envVars, allOptions);
315 dlg.setArgs(m_programArgs);
316 dlg.setWd(m_programWD);
317 dlg.setOptions(m_boolOptions);
318 if (dlg.exec()) {
319 updateProgEnvironment(dlg.args(), dlg.wd(),
320 dlg.envVars(), dlg.options());
325 void KDebugger::programSettings(QWidget* parent)
327 if (!m_haveExecutable)
328 return;
330 ProgramSettings dlg(parent, m_executable);
332 dlg.m_chooseDriver.setDebuggerCmd(m_debuggerCmd);
333 dlg.m_output.setTTYLevel(m_ttyLevel);
335 if (dlg.exec() == QDialog::Accepted)
337 m_debuggerCmd = dlg.m_chooseDriver.debuggerCmd();
338 m_ttyLevel = TTYLevel(dlg.m_output.ttyLevel());
342 bool KDebugger::setBreakpoint(QString file, int lineNo,
343 const DbgAddr& address, bool temporary)
345 if (!isReady()) {
346 return false;
349 Breakpoint* bp = breakpointByFilePos(file, lineNo, address);
350 if (bp == 0)
353 * No such breakpoint, so set a new one. If we have an address, we
354 * set the breakpoint exactly there. Otherwise we use the file name
355 * plus line no.
357 Breakpoint* bp = new Breakpoint;
358 bp->temporary = temporary;
360 if (address.isEmpty())
362 bp->fileName = file;
363 bp->lineNo = lineNo;
365 else
367 bp->address = address;
369 setBreakpoint(bp, false);
371 else
374 * If the breakpoint is disabled, enable it; if it's enabled,
375 * delete that breakpoint.
377 if (bp->enabled) {
378 deleteBreakpoint(bp);
379 } else {
380 enableDisableBreakpoint(bp);
383 return true;
386 void KDebugger::setBreakpoint(Breakpoint* bp, bool queueOnly)
388 CmdQueueItem* cmd;
389 if (!bp->text.isEmpty())
392 * The breakpoint was set using the text box in the breakpoint
393 * list. This is the only way in which watchpoints are set.
395 if (bp->type == Breakpoint::watchpoint) {
396 cmd = m_d->executeCmd(DCwatchpoint, bp->text);
397 } else {
398 cmd = m_d->executeCmd(DCbreaktext, bp->text);
401 else if (bp->address.isEmpty())
403 // strip off directory part of file name
404 QString file = bp->fileName;
405 int offset = file.findRev("/");
406 if (offset >= 0) {
407 file.remove(0, offset+1);
409 if (queueOnly) {
410 cmd = m_d->queueCmd(bp->temporary ? DCtbreakline : DCbreakline,
411 file, bp->lineNo, DebuggerDriver::QMoverride);
412 } else {
413 cmd = m_d->executeCmd(bp->temporary ? DCtbreakline : DCbreakline,
414 file, bp->lineNo);
417 else
419 if (queueOnly) {
420 cmd = m_d->queueCmd(bp->temporary ? DCtbreakaddr : DCbreakaddr,
421 bp->address.asString(), DebuggerDriver::QMoverride);
422 } else {
423 cmd = m_d->executeCmd(bp->temporary ? DCtbreakaddr : DCbreakaddr,
424 bp->address.asString());
427 cmd->m_brkpt = bp; // used in newBreakpoint()
430 bool KDebugger::enableDisableBreakpoint(QString file, int lineNo,
431 const DbgAddr& address)
433 Breakpoint* bp = breakpointByFilePos(file, lineNo, address);
434 return bp == 0 || enableDisableBreakpoint(bp);
437 bool KDebugger::enableDisableBreakpoint(Breakpoint* bp)
440 * Toggle enabled/disabled state.
442 * The driver is not bothered if we are modifying an orphaned
443 * breakpoint.
445 if (!bp->isOrphaned()) {
446 if (!canChangeBreakpoints()) {
447 return false;
449 m_d->executeCmd(bp->enabled ? DCdisable : DCenable, bp->id);
450 } else {
451 bp->enabled = !bp->enabled;
452 emit breakpointsChanged();
454 return true;
457 bool KDebugger::conditionalBreakpoint(Breakpoint* bp,
458 const QString& condition,
459 int ignoreCount)
462 * Change the condition and ignore count.
464 * The driver is not bothered if we are removing an orphaned
465 * breakpoint.
468 if (!bp->isOrphaned()) {
469 if (!canChangeBreakpoints()) {
470 return false;
473 bool changed = false;
475 if (bp->condition != condition) {
476 // change condition
477 m_d->executeCmd(DCcondition, condition, bp->id);
478 changed = true;
480 if (bp->ignoreCount != ignoreCount) {
481 // change ignore count
482 m_d->executeCmd(DCignore, bp->id, ignoreCount);
483 changed = true;
485 if (changed) {
486 // get the changes
487 m_d->queueCmd(DCinfobreak, DebuggerDriver::QMoverride);
489 } else {
490 bp->condition = condition;
491 bp->ignoreCount = ignoreCount;
492 emit breakpointsChanged();
494 return true;
497 bool KDebugger::deleteBreakpoint(Breakpoint* bp)
500 * Remove the breakpoint.
502 * The driver is not bothered if we are removing an orphaned
503 * breakpoint.
505 if (!bp->isOrphaned()) {
506 if (!canChangeBreakpoints()) {
507 return false;
509 m_d->executeCmd(DCdelete, bp->id);
510 } else {
511 // move the last entry to bp's slot and shorten the list
512 int i = m_brkpts.findRef(bp);
513 m_brkpts.insert(i, m_brkpts.take(m_brkpts.size()-1));
514 m_brkpts.resize(m_brkpts.size()-1);
515 emit breakpointsChanged();
517 return false;
520 bool KDebugger::canSingleStep()
522 return isReady() && m_programActive && !m_programRunning;
525 bool KDebugger::canChangeBreakpoints()
527 return isReady() && !m_programRunning;
530 bool KDebugger::canStart()
532 return isReady() && !m_programActive;
535 bool KDebugger::isReady() const
537 return m_haveExecutable &&
538 m_d != 0 && m_d->canExecuteImmediately();
541 bool KDebugger::isIdle() const
543 return m_d == 0 || m_d->isIdle();
547 //////////////////////////////////////////////////////////
548 // debugger driver
550 bool KDebugger::startDriver()
552 emit debuggerStarting(); /* must set m_inferiorTerminal */
555 * If the per-program command string is empty, use the global setting
556 * (which might also be empty, in which case the driver uses its
557 * default).
559 m_explicitKill = false;
560 if (!m_d->startup(m_debuggerCmd)) {
561 return false;
565 * If we have an output terminal, we use it. Otherwise we will run the
566 * program with input and output redirected to /dev/null. Other
567 * redirections are also necessary depending on the tty emulation
568 * level.
570 int redirect = RDNstdin|RDNstdout|RDNstderr; /* redirect everything */
571 if (!m_inferiorTerminal.isEmpty()) {
572 switch (m_ttyLevel) {
573 default:
574 case ttyNone:
575 // redirect everything
576 break;
577 case ttySimpleOutputOnly:
578 redirect = RDNstdin;
579 break;
580 case ttyFull:
581 redirect = 0;
582 break;
585 m_d->executeCmd(DCtty, m_inferiorTerminal, redirect);
587 return true;
590 void KDebugger::stopDriver()
592 m_explicitKill = true;
594 if (m_attachedPid.isEmpty()) {
595 m_d->terminate();
596 } else {
597 m_d->detachAndTerminate();
601 * We MUST wait until the slot gdbExited() has been called. But to
602 * avoid a deadlock, we wait only for some certain maximum time. Should
603 * this timeout be reached, the only reasonable thing one could do then
604 * is exiting kdbg.
606 kapp->processEvents(1000); /* ideally, this will already shut it down */
607 int maxTime = 20; /* about 20 seconds */
608 while (m_haveExecutable && maxTime > 0) {
609 // give gdb time to die (and send a SIGCLD)
610 ::sleep(1);
611 --maxTime;
612 kapp->processEvents(1000);
616 void KDebugger::gdbExited(KProcess*)
619 * Save settings, but only if gdb has already processed "info line
620 * main", otherwise we would save an empty config file, because it
621 * isn't read in until then!
623 if (m_programConfig != 0) {
624 if (m_haveExecutable) {
625 saveProgramSettings();
626 m_programConfig->sync();
628 delete m_programConfig;
629 m_programConfig = 0;
632 // erase types
633 delete m_typeTable;
634 m_typeTable = 0;
636 if (m_explicitKill) {
637 TRACE(m_d->driverName() + " exited normally");
638 } else {
639 QString msg = i18n("%1 exited unexpectedly.\n"
640 "Restart the session (e.g. with File|Executable).");
641 KMessageBox::error(parentWidget(), msg.arg(m_d->driverName()));
644 // reset state
645 m_haveExecutable = false;
646 m_executable = "";
647 m_programActive = false;
648 m_programRunning = false;
649 m_explicitKill = false;
650 m_debuggerCmd = QString(); /* use global setting at next start! */
651 m_attachedPid = QString(); /* we are no longer attached to a process */
652 m_ttyLevel = ttyFull;
653 m_brkpts.clear();
655 // erase PC
656 emit updatePC(QString(), -1, DbgAddr(), 0);
659 QString KDebugger::getConfigForExe(const QString& name)
661 QFileInfo fi(name);
662 QString pgmConfigFile = fi.dirPath(true);
663 if (!pgmConfigFile.isEmpty()) {
664 pgmConfigFile += '/';
666 pgmConfigFile += ".kdbgrc." + fi.fileName();
667 TRACE("program config file = " + pgmConfigFile);
668 return pgmConfigFile;
671 void KDebugger::openProgramConfig(const QString& name)
673 ASSERT(m_programConfig == 0);
675 QString pgmConfigFile = getConfigForExe(name);
677 m_programConfig = new ProgramConfig(pgmConfigFile);
680 const char EnvironmentGroup[] = "Environment";
681 const char WatchGroup[] = "Watches";
682 const char FileVersion[] = "FileVersion";
683 const char ProgramArgs[] = "ProgramArgs";
684 const char WorkingDirectory[] = "WorkingDirectory";
685 const char OptionsSelected[] = "OptionsSelected";
686 const char Variable[] = "Var%d";
687 const char Value[] = "Value%d";
688 const char ExprFmt[] = "Expr%d";
690 void KDebugger::saveProgramSettings()
692 ASSERT(m_programConfig != 0);
693 m_programConfig->setGroup(GeneralGroup);
694 m_programConfig->writeEntry(FileVersion, 1);
695 m_programConfig->writeEntry(ProgramArgs, m_programArgs);
696 m_programConfig->writeEntry(WorkingDirectory, m_programWD);
697 m_programConfig->writeEntry(OptionsSelected, m_boolOptions);
698 m_programConfig->writeEntry(DebuggerCmdStr, m_debuggerCmd);
699 m_programConfig->writeEntry(TTYLevelEntry, int(m_ttyLevel));
700 QString driverName;
701 if (m_d != 0)
702 driverName = m_d->driverName();
703 m_programConfig->writeEntry(DriverNameEntry, driverName);
705 // write environment variables
706 m_programConfig->deleteGroup(EnvironmentGroup);
707 m_programConfig->setGroup(EnvironmentGroup);
708 QDictIterator<EnvVar> it = m_envVars;
709 EnvVar* var;
710 QString varName;
711 QString varValue;
712 for (int i = 0; (var = it) != 0; ++it, ++i) {
713 varName.sprintf(Variable, i);
714 varValue.sprintf(Value, i);
715 m_programConfig->writeEntry(varName, it.currentKey());
716 m_programConfig->writeEntry(varValue, var->value);
719 saveBreakpoints(m_programConfig);
721 // watch expressions
722 // first get rid of whatever was in this group
723 m_programConfig->deleteGroup(WatchGroup);
724 // then start a new group
725 m_programConfig->setGroup(WatchGroup);
726 KTreeViewItem* item = m_watchVariables.itemAt(0);
727 int watchNum = 0;
728 for (; item != 0; item = item->getSibling(), ++watchNum) {
729 varName.sprintf(ExprFmt, watchNum);
730 m_programConfig->writeEntry(varName, item->getText());
733 // give others a chance
734 emit saveProgramSpecific(m_programConfig);
737 void KDebugger::restoreProgramSettings()
739 ASSERT(m_programConfig != 0);
740 m_programConfig->setGroup(GeneralGroup);
742 * We ignore file version for now we will use it in the future to
743 * distinguish different versions of this configuration file.
745 // m_debuggerCmd has been read in already
746 // m_ttyLevel has been read in already
747 QString pgmArgs = m_programConfig->readEntry(ProgramArgs);
748 QString pgmWd = m_programConfig->readEntry(WorkingDirectory);
749 QStringList boolOptions = m_programConfig->readListEntry(OptionsSelected);
750 m_boolOptions = QStringList();
752 // read environment variables
753 m_programConfig->setGroup(EnvironmentGroup);
754 m_envVars.clear();
755 QDict<EnvVar> pgmVars;
756 EnvVar* var;
757 QString varName;
758 QString varValue;
759 for (int i = 0;; ++i) {
760 varName.sprintf(Variable, i);
761 varValue.sprintf(Value, i);
762 if (!m_programConfig->hasKey(varName)) {
763 /* entry not present, assume that we've hit them all */
764 break;
766 QString name = m_programConfig->readEntry(varName);
767 if (name.isEmpty()) {
768 // skip empty names
769 continue;
771 var = new EnvVar;
772 var->value = m_programConfig->readEntry(varValue);
773 var->status = EnvVar::EVnew;
774 pgmVars.insert(name, var);
777 updateProgEnvironment(pgmArgs, pgmWd, pgmVars, boolOptions);
779 restoreBreakpoints(m_programConfig);
781 // watch expressions
782 m_programConfig->setGroup(WatchGroup);
783 m_watchVariables.clear();
784 for (int i = 0;; ++i) {
785 varName.sprintf(ExprFmt, i);
786 if (!m_programConfig->hasKey(varName)) {
787 /* entry not present, assume that we've hit them all */
788 break;
790 QString expr = m_programConfig->readEntry(varName);
791 if (expr.isEmpty()) {
792 // skip empty expressions
793 continue;
795 addWatch(expr);
798 // give others a chance
799 emit restoreProgramSpecific(m_programConfig);
803 * Reads the debugger command line from the program settings. The config
804 * group must have been set by the caller.
806 QString KDebugger::readDebuggerCmd()
808 QString debuggerCmd = m_programConfig->readEntry(DebuggerCmdStr);
810 // always let the user confirm the debugger cmd if we are root
811 if (::geteuid() == 0)
813 if (!debuggerCmd.isEmpty()) {
814 QString msg = i18n(
815 "The settings for this program specify "
816 "the following debugger command:\n%1\n"
817 "Shall this command be used?");
818 if (KMessageBox::warningYesNo(parentWidget(), msg.arg(debuggerCmd))
819 != KMessageBox::Yes)
821 // don't use it
822 debuggerCmd = QString();
826 return debuggerCmd;
830 * Breakpoints are saved one per group.
832 const char BPGroup[] = "Breakpoint %d";
833 const char File[] = "File";
834 const char Line[] = "Line";
835 const char Text[] = "Text";
836 const char Address[] = "Address";
837 const char Temporary[] = "Temporary";
838 const char Enabled[] = "Enabled";
839 const char Condition[] = "Condition";
841 void KDebugger::saveBreakpoints(ProgramConfig* config)
843 QString groupName;
844 int i = 0;
845 for (uint j = 0; j < m_brkpts.size(); j++) {
846 Breakpoint* bp = m_brkpts[j];
847 if (bp->type == Breakpoint::watchpoint)
848 continue; /* don't save watchpoints */
849 groupName.sprintf(BPGroup, i++);
851 /* remove remmants */
852 config->deleteGroup(groupName);
854 config->setGroup(groupName);
855 if (!bp->text.isEmpty()) {
857 * The breakpoint was set using the text box in the breakpoint
858 * list. We do not save the location by filename+line number,
859 * but instead honor what the user typed (a function name, for
860 * example, which could move between sessions).
862 config->writeEntry(Text, bp->text);
863 } else if (!bp->fileName.isEmpty()) {
864 config->writeEntry(File, bp->fileName);
865 config->writeEntry(Line, bp->lineNo);
867 * Addresses are hardly correct across sessions, so we don't
868 * save it.
870 } else {
871 config->writeEntry(Address, bp->address.asString());
873 config->writeEntry(Temporary, bp->temporary);
874 config->writeEntry(Enabled, bp->enabled);
875 if (!bp->condition.isEmpty())
876 config->writeEntry(Condition, bp->condition);
877 // we do not save the ignore count
879 // delete remaining groups
880 // we recognize that a group is present if there is an Enabled entry
881 for (;; i++) {
882 groupName.sprintf(BPGroup, i);
883 config->setGroup(groupName);
884 if (!config->hasKey(Enabled)) {
885 /* group not present, assume that we've hit them all */
886 break;
888 config->deleteGroup(groupName);
892 void KDebugger::restoreBreakpoints(ProgramConfig* config)
894 QString groupName;
896 * We recognize the end of the list if there is no Enabled entry
897 * present.
899 for (int i = 0;; i++) {
900 groupName.sprintf(BPGroup, i);
901 config->setGroup(groupName);
902 if (!config->hasKey(Enabled)) {
903 /* group not present, assume that we've hit them all */
904 break;
906 Breakpoint* bp = new Breakpoint;
907 bp->fileName = config->readEntry(File);
908 bp->lineNo = config->readNumEntry(Line, -1);
909 bp->text = config->readEntry(Text);
910 bp->address = config->readEntry(Address);
911 // check consistency
912 if ((bp->fileName.isEmpty() || bp->lineNo < 0) &&
913 bp->text.isEmpty() &&
914 bp->address.isEmpty())
916 delete bp;
917 continue;
919 bp->enabled = config->readBoolEntry(Enabled, true);
920 bp->temporary = config->readBoolEntry(Temporary, false);
921 bp->condition = config->readEntry(Condition);
924 * Add the breakpoint.
926 setBreakpoint(bp, false);
927 // the new breakpoint is disabled or conditionalized later
928 // in newBreakpoint()
930 m_d->queueCmd(DCinfobreak, DebuggerDriver::QMoverride);
934 // parse output of command cmd
935 void KDebugger::parse(CmdQueueItem* cmd, const char* output)
937 ASSERT(cmd != 0); /* queue mustn't be empty */
939 TRACE(QString(__PRETTY_FUNCTION__) + " parsing " + output);
941 switch (cmd->m_cmd) {
942 case DCtargetremote:
943 // the output (if any) is uninteresting
944 case DCsetargs:
945 case DCtty:
946 // there is no output
947 case DCsetenv:
948 case DCunsetenv:
949 case DCsetoption:
950 /* if value is empty, we see output, but we don't care */
951 break;
952 case DCcd:
953 /* display gdb's message in the status bar */
954 m_d->parseChangeWD(output, m_statusMessage);
955 emit updateStatusMessage();
956 break;
957 case DCinitialize:
958 break;
959 case DCexecutable:
960 if (m_d->parseChangeExecutable(output, m_statusMessage))
962 // success; restore breakpoints etc.
963 if (m_programConfig != 0) {
964 restoreProgramSettings();
966 // load file containing main() or core file
967 if (!m_corefile.isEmpty())
969 // load core file
970 loadCoreFile();
972 else if (!m_attachedPid.isEmpty())
974 m_d->queueCmd(DCattach, m_attachedPid, DebuggerDriver::QMoverride);
975 m_programActive = true;
976 m_programRunning = true;
978 else if (!m_remoteDevice.isEmpty())
980 // handled elsewhere
982 else
984 m_d->queueCmd(DCinfolinemain, DebuggerDriver::QMnormal);
986 if (!m_statusMessage.isEmpty())
987 emit updateStatusMessage();
988 } else {
989 QString msg = m_d->driverName() + ": " + m_statusMessage;
990 KMessageBox::sorry(parentWidget(), msg);
991 m_executable = "";
992 m_corefile = ""; /* don't process core file */
993 m_haveExecutable = false;
995 break;
996 case DCcorefile:
997 // in any event we have an executable at this point
998 m_haveExecutable = true;
999 if (m_d->parseCoreFile(output)) {
1000 // loading a core is like stopping at a breakpoint
1001 m_programActive = true;
1002 handleRunCommands(output);
1003 // do not reset m_corefile
1004 } else {
1005 // report error
1006 QString msg = m_d->driverName() + ": " + QString(output);
1007 KMessageBox::sorry(parentWidget(), msg);
1009 // if core file was loaded from command line, revert to info line main
1010 if (!cmd->m_byUser) {
1011 m_d->queueCmd(DCinfolinemain, DebuggerDriver::QMnormal);
1013 m_corefile = QString(); /* core file not available any more */
1015 break;
1016 case DCinfolinemain:
1017 // ignore the output, marked file info follows
1018 m_haveExecutable = true;
1019 break;
1020 case DCinfolocals:
1021 // parse local variables
1022 if (output[0] != '\0') {
1023 handleLocals(output);
1025 break;
1026 case DCinforegisters:
1027 handleRegisters(output);
1028 break;
1029 case DCexamine:
1030 handleMemoryDump(output);
1031 break;
1032 case DCinfoline:
1033 handleInfoLine(cmd, output);
1034 break;
1035 case DCdisassemble:
1036 handleDisassemble(cmd, output);
1037 break;
1038 case DCframe:
1039 handleFrameChange(output);
1040 updateAllExprs();
1041 break;
1042 case DCbt:
1043 handleBacktrace(output);
1044 updateAllExprs();
1045 break;
1046 case DCprint:
1047 handlePrint(cmd, output);
1048 break;
1049 case DCprintDeref:
1050 handlePrintDeref(cmd, output);
1051 break;
1052 case DCattach:
1053 m_haveExecutable = true;
1054 // fall through
1055 case DCrun:
1056 case DCcont:
1057 case DCstep:
1058 case DCstepi:
1059 case DCnext:
1060 case DCnexti:
1061 case DCfinish:
1062 case DCuntil:
1063 case DCthread:
1064 handleRunCommands(output);
1065 break;
1066 case DCkill:
1067 m_programRunning = m_programActive = false;
1068 // erase PC
1069 emit updatePC(QString(), -1, DbgAddr(), 0);
1070 break;
1071 case DCbreaktext:
1072 case DCbreakline:
1073 case DCtbreakline:
1074 case DCbreakaddr:
1075 case DCtbreakaddr:
1076 case DCwatchpoint:
1077 newBreakpoint(cmd, output);
1078 // fall through
1079 case DCdelete:
1080 case DCenable:
1081 case DCdisable:
1082 // these commands need immediate response
1083 m_d->queueCmd(DCinfobreak, DebuggerDriver::QMoverrideMoreEqual);
1084 break;
1085 case DCinfobreak:
1086 // note: this handler must not enqueue a command, since
1087 // DCinfobreak is used at various different places.
1088 updateBreakList(output);
1089 break;
1090 case DCfindType:
1091 handleFindType(cmd, output);
1092 break;
1093 case DCprintStruct:
1094 case DCprintQStringStruct:
1095 case DCprintWChar:
1096 handlePrintStruct(cmd, output);
1097 break;
1098 case DCinfosharedlib:
1099 handleSharedLibs(output);
1100 break;
1101 case DCcondition:
1102 case DCignore:
1103 // we are not interested in the output
1104 break;
1105 case DCinfothreads:
1106 handleThreadList(output);
1107 break;
1108 case DCsetpc:
1109 handleSetPC(output);
1110 break;
1111 case DCsetvariable:
1112 handleSetVariable(cmd, output);
1113 break;
1117 void KDebugger::backgroundUpdate()
1120 * If there are still expressions that need to be updated, then do so.
1122 if (m_programActive)
1123 evalExpressions();
1126 void KDebugger::handleRunCommands(const char* output)
1128 uint flags = m_d->parseProgramStopped(output, m_statusMessage);
1129 emit updateStatusMessage();
1131 m_programActive = flags & DebuggerDriver::SFprogramActive;
1133 // refresh files if necessary
1134 if (flags & DebuggerDriver::SFrefreshSource) {
1135 TRACE("re-reading files");
1136 emit executableUpdated();
1140 * Try to set any orphaned breakpoints now.
1142 for (int i = m_brkpts.size()-1; i >= 0; i--) {
1143 if (m_brkpts[i]->isOrphaned()) {
1144 TRACE("re-trying brkpt loc: "+m_brkpts[i]->location+
1145 " file: "+m_brkpts[i]->fileName+
1146 QString().sprintf(" line: %d", m_brkpts[i]->lineNo));
1147 setBreakpoint(m_brkpts[i], true);
1148 flags |= DebuggerDriver::SFrefreshBreak;
1153 * If we stopped at a breakpoint, we must update the breakpoint list
1154 * because the hit count changes. Also, if the breakpoint was temporary
1155 * it would go away now.
1157 if ((flags & (DebuggerDriver::SFrefreshBreak|DebuggerDriver::SFrefreshSource)) ||
1158 stopMayChangeBreakList())
1160 m_d->queueCmd(DCinfobreak, DebuggerDriver::QMoverride);
1164 * If we haven't listed the shared libraries yet, do so. We must do
1165 * this before we emit any commands that list variables, since the type
1166 * libraries depend on the shared libraries.
1168 if (!m_sharedLibsListed) {
1169 // must be a high-priority command!
1170 m_d->executeCmd(DCinfosharedlib);
1173 // get the backtrace if the program is running
1174 if (m_programActive) {
1175 m_d->queueCmd(DCbt, DebuggerDriver::QMoverride);
1176 } else {
1177 // program finished: erase PC
1178 emit updatePC(QString(), -1, DbgAddr(), 0);
1179 // dequeue any commands in the queues
1180 m_d->flushCommands();
1183 /* Update threads list */
1184 if (m_programActive && (flags & DebuggerDriver::SFrefreshThreads)) {
1185 m_d->queueCmd(DCinfothreads, DebuggerDriver::QMoverride);
1188 m_programRunning = false;
1189 emit programStopped();
1192 void KDebugger::slotInferiorRunning()
1194 m_programRunning = true;
1197 void KDebugger::updateAllExprs()
1199 if (!m_programActive)
1200 return;
1202 // retrieve local variables
1203 m_d->queueCmd(DCinfolocals, DebuggerDriver::QMoverride);
1205 // retrieve registers
1206 m_d->queueCmd(DCinforegisters, DebuggerDriver::QMoverride);
1208 // get new memory dump
1209 if (!m_memoryExpression.isEmpty()) {
1210 queueMemoryDump(false);
1213 // update watch expressions
1214 KTreeViewItem* item = m_watchVariables.itemAt(0);
1215 for (; item != 0; item = item->getSibling()) {
1216 m_watchEvalExpr.push_back(static_cast<VarTree*>(item)->getText());
1220 void KDebugger::updateProgEnvironment(const QString& args, const QString& wd,
1221 const QDict<EnvVar>& newVars,
1222 const QStringList& newOptions)
1224 m_programArgs = args;
1225 m_d->executeCmd(DCsetargs, m_programArgs);
1226 TRACE("new pgm args: " + m_programArgs + "\n");
1228 m_programWD = wd.stripWhiteSpace();
1229 if (!m_programWD.isEmpty()) {
1230 m_d->executeCmd(DCcd, m_programWD);
1231 TRACE("new wd: " + m_programWD + "\n");
1234 // update environment variables
1235 QDictIterator<EnvVar> it = newVars;
1236 EnvVar* val;
1237 for (; (val = it) != 0; ++it) {
1238 QString var = it.currentKey();
1239 switch (val->status) {
1240 case EnvVar::EVnew:
1241 m_envVars.insert(var, val);
1242 // fall thru
1243 case EnvVar::EVdirty:
1244 // the value must be in our list
1245 ASSERT(m_envVars[var] == val);
1246 // update value
1247 m_d->executeCmd(DCsetenv, var, val->value);
1248 break;
1249 case EnvVar::EVdeleted:
1250 // must be in our list
1251 ASSERT(m_envVars[var] == val);
1252 // delete value
1253 m_d->executeCmd(DCunsetenv, var);
1254 m_envVars.remove(var);
1255 break;
1256 default:
1257 ASSERT(false);
1258 case EnvVar::EVclean:
1259 // variable not changed
1260 break;
1264 // update options
1265 QStringList::ConstIterator oi;
1266 for (oi = newOptions.begin(); oi != newOptions.end(); ++oi)
1268 if (m_boolOptions.findIndex(*oi) < 0) {
1269 // the options is currently not set, so set it
1270 m_d->executeCmd(DCsetoption, *oi, 1);
1271 } else {
1272 // option is set, no action required, but move it to the end
1273 m_boolOptions.remove(*oi);
1275 m_boolOptions.append(*oi);
1278 * Now all options that should be set are at the end of m_boolOptions.
1279 * If some options need to be unset, they are at the front of the list.
1280 * Here we unset and remove them.
1282 while (m_boolOptions.count() > newOptions.count()) {
1283 m_d->executeCmd(DCsetoption, m_boolOptions.first(), 0);
1284 m_boolOptions.remove(m_boolOptions.begin());
1288 void KDebugger::handleLocals(const char* output)
1290 // retrieve old list of local variables
1291 QStrList oldVars;
1292 m_localVariables.exprList(oldVars);
1295 * Get local variables.
1297 QList<VarTree> newVars;
1298 parseLocals(output, newVars);
1301 * Clear any old VarTree item pointers, so that later we don't access
1302 * dangling pointers.
1304 m_localVariables.clearPendingUpdates();
1306 // reduce flicker
1307 bool autoU = m_localVariables.autoUpdate();
1308 m_localVariables.setAutoUpdate(false);
1309 bool repaintNeeded = false;
1312 * Match old variables against new ones.
1314 for (const char* n = oldVars.first(); n != 0; n = oldVars.next()) {
1315 // lookup this variable in the list of new variables
1316 VarTree* v = newVars.first();
1317 while (v != 0 && strcmp(v->getText(), n) != 0) {
1318 v = newVars.next();
1320 if (v == 0) {
1321 // old variable not in the new variables
1322 TRACE(QString("old var deleted: ") + n);
1323 v = m_localVariables.topLevelExprByName(n);
1324 if (v != 0) {
1325 m_localVariables.removeExpr(v);
1326 repaintNeeded = true;
1328 } else {
1329 // variable in both old and new lists: update
1330 TRACE(QString("update var: ") + n);
1331 m_localVariables.updateExpr(newVars.current(), *m_typeTable);
1332 // remove the new variable from the list
1333 newVars.remove();
1334 delete v;
1335 repaintNeeded = true;
1338 // insert all remaining new variables
1339 while (!newVars.isEmpty())
1341 VarTree* v = newVars.take(0);
1342 TRACE("new var: " + v->getText());
1343 m_localVariables.insertExpr(v, *m_typeTable);
1344 delete v;
1345 repaintNeeded = true;
1348 // repaint
1349 m_localVariables.setAutoUpdate(autoU);
1350 if (repaintNeeded && autoU && m_localVariables.isVisible())
1351 m_localVariables.repaint();
1354 void KDebugger::parseLocals(const char* output, QList<VarTree>& newVars)
1356 QList<VarTree> vars;
1357 m_d->parseLocals(output, vars);
1359 QString origName; /* used in renaming variables */
1360 while (vars.count() > 0)
1362 VarTree* variable = vars.take(0);
1364 * When gdb prints local variables, those from the innermost block
1365 * come first. We run through the list of already parsed variables
1366 * to find duplicates (ie. variables that hide local variables from
1367 * a surrounding block). We keep the name of the inner variable, but
1368 * rename those from the outer block so that, when the value is
1369 * updated in the window, the value of the variable that is
1370 * _visible_ changes the color!
1372 int block = 0;
1373 origName = variable->getText();
1374 for (VarTree* v = newVars.first(); v != 0; v = newVars.next()) {
1375 if (variable->getText() == v->getText()) {
1376 // we found a duplicate, change name
1377 block++;
1378 QString newName = origName + " (" + QString().setNum(block) + ")";
1379 variable->setText(newName);
1382 newVars.append(variable);
1386 bool KDebugger::handlePrint(CmdQueueItem* cmd, const char* output)
1388 ASSERT(cmd->m_expr != 0);
1390 VarTree* variable = m_d->parsePrintExpr(output, true);
1391 if (variable == 0)
1392 return false;
1394 // set expression "name"
1395 variable->setText(cmd->m_expr->getText());
1398 TRACE("update expr: " + cmd->m_expr->getText());
1399 cmd->m_exprWnd->updateExpr(cmd->m_expr, variable, *m_typeTable);
1400 delete variable;
1403 evalExpressions(); /* enqueue dereferenced pointers */
1405 return true;
1408 bool KDebugger::handlePrintDeref(CmdQueueItem* cmd, const char* output)
1410 ASSERT(cmd->m_expr != 0);
1412 VarTree* variable = m_d->parsePrintExpr(output, true);
1413 if (variable == 0)
1414 return false;
1416 // set expression "name"
1417 variable->setText(cmd->m_expr->getText());
1421 * We must insert a dummy parent, because otherwise variable's value
1422 * would overwrite cmd->m_expr's value.
1424 VarTree* dummyParent = new VarTree(variable->getText(), VarTree::NKplain);
1425 dummyParent->m_varKind = VarTree::VKdummy;
1426 // the name of the parsed variable is the address of the pointer
1427 QString addr = "*" + cmd->m_expr->m_value;
1428 variable->setText(addr);
1429 variable->m_nameKind = VarTree::NKaddress;
1431 dummyParent->appendChild(variable);
1432 // expand the first level for convenience
1433 variable->setExpanded(true);
1434 TRACE("update ptr: " + cmd->m_expr->getText());
1435 cmd->m_exprWnd->updateExpr(cmd->m_expr, dummyParent, *m_typeTable);
1436 delete dummyParent;
1439 evalExpressions(); /* enqueue dereferenced pointers */
1441 return true;
1444 // parse the output of bt
1445 void KDebugger::handleBacktrace(const char* output)
1447 // reduce flicker
1448 m_btWindow.setAutoUpdate(false);
1450 m_btWindow.clear();
1452 QList<StackFrame> stack;
1453 m_d->parseBackTrace(output, stack);
1455 if (stack.count() > 0) {
1456 StackFrame* frm = stack.take(0);
1457 // first frame must set PC
1458 // note: frm->lineNo is zero-based
1459 emit updatePC(frm->fileName, frm->lineNo, frm->address, frm->frameNo);
1461 do {
1462 QString func;
1463 if (frm->var != 0)
1464 func = frm->var->getText();
1465 else
1466 func = frm->fileName + ":" + QString().setNum(frm->lineNo+1);
1467 m_btWindow.insertItem(func);
1468 TRACE("frame " + func + " (" + frm->fileName + ":" +
1469 QString().setNum(frm->lineNo+1) + ")");
1470 delete frm;
1472 while ((frm = stack.take()) != 0);
1475 m_btWindow.setAutoUpdate(true);
1476 m_btWindow.repaint();
1479 void KDebugger::gotoFrame(int frame)
1481 m_d->executeCmd(DCframe, frame);
1484 void KDebugger::handleFrameChange(const char* output)
1486 QString fileName;
1487 int frameNo;
1488 int lineNo;
1489 DbgAddr address;
1490 if (m_d->parseFrameChange(output, frameNo, fileName, lineNo, address)) {
1491 /* lineNo can be negative here if we can't find a file name */
1492 emit updatePC(fileName, lineNo, address, frameNo);
1493 } else {
1494 emit updatePC(fileName, -1, address, frameNo);
1498 void KDebugger::evalExpressions()
1500 // evaluate expressions in the following order:
1501 // watch expressions
1502 // pointers in local variables
1503 // pointers in watch expressions
1504 // types in local variables
1505 // types in watch expressions
1506 // struct members in local variables
1507 // struct members in watch expressions
1508 VarTree* exprItem = 0;
1509 if (!m_watchEvalExpr.empty())
1511 QString expr = m_watchEvalExpr.front();
1512 m_watchEvalExpr.pop_front();
1513 exprItem = m_watchVariables.topLevelExprByName(expr);
1515 if (exprItem != 0) {
1516 CmdQueueItem* cmd = m_d->queueCmd(DCprint, exprItem->getText(), DebuggerDriver::QMoverride);
1517 // remember which expr this was
1518 cmd->m_expr = exprItem;
1519 cmd->m_exprWnd = &m_watchVariables;
1520 } else {
1521 ExprWnd* wnd;
1522 #define POINTER(widget) \
1523 wnd = &widget; \
1524 exprItem = widget.nextUpdatePtr(); \
1525 if (exprItem != 0) goto pointer
1526 #define STRUCT(widget) \
1527 wnd = &widget; \
1528 exprItem = widget.nextUpdateStruct(); \
1529 if (exprItem != 0) goto ustruct
1530 #define TYPE(widget) \
1531 wnd = &widget; \
1532 exprItem = widget.nextUpdateType(); \
1533 if (exprItem != 0) goto type
1534 repeat:
1535 POINTER(m_localVariables);
1536 POINTER(m_watchVariables);
1537 STRUCT(m_localVariables);
1538 STRUCT(m_watchVariables);
1539 TYPE(m_localVariables);
1540 TYPE(m_watchVariables);
1541 #undef POINTER
1542 #undef STRUCT
1543 #undef TYPE
1544 return;
1546 pointer:
1547 // we have an expression to send
1548 dereferencePointer(wnd, exprItem, false);
1549 return;
1551 ustruct:
1552 // paranoia
1553 if (exprItem->m_type == 0 || exprItem->m_type == TypeInfo::unknownType())
1554 goto repeat;
1555 evalInitialStructExpression(exprItem, wnd, false);
1556 return;
1558 type:
1560 * Sometimes a VarTree gets registered twice for a type update. So
1561 * it may happen that it has already been updated. Hence, we ignore
1562 * it here and go on to the next task.
1564 if (exprItem->m_type != 0)
1565 goto repeat;
1566 determineType(wnd, exprItem);
1570 void KDebugger::dereferencePointer(ExprWnd* wnd, VarTree* exprItem,
1571 bool immediate)
1573 ASSERT(exprItem->m_varKind == VarTree::VKpointer);
1575 QString expr = exprItem->computeExpr();
1576 TRACE("dereferencing pointer: " + expr);
1577 CmdQueueItem* cmd;
1578 if (immediate) {
1579 cmd = m_d->queueCmd(DCprintDeref, expr, DebuggerDriver::QMoverrideMoreEqual);
1580 } else {
1581 cmd = m_d->queueCmd(DCprintDeref, expr, DebuggerDriver::QMoverride);
1583 // remember which expr this was
1584 cmd->m_expr = exprItem;
1585 cmd->m_exprWnd = wnd;
1588 void KDebugger::determineType(ExprWnd* wnd, VarTree* exprItem)
1590 ASSERT(exprItem->m_varKind == VarTree::VKstruct);
1592 QString expr = exprItem->computeExpr();
1593 TRACE("get type of: " + expr);
1594 CmdQueueItem* cmd;
1595 cmd = m_d->queueCmd(DCfindType, expr, DebuggerDriver::QMoverride);
1597 // remember which expr this was
1598 cmd->m_expr = exprItem;
1599 cmd->m_exprWnd = wnd;
1602 void KDebugger::handleFindType(CmdQueueItem* cmd, const char* output)
1604 QString type;
1605 if (m_d->parseFindType(output, type))
1607 ASSERT(cmd != 0 && cmd->m_expr != 0);
1609 TypeInfo* info = m_typeTable->lookup(type);
1611 if (info == 0) {
1613 * We've asked gdb for the type of the expression in
1614 * cmd->m_expr, but it returned a name we don't know. The base
1615 * class (and member) types have been checked already (at the
1616 * time when we parsed that particular expression). Now it's
1617 * time to derive the type from the base classes as a last
1618 * resort.
1620 info = cmd->m_expr->inferTypeFromBaseClass();
1621 // if we found a type through this method, register an alias
1622 if (info != 0) {
1623 TRACE("infered alias: " + type);
1624 m_typeTable->registerAlias(type, info);
1627 if (info == 0) {
1628 TRACE("unknown type "+type);
1629 cmd->m_expr->m_type = TypeInfo::unknownType();
1630 } else {
1631 cmd->m_expr->m_type = info;
1632 /* since this node has a new type, we get its value immediately */
1633 evalInitialStructExpression(cmd->m_expr, cmd->m_exprWnd, false);
1634 return;
1638 evalExpressions(); /* queue more of them */
1641 void KDebugger::handlePrintStruct(CmdQueueItem* cmd, const char* output)
1643 VarTree* var = cmd->m_expr;
1644 ASSERT(var != 0);
1645 ASSERT(var->m_varKind == VarTree::VKstruct);
1647 VarTree* partExpr;
1648 if (cmd->m_cmd == DCprintQStringStruct) {
1649 partExpr = m_d->parseQCharArray(output, false, m_typeTable->qCharIsShort());
1650 } else if (cmd->m_cmd == DCprintWChar) {
1651 partExpr = m_d->parseQCharArray(output, false, true);
1652 } else {
1653 partExpr = m_d->parsePrintExpr(output, false);
1655 bool errorValue =
1656 partExpr == 0 ||
1657 /* we only allow simple values at the moment */
1658 partExpr->childCount() != 0;
1660 QString partValue;
1661 if (errorValue)
1663 partValue = "?""?""?"; // 2 question marks in a row would be a trigraph
1664 } else {
1665 partValue = partExpr->m_value;
1667 delete partExpr;
1668 partExpr = 0;
1671 * Updating a struct value works like this: var->m_partialValue holds
1672 * the value that we have gathered so far (it's been initialized with
1673 * var->m_type->m_displayString[0] earlier). Each time we arrive here,
1674 * we append the printed result followed by the next
1675 * var->m_type->m_displayString to var->m_partialValue.
1677 * If the expression we just evaluated was a guard expression, and it
1678 * resulted in an error, we must not evaluate the real expression, but
1679 * go on to the next index. (We must still add the question marks to
1680 * the value).
1682 * Next, if this was the length expression, we still have not seen the
1683 * real expression, but the length of a QString.
1685 ASSERT(var->m_exprIndex >= 0 && var->m_exprIndex <= typeInfoMaxExpr);
1687 if (errorValue || !var->m_exprIndexUseGuard)
1689 // add current partValue (which might be the question marks)
1690 var->m_partialValue += partValue;
1691 var->m_exprIndex++; /* next part */
1692 var->m_exprIndexUseGuard = true;
1693 var->m_partialValue += var->m_type->m_displayString[var->m_exprIndex];
1695 else
1697 // this was a guard expression that succeeded
1698 // go for the real expression
1699 var->m_exprIndexUseGuard = false;
1702 /* go for more sub-expressions if needed */
1703 if (var->m_exprIndex < var->m_type->m_numExprs) {
1704 /* queue a new print command with quite high priority */
1705 evalStructExpression(var, cmd->m_exprWnd, true);
1706 return;
1709 cmd->m_exprWnd->updateStructValue(var);
1711 evalExpressions(); /* enqueue dereferenced pointers */
1714 /* queues the first printStruct command for a struct */
1715 void KDebugger::evalInitialStructExpression(VarTree* var, ExprWnd* wnd, bool immediate)
1717 var->m_exprIndex = 0;
1718 if (var->m_type != TypeInfo::wchartType())
1720 var->m_exprIndexUseGuard = true;
1721 var->m_partialValue = var->m_type->m_displayString[0];
1722 evalStructExpression(var, wnd, immediate);
1724 else
1726 var->m_exprIndexUseGuard = false;
1727 QString expr = var->computeExpr();
1728 CmdQueueItem* cmd = m_d->queueCmd(DCprintWChar, expr,
1729 immediate ? DebuggerDriver::QMoverrideMoreEqual
1730 : DebuggerDriver::QMoverride);
1731 // remember which expression this was
1732 cmd->m_expr = var;
1733 cmd->m_exprWnd = wnd;
1737 /* queues a printStruct command; var must have been initialized correctly */
1738 void KDebugger::evalStructExpression(VarTree* var, ExprWnd* wnd, bool immediate)
1740 QString base = var->computeExpr();
1741 QString exprFmt;
1742 if (var->m_exprIndexUseGuard) {
1743 exprFmt = var->m_type->m_guardStrings[var->m_exprIndex];
1744 if (exprFmt.isEmpty()) {
1745 // no guard, omit it and go to expression
1746 var->m_exprIndexUseGuard = false;
1749 if (!var->m_exprIndexUseGuard) {
1750 exprFmt = var->m_type->m_exprStrings[var->m_exprIndex];
1753 QString expr;
1754 expr.sprintf(exprFmt, base.data());
1756 DbgCommand dbgCmd = DCprintStruct;
1757 // check if this is a QString::Data
1758 if (strncmp(expr, "/QString::Data ", 15) == 0)
1760 if (m_typeTable->parseQt2QStrings())
1762 expr = expr.mid(15, expr.length()); /* strip off /QString::Data */
1763 dbgCmd = DCprintQStringStruct;
1764 } else {
1766 * This should not happen: the type libraries should be set up
1767 * in a way that this can't happen. If this happens
1768 * nevertheless it means that, eg., kdecore was loaded but qt2
1769 * was not (only qt2 enables the QString feature).
1771 // TODO: remove this "print"; queue the next printStruct instead
1772 expr = "*0";
1774 } else {
1775 expr = expr;
1777 TRACE("evalStruct: " + expr + (var->m_exprIndexUseGuard ? " // guard" : " // real"));
1778 CmdQueueItem* cmd = m_d->queueCmd(dbgCmd, expr,
1779 immediate ? DebuggerDriver::QMoverrideMoreEqual
1780 : DebuggerDriver::QMnormal);
1782 // remember which expression this was
1783 cmd->m_expr = var;
1784 cmd->m_exprWnd = wnd;
1787 void KDebugger::handleSharedLibs(const char* output)
1789 // delete all known libraries
1790 m_sharedLibs.clear();
1792 // parse the table of shared libraries
1793 m_d->parseSharedLibs(output, m_sharedLibs);
1794 m_sharedLibsListed = true;
1796 // get type libraries
1797 m_typeTable->loadLibTypes(m_sharedLibs);
1799 // hand over the QString data cmd
1800 m_d->setPrintQStringDataCmd(m_typeTable->printQStringDataCmd());
1803 CmdQueueItem* KDebugger::loadCoreFile()
1805 return m_d->queueCmd(DCcorefile, m_corefile, DebuggerDriver::QMoverride);
1808 void KDebugger::slotLocalsExpanding(KTreeViewItem* item, bool& allow)
1810 exprExpandingHelper(&m_localVariables, item, allow);
1813 void KDebugger::slotWatchExpanding(KTreeViewItem* item, bool& allow)
1815 exprExpandingHelper(&m_watchVariables, item, allow);
1818 void KDebugger::exprExpandingHelper(ExprWnd* wnd, KTreeViewItem* item, bool&)
1820 VarTree* exprItem = static_cast<VarTree*>(item);
1821 if (exprItem->m_varKind != VarTree::VKpointer) {
1822 return;
1824 dereferencePointer(wnd, exprItem, true);
1827 // add the expression in the edit field to the watch expressions
1828 void KDebugger::addWatch(const QString& t)
1830 QString expr = t.stripWhiteSpace();
1831 // don't add a watched expression again
1832 if (expr.isEmpty() || m_watchVariables.topLevelExprByName(expr) != 0)
1833 return;
1834 VarTree e(expr, VarTree::NKplain);
1835 m_watchVariables.insertExpr(&e, *m_typeTable);
1837 // if we are boring ourselves, send down the command
1838 if (m_programActive) {
1839 m_watchEvalExpr.push_back(expr);
1840 if (m_d->isIdle()) {
1841 evalExpressions();
1846 // delete a toplevel watch expression
1847 void KDebugger::slotDeleteWatch()
1849 // delete only allowed while debugger is idle; or else we might delete
1850 // the very expression the debugger is currently working on...
1851 if (!m_d->isIdle())
1852 return;
1854 int index = m_watchVariables.currentItem();
1855 if (index < 0)
1856 return;
1858 VarTree* item = static_cast<VarTree*>(m_watchVariables.itemAt(index));
1859 if (!item->isToplevelExpr())
1860 return;
1862 // remove the variable from the list to evaluate
1863 QStringList::iterator i = m_watchEvalExpr.find(item->getText());
1864 if (i != m_watchEvalExpr.end()) {
1865 m_watchEvalExpr.erase(i);
1867 m_watchVariables.removeExpr(item);
1868 // item is invalid at this point!
1871 void KDebugger::handleRegisters(const char* output)
1873 QList<RegisterInfo> regs;
1874 m_d->parseRegisters(output, regs);
1876 emit registersChanged(regs);
1878 // delete them all
1879 regs.setAutoDelete(true);
1883 * The output of the DCbreak* commands has more accurate information about
1884 * the file and the line number.
1886 * All newly set breakpoints are inserted in the m_brkpts, even those that
1887 * were not set sucessfully. The unsuccessful breakpoints ("orphaned
1888 * breakpoints") are assigned negative ids, and they are tried to set later
1889 * when the program stops again at a breakpoint.
1891 void KDebugger::newBreakpoint(CmdQueueItem* cmd, const char* output)
1893 Breakpoint* bp = cmd->m_brkpt;
1894 assert(bp != 0);
1895 if (bp == 0)
1896 return;
1898 // if this is a new breakpoint, put it in the list
1899 bool isNew = !m_brkpts.contains(bp);
1900 if (isNew) {
1901 assert(bp->id == 0);
1902 int n = m_brkpts.size();
1903 m_brkpts.resize(n+1);
1904 m_brkpts.insert(n, bp);
1907 // parse the output to determine success or failure
1908 int id;
1909 QString file;
1910 int lineNo;
1911 QString address;
1912 if (!m_d->parseBreakpoint(output, id, file, lineNo, address))
1915 * Failure, the breakpoint could not be set. If this is a new
1916 * breakpoint, assign it a negative id. We look for the minimal id
1917 * of all breakpoints (that are already in the list) to get the new
1918 * id.
1920 if (isNew)
1922 assert(bp->id == 0);
1923 for (int i = m_brkpts.size()-2; i >= 0; i--) {
1924 if (m_brkpts[i]->id < bp->id) {
1925 bp->id = m_brkpts[i]->id;
1926 break;
1929 --bp->id;
1931 return;
1934 // The breakpoint was successfully set.
1935 if (bp->id <= 0)
1937 // this is a new or orphaned breakpoint:
1938 // set the remaining properties
1939 if (!cmd->m_brkpt->enabled) {
1940 m_d->executeCmd(DCdisable, id);
1942 if (!cmd->m_brkpt->condition.isEmpty()) {
1943 m_d->executeCmd(DCcondition, cmd->m_brkpt->condition, id);
1947 bp->id = id;
1948 bp->fileName = file;
1949 bp->lineNo = lineNo;
1950 if (!address.isEmpty())
1951 bp->address = address;
1954 void KDebugger::updateBreakList(const char* output)
1956 // get the new list
1957 QList<Breakpoint> brks;
1958 brks.setAutoDelete(false);
1959 m_d->parseBreakList(output, brks);
1961 // merge new information into existing breakpoints
1963 for (int i = m_brkpts.size()-1; i >= 0; i--) // decrement!
1965 // skip orphaned breakpoints
1966 if (m_brkpts[i]->id < 0)
1967 continue;
1969 for (Breakpoint* bp = brks.first(); bp != 0; bp = brks.next())
1971 if (bp->id == m_brkpts[i]->id) {
1972 // keep accurate location
1973 // except that xsldbg doesn't have a location in
1974 // the old breakpoint if it's just been set
1975 bp->text = m_brkpts[i]->text;
1976 if (!m_brkpts[i]->fileName.isEmpty()) {
1977 bp->fileName = m_brkpts[i]->fileName;
1978 bp->lineNo = m_brkpts[i]->lineNo;
1980 m_brkpts.insert(i, bp); // old object is deleted
1981 goto stillAlive;
1985 * If we get here, this breakpoint is no longer present.
1987 * To delete the breakpoint at i, we place the last breakpoint in
1988 * the list into the slot i. This will delete the old object at i.
1989 * Then we shorten the list by one.
1991 m_brkpts.insert(i, m_brkpts.take(m_brkpts.size()-1));
1992 m_brkpts.resize(m_brkpts.size()-1);
1993 TRACE(QString().sprintf("deleted brkpt %d, have now %d brkpts", i, m_brkpts.size()));
1995 stillAlive:;
1998 // brks may contain new breakpoints not already in m_brkpts
1999 for (const Breakpoint* bp = brks.first(); bp != 0; bp = brks.next())
2001 bool found = false;
2002 for (uint i = 0; i < m_brkpts.size(); i++) {
2003 if (bp->id == m_brkpts[i]->id) {
2004 found = true;
2005 break;
2008 if (!found){
2009 int n = m_brkpts.size();
2010 m_brkpts.resize(n+1);
2011 m_brkpts.insert(n, bp);
2015 emit breakpointsChanged();
2018 // look if there is at least one temporary breakpoint
2019 // or a watchpoint
2020 bool KDebugger::stopMayChangeBreakList() const
2022 for (int i = m_brkpts.size()-1; i >= 0; i--) {
2023 Breakpoint* bp = m_brkpts[i];
2024 if (bp->temporary || bp->type == Breakpoint::watchpoint)
2025 return true;
2027 return false;
2030 Breakpoint* KDebugger::breakpointByFilePos(QString file, int lineNo,
2031 const DbgAddr& address)
2033 // look for exact file name match
2034 int i;
2035 for (i = m_brkpts.size()-1; i >= 0; i--) {
2036 if (m_brkpts[i]->lineNo == lineNo &&
2037 m_brkpts[i]->fileName == file &&
2038 (address.isEmpty() || m_brkpts[i]->address == address))
2040 return m_brkpts[i];
2043 // not found, so try basename
2044 // strip off directory part of file name
2045 int offset = file.findRev("/");
2046 file.remove(0, offset+1);
2048 for (i = m_brkpts.size()-1; i >= 0; i--) {
2049 // get base name of breakpoint's file
2050 QString basename = m_brkpts[i]->fileName;
2051 int offset = basename.findRev("/");
2052 if (offset >= 0) {
2053 basename.remove(0, offset+1);
2056 if (m_brkpts[i]->lineNo == lineNo &&
2057 basename == file &&
2058 (address.isEmpty() || m_brkpts[i]->address == address))
2060 return m_brkpts[i];
2064 // not found
2065 return 0;
2068 Breakpoint* KDebugger::breakpointById(int id)
2070 for (int i = m_brkpts.size()-1; i >= 0; i--)
2072 if (m_brkpts[i]->id == id) {
2073 return m_brkpts[i];
2076 // not found
2077 return 0;
2080 void KDebugger::slotValuePopup(const QString& expr)
2082 // search the local variables for a match
2083 VarTree* v = m_localVariables.topLevelExprByName(expr);
2084 if (v == 0) {
2085 // not found, check watch expressions
2086 v = m_watchVariables.topLevelExprByName(expr);
2087 if (v == 0) {
2088 // try a member of 'this'
2089 v = m_localVariables.topLevelExprByName("this");
2090 if (v != 0)
2091 v = ExprWnd::ptrMemberByName(v, expr);
2092 if (v == 0) {
2093 // nothing found; do nothing
2094 return;
2099 // construct the tip
2100 QString tip = v->getText() + " = ";
2101 if (!v->m_value.isEmpty())
2103 tip += v->m_value;
2105 else
2107 // no value: we use some hint
2108 switch (v->m_varKind) {
2109 case VarTree::VKstruct:
2110 tip += "{...}";
2111 break;
2112 case VarTree::VKarray:
2113 tip += "[...]";
2114 break;
2115 default:
2116 tip += "?""?""?"; // 2 question marks in a row would be a trigraph
2117 break;
2120 emit valuePopup(tip);
2123 void KDebugger::slotDisassemble(const QString& fileName, int lineNo)
2125 CmdQueueItem* cmd = m_d->queueCmd(DCinfoline, fileName, lineNo,
2126 DebuggerDriver::QMoverrideMoreEqual);
2127 cmd->m_fileName = fileName;
2128 cmd->m_lineNo = lineNo;
2131 void KDebugger::handleInfoLine(CmdQueueItem* cmd, const char* output)
2133 QString addrFrom, addrTo;
2134 if (cmd->m_lineNo >= 0) {
2135 // disassemble
2136 if (m_d->parseInfoLine(output, addrFrom, addrTo)) {
2137 // got the address range, now get the real code
2138 CmdQueueItem* c = m_d->queueCmd(DCdisassemble, addrFrom, addrTo,
2139 DebuggerDriver::QMoverrideMoreEqual);
2140 c->m_fileName = cmd->m_fileName;
2141 c->m_lineNo = cmd->m_lineNo;
2142 } else {
2143 // no code
2144 QList<DisassembledCode> empty;
2145 emit disassembled(cmd->m_fileName, cmd->m_lineNo, empty);
2147 } else {
2148 // set program counter
2149 if (m_d->parseInfoLine(output, addrFrom, addrTo)) {
2150 // move the program counter to the start address
2151 m_d->executeCmd(DCsetpc, addrFrom);
2156 void KDebugger::handleDisassemble(CmdQueueItem* cmd, const char* output)
2158 QList<DisassembledCode> code;
2159 code.setAutoDelete(true);
2160 m_d->parseDisassemble(output, code);
2161 emit disassembled(cmd->m_fileName, cmd->m_lineNo, code);
2164 void KDebugger::handleThreadList(const char* output)
2166 QList<ThreadInfo> threads;
2167 threads.setAutoDelete(true);
2168 m_d->parseThreadList(output, threads);
2169 emit threadsChanged(threads);
2172 void KDebugger::setThread(int id)
2174 m_d->queueCmd(DCthread, id, DebuggerDriver::QMoverrideMoreEqual);
2177 void KDebugger::setMemoryExpression(const QString& memexpr)
2179 m_memoryExpression = memexpr;
2181 // queue the new expression
2182 if (!m_memoryExpression.isEmpty() &&
2183 isProgramActive() &&
2184 !isProgramRunning())
2186 queueMemoryDump(true);
2190 void KDebugger::queueMemoryDump(bool immediate)
2192 m_d->queueCmd(DCexamine, m_memoryExpression, m_memoryFormat,
2193 immediate ? DebuggerDriver::QMoverrideMoreEqual :
2194 DebuggerDriver::QMoverride);
2197 void KDebugger::handleMemoryDump(const char* output)
2199 QList<MemoryDump> memdump;
2200 memdump.setAutoDelete(true);
2201 QString msg = m_d->parseMemoryDump(output, memdump);
2202 emit memoryDumpChanged(msg, memdump);
2205 void KDebugger::setProgramCounter(const QString& file, int line, const DbgAddr& addr)
2207 if (addr.isEmpty()) {
2208 // find address of the specified line
2209 CmdQueueItem* cmd = m_d->executeCmd(DCinfoline, file, line);
2210 cmd->m_lineNo = -1; /* indicates "Set PC" UI command */
2211 } else {
2212 // move the program counter to that address
2213 m_d->executeCmd(DCsetpc, addr.asString());
2217 void KDebugger::handleSetPC(const char* /*output*/)
2219 // TODO: handle errors
2221 // now go to the top-most frame
2222 // this also modifies the program counter indicator in the UI
2223 gotoFrame(0);
2226 void KDebugger::slotValueEdited(int row, const QString& text)
2228 if (text.simplifyWhiteSpace().isEmpty())
2229 return; /* no text entered: ignore request */
2231 ASSERT(sender()->inherits("ExprWnd"));
2232 ExprWnd* wnd = const_cast<ExprWnd*>(static_cast<const ExprWnd*>(sender()));
2233 TRACE(QString().sprintf("Changing %s at row %d to ",
2234 wnd->name(), row) + text);
2236 // determine the lvalue to edit
2237 VarTree* expr = static_cast<VarTree*>(wnd->itemAt(row));
2238 QString lvalue = expr->computeExpr();
2239 CmdQueueItem* cmd = m_d->executeCmd(DCsetvariable, lvalue, text);
2240 cmd->m_expr = expr;
2241 cmd->m_exprWnd = wnd;
2244 void KDebugger::handleSetVariable(CmdQueueItem* cmd, const char* output)
2246 QString msg = m_d->parseSetVariable(output);
2247 if (!msg.isEmpty())
2249 // there was an error; display it in the status bar
2250 m_statusMessage = msg;
2251 emit updateStatusMessage();
2252 return;
2255 // get the new value
2256 QString expr = cmd->m_expr->computeExpr();
2257 CmdQueueItem* printCmd =
2258 m_d->queueCmd(DCprint, expr, DebuggerDriver::QMoverrideMoreEqual);
2259 printCmd->m_expr = cmd->m_expr;
2260 printCmd->m_exprWnd = cmd->m_exprWnd;
2264 #include "debugger.moc"