| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | /**************************************************************************
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** This file is part of Qt Creator | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Commercial Usage | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Licensees holding valid Qt Commercial licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the Qt Commercial License Agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							|  |  |  | ** a written agreement between you and Nokia. | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** GNU Lesser General Public License Usage | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
					
						
							|  |  |  | ** General Public License version 2.1 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
					
						
							|  |  |  | ** packaging of this file.  Please review the following information to | 
					
						
							|  |  |  | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
					
						
							|  |  |  | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** If you are unsure which license is appropriate for your use, please | 
					
						
							|  |  |  | ** contact the sales department at http://www.qtsoftware.com/contact.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | **************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  | #include "trkutils.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | #include <QtCore/QCoreApplication>
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | #include <QtCore/QQueue>
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | #include <QtCore/QTimer>
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  | #include <QtCore/QStringList>
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <QtNetwork/QTcpServer>
 | 
					
						
							|  |  |  | #include <QtNetwork/QTcpSocket>
 | 
					
						
							|  |  |  | #include <QtNetwork/QLocalServer>
 | 
					
						
							|  |  |  | #include <QtNetwork/QLocalSocket>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  | #if USE_NATIVE
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2009-07-28 15:54:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Non-blocking replacement for win-api ReadFile function
 | 
					
						
							|  |  |  | BOOL WINAPI TryReadFile(HANDLE          hFile, | 
					
						
							|  |  |  |                         LPVOID          lpBuffer, | 
					
						
							|  |  |  |                         DWORD           nNumberOfBytesToRead, | 
					
						
							|  |  |  |                         LPDWORD         lpNumberOfBytesRead, | 
					
						
							|  |  |  |                         LPOVERLAPPED    lpOverlapped) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     COMSTAT comStat; | 
					
						
							|  |  |  |     if(!ClearCommError(hFile, NULL, &comStat)){ | 
					
						
							|  |  |  |         qDebug() << "ClearCommError() failed"; | 
					
						
							|  |  |  |         return FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ReadFile(hFile, | 
					
						
							|  |  |  |                     lpBuffer, | 
					
						
							|  |  |  |                     qMin(comStat.cbInQue, nNumberOfBytesToRead), | 
					
						
							|  |  |  |                     lpNumberOfBytesRead, | 
					
						
							|  |  |  |                     lpOverlapped); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | #ifdef Q_OS_UNIX
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void signalHandler(int) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qApp->exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  | using namespace trk; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | enum { TRK_SYNC = 0x7f }; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | enum { KnownRegisters = RegisterPSGdb + 1}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const char *registerNames[KnownRegisters] = | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     "A1", "A2", "A3", "A4", | 
					
						
							|  |  |  |     0, 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, "AP", | 
					
						
							|  |  |  |     "IP", "SP", "LR", "PC", | 
					
						
							|  |  |  |     "PSTrk", 0, 0, 0, | 
					
						
							|  |  |  |     0, 0, 0, 0, | 
					
						
							|  |  |  |     0, "PSGdb" | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline void dumpRegister(int n, uint value, QByteArray &a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     a += ' '; | 
					
						
							|  |  |  |     if (n < KnownRegisters && registerNames[n]) { | 
					
						
							|  |  |  |         a += registerNames[n]; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         a += '#'; | 
					
						
							|  |  |  |         a += QByteArray::number(n); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     a += "=0x"; | 
					
						
							|  |  |  |     a += QByteArray::number(value, 16); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct AdapterOptions { | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     AdapterOptions() : verbose(1),registerEndianness(BigEndian) {} | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int verbose; | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     Endianness registerEndianness; | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     QString gdbServer; | 
					
						
							|  |  |  |     QString trkServer; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | #define CB(s) &Adapter::s
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | class Adapter : public QObject | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Q_OBJECT | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     Adapter(); | 
					
						
							|  |  |  |     ~Adapter(); | 
					
						
							|  |  |  |     void setGdbServerName(const QString &name); | 
					
						
							|  |  |  |     void setTrkServerName(const QString &name) { m_trkServerName = name; } | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     void setVerbose(int verbose) { m_verbose = verbose; } | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     void setRegisterEndianness(Endianness r) { m_registerEndianness = r; } | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |     bool startServer(); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     //
 | 
					
						
							|  |  |  |     // TRK
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     Q_SLOT void readFromTrk(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     typedef void (Adapter::*TrkCallBack)(const TrkResult &); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     struct TrkMessage | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |         TrkMessage() : code(0), token(0), callBack(0), invokeOnFailure(0) {} | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         byte code; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |         byte token; | 
					
						
							|  |  |  |         QByteArray data; | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         QVariant cookie; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |         TrkCallBack callBack; | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |         bool invokeOnFailure; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool openTrkPort(const QString &port); // or server name for local server
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     void sendTrkMessage(byte code, | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |         TrkCallBack calBack = 0, | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         const QByteArray &data = QByteArray(), | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |         const QVariant &cookie = QVariant(), | 
					
						
							|  |  |  |         bool invokeOnFailure = false); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     // adds message to 'send' queue
 | 
					
						
							|  |  |  |     void queueTrkMessage(const TrkMessage &msg); | 
					
						
							|  |  |  |     void tryTrkWrite(); | 
					
						
							|  |  |  |     void tryTrkRead(); | 
					
						
							|  |  |  |     // actually writes a message to the device
 | 
					
						
							|  |  |  |     void trkWrite(const TrkMessage &msg); | 
					
						
							|  |  |  |     // convienience messages
 | 
					
						
							|  |  |  |     void sendTrkInitialPing(); | 
					
						
							|  |  |  |     void waitForTrkFinished(); | 
					
						
							|  |  |  |     void sendTrkAck(byte token); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // kill process and breakpoints
 | 
					
						
							|  |  |  |     void cleanUp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void timerEvent(QTimerEvent *ev); | 
					
						
							|  |  |  |     byte nextTrkWriteToken(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     void handleCpuType(const TrkResult &result); | 
					
						
							|  |  |  |     void handleCreateProcess(const TrkResult &result); | 
					
						
							|  |  |  |     void handleSetBreakpoint(const TrkResult &result); | 
					
						
							|  |  |  |     void handleClearBreakpoint(const TrkResult &result); | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     void handleSignalContinue(const TrkResult &result); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     void handleWaitForFinished(const TrkResult &result); | 
					
						
							|  |  |  |     void handleStop(const TrkResult &result); | 
					
						
							|  |  |  |     void handleSupportMask(const TrkResult &result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void handleAndReportCreateProcess(const TrkResult &result); | 
					
						
							|  |  |  |     void handleAndReportReadRegisters(const TrkResult &result); | 
					
						
							| 
									
										
										
										
											2009-07-29 14:06:44 +02:00
										 |  |  |     QByteArray memoryReadLogMessage(uint addr, uint len, const QByteArray &ba) const; | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     void handleReadMemory(const TrkResult &result); | 
					
						
							|  |  |  |     void reportReadMemory(const TrkResult &result); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     void reportToGdb(const TrkResult &result); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void setTrkBreakpoint(const Breakpoint &bp); | 
					
						
							|  |  |  |     void clearTrkBreakpoint(const Breakpoint &bp); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     void handleResult(const TrkResult &data); | 
					
						
							|  |  |  |     void readMemory(uint addr, uint len); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     void startInferiorIfNeeded(); | 
					
						
							|  |  |  |     void interruptInferior(); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  | #if USE_NATIVE
 | 
					
						
							|  |  |  |     HANDLE m_hdevice; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |      QLocalSocket *m_trkDevice; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     QString m_trkServerName; | 
					
						
							|  |  |  |     QByteArray m_trkReadBuffer; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     unsigned char m_trkWriteToken; | 
					
						
							|  |  |  |     QQueue<TrkMessage> m_trkWriteQueue; | 
					
						
							|  |  |  |     QHash<byte, TrkMessage> m_writtenTrkMessages; | 
					
						
							|  |  |  |     QByteArray m_trkReadQueue; | 
					
						
							|  |  |  |     bool m_trkWriteBusy; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QList<Breakpoint> m_breakpoints; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     //
 | 
					
						
							|  |  |  |     // Gdb
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     Q_SLOT void handleGdbConnection(); | 
					
						
							|  |  |  |     Q_SLOT void readFromGdb(); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     void handleGdbResponse(const QByteArray &ba); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     void sendGdbMessage(const QByteArray &msg, const QByteArray &logNote = QByteArray()); | 
					
						
							|  |  |  |     void sendGdbMessageAfterSync(const QByteArray &msg, const QByteArray &logNote = QByteArray()); | 
					
						
							|  |  |  |     void sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     void logMessage(const QString &msg, bool force = false); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     QTcpServer m_gdbServer; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     QTcpSocket *m_gdbConnection; | 
					
						
							|  |  |  |     QString m_gdbServerName; | 
					
						
							|  |  |  |     quint16 m_gdbServerPort; | 
					
						
							|  |  |  |     QByteArray m_gdbReadBuffer; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     bool m_gdbAckMode; | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     // Debuggee state
 | 
					
						
							|  |  |  |     Session m_session; // global-ish data (process id, target information)
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     Snapshot m_snapshot; // local-ish data (memory and registers)
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     int m_verbose; | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     Endianness m_registerEndianness; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  | Adapter::Adapter() : | 
					
						
							|  |  |  |     m_registerEndianness(BigEndian) | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     // Trk
 | 
					
						
							|  |  |  | #if USE_NATIVE
 | 
					
						
							|  |  |  |     m_hdevice = NULL; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     m_trkDevice = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     m_trkWriteToken = 0; | 
					
						
							|  |  |  |     m_trkWriteBusy = false; | 
					
						
							|  |  |  |     //m_breakpoints.append(Breakpoint(0x0370));
 | 
					
						
							|  |  |  |     //m_breakpoints.append(Breakpoint(0x0340));
 | 
					
						
							|  |  |  |     //m_breakpoints.append(Breakpoint(0x0040)); // E32Main
 | 
					
						
							|  |  |  |     startTimer(100); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Gdb
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     m_gdbConnection = 0; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     m_gdbAckMode = true; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Adapter::~Adapter() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     // Trk
 | 
					
						
							|  |  |  | #if USE_NATIVE
 | 
					
						
							|  |  |  |     CloseHandle(m_hdevice); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |     m_trkDevice->abort(); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     delete m_trkDevice; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Gdb
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     m_gdbServer.close(); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     //>disconnectFromServer();
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     logMessage("Shutting down.\n", true); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::setGdbServerName(const QString &name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int pos = name.indexOf(':'); | 
					
						
							|  |  |  |     if (pos == -1) { | 
					
						
							|  |  |  |         m_gdbServerPort = 0; | 
					
						
							|  |  |  |         m_gdbServerName = name; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         m_gdbServerPort = name.mid(pos + 1).toUInt(); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         m_gdbServerName = name.left(pos); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  | bool Adapter::startServer() | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     if (!openTrkPort(m_trkServerName)) { | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |         logMessage("Unable to connect to TRK server " + m_trkServerName + " " +m_trkDevice->errorString(), true); | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     sendTrkInitialPing(); | 
					
						
							|  |  |  |     sendTrkMessage(0x01); // Connect
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     sendTrkMessage(0x05, CB(handleSupportMask)); | 
					
						
							|  |  |  |     sendTrkMessage(0x06, CB(handleCpuType)); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     sendTrkMessage(0x04); // Versions
 | 
					
						
							|  |  |  |     sendTrkMessage(0x09); // Unrecognized command
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     //sendTrkMessage(0x4a, 0,
 | 
					
						
							|  |  |  |     //    "10 " + formatString("C:\\data\\usingdlls.sisx")); // Open File
 | 
					
						
							|  |  |  |     //sendTrkMessage(0x4B, 0, "00 00 00 01 73 1C 3A C8"); // Close File
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     logMessage("Connected to TRK server"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_gdbServer.listen(QHostAddress(m_gdbServerName), m_gdbServerPort)) { | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         logMessage(QString("Unable to start the gdb server at %1:%2: %3.") | 
					
						
							|  |  |  |             .arg(m_gdbServerName).arg(m_gdbServerPort) | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |             .arg(m_gdbServer.errorString()), true); | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     logMessage(QString("Gdb server running on %1:%2.\nRegister endianness: %3\nRun arm-gdb now.") | 
					
						
							|  |  |  |         .arg(m_gdbServerName).arg(m_gdbServer.serverPort()).arg(m_registerEndianness), true); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     connect(&m_gdbServer, SIGNAL(newConnection()), | 
					
						
							|  |  |  |         this, SLOT(handleGdbConnection())); | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  | void Adapter::logMessage(const QString &msg, bool force) | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     if (m_verbose || force) | 
					
						
							|  |  |  |         qDebug("ADAPTER: %s ", qPrintable(msg)); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Gdb
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | void Adapter::handleGdbConnection() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     logMessage("HANDLING GDB CONNECTION"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     m_gdbConnection = m_gdbServer.nextPendingConnection(); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     connect(m_gdbConnection, SIGNAL(disconnected()), | 
					
						
							|  |  |  |             m_gdbConnection, SLOT(deleteLater())); | 
					
						
							|  |  |  |     connect(m_gdbConnection, SIGNAL(readyRead()), | 
					
						
							|  |  |  |             this, SLOT(readFromGdb())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::readFromGdb() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QByteArray packet = m_gdbConnection->readAll(); | 
					
						
							|  |  |  |     m_gdbReadBuffer.append(packet); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     logMessage("gdb: -> " + packet); | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     if (packet != m_gdbReadBuffer) | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         logMessage("buffer: " + m_gdbReadBuffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QByteArray &ba = m_gdbReadBuffer; | 
					
						
							|  |  |  |     while (ba.size()) { | 
					
						
							|  |  |  |         char code = ba.at(0); | 
					
						
							|  |  |  |         ba = ba.mid(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (code == '+') { | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |             //logMessage("ACK");
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         if (code == '-') { | 
					
						
							|  |  |  |             logMessage("NAK: Retransmission requested"); | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         if (code == char(0x03)) { | 
					
						
							|  |  |  |             logMessage("INTERRUPT RECEIVED"); | 
					
						
							|  |  |  |             interruptInferior(); | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         if (code != '$') { | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |             logMessage("Broken package (2) " + quoteUnprintableLatin1(ba) | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |                 + hexNumber(code)); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         int pos = ba.indexOf('#'); | 
					
						
							|  |  |  |         if (pos == -1) { | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             logMessage("Invalid checksum format in " | 
					
						
							|  |  |  |                 + quoteUnprintableLatin1(ba)); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bool ok = false; | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         uint checkSum = ba.mid(pos + 1, 2).toUInt(&ok, 16); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         if (!ok) { | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             logMessage("Invalid checksum format 2 in " | 
					
						
							|  |  |  |                 + quoteUnprintableLatin1(ba)); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |         //logMessage(QString("Packet checksum: %1").arg(checkSum));
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         byte sum = 0; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         for (int i = 0; i < pos; ++i) | 
					
						
							|  |  |  |             sum += ba.at(i); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         if (sum != checkSum) { | 
					
						
							|  |  |  |             logMessage(QString("ERROR: Packet checksum wrong: %1 %2 in " | 
					
						
							|  |  |  |                 + quoteUnprintableLatin1(ba)).arg(checkSum).arg(sum)); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         QByteArray response = ba.left(pos); | 
					
						
							|  |  |  |         ba = ba.mid(pos + 3); | 
					
						
							|  |  |  |         handleGdbResponse(response); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  | void Adapter::sendGdbAckMessage() | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     if (!m_gdbAckMode) | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     QByteArray packet = "+"; | 
					
						
							|  |  |  |     logMessage("gdb: <- " + packet); | 
					
						
							|  |  |  |     m_gdbConnection->write(packet); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     //m_gdbConnection->flush();
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  | void Adapter::sendGdbMessage(const QByteArray &msg, const QByteArray &logNote) | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     byte sum = 0; | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     for (int i = 0; i != msg.size(); ++i) | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |         sum += msg.at(i); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     char checkSum[30]; | 
					
						
							|  |  |  |     qsnprintf(checkSum, sizeof(checkSum) - 1, "%02x ", sum); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     //logMessage(QString("Packet checksum: %1").arg(sum));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QByteArray packet; | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     packet.append("$"); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     packet.append(msg); | 
					
						
							|  |  |  |     packet.append('#'); | 
					
						
							|  |  |  |     packet.append(checkSum); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     int pad = qMax(0, 24 - packet.size()); | 
					
						
							|  |  |  |     logMessage("gdb: <- " + packet + QByteArray(pad, ' ') + logNote); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     m_gdbConnection->write(packet); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     m_gdbConnection->flush(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::sendGdbMessageAfterSync(const QByteArray &msg, const QByteArray &logNote) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QByteArray ba = msg + char(1) + logNote; | 
					
						
							|  |  |  |     sendTrkMessage(TRK_SYNC, CB(reportToGdb), "", ba); // Answer gdb
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::reportToGdb(const TrkResult &result) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QByteArray message = result.cookie.toByteArray(); | 
					
						
							|  |  |  |     QByteArray note; | 
					
						
							|  |  |  |     int pos = message.lastIndexOf(char(1)); // HACK
 | 
					
						
							|  |  |  |     if (pos != -1) { | 
					
						
							|  |  |  |         note = message.mid(pos + 1); | 
					
						
							|  |  |  |         message = message.left(pos); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     message.replace("@CODESEG@", hexNumber(m_session.codeseg)); | 
					
						
							|  |  |  |     message.replace("@DATASEG@", hexNumber(m_session.dataseg)); | 
					
						
							|  |  |  |     message.replace("@PID@", hexNumber(m_session.pid)); | 
					
						
							|  |  |  |     message.replace("@TID@", hexNumber(m_session.tid)); | 
					
						
							|  |  |  |     sendGdbMessage(message, note); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::handleGdbResponse(const QByteArray &response) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // http://sourceware.org/gdb/current/onlinedocs/gdb_34.html
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (0) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     else if (response == "!") { | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-24 15:50:30 +02:00
										 |  |  |         sendGdbMessage("", "extended mode not enabled"); | 
					
						
							|  |  |  |         //sendGdbMessage("OK", "extended mode enabled");
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else if (response.startsWith("?")) { | 
					
						
							|  |  |  |         // Indicate the reason the target halted.
 | 
					
						
							|  |  |  |         // The reply is the same as for step and continue.
 | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         startInferiorIfNeeded(); | 
					
						
							|  |  |  |         //sendGdbMessageAfterSync("T05thread:p@PID@.@TID@;", "current thread Id");
 | 
					
						
							|  |  |  |         //sendGdbMessageAfterSync("T05thread:@TID@;", "current thread Id");
 | 
					
						
							|  |  |  |         //sendGdbMessage("T0505:00000000;04:e0c1ddbf;08:10f80bb8;thread:1f43;",
 | 
					
						
							|  |  |  |         //    sendGdbMessage("T0505:00000000;thread:" + QByteArray(buf),
 | 
					
						
							|  |  |  |         sendGdbMessage("S05", "target halted"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else if (response == "c") { | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         QByteArray ba; | 
					
						
							|  |  |  |         appendByte(&ba, 0); // options
 | 
					
						
							|  |  |  |         appendInt(&ba, 0); // start address
 | 
					
						
							|  |  |  |         appendInt(&ba, 0); // end address
 | 
					
						
							|  |  |  |         appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |         appendInt(&ba, m_session.tid); | 
					
						
							|  |  |  |         sendTrkMessage(0x18, 0, ba); | 
					
						
							|  |  |  |         // FIXME: should be triggered by real stop
 | 
					
						
							|  |  |  |         //sendGdbMessageAfterSync("S11", "target stopped");
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     else if (response.startsWith("C")) { | 
					
						
							|  |  |  |         // C sig[;addr] Continue with signal sig (hex signal number)
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |         //Reply: See section D.3 Stop Reply Packets, for the reply specifications.
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |         bool ok = false; | 
					
						
							|  |  |  |         uint signalNumber = response.mid(1).toInt(&ok, 16); | 
					
						
							|  |  |  |         QByteArray ba; | 
					
						
							|  |  |  |         appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |         appendInt(&ba, m_session.tid); | 
					
						
							|  |  |  |         sendTrkMessage(0x18, CB(handleSignalContinue), ba, signalNumber); // Continue
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     else if (response.startsWith("D")) { | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         sendGdbMessage("OK", "shutting down"); | 
					
						
							|  |  |  |         qApp->exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     else if (response == "g") { | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |         // Read general registers.
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         //sendGdbMessage("00000000", "read registers");
 | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         QByteArray ba; | 
					
						
							|  |  |  |         appendByte(&ba, 0); // ?
 | 
					
						
							|  |  |  |         appendByte(&ba, 0); // ?
 | 
					
						
							|  |  |  |         appendByte(&ba, 0); // ?
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         appendByte(&ba, 0); // first register
 | 
					
						
							|  |  |  |         // FIXME: off by one?
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         appendByte(&ba, RegisterCount - 1); // last register
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |         appendInt(&ba, m_session.tid); | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         sendTrkMessage(0x12, CB(handleAndReportReadRegisters), ba); | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     else if (response.startsWith("Hc")) { | 
					
						
							|  |  |  |         // Set thread for subsequent operations (`m', `M', `g', `G', et.al.).
 | 
					
						
							|  |  |  |         // for step and continue operations
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |         //$Hc-1#09
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         sendGdbMessage("OK", "set current thread for step & continue"); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else if (response.startsWith("Hg")) { | 
					
						
							|  |  |  |         // Set thread for subsequent operations (`m', `M', `g', `G', et.al.).
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |         // for 'other operations.  0 - any thread
 | 
					
						
							|  |  |  |         //$Hg0#df
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         m_session.currentThread = response.mid(2).toInt(); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbMessage("OK", "set current thread " | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |             + QByteArray::number(m_session.currentThread)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     else if (response == "k") { | 
					
						
							|  |  |  |         // kill
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |         QByteArray ba; | 
					
						
							|  |  |  |         appendByte(&ba, 0); // Sub-command: Delete Process
 | 
					
						
							|  |  |  |         appendInt(&ba, m_session.pid); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendTrkMessage(0x41, 0, ba, "Delete process"); // Delete Item
 | 
					
						
							|  |  |  |         sendGdbMessageAfterSync("", "process killed"); | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     else if (response.startsWith("m")) { | 
					
						
							|  |  |  |         // m addr,length
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         int pos = response.indexOf(','); | 
					
						
							|  |  |  |         bool ok = false; | 
					
						
							|  |  |  |         uint addr = response.mid(1, pos - 1).toInt(&ok, 16); | 
					
						
							|  |  |  |         uint len = response.mid(pos + 1).toInt(&ok, 16); | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |         //qDebug() << "GDB ADDR: " << hexNumber(addr) << " " << hexNumber(len);
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         readMemory(addr, len); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     else if (response.startsWith("p")) { | 
					
						
							|  |  |  |         // 0xf == current instruction pointer?
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         //sendGdbMessage("0000", "current IP");
 | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |         #if 0
 | 
					
						
							|  |  |  |           A1 = 0,	 first integer-like argument | 
					
						
							|  |  |  |           A4 = 3,	 last integer-like argument | 
					
						
							|  |  |  |           AP = 11, | 
					
						
							|  |  |  |           IP = 12, | 
					
						
							|  |  |  |           SP = 13,	 Contains address of top of stack | 
					
						
							|  |  |  |           LR = 14,	 address to return to from a function call | 
					
						
							|  |  |  |           PC = 15,	 Contains program counter | 
					
						
							|  |  |  |           F0 = 16,	 first floating point register | 
					
						
							|  |  |  |           F3 = 19,	 last floating point argument register | 
					
						
							|  |  |  |           F7 = 23, 	 last floating point register | 
					
						
							|  |  |  |           FPS = 24,	 floating point status register | 
					
						
							|  |  |  |           PS = 25,	 Contains processor status | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |           WR0,		 WMMX data registers. | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |           WR15 = WR0 + 15, | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |           WC0,		 WMMX control registers. | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |           WCSSF = WC0 + 2, | 
					
						
							|  |  |  |           WCASF = WC0 + 3, | 
					
						
							|  |  |  |           WC7 = WC0 + 7, | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |           WCGR0,		WMMX general purpose registers. | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |           WCGR3 = WCGR0 + 3, | 
					
						
							|  |  |  |           WCGR7 = WCGR0 + 7, | 
					
						
							|  |  |  |           NUM_REGS, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |           // Other useful registers.
 | 
					
						
							|  |  |  |           FP = 11,		Frame register in ARM code, if used. | 
					
						
							|  |  |  |           THUMB_FP = 7,		Frame register in Thumb code, if used. | 
					
						
							|  |  |  |           NUM_ARG_REGS = 4, | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |           LAST_ARG = A4, | 
					
						
							|  |  |  |           NUM_FP_ARG_REGS = 4, | 
					
						
							|  |  |  |           LAST_FP_ARG = F3 | 
					
						
							|  |  |  |         #endif
 | 
					
						
							|  |  |  |         bool ok = false; | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |         const uint registerNumber = response.mid(1).toInt(&ok, 16); | 
					
						
							|  |  |  |         QByteArray logMsg = "read register"; | 
					
						
							| 
									
										
										
										
											2009-07-24 09:50:31 +02:00
										 |  |  |         if (registerNumber == RegisterPSGdb) { | 
					
						
							|  |  |  |             QByteArray ba; | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |             appendInt(&ba, m_snapshot.registers[RegisterPSTrk], m_registerEndianness); | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |             dumpRegister(registerNumber, m_snapshot.registers[RegisterPSTrk], logMsg); | 
					
						
							|  |  |  |             sendGdbMessage(ba.toHex(), logMsg); | 
					
						
							| 
									
										
										
										
											2009-07-24 09:50:31 +02:00
										 |  |  |         } else if (registerNumber < RegisterCount) { | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |             QByteArray ba; | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |             appendInt(&ba, m_snapshot.registers[registerNumber], m_registerEndianness); | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |             dumpRegister(registerNumber, m_snapshot.registers[registerNumber], logMsg); | 
					
						
							|  |  |  |             sendGdbMessage(ba.toHex(), logMsg); | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |             sendGdbMessage("0000", "read single unknown register #" + QByteArray::number(registerNumber)); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             //sendGdbMessage("E01", "read single unknown register");
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else if (response == "qAttached") { | 
					
						
							|  |  |  |         //$qAttached#8f
 | 
					
						
							|  |  |  |         // 1: attached to an existing process
 | 
					
						
							|  |  |  |         // 0: created a new process
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         sendGdbMessage("0", "new process created"); | 
					
						
							|  |  |  |         //sendGdbMessage("1", "attached to existing process");
 | 
					
						
							|  |  |  |         //sendGdbMessage("E01", "new process created");
 | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else if (response.startsWith("qC")) { | 
					
						
							|  |  |  |         // Return the current thread ID
 | 
					
						
							|  |  |  |         //$qC#b4
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         startInferiorIfNeeded(); | 
					
						
							|  |  |  |         sendGdbMessageAfterSync("QC@TID@"); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     else if (response.startsWith("qSupported")) { | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         //$qSupported#37
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         //$qSupported:multiprocess+#c6
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         //logMessage("Handling 'qSupported'");
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |         if (0) | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             sendGdbMessage(QByteArray(), "nothing supported"); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             sendGdbMessage( | 
					
						
							|  |  |  |                 "PacketSize=7cf;" | 
					
						
							|  |  |  |                 "QPassSignals+;" | 
					
						
							|  |  |  |                 "qXfer:libraries:read+;" | 
					
						
							|  |  |  |                 //"qXfer:auxv:read+;"
 | 
					
						
							|  |  |  |                 "qXfer:features:read+"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else if (response == "qPacketInfo") { | 
					
						
							|  |  |  |         // happens with  gdb 6.4.50.20060226-cvs / CodeSourcery
 | 
					
						
							|  |  |  |         // deprecated by qSupported?
 | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         sendGdbMessage("", "FIXME: nothing?"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     else if (response == "qOffsets") { | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         startInferiorIfNeeded(); | 
					
						
							|  |  |  |         sendGdbMessageAfterSync("TextSeg=@CODESEG@;DataSeg=@DATASEG@"); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     else if (response == "qSymbol::") { | 
					
						
							|  |  |  |         // Notify the target that GDB is prepared to serve symbol lookup requests.
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         if (1) | 
					
						
							|  |  |  |             sendGdbMessage("OK", "no further symbols needed"); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             sendGdbMessage("qSymbol:" + QByteArray("_Z7E32Mainv").toHex(), "ask for more"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else if (response.startsWith("qXfer:features:read:target.xml:")) { | 
					
						
							|  |  |  |         //  $qXfer:features:read:target.xml:0,7ca#46...Ack
 | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         sendGdbMessage("l<target><architecture>symbianelf</architecture></target>"); | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     else if (response == "QStartNoAckMode") { | 
					
						
							|  |  |  |         //$qSupported#37
 | 
					
						
							|  |  |  |         //logMessage("Handling 'QStartNoAckMode'");
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         sendGdbMessage("OK", "ack no-ack mode"); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         m_gdbAckMode = false; | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     else if (response == "s") { | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         QByteArray ba; | 
					
						
							|  |  |  |         appendByte(&ba, 0); // options
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         appendInt(&ba, m_snapshot.registers[RegisterPC]); // start address
 | 
					
						
							|  |  |  |         appendInt(&ba, m_snapshot.registers[RegisterPC] + 4); // end address
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |         appendInt(&ba, m_session.tid); | 
					
						
							|  |  |  |         sendTrkMessage(0x19, 0, ba, "Step range"); | 
					
						
							|  |  |  |         // FIXME: should be triggered by "real" stop"
 | 
					
						
							|  |  |  |         //sendGdbMessageAfterSync("S05", "target halted");
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     else if (response == "vCont?") { | 
					
						
							|  |  |  |         // actions supported by the vCont packet
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         sendGdbMessage(""); // we don't support vCont.
 | 
					
						
							|  |  |  |         //sendGdbMessage("vCont;c");
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //else if (response.startsWith("vCont")) {
 | 
					
						
							|  |  |  |     //    // vCont[;action[:thread-id]]...'
 | 
					
						
							|  |  |  |     //}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-24 15:50:30 +02:00
										 |  |  |     else if (response.startsWith("vKill")) { | 
					
						
							|  |  |  |         // kill
 | 
					
						
							|  |  |  |         sendGdbAckMessage(); | 
					
						
							|  |  |  |         QByteArray ba; | 
					
						
							|  |  |  |         appendByte(&ba, 0); // Sub-command: Delete Process
 | 
					
						
							|  |  |  |         appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |         sendTrkMessage(0x41, 0, ba, "Delete process"); // Delete Item
 | 
					
						
							|  |  |  |         sendGdbMessageAfterSync("", "process killed"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2009-07-16 10:13:04 +02:00
										 |  |  |         logMessage("FIXME unknown: " + response); | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::readFromTrk() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     //QByteArray ba = m_gdbConnection->readAll();
 | 
					
						
							|  |  |  |     //logMessage("Read from gdb: " + ba);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | bool Adapter::openTrkPort(const QString &port) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  | #if USE_NATIVE
 | 
					
						
							|  |  |  |     m_hdevice = CreateFile(port.toStdWString().c_str(), | 
					
						
							|  |  |  |                            GENERIC_READ | GENERIC_WRITE, | 
					
						
							|  |  |  |                            0, | 
					
						
							|  |  |  |                            NULL, | 
					
						
							|  |  |  |                            OPEN_EXISTING, | 
					
						
							|  |  |  |                            FILE_ATTRIBUTE_NORMAL, | 
					
						
							|  |  |  |                            NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (INVALID_HANDLE_VALUE == m_hdevice){ | 
					
						
							|  |  |  |         logMessage("Could not open device " + port); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  |     m_trkDevice = new Win_QextSerialPort(port); | 
					
						
							|  |  |  |     m_trkDevice->setBaudRate(BAUD115200); | 
					
						
							|  |  |  |     m_trkDevice->setDataBits(DATA_8); | 
					
						
							|  |  |  |     m_trkDevice->setParity(PAR_NONE); | 
					
						
							|  |  |  |     //m_trkDevice->setStopBits(STO);
 | 
					
						
							|  |  |  |     m_trkDevice->setFlowControl(FLOW_OFF); | 
					
						
							|  |  |  |     m_trkDevice->setTimeout(0, 500); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_trkDevice->open(QIODevice::ReadWrite)) { | 
					
						
							|  |  |  |         QByteArray ba = m_trkDevice->errorString().toLatin1(); | 
					
						
							|  |  |  |         logMessage("Could not open device " << ba); | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |     return true | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     m_trkDevice = new QLocalSocket(this); | 
					
						
							|  |  |  |     m_trkDevice->connectToServer(port); | 
					
						
							|  |  |  |     return m_trkDevice->waitForConnected(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::timerEvent(QTimerEvent *) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     //qDebug(".");
 | 
					
						
							|  |  |  |     tryTrkWrite(); | 
					
						
							|  |  |  |     tryTrkRead(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  | byte Adapter::nextTrkWriteToken() | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ++m_trkWriteToken; | 
					
						
							|  |  |  |     if (m_trkWriteToken == 0) | 
					
						
							|  |  |  |         ++m_trkWriteToken; | 
					
						
							|  |  |  |     return m_trkWriteToken; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::sendTrkMessage(byte code, TrkCallBack callBack, | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     const QByteArray &data, const QVariant &cookie, bool invokeOnFailure) | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     TrkMessage msg; | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     msg.code = code; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     msg.token = nextTrkWriteToken(); | 
					
						
							|  |  |  |     msg.callBack = callBack; | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     msg.data = data; | 
					
						
							|  |  |  |     msg.cookie = cookie; | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     msg.invokeOnFailure = invokeOnFailure; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     queueTrkMessage(msg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::sendTrkInitialPing() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TrkMessage msg; | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     msg.code = 0x00; // Ping
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     msg.token = 0; // reset sequence count
 | 
					
						
							|  |  |  |     queueTrkMessage(msg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::waitForTrkFinished() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TrkMessage msg; | 
					
						
							|  |  |  |     // initiate one last roundtrip to ensure all is flushed
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     msg.code = 0x00; // Ping
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     msg.token = nextTrkWriteToken(); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     msg.callBack = CB(handleWaitForFinished); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     queueTrkMessage(msg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::sendTrkAck(byte token) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     logMessage(QString("SENDING ACKNOWLEDGEMENT FOR TOKEN %1").arg(int(token))); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     TrkMessage msg; | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     msg.code = 0x80; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     msg.token = token; | 
					
						
							|  |  |  |     msg.data.append('\0'); | 
					
						
							|  |  |  |     // The acknowledgement must not be queued!
 | 
					
						
							|  |  |  |     //queueMessage(msg);
 | 
					
						
							|  |  |  |     trkWrite(msg); | 
					
						
							|  |  |  |     // 01 90 00 07 7e 80 01 00 7d 5e 7e
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::queueTrkMessage(const TrkMessage &msg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_trkWriteQueue.append(msg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::tryTrkWrite() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_trkWriteBusy) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     if (m_trkWriteQueue.isEmpty()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     TrkMessage msg = m_trkWriteQueue.dequeue(); | 
					
						
							|  |  |  |     if (msg.code == TRK_SYNC) { | 
					
						
							|  |  |  |         //logMessage("TRK SYNC");
 | 
					
						
							|  |  |  |         TrkResult result; | 
					
						
							|  |  |  |         result.code = msg.code; | 
					
						
							|  |  |  |         result.token = msg.token; | 
					
						
							|  |  |  |         result.data = msg.data; | 
					
						
							|  |  |  |         result.cookie = msg.cookie; | 
					
						
							|  |  |  |         TrkCallBack cb = msg.callBack; | 
					
						
							|  |  |  |         if (cb) | 
					
						
							|  |  |  |             (this->*cb)(result); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         trkWrite(msg); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::trkWrite(const TrkMessage &msg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     QByteArray ba = frameMessage(msg.code, msg.token, msg.data); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_writtenTrkMessages.insert(msg.token, msg); | 
					
						
							|  |  |  |     m_trkWriteBusy = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-29 14:06:44 +02:00
										 |  |  |     if (m_verbose > 1) | 
					
						
							|  |  |  |         logMessage("WRITE: " + stringFromArray(ba)); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  | #if USE_NATIVE
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     DWORD charsWritten; | 
					
						
							|  |  |  |     if (!WriteFile(m_hdevice, ba.data(), ba.size(), &charsWritten, NULL)) | 
					
						
							|  |  |  |         logMessage("WRITE ERROR: "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     FlushFileBuffers(m_hdevice); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     if (!m_trkDevice->write(ba)) | 
					
						
							|  |  |  |         logMessage("WRITE ERROR: " + m_trkDevice->errorString()); | 
					
						
							|  |  |  |     m_trkDevice->flush(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::tryTrkRead() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     //logMessage("TRY READ: " << m_trkDevice->bytesAvailable()
 | 
					
						
							|  |  |  |     //        << stringFromArray(m_trkReadQueue);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if USE_NATIVE
 | 
					
						
							| 
									
										
										
										
											2009-07-28 15:54:17 +02:00
										 |  |  |     const DWORD BUFFERSIZE = 1024; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     char buffer[BUFFERSIZE]; | 
					
						
							|  |  |  |     DWORD charsRead; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 15:54:17 +02:00
										 |  |  |     while (TryReadFile(m_hdevice, buffer, BUFFERSIZE, &charsRead, NULL)) { | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |         m_trkReadQueue.append(buffer, charsRead); | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |         if (isValidTrkResult(m_trkReadQueue)) | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | #else // USE_NATIVE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_trkDevice->bytesAvailable() == 0 && m_trkReadQueue.isEmpty()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QByteArray res = m_trkDevice->readAll(); | 
					
						
							|  |  |  |     m_trkReadQueue.append(res); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // USE_NATIVE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_trkReadQueue.size() < 9) { | 
					
						
							|  |  |  |         logMessage("ERROR READBUFFER INVALID (1): " | 
					
						
							|  |  |  |             + stringFromArray(m_trkReadQueue)); | 
					
						
							|  |  |  |         m_trkReadQueue.clear(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (!m_trkReadQueue.isEmpty()) | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         handleResult(extractResult(&m_trkReadQueue)); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_trkWriteBusy = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::handleResult(const TrkResult &result) | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     QByteArray prefix = "READ BUF:                                       "; | 
					
						
							|  |  |  |     QByteArray str = result.toString().toUtf8(); | 
					
						
							|  |  |  |     switch (result.code) { | 
					
						
							|  |  |  |         case 0x80: { // ACK
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             //logMessage(prefix + "ACK: " + str);
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |             if (!result.data.isEmpty() && result.data.at(0)) | 
					
						
							|  |  |  |                 logMessage(prefix + "ERR: " +QByteArray::number(result.data.at(0))); | 
					
						
							|  |  |  |             //logMessage("READ RESULT FOR TOKEN: " << token);
 | 
					
						
							|  |  |  |             if (!m_writtenTrkMessages.contains(result.token)) { | 
					
						
							|  |  |  |                 logMessage("NO ENTRY FOUND!"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             TrkMessage msg = m_writtenTrkMessages.take(result.token); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |             TrkResult result1 = result; | 
					
						
							|  |  |  |             result1.cookie = msg.cookie; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |             TrkCallBack cb = msg.callBack; | 
					
						
							|  |  |  |             if (cb) { | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |                 (this->*cb)(result1); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 QString msg = result.cookie.toString(); | 
					
						
							|  |  |  |                 if (!msg.isEmpty()) | 
					
						
							|  |  |  |                     logMessage("HANDLE: " + msg + stringFromArray(result.data)); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case 0xff: { // NAK
 | 
					
						
							|  |  |  |             logMessage(prefix + "NAK: " + str); | 
					
						
							|  |  |  |             logMessage(prefix + "ERROR: " + errorMessage(result.data.at(0))); | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |             TrkMessage msg = m_writtenTrkMessages.take(result.token); | 
					
						
							|  |  |  |             // Invoke failure if desired
 | 
					
						
							|  |  |  |             if (msg.callBack && msg.invokeOnFailure) { | 
					
						
							|  |  |  |                 TrkResult result1 = result; | 
					
						
							|  |  |  |                 result1.cookie = msg.cookie; | 
					
						
							|  |  |  |                 (this->*msg.callBack)(result1); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case 0x90: { // Notified Stopped
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             logMessage(prefix + "NOTE: STOPPED  " + str); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |             // 90 01   78 6a 40 40   00 00 07 23   00 00 07 24  00 00
 | 
					
						
							|  |  |  |             //const char *data = result.data.data();
 | 
					
						
							|  |  |  | //            uint addr = extractInt(data); //code address: 4 bytes; code base address for the library
 | 
					
						
							|  |  |  | //            uint pid = extractInt(data + 4); // ProcessID: 4 bytes;
 | 
					
						
							|  |  |  | //            uint tid = extractInt(data + 8); // ThreadID: 4 bytes
 | 
					
						
							|  |  |  |             //logMessage(prefix << "      ADDR: " << addr << " PID: " << pid << " TID: " << tid);
 | 
					
						
							|  |  |  |             sendTrkAck(result.token); | 
					
						
							| 
									
										
										
										
											2009-07-24 15:50:30 +02:00
										 |  |  |             //sendGdbMessage("S11", "Target stopped");
 | 
					
						
							|  |  |  |             sendGdbMessage("S05", "Target stopped"); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case 0x91: { // Notify Exception (obsolete)
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             logMessage(prefix + "NOTE: EXCEPTION  " + str); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |             sendTrkAck(result.token); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case 0x92: { //
 | 
					
						
							|  |  |  |             logMessage(prefix + "NOTE: INTERNAL ERROR: " + str); | 
					
						
							|  |  |  |             sendTrkAck(result.token); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // target->host OS notification
 | 
					
						
							|  |  |  |         case 0xa0: { // Notify Created
 | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |             const char *data = result.data.data(); | 
					
						
							|  |  |  |             byte error = result.data.at(0); | 
					
						
							|  |  |  |             byte type = result.data.at(1); // type: 1 byte; for dll item, this value is 2.
 | 
					
						
							|  |  |  |             uint pid = extractInt(data + 2); //  ProcessID: 4 bytes;
 | 
					
						
							|  |  |  |             uint tid = extractInt(data + 6); //threadID: 4 bytes
 | 
					
						
							|  |  |  |             uint codeseg = extractInt(data + 10); //code address: 4 bytes; code base address for the library
 | 
					
						
							|  |  |  |             uint dataseg = extractInt(data + 14); //data address: 4 bytes; data base address for the library
 | 
					
						
							|  |  |  |             uint len = extractShort(data + 18); //length: 2 bytes; length of the library name string to follow
 | 
					
						
							|  |  |  |             QByteArray name = result.data.mid(20, len); // name: library name
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             logMessage(prefix + "NOTE: LIBRARY LOAD: " + str); | 
					
						
							|  |  |  |             logMessage(prefix + "TOKEN: " + result.token); | 
					
						
							|  |  |  |             logMessage(prefix + "ERROR: " + int(error)); | 
					
						
							|  |  |  |             logMessage(prefix + "TYPE:  " + int(type)); | 
					
						
							|  |  |  |             logMessage(prefix + "PID:   " + pid); | 
					
						
							|  |  |  |             logMessage(prefix + "TID:   " + tid); | 
					
						
							|  |  |  |             logMessage(prefix + "CODE:  " + codeseg); | 
					
						
							|  |  |  |             logMessage(prefix + "DATA:  " + dataseg); | 
					
						
							|  |  |  |             logMessage(prefix + "LEN:   " + len); | 
					
						
							|  |  |  |             logMessage(prefix + "NAME:  " + name); | 
					
						
							|  |  |  |             */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |             QByteArray ba; | 
					
						
							|  |  |  |             appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |             appendInt(&ba, m_session.tid); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             sendTrkMessage(0x18, 0, ba, "CONTINUE"); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |             //sendTrkAck(result.token)
 | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case 0xa1: { // NotifyDeleted
 | 
					
						
							|  |  |  |             logMessage(prefix + "NOTE: LIBRARY UNLOAD: " + str); | 
					
						
							|  |  |  |             sendTrkAck(result.token); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case 0xa2: { // NotifyProcessorStarted
 | 
					
						
							|  |  |  |             logMessage(prefix + "NOTE: PROCESSOR STARTED: " + str); | 
					
						
							|  |  |  |             sendTrkAck(result.token); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case 0xa6: { // NotifyProcessorStandby
 | 
					
						
							|  |  |  |             logMessage(prefix + "NOTE: PROCESSOR STANDBY: " + str); | 
					
						
							|  |  |  |             sendTrkAck(result.token); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case 0xa7: { // NotifyProcessorReset
 | 
					
						
							|  |  |  |             logMessage(prefix + "NOTE: PROCESSOR RESET: " + str); | 
					
						
							|  |  |  |             sendTrkAck(result.token); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         default: { | 
					
						
							|  |  |  |             logMessage(prefix + "INVALID: " + str); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::handleCpuType(const TrkResult &result) | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     logMessage("HANDLE CPU TYPE: " + result.toString()); | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x00
 | 
					
						
							|  |  |  |     // [80 03 00  04 00 00 04 00 00 00]
 | 
					
						
							|  |  |  |     m_session.cpuMajor = result.data[0]; | 
					
						
							|  |  |  |     m_session.cpuMinor = result.data[1]; | 
					
						
							|  |  |  |     m_session.bigEndian = result.data[2]; | 
					
						
							|  |  |  |     m_session.defaultTypeSize = result.data[3]; | 
					
						
							|  |  |  |     m_session.fpTypeSize = result.data[4]; | 
					
						
							|  |  |  |     m_session.extended1TypeSize = result.data[5]; | 
					
						
							|  |  |  |     //m_session.extended2TypeSize = result.data[6];
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::handleCreateProcess(const TrkResult &result) | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     //  40 00 00]
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     //logMessage("       RESULT: " + result.toString());
 | 
					
						
							|  |  |  |     // [80 08 00   00 00 01 B5   00 00 01 B6   78 67 40 00   00 40 00 00]
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     const char *data = result.data.data(); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     m_session.pid = extractInt(data + 1); | 
					
						
							|  |  |  |     m_session.tid = extractInt(data + 5); | 
					
						
							|  |  |  |     m_session.codeseg = extractInt(data + 9); | 
					
						
							|  |  |  |     m_session.dataseg = extractInt(data + 13); | 
					
						
							|  |  |  |     qDebug() << "    READ PID: " << m_session.pid; | 
					
						
							|  |  |  |     qDebug() << "    READ TID: " << m_session.tid; | 
					
						
							|  |  |  |     qDebug() << "    READ CODE: " << m_session.codeseg; | 
					
						
							|  |  |  |     qDebug() << "    READ DATA: " << m_session.dataseg; | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |     logMessage("PID: " + formatInt(m_session.pid) + m_session.pid); | 
					
						
							|  |  |  |     logMessage("TID: " + formatInt(m_session.tid) + m_session.tid); | 
					
						
							|  |  |  |     logMessage("COD: " + formatInt(m_session.codeseg) + m_session.codeseg); | 
					
						
							|  |  |  |     logMessage("DAT: " + formatInt(m_session.dataseg) + m_session.dataseg); | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //setTrkBreakpoint(0x0000, ArmMode);
 | 
					
						
							|  |  |  |     //clearTrkBreakpoint(0x0000);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 1
 | 
					
						
							|  |  |  |     foreach (const Breakpoint &bp, m_breakpoints) | 
					
						
							|  |  |  |         setTrkBreakpoint(bp); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 1
 | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x42 Read Info
 | 
					
						
							|  |  |  |     //          [42 0C 00 06 00 00 00 00 00 14 50 6F 6C 79 6D 6F
 | 
					
						
							|  |  |  |     //  72 70 68 69 63 44 4C 4C 32 2E 64 6C 6C 00]
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     sendTrkMessage(0x42, CB(handleReadInfo), | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |         "00 06 00 00 00 00 00 14 50 6F 6C 79 6D 6F " | 
					
						
							|  |  |  |         "72 70 68 69 63 44 4C 4C 32 2E 64 6C 6C 00"); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     //sendTrkMessage(0x42, CB(handleReadInfo),
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     //        "00 01 00 00 00 00");
 | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x20 Unspecified general OS-related error
 | 
					
						
							|  |  |  |     // [80 0C 20]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x42 Read Info
 | 
					
						
							|  |  |  |     // [42 0D 00 06 00 00 00 00 00 14 50 6F 6C 79 6D 6F
 | 
					
						
							|  |  |  |     //  72 70 68 69 63 44 4C 4C 31 2E 64 6C 6C 00]
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     sendTrkMessage(0x42, CB(handleReadInfo), | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |         "00 06 00 00 00 00 00 14 50 6F 6C 79 6D 6F " | 
					
						
							|  |  |  |         "72 70 68 69 63 44 4C 4C 31 2E 64 6C 6C 00"); | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x20 Unspecified general OS-related error
 | 
					
						
							|  |  |  |     // [80 0D 20]
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     //sendTrkMessage(0x18, CB(handleStop),
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     //    "01 " + formatInt(m_session.pid) + formatInt(m_session.tid));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x18 Continue
 | 
					
						
							|  |  |  |     //ProcessID: 0x000001B5 (437)
 | 
					
						
							|  |  |  |     // ThreadID: 0x000001B6 (438)
 | 
					
						
							|  |  |  |     // [18 0E 00 00 01 B5 00 00 01 B6]
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     QByteArray ba; | 
					
						
							|  |  |  |     appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |     appendInt(&ba, m_session.tid); | 
					
						
							|  |  |  |     sendTrkMessage(0x18, CB(handleContinue), ba); | 
					
						
							|  |  |  |     //sendTrkMessage(0x18, CB(handleContinue),
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     //    formatInt(m_session.pid) + "ff ff ff ff");
 | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x00
 | 
					
						
							|  |  |  |     // [80 0E 00]
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::handleAndReportReadRegisters(const TrkResult &result) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     //logMessage("       RESULT: " + result.toString());
 | 
					
						
							|  |  |  |     // [80 0B 00   00 00 00 00   C9 24 FF BC   00 00 00 00   00
 | 
					
						
							|  |  |  |     //  60 00 00   00 00 00 00   78 67 79 70   00 00 00 00   00...]
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     const char *data = result.data.data(); | 
					
						
							| 
									
										
										
										
											2009-07-24 09:50:31 +02:00
										 |  |  |     for (int i = 0; i < RegisterCount; ++i) { | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |         m_snapshot.registers[i] = extractInt(data + 4 * i); | 
					
						
							| 
									
										
										
										
											2009-07-24 09:50:31 +02:00
										 |  |  |         //qDebug() << i << hexNumber(m_snapshot.registers[i], 8);
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     //QByteArray ba = result.data.toHex();
 | 
					
						
							|  |  |  |     QByteArray ba; | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     for (int i = 0; i < 16; ++i) { | 
					
						
							|  |  |  |         const uint reg = m_registerEndianness == LittleEndian ? swapEndian(m_snapshot.registers[i]) : m_snapshot.registers[i]; | 
					
						
							|  |  |  |         ba += hexNumber(reg, 8); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     QByteArray logMsg = "register contents"; | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     if (m_verbose > 1) { | 
					
						
							|  |  |  |         for (int i = 0; i < RegisterCount; ++i) | 
					
						
							|  |  |  |             dumpRegister(i, m_snapshot.registers[i], logMsg); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     sendGdbMessage(ba, logMsg); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  | static inline QString msgMemoryReadError(uint addr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return QString::fromLatin1("Memory read error at: 0x%1").arg(addr, 0 ,16); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::handleReadMemory(const TrkResult &result) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     const uint blockaddr = result.cookie.toInt(); | 
					
						
							|  |  |  |     if (result.code == 0xff) { | 
					
						
							|  |  |  |         logMessage(msgMemoryReadError(blockaddr)); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         const QByteArray ba = result.data.mid(1); | 
					
						
							|  |  |  |         m_snapshot.memory.insert(blockaddr , ba); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-29 14:06:44 +02:00
										 |  |  | // Format log message for memory access with some smartness about registers
 | 
					
						
							|  |  |  | QByteArray Adapter::memoryReadLogMessage(uint addr, uint len, const QByteArray &ba) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QByteArray logMsg = "memory contents"; | 
					
						
							|  |  |  |     if (m_verbose > 1) { | 
					
						
							|  |  |  |         logMsg += " addr: 0x"; | 
					
						
							|  |  |  |         logMsg += QByteArray::number(addr, 16); | 
					
						
							|  |  |  |         // indicate dereferencing of registers
 | 
					
						
							|  |  |  |         if (len == 4) { | 
					
						
							|  |  |  |             if (addr == m_snapshot.registers[RegisterPC]) { | 
					
						
							|  |  |  |                 logMsg += "[PC]"; | 
					
						
							|  |  |  |             } else if (addr == m_snapshot.registers[RegisterPSTrk]) { | 
					
						
							|  |  |  |                 logMsg += "[PSTrk]"; | 
					
						
							|  |  |  |             } else if (addr == m_snapshot.registers[RegisterSP]) { | 
					
						
							|  |  |  |                 logMsg += "[SP]"; | 
					
						
							|  |  |  |             } else if (addr == m_snapshot.registers[RegisterLR]) { | 
					
						
							|  |  |  |                 logMsg += "[LR]"; | 
					
						
							|  |  |  |             } else if (addr > m_snapshot.registers[RegisterSP] && (addr - m_snapshot.registers[RegisterSP]) < 10240) { | 
					
						
							|  |  |  |                 logMsg += "[SP+"; // Stack area ...stack seems to be top-down
 | 
					
						
							|  |  |  |                 logMsg += QByteArray::number(addr - m_snapshot.registers[RegisterSP]); | 
					
						
							|  |  |  |                 logMsg += ']'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         logMsg += " length "; | 
					
						
							|  |  |  |         logMsg += QByteArray::number(len); | 
					
						
							|  |  |  |         logMsg += " :"; | 
					
						
							|  |  |  |         logMsg += stringFromArray(ba, 16); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return logMsg; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::reportReadMemory(const TrkResult &result) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     const qulonglong cookie = result.cookie.toLongLong(); | 
					
						
							|  |  |  |     const uint addr = cookie >> 32; | 
					
						
							|  |  |  |     const uint len = uint(cookie); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     // Gdb accepts less memory according to documentation.
 | 
					
						
							|  |  |  |     // Send E on complete failure.
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     QByteArray ba; | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     uint blockaddr = (addr / MemoryChunkSize) * MemoryChunkSize; | 
					
						
							|  |  |  |     for (; blockaddr < addr + len; blockaddr += MemoryChunkSize) { | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |         const Snapshot::Memory::const_iterator it = m_snapshot.memory.constFind(blockaddr); | 
					
						
							|  |  |  |         if (it == m_snapshot.memory.constEnd()) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         ba.append(it.value()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const int previousChunkOverlap = addr % MemoryChunkSize; | 
					
						
							|  |  |  |     if (previousChunkOverlap != 0 && ba.size() > previousChunkOverlap) | 
					
						
							|  |  |  |         ba.remove(0, previousChunkOverlap); | 
					
						
							|  |  |  |     if (ba.size() > int(len)) | 
					
						
							|  |  |  |         ba.truncate(len); | 
					
						
							|  |  |  |     if (ba.isEmpty()) { | 
					
						
							|  |  |  |         ba = "E20"; | 
					
						
							|  |  |  |         sendGdbMessage(ba, msgMemoryReadError(addr).toLatin1()); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         sendGdbMessage(ba.toHex(), memoryReadLogMessage(addr, len, ba)); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | void Adapter::setTrkBreakpoint(const Breakpoint &bp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x1B Set Break
 | 
					
						
							|  |  |  |     //BreakType: 0x82
 | 
					
						
							|  |  |  |     //  Options: 0x00
 | 
					
						
							|  |  |  |     //  Address: 0x78674340 (2020033344)    i.e + 0x00000340
 | 
					
						
							|  |  |  |     //   Length: 0x00000001 (1)
 | 
					
						
							|  |  |  |     //    Count: 0x00000000 (0)
 | 
					
						
							|  |  |  |     //ProcessID: 0x000001b5 (437)
 | 
					
						
							|  |  |  |     // ThreadID: 0xffffffff (-1)
 | 
					
						
							|  |  |  |     // [1B 09 82 00 78 67 43 40 00 00 00 01 00 00 00 00
 | 
					
						
							|  |  |  |     //  00 00 01 B5 FF FF FF FF]
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     QByteArray ba; | 
					
						
							|  |  |  |     appendByte(&ba, 0x82); | 
					
						
							|  |  |  |     appendByte(&ba, bp.mode == ArmMode ? 0x00 : 0x01); | 
					
						
							|  |  |  |     appendInt(&ba, m_session.codeseg + bp.offset); | 
					
						
							|  |  |  |     appendInt(&ba, 0x00000001); | 
					
						
							|  |  |  |     appendInt(&ba, 0x00000001); | 
					
						
							|  |  |  |     appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |     appendInt(&ba, 0xFFFFFFFF); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sendTrkMessage(0x1B, CB(handleSetBreakpoint), ba); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     //m_session.toekn
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x00
 | 
					
						
							|  |  |  |     // [80 09 00 00 00 00 0A]
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::handleSetBreakpoint(const TrkResult &result) | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x00
 | 
					
						
							|  |  |  |     // [80 09 00 00 00 00 0A]
 | 
					
						
							|  |  |  |     uint bpnr = extractInt(result.data.data()); | 
					
						
							|  |  |  |     logMessage("SET BREAKPOINT " + bpnr | 
					
						
							|  |  |  |         + stringFromArray(result.data.data())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::clearTrkBreakpoint(const Breakpoint &bp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x1C Clear Break
 | 
					
						
							|  |  |  |     // [1C 25 00 00 00 0A 78 6A 43 40]
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     QByteArray ba; | 
					
						
							|  |  |  |     appendByte(&ba, 0x00); | 
					
						
							|  |  |  |     appendShort(&ba, bp.number); | 
					
						
							|  |  |  |     appendInt(&ba, m_session.codeseg + bp.offset); | 
					
						
							|  |  |  |     sendTrkMessage(0x1C, CB(handleClearBreakpoint), ba); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::handleClearBreakpoint(const TrkResult &result) | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Q_UNUSED(result); | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x00
 | 
					
						
							|  |  |  |     // [80 09 00 00 00 00 0A]
 | 
					
						
							|  |  |  |     logMessage("CLEAR BREAKPOINT "); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 14:37:51 +02:00
										 |  |  | void Adapter::handleSignalContinue(const TrkResult &result) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int signalNumber = result.cookie.toInt(); | 
					
						
							|  |  |  |     logMessage("   HANDLE SIGNAL CONTINUE: " + stringFromArray(result.data)); | 
					
						
							|  |  |  |     qDebug() << "NUMBER" << signalNumber; | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     sendGdbMessage("O" + QByteArray("Console output").toHex()); | 
					
						
							|  |  |  |     sendGdbMessage("W81"); // "Process exited with result 1
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::handleWaitForFinished(const TrkResult &result) | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     logMessage("   FINISHED: " + stringFromArray(result.data)); | 
					
						
							|  |  |  |     //qApp->exit(1);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::handleSupportMask(const TrkResult &result) | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     const char *data = result.data.data(); | 
					
						
							|  |  |  |     QByteArray str; | 
					
						
							|  |  |  |     for (int i = 0; i < 32; ++i) { | 
					
						
							|  |  |  |         //str.append("  [" + formatByte(data[i]) + "]: ");
 | 
					
						
							|  |  |  |         for (int j = 0; j < 8; ++j) | 
					
						
							|  |  |  |         if (data[i] & (1 << j)) | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |             str.append(QByteArray::number(i * 8 + j, 16)); | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     logMessage("SUPPORTED: " + str); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::cleanUp() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x41 Delete Item
 | 
					
						
							|  |  |  |     //  Sub Cmd: Delete Process
 | 
					
						
							|  |  |  |     //ProcessID: 0x0000071F (1823)
 | 
					
						
							|  |  |  |     // [41 24 00 00 00 00 07 1F]
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |     QByteArray ba; | 
					
						
							|  |  |  |     appendByte(&ba, 0x00); | 
					
						
							|  |  |  |     appendByte(&ba, 0x00); | 
					
						
							|  |  |  |     appendInt(&ba, m_session.pid); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     sendTrkMessage(0x41, 0, ba, "Delete process"); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x00
 | 
					
						
							|  |  |  |     // [80 24 00]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     foreach (const Breakpoint &bp, m_breakpoints) | 
					
						
							|  |  |  |         clearTrkBreakpoint(bp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x1C Clear Break
 | 
					
						
							|  |  |  |     // [1C 25 00 00 00 0A 78 6A 43 40]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |         //  Command: 0xA1 Notify Deleted
 | 
					
						
							|  |  |  |         // [A1 09 00 00 00 00 00 00 00 00 07 1F]
 | 
					
						
							|  |  |  |         //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |         //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |         //    Error: 0x00
 | 
					
						
							|  |  |  |         // [80 09 00]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x00
 | 
					
						
							|  |  |  |     // [80 25 00]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x1C Clear Break
 | 
					
						
							|  |  |  |     // [1C 26 00 00 00 0B 78 6A 43 70]
 | 
					
						
							|  |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     //    Error: 0x00
 | 
					
						
							|  |  |  |     // [80 26 00]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //---IDE------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x02 Disconnect
 | 
					
						
							|  |  |  |     // [02 27]
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  | //    sendTrkMessage(0x02, CB(handleDisconnect));
 | 
					
						
							| 
									
										
										
										
											2009-07-16 16:05:43 +02:00
										 |  |  |     //---TRK------------------------------------------------------
 | 
					
						
							|  |  |  |     //  Command: 0x80 Acknowledge
 | 
					
						
							|  |  |  |     // Error: 0x00
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  | void Adapter::readMemory(uint addr, uint len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Q_ASSERT(len < (2 << 16)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // We try to get medium-sized chunks of data from the device
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QList <uint> blocksToFetch; | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     uint blockaddr = (addr / MemoryChunkSize) * MemoryChunkSize; | 
					
						
							|  |  |  |     for (; blockaddr < addr + len; blockaddr += MemoryChunkSize) { | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         QByteArray blockdata = m_snapshot.memory[blockaddr]; | 
					
						
							|  |  |  |         if (blockdata.isEmpty()) { | 
					
						
							|  |  |  |             // fetch it
 | 
					
						
							|  |  |  |             QByteArray ba; | 
					
						
							|  |  |  |             appendByte(&ba, 0x08); // Options, FIXME: why?
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |             appendShort(&ba, MemoryChunkSize); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |             appendInt(&ba, blockaddr); | 
					
						
							|  |  |  |             appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |             appendInt(&ba, m_session.tid); | 
					
						
							|  |  |  |             // Read Memory
 | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |             sendTrkMessage(0x10, CB(handleReadMemory), ba, QVariant(blockaddr), true); | 
					
						
							| 
									
										
										
										
											2009-07-17 12:13:38 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     qulonglong cookie = (qulonglong(addr) << 32) + len; | 
					
						
							|  |  |  |     sendTrkMessage(TRK_SYNC, CB(reportReadMemory), QByteArray(), cookie); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  | void Adapter::startInferiorIfNeeded() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_session.pid != 0) { | 
					
						
							|  |  |  |         qDebug() << "Process already 'started'"; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // It's not started yet
 | 
					
						
							|  |  |  |     QByteArray ba; | 
					
						
							|  |  |  |     appendByte(&ba, 0); // ?
 | 
					
						
							|  |  |  |     appendByte(&ba, 0); // ?
 | 
					
						
							|  |  |  |     appendByte(&ba, 0); // ?
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-24 16:50:38 +02:00
										 |  |  |     QByteArray file("C:\\sys\\bin\\filebrowseapp.exe"); | 
					
						
							|  |  |  |     appendString(&ba, file, TargetByteOrder); | 
					
						
							| 
									
										
										
										
											2009-07-23 12:35:57 +02:00
										 |  |  |     sendTrkMessage(0x40, CB(handleCreateProcess), ba); // Create Item
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Adapter::interruptInferior() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QByteArray ba; | 
					
						
							|  |  |  |     // stop the thread (2) or the process (1) or the whole system (0)
 | 
					
						
							|  |  |  |     appendByte(&ba, 1); | 
					
						
							|  |  |  |     appendInt(&ba, m_session.pid); | 
					
						
							|  |  |  |     appendInt(&ba, m_session.tid); // threadID: 4 bytes Variable number of bytes.
 | 
					
						
							|  |  |  |     sendTrkMessage(0x1A, 0, ba, "Interrupting..."); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  | static bool readAdapterArgs(const QStringList &args, AdapterOptions *o) | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     int argNumber = 0; | 
					
						
							|  |  |  |     const QStringList::const_iterator cend = args.constEnd(); | 
					
						
							|  |  |  |     QStringList::const_iterator it = args.constBegin(); | 
					
						
							|  |  |  |     for (++it; it != cend; ++it) { | 
					
						
							|  |  |  |         if (it->startsWith(QLatin1Char('-'))) { | 
					
						
							|  |  |  |             if (*it == QLatin1String("-v")) { | 
					
						
							|  |  |  |                 o->verbose++; | 
					
						
							|  |  |  |             } else if (*it == QLatin1String("-q")) { | 
					
						
							|  |  |  |                 o->verbose = 0; | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |             } else if (*it == QLatin1String("-l")) { | 
					
						
							|  |  |  |                 o->registerEndianness = LittleEndian; | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             switch (argNumber++) { | 
					
						
							|  |  |  |             case 0: | 
					
						
							|  |  |  |                 o->trkServer = *it; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 1: | 
					
						
							|  |  |  |                 o->gdbServer = *it; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     return !o->gdbServer.isEmpty(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  | int main(int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | #ifdef Q_OS_UNIX
 | 
					
						
							|  |  |  |     signal(SIGUSR1, signalHandler); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QCoreApplication app(argc, argv); | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     AdapterOptions options; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!readAdapterArgs(app.arguments(), &options)) { | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |         qDebug("Usage: %s [-v|-q] [-l] <trkservername> <gdbserverport>\n" | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |                "Options: -v verbose\n" | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |                "         -q quiet\n" | 
					
						
							|  |  |  |                "         -l Set register endianness to little\n", argv[0]); | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Adapter adapter; | 
					
						
							| 
									
										
										
										
											2009-07-28 12:54:25 +02:00
										 |  |  |     adapter.setTrkServerName(options.trkServer); | 
					
						
							|  |  |  |     adapter.setGdbServerName(options.gdbServer); | 
					
						
							|  |  |  |     adapter.setVerbose(options.verbose); | 
					
						
							| 
									
										
										
										
											2009-07-29 16:40:25 +02:00
										 |  |  |     adapter.setRegisterEndianness(options.registerEndianness); | 
					
						
							| 
									
										
										
										
											2009-07-24 16:27:03 +02:00
										 |  |  |     if (adapter.startServer()) | 
					
						
							|  |  |  |         return app.exec(); | 
					
						
							|  |  |  |     return 4; | 
					
						
							| 
									
										
										
										
											2009-07-15 14:21:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "adapter.moc"
 |