Commit 66216448 authored by hjk's avatar hjk

debugger: implementation progress in the trk manual test

parent d88fceb2
This diff is collapsed.
......@@ -177,14 +177,18 @@ void TrkServer::writeToAdapter(byte command, byte token, const QByteArray &data)
void TrkServer::handleAdapterMessage(const TrkResult &result)
{
QByteArray data;
data.append(char(0x00)); // No error
switch (result.code) {
case 0x00: { // Ping
data.append(char(0x00)); // No error
writeToAdapter(0x80, 0x00, data);
break;
}
case 0x01: { // Connect
writeToAdapter(0x80, result.token, data);
break;
}
default:
data.append(char(0x10)); // Command not supported
data[0] = 0x10; // Command not supported
writeToAdapter(0xff, result.token, data);
break;
}
......
......@@ -192,8 +192,6 @@ QByteArray encode7d(const QByteArray &ba)
return res;
}
#define CB(s) &TrkClient::s
// FIXME: Use the QByteArray based version below?
QString stringFromByte(byte c)
{
......@@ -278,608 +276,6 @@ QByteArray errorMessage(byte code)
return "Unknown error";
}
TrkClient::TrkClient()
{
#if USE_NATIVE
m_hdevice = NULL;
#else
m_device = 0;
#endif
m_writeToken = 0;
m_readToken = 0;
m_writeBusy = false;
//m_breakpoints.append(Breakpoint(0x0370));
//m_breakpoints.append(Breakpoint(0x0340));
//m_breakpoints.append(Breakpoint(0x0040)); // E32Main
startTimer(100);
}
TrkClient::~TrkClient()
{
#if USE_NATIVE
CloseHandle(m_hdevice);
#else
delete m_device;
#endif
}
bool TrkClient::openPort(const QString &port)
{
// QFile does not work with "COM3", so work around
/*
FILE *f = fopen("COM3", "r+");
if (!f) {
logMessage("Could not open file ");
return;
}
m_device = new QFile;
if (!m_device->open(f, QIODevice::ReadWrite))
*/
#if 0
m_device = new Win_QextSerialPort(port);
m_device->setBaudRate(BAUD115200);
m_device->setDataBits(DATA_8);
m_device->setParity(PAR_NONE);
//m_device->setStopBits(STO);
m_device->setFlowControl(FLOW_OFF);
m_device->setTimeout(0, 500);
if (!m_device->open(QIODevice::ReadWrite)) {
QByteArray ba = m_device->errorString().toLatin1();
logMessage("Could not open device " << ba);
return;
}
#else
m_device = new QLocalSocket(this);
m_device->connectToServer(port);
return m_device->waitForConnected();
#endif
}
void TrkClient::timerEvent(QTimerEvent *)
{
//qDebug(".");
tryWrite();
tryRead();
}
unsigned char TrkClient::nextWriteToken()
{
++m_writeToken;
if (m_writeToken == 0)
++m_writeToken;
return m_writeToken;
}
void TrkClient::sendMessage(byte command,
CallBack callBack, const QByteArray &lit)
{
Message msg;
msg.command = command;
msg.token = nextWriteToken();
msg.callBack = callBack;
QList<QByteArray> list = lit.split(' ');
foreach (const QByteArray &item, list) {
if (item.isEmpty())
continue;
bool ok = false;
int i = item.toInt(&ok, 16);
msg.data.append(char(i));
}
//logMessage("PARSED: " << lit << " -> " << stringFromArray(data).toLatin1().data());
queueMessage(msg);
}
void TrkClient::sendInitialPing()
{
Message msg;
msg.command = 0x00; // Ping
msg.token = 0; // reset sequence count
queueMessage(msg);
}
void TrkClient::waitForFinished()
{
Message msg;
// initiate one last roundtrip to ensure all is flushed
msg.command = 0x00; // Ping
msg.token = nextWriteToken();
msg.callBack = CB(handleWaitForFinished);
queueMessage(msg);
}
void TrkClient::sendAck(byte token)
{
logMessage(QString("SENDING ACKNOWLEDGEMENT FOR TOKEN ").arg(int(token)));
Message msg;
msg.command = 0x80;
msg.token = token;
msg.data.append('\0');
// The acknowledgement must not be queued!
//queueMessage(msg);
doWrite(msg);
// 01 90 00 07 7e 80 01 00 7d 5e 7e
}
void TrkClient::queueMessage(const Message &msg)
{
m_writeQueue.append(msg);
}
void TrkClient::tryWrite()
{
if (m_writeBusy)
return;
if (m_writeQueue.isEmpty())
return;
doWrite(m_writeQueue.dequeue());
}
void TrkClient::doWrite(const Message &msg)
{
QByteArray ba = frameMessage(msg.command, msg.token, msg.data);
m_written.insert(msg.token, msg);
m_writeBusy = true;
#if USE_NATIVE
DWORD charsWritten;
if (!WriteFile( m_hdevice,
ba.data(),
ba.size(),
&charsWritten,
NULL)){
logMessage("WRITE ERROR: ");
}
logMessage("WRITE: " << qPrintable(stringFromArray(ba)));
FlushFileBuffers(m_hdevice);
#else
logMessage("WRITE: " << qPrintable(stringFromArray(ba)));
if (!m_device->write(ba))
logMessage("WRITE ERROR: " << m_device->errorString());
m_device->flush();
#endif
}
void TrkClient::tryRead()
{
//logMessage("TRY READ: " << m_device->bytesAvailable()
// << stringFromArray(m_readQueue);
#if USE_NATIVE
const int BUFFERSIZE = 1024;
char buffer[BUFFERSIZE];
DWORD charsRead;
while (ReadFile(m_hdevice, buffer, BUFFERSIZE, &charsRead, NULL)
&& BUFFERSIZE == charsRead) {
m_readQueue.append(buffer, charsRead);
}
m_readQueue.append(buffer, charsRead);
#else // USE_NATIVE
if (m_device->bytesAvailable() == 0 && m_readQueue.isEmpty())
return;
QByteArray res = m_device->readAll();
m_readQueue.append(res);
#endif // USE_NATIVE
if (m_readQueue.size() < 9) {
logMessage("ERROR READBUFFER INVALID (1): "
<< stringFromArray(m_readQueue));
m_readQueue.clear();
return;
}
while (!m_readQueue.isEmpty())
handleResult(extractResult(&m_readQueue));
m_writeBusy = false;
}
void TrkClient::handleResult(const TrkResult &result)
{
const char *prefix = "READ BUF: ";
QByteArray str = result.toString().toUtf8();
switch (result.code) {
case 0x80: { // ACK
logMessage(prefix << "ACK: " << str.data());
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_written.contains(result.token)) {
logMessage("NO ENTRY FOUND!");
}
Message msg = m_written.take(result.token);
CallBack cb = msg.callBack;
if (cb) {
//logMessage("HANDLE: " << stringFromArray(result.data));
(this->*cb)(result);
}
break;
}
case 0xff: { // NAK
logMessage(prefix << "NAK: " << str.data());
//logMessage(prefix << "TOKEN: " << result.token);
logMessage(prefix << "ERROR: " << errorMessage(result.data.at(0)));
break;
}
case 0x90: { // Notified Stopped
logMessage(prefix << "NOTE: STOPPED" << str.data());
// 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);
sendAck(result.token);
//Sleep(10000);
//cleanUp();
break;
}
case 0x91: { // Notify Exception (obsolete)
logMessage(prefix << "NOTE: EXCEPTION" << str.data());
sendAck(result.token);
break;
}
case 0x92: { //
logMessage(prefix << "NOTE: INTERNAL ERROR: " << str.data());
sendAck(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.data());
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);
sendMessage(0x18, CB(handleContinue),
formatInt(m_session.pid) + formatInt(m_session.tid));
//sendAck(result.token)
break;
}
case 0xa1: { // NotifyDeleted
logMessage(prefix << "NOTE: LIBRARY UNLOAD: " << str.data());
sendAck(result.token);
break;
}
case 0xa2: { // NotifyProcessorStarted
logMessage(prefix << "NOTE: PROCESSOR STARTED: " << str.data());
sendAck(result.token);
break;
}
case 0xa6: { // NotifyProcessorStandby
logMessage(prefix << "NOTE: PROCESSOR STANDBY: " << str.data());
sendAck(result.token);
break;
}
case 0xa7: { // NotifyProcessorReset
logMessage(prefix << "NOTE: PROCESSOR RESET: " << str.data());
sendAck(result.token);
break;
}
default: {
logMessage(prefix << "INVALID: " << str << result.data.size());
break;
}
}
}
void TrkClient::handleCpuType(const TrkResult &result)
{
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];
}
void TrkClient::handleCreateProcess(const TrkResult &result)
{
//---TRK------------------------------------------------------
// Command: 0x80 Acknowledge
// Error: 0x00
// [80 08 00 00 00 01 B5 00 00 01 B6 78 67 40 00 00
// 40 00 00]
logMessage(" RESULT: " << qPrintable(result.toString()));
const char *data = result.data.data();
m_session.pid = extractInt(data);
m_session.tid = extractInt(data + 4);
m_session.codeseg = extractInt(data + 8);
m_session.dataseg = extractInt(data + 12);
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);
//setBreakpoint(0x0000, ArmMode);
//clearBreakpoint(0x0000);
#if 1
foreach (const Breakpoint &bp, m_breakpoints)
setBreakpoint(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]
sendMessage(0x42, CB(handleReadInfo),
"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");
//sendMessage(0x42, CB(handleReadInfo),
// "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]
sendMessage(0x42, CB(handleReadInfo),
"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
//sendMessage(0x18, CB(handleStop),
// "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]
sendMessage(0x18, CB(handleContinue),
formatInt(m_session.pid) + formatInt(m_session.tid));
//sendMessage(0x18, CB(handleContinue),
// formatInt(m_session.pid) + "ff ff ff ff");
//---TRK------------------------------------------------------
// Command: 0x80 Acknowledge
// Error: 0x00
// [80 0E 00]
}
void TrkClient::setBreakpoint(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]
sendMessage(0x1B, CB(handleSetBreakpoint),
"82 "
+ QByteArray(bp.mode == ArmMode ? "00 " : "01 ")
+ formatInt(m_session.codeseg + bp.offset)
+ "00 00 00 01 00 00 00 00 " + formatInt(m_session.pid)
+ "FF FF FF FF");
//m_session.toekn
//---TRK------------------------------------------------------
// Command: 0x80 Acknowledge
// Error: 0x00
// [80 09 00 00 00 00 0A]
}
void TrkClient::handleSetBreakpoint(const TrkResult &result)
{
//---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 TrkClient::clearBreakpoint(const Breakpoint &bp)
{
sendMessage(0x1C, CB(handleClearBreakpoint),
//formatInt(m_session.codeseg + bp.offset));
"00 " + formatShort(bp.number)
+ formatInt(m_session.codeseg + bp.offset));
//---IDE------------------------------------------------------
// Command: 0x1C Clear Break
// [1C 25 00 00 00 0A 78 6A 43 40]
}
void TrkClient::handleClearBreakpoint(const TrkResult &result)
{
Q_UNUSED(result);
//---TRK------------------------------------------------------
// Command: 0x80 Acknowledge
// Error: 0x00
// [80 09 00 00 00 00 0A]
logMessage("CLEAR BREAKPOINT ");
}
void TrkClient::handleContinue(const TrkResult &result)
{
logMessage(" HANDLE CONTINUE: " << qPrintable(stringFromArray(result.data)));
//if (result.result.token)
//logMessage(" ERROR: " << byte(result.result.token)
// sendMessage(0x18, CB(handleContinue),
// formatInt(m_session.pid) + formatInt(m_session.tid));
//}
}
void TrkClient::handleDisconnect(const TrkResult &result)
{
logMessage(" HANDLE DISCONNECT: "
<< qPrintable(stringFromArray(result.data)));
//if (result.result.token)
//logMessage(" ERROR: " << byte(result.result.token)
// sendMessage(0x18, CB(handleContinue),
// formatInt(m_session.pid) + formatInt(m_session.tid));
//}
}
void TrkClient::handleDeleteProcess(const TrkResult &result)
{
logMessage(" HANDLE DELETE PROCESS: " <<
qPrintable(stringFromArray(result.data)));
//if (result.result.token)
//logMessage(" ERROR: " << byte(result.token)
// sendMessage(0x18, CB(handleContinue),
// formatInt(m_session.pid) + formatInt(m_session.tid));
//}
}
void TrkClient::handleStep(const TrkResult &result)
{
logMessage(" HANDLE STEP: " <<
qPrintable(stringFromArray(result.data)));
}
void TrkClient::handleStop(const TrkResult &result)
{
logMessage(" HANDLE STOP: " <<
qPrintable(stringFromArray(result.data)));
}
void TrkClient::handleReadInfo(const TrkResult &result)
{
logMessage(" HANDLE READ INFO: " <<
qPrintable(stringFromArray(result.data)));
}
void TrkClient::handleWaitForFinished(const TrkResult &result)
{
logMessage(" FINISHED: " << qPrintable(stringFromArray(result.data)));
//qApp->exit(1);
}
void TrkClient::handleSupportMask(const TrkResult &result)
{
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))
str.append(formatByte(i * 8 + j));
}
logMessage("SUPPORTED: " << str);
}
void TrkClient::cleanUp()
{
//
//---IDE------------------------------------------------------
// Command: 0x41 Delete Item
// Sub Cmd: Delete Process
//ProcessID: 0x0000071F (1823)
// [41 24 00 00 00 00 07 1F]
sendMessage(0x41, CB(handleDeleteProcess),
"00 00 " + formatInt(m_session.pid));
//---TRK------------------------------------------------------
// Command: 0x80 Acknowledge
// Error: 0x00
// [80 24 00]
foreach (const Breakpoint &bp, m_breakpoints)
clearBreakpoint(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]