3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
11 #include "pgmsettings.h"
12 #include "programconfig.h"
15 #include <qfileinfo.h>
17 #include <qstringlist.h>
20 #include <klocale.h> /* i18n */
21 #include <kmessagebox.h>
23 #include <stdlib.h> /* strtol, atoi */
25 #include <unistd.h> /* sleep(3) */
30 KDebugger::KDebugger(QWidget
* parent
,
33 QListBox
* backtrace
) :
34 QObject(parent
, "debugger"),
36 m_memoryFormat(MDTword
| MDThex
),
37 m_haveExecutable(false),
38 m_programActive(false),
39 m_programRunning(false),
40 m_sharedLibsListed(false),
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)));
65 KDebugger::~KDebugger()
67 if (m_programConfig
!= 0) {
68 saveProgramSettings();
69 m_programConfig
->sync();
70 delete m_programConfig
;
77 void KDebugger::saveSettings(KConfig
* /*config*/)
81 void KDebugger::restoreSettings(KConfig
* /*config*/)
86 //////////////////////////////////////////////////////////////////////
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
);
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!");
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
143 if (!startDriver()) {
144 TRACE("startDriver failed");
149 TRACE("before file cmd");
150 m_d
->executeCmd(DCexecutable
, name
);
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;
171 void KDebugger::shutdown()
173 // shut down debugger driver
174 if (m_d
!= 0 && m_d
->isRunning())
180 void KDebugger::useCoreFile(QString corefile
, bool batch
)
182 m_corefile
= corefile
;
184 CmdQueueItem
* cmd
= loadCoreFile();
185 cmd
->m_byUser
= true;
189 void KDebugger::setAttachPid(const QString
& pid
)
194 void KDebugger::programRun()
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);
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
)
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("/");
293 file
.remove(0, offset
+1);
295 m_d
->executeCmd(DCuntil
, file
, lineNo
, true);
296 m_programRunning
= true;
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
);
319 updateProgEnvironment(dlg
.args(), dlg
.wd(),
320 dlg
.envVars(), dlg
.options());
325 void KDebugger::programSettings(QWidget
* parent
)
327 if (!m_haveExecutable
)
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
)
349 Breakpoint
* bp
= breakpointByFilePos(file
, lineNo
, address
);
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
357 Breakpoint
* bp
= new Breakpoint
;
358 bp
->temporary
= temporary
;
360 if (address
.isEmpty())
367 bp
->address
= address
;
369 setBreakpoint(bp
, false);
374 * If the breakpoint is disabled, enable it; if it's enabled,
375 * delete that breakpoint.
378 deleteBreakpoint(bp
);
380 enableDisableBreakpoint(bp
);
386 void KDebugger::setBreakpoint(Breakpoint
* bp
, bool queueOnly
)
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
);
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("/");
407 file
.remove(0, offset
+1);
410 cmd
= m_d
->queueCmd(bp
->temporary
? DCtbreakline
: DCbreakline
,
411 file
, bp
->lineNo
, DebuggerDriver::QMoverride
);
413 cmd
= m_d
->executeCmd(bp
->temporary
? DCtbreakline
: DCbreakline
,
420 cmd
= m_d
->queueCmd(bp
->temporary
? DCtbreakaddr
: DCbreakaddr
,
421 bp
->address
.asString(), DebuggerDriver::QMoverride
);
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
445 if (!bp
->isOrphaned()) {
446 if (!canChangeBreakpoints()) {
449 m_d
->executeCmd(bp
->enabled
? DCdisable
: DCenable
, bp
->id
);
451 bp
->enabled
= !bp
->enabled
;
452 emit
breakpointsChanged();
457 bool KDebugger::conditionalBreakpoint(Breakpoint
* bp
,
458 const QString
& condition
,
462 * Change the condition and ignore count.
464 * The driver is not bothered if we are removing an orphaned
468 if (!bp
->isOrphaned()) {
469 if (!canChangeBreakpoints()) {
473 bool changed
= false;
475 if (bp
->condition
!= condition
) {
477 m_d
->executeCmd(DCcondition
, condition
, bp
->id
);
480 if (bp
->ignoreCount
!= ignoreCount
) {
481 // change ignore count
482 m_d
->executeCmd(DCignore
, bp
->id
, ignoreCount
);
487 m_d
->queueCmd(DCinfobreak
, DebuggerDriver::QMoverride
);
490 bp
->condition
= condition
;
491 bp
->ignoreCount
= ignoreCount
;
492 emit
breakpointsChanged();
497 bool KDebugger::deleteBreakpoint(Breakpoint
* bp
)
500 * Remove the breakpoint.
502 * The driver is not bothered if we are removing an orphaned
505 if (!bp
->isOrphaned()) {
506 if (!canChangeBreakpoints()) {
509 m_d
->executeCmd(DCdelete
, bp
->id
);
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();
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 //////////////////////////////////////////////////////////
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
559 m_explicitKill
= false;
560 if (!m_d
->startup(m_debuggerCmd
)) {
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
570 int redirect
= RDNstdin
|RDNstdout
|RDNstderr
; /* redirect everything */
571 if (!m_inferiorTerminal
.isEmpty()) {
572 switch (m_ttyLevel
) {
575 // redirect everything
577 case ttySimpleOutputOnly
:
585 m_d
->executeCmd(DCtty
, m_inferiorTerminal
, redirect
);
590 void KDebugger::stopDriver()
592 m_explicitKill
= true;
594 if (m_attachedPid
.isEmpty()) {
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
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)
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
;
636 if (m_explicitKill
) {
637 TRACE(m_d
->driverName() + " exited normally");
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()));
645 m_haveExecutable
= false;
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
;
656 emit
updatePC(QString(), -1, DbgAddr(), 0);
659 QString
KDebugger::getConfigForExe(const QString
& 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
));
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
;
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
);
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);
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
);
755 QDict
<EnvVar
> pgmVars
;
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 */
766 QString name
= m_programConfig
->readEntry(varName
);
767 if (name
.isEmpty()) {
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
);
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 */
790 QString expr
= m_programConfig
->readEntry(varName
);
791 if (expr
.isEmpty()) {
792 // skip empty expressions
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()) {
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
))
822 debuggerCmd
= QString();
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
)
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
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
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 */
888 config
->deleteGroup(groupName
);
892 void KDebugger::restoreBreakpoints(ProgramConfig
* config
)
896 * We recognize the end of the list if there is no Enabled entry
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 */
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
);
912 if ((bp
->fileName
.isEmpty() || bp
->lineNo
< 0) &&
913 bp
->text
.isEmpty() &&
914 bp
->address
.isEmpty())
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
) {
943 // the output (if any) is uninteresting
946 // there is no output
950 /* if value is empty, we see output, but we don't care */
953 /* display gdb's message in the status bar */
954 m_d
->parseChangeWD(output
, m_statusMessage
);
955 emit
updateStatusMessage();
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())
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())
984 m_d
->queueCmd(DCinfolinemain
, DebuggerDriver::QMnormal
);
986 if (!m_statusMessage
.isEmpty())
987 emit
updateStatusMessage();
989 QString msg
= m_d
->driverName() + ": " + m_statusMessage
;
990 KMessageBox::sorry(parentWidget(), msg
);
992 m_corefile
= ""; /* don't process core file */
993 m_haveExecutable
= false;
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
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 */
1016 case DCinfolinemain
:
1017 // ignore the output, marked file info follows
1018 m_haveExecutable
= true;
1021 // parse local variables
1022 if (output
[0] != '\0') {
1023 handleLocals(output
);
1026 case DCinforegisters
:
1027 handleRegisters(output
);
1030 handleMemoryDump(output
);
1033 handleInfoLine(cmd
, output
);
1036 handleDisassemble(cmd
, output
);
1039 handleFrameChange(output
);
1043 handleBacktrace(output
);
1047 handlePrint(cmd
, output
);
1050 handlePrintDeref(cmd
, output
);
1053 m_haveExecutable
= true;
1064 handleRunCommands(output
);
1067 m_programRunning
= m_programActive
= false;
1069 emit
updatePC(QString(), -1, DbgAddr(), 0);
1077 newBreakpoint(cmd
, output
);
1082 // these commands need immediate response
1083 m_d
->queueCmd(DCinfobreak
, DebuggerDriver::QMoverrideMoreEqual
);
1086 // note: this handler must not enqueue a command, since
1087 // DCinfobreak is used at various different places.
1088 updateBreakList(output
);
1091 handleFindType(cmd
, output
);
1094 case DCprintQStringStruct
:
1096 handlePrintStruct(cmd
, output
);
1098 case DCinfosharedlib
:
1099 handleSharedLibs(output
);
1103 // we are not interested in the output
1106 handleThreadList(output
);
1109 handleSetPC(output
);
1112 handleSetVariable(cmd
, output
);
1117 void KDebugger::backgroundUpdate()
1120 * If there are still expressions that need to be updated, then do so.
1122 if (m_programActive
)
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
);
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
)
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
.append(static_cast<VarTree
*>(item
));
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
;
1237 for (; (val
= it
) != 0; ++it
) {
1238 QString var
= it
.currentKey();
1239 switch (val
->status
) {
1241 m_envVars
.insert(var
, val
);
1243 case EnvVar::EVdirty
:
1244 // the value must be in our list
1245 ASSERT(m_envVars
[var
] == val
);
1247 m_d
->executeCmd(DCsetenv
, var
, val
->value
);
1249 case EnvVar::EVdeleted
:
1250 // must be in our list
1251 ASSERT(m_envVars
[var
] == val
);
1253 m_d
->executeCmd(DCunsetenv
, var
);
1254 m_envVars
.remove(var
);
1258 case EnvVar::EVclean
:
1259 // variable not changed
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);
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
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();
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) {
1321 // old variable not in the new variables
1322 TRACE(QString("old var deleted: ") + n
);
1323 v
= m_localVariables
.topLevelExprByName(n
);
1325 m_localVariables
.removeExpr(v
);
1326 repaintNeeded
= true;
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
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
);
1345 repaintNeeded
= true;
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!
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
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);
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
);
1403 evalExpressions(); /* enqueue dereferenced pointers */
1408 bool KDebugger::handlePrintDeref(CmdQueueItem
* cmd
, const char* output
)
1410 ASSERT(cmd
->m_expr
!= 0);
1412 VarTree
* variable
= m_d
->parsePrintExpr(output
, true);
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
);
1439 evalExpressions(); /* enqueue dereferenced pointers */
1444 // parse the output of bt
1445 void KDebugger::handleBacktrace(const char* output
)
1448 m_btWindow
.setAutoUpdate(false);
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
);
1464 func
= frm
->var
->getText();
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) + ")");
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
)
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
);
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 // pointers in 'this'
1509 VarTree
* exprItem
= m_watchEvalExpr
.first();
1510 if (exprItem
!= 0) {
1511 m_watchEvalExpr
.remove();
1512 QString expr
= exprItem
->computeExpr();
1513 TRACE("watch expr: " + expr
);
1514 CmdQueueItem
* cmd
= m_d
->queueCmd(DCprint
, expr
, DebuggerDriver::QMoverride
);
1515 // remember which expr this was
1516 cmd
->m_expr
= exprItem
;
1517 cmd
->m_exprWnd
= &m_watchVariables
;
1521 #define POINTER(widget) \
1523 exprItem = widget.nextUpdatePtr(); \
1524 if (exprItem != 0) goto pointer
1525 #define STRUCT(widget) \
1527 exprItem = widget.nextUpdateStruct(); \
1528 if (exprItem != 0) goto ustruct
1529 #define TYPE(widget) \
1531 exprItem = widget.nextUpdateType(); \
1532 if (exprItem != 0) goto type
1534 POINTER(m_localVariables
);
1535 POINTER(m_watchVariables
);
1536 STRUCT(m_localVariables
);
1537 STRUCT(m_watchVariables
);
1538 TYPE(m_localVariables
);
1539 TYPE(m_watchVariables
);
1546 // we have an expression to send
1547 dereferencePointer(wnd
, exprItem
, false);
1552 if (exprItem
->m_type
== 0 || exprItem
->m_type
== TypeInfo::unknownType())
1554 evalInitialStructExpression(exprItem
, wnd
, false);
1559 * Sometimes a VarTree gets registered twice for a type update. So
1560 * it may happen that it has already been updated. Hence, we ignore
1561 * it here and go on to the next task.
1563 if (exprItem
->m_type
!= 0)
1565 determineType(wnd
, exprItem
);
1569 void KDebugger::dereferencePointer(ExprWnd
* wnd
, VarTree
* exprItem
,
1572 ASSERT(exprItem
->m_varKind
== VarTree::VKpointer
);
1574 QString expr
= exprItem
->computeExpr();
1575 TRACE("dereferencing pointer: " + expr
);
1578 cmd
= m_d
->queueCmd(DCprintDeref
, expr
, DebuggerDriver::QMoverrideMoreEqual
);
1580 cmd
= m_d
->queueCmd(DCprintDeref
, expr
, DebuggerDriver::QMoverride
);
1582 // remember which expr this was
1583 cmd
->m_expr
= exprItem
;
1584 cmd
->m_exprWnd
= wnd
;
1587 void KDebugger::determineType(ExprWnd
* wnd
, VarTree
* exprItem
)
1589 ASSERT(exprItem
->m_varKind
== VarTree::VKstruct
);
1591 QString expr
= exprItem
->computeExpr();
1592 TRACE("get type of: " + expr
);
1594 cmd
= m_d
->queueCmd(DCfindType
, expr
, DebuggerDriver::QMoverride
);
1596 // remember which expr this was
1597 cmd
->m_expr
= exprItem
;
1598 cmd
->m_exprWnd
= wnd
;
1601 void KDebugger::handleFindType(CmdQueueItem
* cmd
, const char* output
)
1604 if (m_d
->parseFindType(output
, type
))
1606 ASSERT(cmd
!= 0 && cmd
->m_expr
!= 0);
1608 TypeInfo
* info
= m_typeTable
->lookup(type
);
1612 * We've asked gdb for the type of the expression in
1613 * cmd->m_expr, but it returned a name we don't know. The base
1614 * class (and member) types have been checked already (at the
1615 * time when we parsed that particular expression). Now it's
1616 * time to derive the type from the base classes as a last
1619 info
= cmd
->m_expr
->inferTypeFromBaseClass();
1620 // if we found a type through this method, register an alias
1622 TRACE("infered alias: " + type
);
1623 m_typeTable
->registerAlias(type
, info
);
1627 TRACE("unknown type "+type
);
1628 cmd
->m_expr
->m_type
= TypeInfo::unknownType();
1630 cmd
->m_expr
->m_type
= info
;
1631 /* since this node has a new type, we get its value immediately */
1632 evalInitialStructExpression(cmd
->m_expr
, cmd
->m_exprWnd
, false);
1637 evalExpressions(); /* queue more of them */
1640 void KDebugger::handlePrintStruct(CmdQueueItem
* cmd
, const char* output
)
1642 VarTree
* var
= cmd
->m_expr
;
1644 ASSERT(var
->m_varKind
== VarTree::VKstruct
);
1647 if (cmd
->m_cmd
== DCprintQStringStruct
) {
1648 partExpr
= m_d
->parseQCharArray(output
, false, m_typeTable
->qCharIsShort());
1649 } else if (cmd
->m_cmd
== DCprintWChar
) {
1650 partExpr
= m_d
->parseQCharArray(output
, false, true);
1652 partExpr
= m_d
->parsePrintExpr(output
, false);
1656 /* we only allow simple values at the moment */
1657 partExpr
->childCount() != 0;
1662 partValue
= "?""?""?"; // 2 question marks in a row would be a trigraph
1664 partValue
= partExpr
->m_value
;
1670 * Updating a struct value works like this: var->m_partialValue holds
1671 * the value that we have gathered so far (it's been initialized with
1672 * var->m_type->m_displayString[0] earlier). Each time we arrive here,
1673 * we append the printed result followed by the next
1674 * var->m_type->m_displayString to var->m_partialValue.
1676 * If the expression we just evaluated was a guard expression, and it
1677 * resulted in an error, we must not evaluate the real expression, but
1678 * go on to the next index. (We must still add the question marks to
1681 * Next, if this was the length expression, we still have not seen the
1682 * real expression, but the length of a QString.
1684 ASSERT(var
->m_exprIndex
>= 0 && var
->m_exprIndex
<= typeInfoMaxExpr
);
1686 if (errorValue
|| !var
->m_exprIndexUseGuard
)
1688 // add current partValue (which might be the question marks)
1689 var
->m_partialValue
+= partValue
;
1690 var
->m_exprIndex
++; /* next part */
1691 var
->m_exprIndexUseGuard
= true;
1692 var
->m_partialValue
+= var
->m_type
->m_displayString
[var
->m_exprIndex
];
1696 // this was a guard expression that succeeded
1697 // go for the real expression
1698 var
->m_exprIndexUseGuard
= false;
1701 /* go for more sub-expressions if needed */
1702 if (var
->m_exprIndex
< var
->m_type
->m_numExprs
) {
1703 /* queue a new print command with quite high priority */
1704 evalStructExpression(var
, cmd
->m_exprWnd
, true);
1708 cmd
->m_exprWnd
->updateStructValue(var
);
1710 evalExpressions(); /* enqueue dereferenced pointers */
1713 /* queues the first printStruct command for a struct */
1714 void KDebugger::evalInitialStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
)
1716 var
->m_exprIndex
= 0;
1717 if (var
->m_type
!= TypeInfo::wchartType())
1719 var
->m_exprIndexUseGuard
= true;
1720 var
->m_partialValue
= var
->m_type
->m_displayString
[0];
1721 evalStructExpression(var
, wnd
, immediate
);
1725 var
->m_exprIndexUseGuard
= false;
1726 QString expr
= var
->computeExpr();
1727 CmdQueueItem
* cmd
= m_d
->queueCmd(DCprintWChar
, expr
,
1728 immediate
? DebuggerDriver::QMoverrideMoreEqual
1729 : DebuggerDriver::QMoverride
);
1730 // remember which expression this was
1732 cmd
->m_exprWnd
= wnd
;
1736 /* queues a printStruct command; var must have been initialized correctly */
1737 void KDebugger::evalStructExpression(VarTree
* var
, ExprWnd
* wnd
, bool immediate
)
1739 QString base
= var
->computeExpr();
1741 if (var
->m_exprIndexUseGuard
) {
1742 exprFmt
= var
->m_type
->m_guardStrings
[var
->m_exprIndex
];
1743 if (exprFmt
.isEmpty()) {
1744 // no guard, omit it and go to expression
1745 var
->m_exprIndexUseGuard
= false;
1748 if (!var
->m_exprIndexUseGuard
) {
1749 exprFmt
= var
->m_type
->m_exprStrings
[var
->m_exprIndex
];
1753 expr
.sprintf(exprFmt
, base
.data());
1755 DbgCommand dbgCmd
= DCprintStruct
;
1756 // check if this is a QString::Data
1757 if (strncmp(expr
, "/QString::Data ", 15) == 0)
1759 if (m_typeTable
->parseQt2QStrings())
1761 expr
= expr
.mid(15, expr
.length()); /* strip off /QString::Data */
1762 dbgCmd
= DCprintQStringStruct
;
1765 * This should not happen: the type libraries should be set up
1766 * in a way that this can't happen. If this happens
1767 * nevertheless it means that, eg., kdecore was loaded but qt2
1768 * was not (only qt2 enables the QString feature).
1770 // TODO: remove this "print"; queue the next printStruct instead
1776 TRACE("evalStruct: " + expr
+ (var
->m_exprIndexUseGuard
? " // guard" : " // real"));
1777 CmdQueueItem
* cmd
= m_d
->queueCmd(dbgCmd
, expr
,
1778 immediate
? DebuggerDriver::QMoverrideMoreEqual
1779 : DebuggerDriver::QMnormal
);
1781 // remember which expression this was
1783 cmd
->m_exprWnd
= wnd
;
1786 void KDebugger::handleSharedLibs(const char* output
)
1788 // delete all known libraries
1789 m_sharedLibs
.clear();
1791 // parse the table of shared libraries
1792 m_d
->parseSharedLibs(output
, m_sharedLibs
);
1793 m_sharedLibsListed
= true;
1795 // get type libraries
1796 m_typeTable
->loadLibTypes(m_sharedLibs
);
1798 // hand over the QString data cmd
1799 m_d
->setPrintQStringDataCmd(m_typeTable
->printQStringDataCmd());
1802 CmdQueueItem
* KDebugger::loadCoreFile()
1804 return m_d
->queueCmd(DCcorefile
, m_corefile
, DebuggerDriver::QMoverride
);
1807 void KDebugger::slotLocalsExpanding(KTreeViewItem
* item
, bool& allow
)
1809 exprExpandingHelper(&m_localVariables
, item
, allow
);
1812 void KDebugger::slotWatchExpanding(KTreeViewItem
* item
, bool& allow
)
1814 exprExpandingHelper(&m_watchVariables
, item
, allow
);
1817 void KDebugger::exprExpandingHelper(ExprWnd
* wnd
, KTreeViewItem
* item
, bool&)
1819 VarTree
* exprItem
= static_cast<VarTree
*>(item
);
1820 if (exprItem
->m_varKind
!= VarTree::VKpointer
) {
1823 dereferencePointer(wnd
, exprItem
, true);
1826 // add the expression in the edit field to the watch expressions
1827 void KDebugger::addWatch(const QString
& t
)
1829 QString expr
= t
.stripWhiteSpace();
1832 VarTree
e(expr
, VarTree::NKplain
);
1833 VarTree
* exprItem
= m_watchVariables
.insertExpr(&e
, *m_typeTable
);
1835 // if we are boring ourselves, send down the command
1836 if (m_programActive
) {
1837 m_watchEvalExpr
.append(exprItem
);
1838 if (m_d
->isIdle()) {
1844 // delete a toplevel watch expression
1845 void KDebugger::slotDeleteWatch()
1847 // delete only allowed while debugger is idle; or else we might delete
1848 // the very expression the debugger is currently working on...
1852 int index
= m_watchVariables
.currentItem();
1856 VarTree
* item
= static_cast<VarTree
*>(m_watchVariables
.itemAt(index
));
1857 if (!item
->isToplevelExpr())
1860 // remove the variable from the list to evaluate
1861 if (m_watchEvalExpr
.findRef(item
) >= 0) {
1862 m_watchEvalExpr
.remove();
1864 m_watchVariables
.removeExpr(item
);
1865 // item is invalid at this point!
1868 void KDebugger::handleRegisters(const char* output
)
1870 QList
<RegisterInfo
> regs
;
1871 m_d
->parseRegisters(output
, regs
);
1873 emit
registersChanged(regs
);
1876 regs
.setAutoDelete(true);
1880 * The output of the DCbreak* commands has more accurate information about
1881 * the file and the line number.
1883 * All newly set breakpoints are inserted in the m_brkpts, even those that
1884 * were not set sucessfully. The unsuccessful breakpoints ("orphaned
1885 * breakpoints") are assigned negative ids, and they are tried to set later
1886 * when the program stops again at a breakpoint.
1888 void KDebugger::newBreakpoint(CmdQueueItem
* cmd
, const char* output
)
1890 Breakpoint
* bp
= cmd
->m_brkpt
;
1895 // if this is a new breakpoint, put it in the list
1896 bool isNew
= !m_brkpts
.contains(bp
);
1898 assert(bp
->id
== 0);
1899 int n
= m_brkpts
.size();
1900 m_brkpts
.resize(n
+1);
1901 m_brkpts
.insert(n
, bp
);
1904 // parse the output to determine success or failure
1909 if (!m_d
->parseBreakpoint(output
, id
, file
, lineNo
, address
))
1912 * Failure, the breakpoint could not be set. If this is a new
1913 * breakpoint, assign it a negative id. We look for the minimal id
1914 * of all breakpoints (that are already in the list) to get the new
1919 assert(bp
->id
== 0);
1920 for (int i
= m_brkpts
.size()-2; i
>= 0; i
--) {
1921 if (m_brkpts
[i
]->id
< bp
->id
) {
1922 bp
->id
= m_brkpts
[i
]->id
;
1931 // The breakpoint was successfully set.
1934 // this is a new or orphaned breakpoint:
1935 // set the remaining properties
1936 if (!cmd
->m_brkpt
->enabled
) {
1937 m_d
->executeCmd(DCdisable
, id
);
1939 if (!cmd
->m_brkpt
->condition
.isEmpty()) {
1940 m_d
->executeCmd(DCcondition
, cmd
->m_brkpt
->condition
, id
);
1945 bp
->fileName
= file
;
1946 bp
->lineNo
= lineNo
;
1947 if (!address
.isEmpty())
1948 bp
->address
= address
;
1951 void KDebugger::updateBreakList(const char* output
)
1954 QList
<Breakpoint
> brks
;
1955 brks
.setAutoDelete(false);
1956 m_d
->parseBreakList(output
, brks
);
1958 // merge new information into existing breakpoints
1960 for (int i
= m_brkpts
.size()-1; i
>= 0; i
--) // decrement!
1962 // skip orphaned breakpoints
1963 if (m_brkpts
[i
]->id
< 0)
1966 for (Breakpoint
* bp
= brks
.first(); bp
!= 0; bp
= brks
.next())
1968 if (bp
->id
== m_brkpts
[i
]->id
) {
1969 // keep accurate location
1970 // except that xsldbg doesn't have a location in
1971 // the old breakpoint if it's just been set
1972 bp
->text
= m_brkpts
[i
]->text
;
1973 if (!m_brkpts
[i
]->fileName
.isEmpty()) {
1974 bp
->fileName
= m_brkpts
[i
]->fileName
;
1975 bp
->lineNo
= m_brkpts
[i
]->lineNo
;
1977 m_brkpts
.insert(i
, bp
); // old object is deleted
1982 * If we get here, this breakpoint is no longer present.
1984 * To delete the breakpoint at i, we place the last breakpoint in
1985 * the list into the slot i. This will delete the old object at i.
1986 * Then we shorten the list by one.
1988 m_brkpts
.insert(i
, m_brkpts
.take(m_brkpts
.size()-1));
1989 m_brkpts
.resize(m_brkpts
.size()-1);
1990 TRACE(QString().sprintf("deleted brkpt %d, have now %d brkpts", i
, m_brkpts
.size()));
1995 // brks may contain new breakpoints not already in m_brkpts
1996 for (const Breakpoint
* bp
= brks
.first(); bp
!= 0; bp
= brks
.next())
1999 for (uint i
= 0; i
< m_brkpts
.size(); i
++) {
2000 if (bp
->id
== m_brkpts
[i
]->id
) {
2006 int n
= m_brkpts
.size();
2007 m_brkpts
.resize(n
+1);
2008 m_brkpts
.insert(n
, bp
);
2012 emit
breakpointsChanged();
2015 // look if there is at least one temporary breakpoint
2017 bool KDebugger::stopMayChangeBreakList() const
2019 for (int i
= m_brkpts
.size()-1; i
>= 0; i
--) {
2020 Breakpoint
* bp
= m_brkpts
[i
];
2021 if (bp
->temporary
|| bp
->type
== Breakpoint::watchpoint
)
2027 Breakpoint
* KDebugger::breakpointByFilePos(QString file
, int lineNo
,
2028 const DbgAddr
& address
)
2030 // look for exact file name match
2032 for (i
= m_brkpts
.size()-1; i
>= 0; i
--) {
2033 if (m_brkpts
[i
]->lineNo
== lineNo
&&
2034 m_brkpts
[i
]->fileName
== file
&&
2035 (address
.isEmpty() || m_brkpts
[i
]->address
== address
))
2040 // not found, so try basename
2041 // strip off directory part of file name
2042 int offset
= file
.findRev("/");
2043 file
.remove(0, offset
+1);
2045 for (i
= m_brkpts
.size()-1; i
>= 0; i
--) {
2046 // get base name of breakpoint's file
2047 QString basename
= m_brkpts
[i
]->fileName
;
2048 int offset
= basename
.findRev("/");
2050 basename
.remove(0, offset
+1);
2053 if (m_brkpts
[i
]->lineNo
== lineNo
&&
2055 (address
.isEmpty() || m_brkpts
[i
]->address
== address
))
2065 Breakpoint
* KDebugger::breakpointById(int id
)
2067 for (int i
= m_brkpts
.size()-1; i
>= 0; i
--)
2069 if (m_brkpts
[i
]->id
== id
) {
2077 void KDebugger::slotValuePopup(const QString
& expr
)
2079 // search the local variables for a match
2080 VarTree
* v
= m_localVariables
.topLevelExprByName(expr
);
2082 // not found, check watch expressions
2083 v
= m_watchVariables
.topLevelExprByName(expr
);
2085 // try a member of 'this'
2086 v
= m_localVariables
.topLevelExprByName("this");
2088 v
= ExprWnd::ptrMemberByName(v
, expr
);
2090 // nothing found; do nothing
2096 // construct the tip
2097 QString tip
= v
->getText() + " = ";
2098 if (!v
->m_value
.isEmpty())
2104 // no value: we use some hint
2105 switch (v
->m_varKind
) {
2106 case VarTree::VKstruct
:
2109 case VarTree::VKarray
:
2113 tip
+= "?""?""?"; // 2 question marks in a row would be a trigraph
2117 emit
valuePopup(tip
);
2120 void KDebugger::slotDisassemble(const QString
& fileName
, int lineNo
)
2122 CmdQueueItem
* cmd
= m_d
->queueCmd(DCinfoline
, fileName
, lineNo
,
2123 DebuggerDriver::QMoverrideMoreEqual
);
2124 cmd
->m_fileName
= fileName
;
2125 cmd
->m_lineNo
= lineNo
;
2128 void KDebugger::handleInfoLine(CmdQueueItem
* cmd
, const char* output
)
2130 QString addrFrom
, addrTo
;
2131 if (cmd
->m_lineNo
>= 0) {
2133 if (m_d
->parseInfoLine(output
, addrFrom
, addrTo
)) {
2134 // got the address range, now get the real code
2135 CmdQueueItem
* c
= m_d
->queueCmd(DCdisassemble
, addrFrom
, addrTo
,
2136 DebuggerDriver::QMoverrideMoreEqual
);
2137 c
->m_fileName
= cmd
->m_fileName
;
2138 c
->m_lineNo
= cmd
->m_lineNo
;
2141 QList
<DisassembledCode
> empty
;
2142 emit
disassembled(cmd
->m_fileName
, cmd
->m_lineNo
, empty
);
2145 // set program counter
2146 if (m_d
->parseInfoLine(output
, addrFrom
, addrTo
)) {
2147 // move the program counter to the start address
2148 m_d
->executeCmd(DCsetpc
, addrFrom
);
2153 void KDebugger::handleDisassemble(CmdQueueItem
* cmd
, const char* output
)
2155 QList
<DisassembledCode
> code
;
2156 code
.setAutoDelete(true);
2157 m_d
->parseDisassemble(output
, code
);
2158 emit
disassembled(cmd
->m_fileName
, cmd
->m_lineNo
, code
);
2161 void KDebugger::handleThreadList(const char* output
)
2163 QList
<ThreadInfo
> threads
;
2164 threads
.setAutoDelete(true);
2165 m_d
->parseThreadList(output
, threads
);
2166 emit
threadsChanged(threads
);
2169 void KDebugger::setThread(int id
)
2171 m_d
->queueCmd(DCthread
, id
, DebuggerDriver::QMoverrideMoreEqual
);
2174 void KDebugger::setMemoryExpression(const QString
& memexpr
)
2176 m_memoryExpression
= memexpr
;
2178 // queue the new expression
2179 if (!m_memoryExpression
.isEmpty() &&
2180 isProgramActive() &&
2181 !isProgramRunning())
2183 queueMemoryDump(true);
2187 void KDebugger::queueMemoryDump(bool immediate
)
2189 m_d
->queueCmd(DCexamine
, m_memoryExpression
, m_memoryFormat
,
2190 immediate
? DebuggerDriver::QMoverrideMoreEqual
:
2191 DebuggerDriver::QMoverride
);
2194 void KDebugger::handleMemoryDump(const char* output
)
2196 QList
<MemoryDump
> memdump
;
2197 memdump
.setAutoDelete(true);
2198 QString msg
= m_d
->parseMemoryDump(output
, memdump
);
2199 emit
memoryDumpChanged(msg
, memdump
);
2202 void KDebugger::setProgramCounter(const QString
& file
, int line
, const DbgAddr
& addr
)
2204 if (addr
.isEmpty()) {
2205 // find address of the specified line
2206 CmdQueueItem
* cmd
= m_d
->executeCmd(DCinfoline
, file
, line
);
2207 cmd
->m_lineNo
= -1; /* indicates "Set PC" UI command */
2209 // move the program counter to that address
2210 m_d
->executeCmd(DCsetpc
, addr
.asString());
2214 void KDebugger::handleSetPC(const char* /*output*/)
2216 // TODO: handle errors
2218 // now go to the top-most frame
2219 // this also modifies the program counter indicator in the UI
2223 void KDebugger::slotValueEdited(int row
, const QString
& text
)
2225 if (text
.simplifyWhiteSpace().isEmpty())
2226 return; /* no text entered: ignore request */
2228 ASSERT(sender()->inherits("ExprWnd"));
2229 ExprWnd
* wnd
= const_cast<ExprWnd
*>(static_cast<const ExprWnd
*>(sender()));
2230 TRACE(QString().sprintf("Changing %s at row %d to ",
2231 wnd
->name(), row
) + text
);
2233 // determine the lvalue to edit
2234 VarTree
* expr
= static_cast<VarTree
*>(wnd
->itemAt(row
));
2235 QString lvalue
= expr
->computeExpr();
2236 CmdQueueItem
* cmd
= m_d
->executeCmd(DCsetvariable
, lvalue
, text
);
2238 cmd
->m_exprWnd
= wnd
;
2241 void KDebugger::handleSetVariable(CmdQueueItem
* cmd
, const char* output
)
2243 QString msg
= m_d
->parseSetVariable(output
);
2246 // there was an error; display it in the status bar
2247 m_statusMessage
= msg
;
2248 emit
updateStatusMessage();
2252 // get the new value
2253 QString expr
= cmd
->m_expr
->computeExpr();
2254 CmdQueueItem
* printCmd
=
2255 m_d
->queueCmd(DCprint
, expr
, DebuggerDriver::QMoverrideMoreEqual
);
2256 printCmd
->m_expr
= cmd
->m_expr
;
2257 printCmd
->m_exprWnd
= cmd
->m_exprWnd
;
2261 #include "debugger.moc"