2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "gdbmi.h"
|
2008-12-09 16:18:28 +01:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <QtCore/QByteArray>
|
|
|
|
|
#include <QtCore/QTextStream>
|
|
|
|
|
|
2009-05-04 20:49:40 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace Debugger {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2009-03-23 14:34:01 +01:00
|
|
|
QTextStream &operator<<(QTextStream &os, const GdbMi &mi)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
return os << mi.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 11:12:12 +01:00
|
|
|
void GdbMi::parseResultOrValue(const char *&from, const char *to)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-05-04 20:49:40 +02:00
|
|
|
while (from != to && isspace(*from))
|
2008-12-02 12:01:29 +01:00
|
|
|
++from;
|
|
|
|
|
|
2009-01-21 11:12:12 +01:00
|
|
|
//qDebug() << "parseResultOrValue: " << QByteArray(from, to - from);
|
2008-12-02 12:01:29 +01:00
|
|
|
parseValue(from, to);
|
|
|
|
|
if (isValid()) {
|
2009-05-18 14:15:25 +02:00
|
|
|
//qDebug() << "no valid result in " << QByteArray(from, to - from);
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (from == to || *from == '(')
|
|
|
|
|
return;
|
2009-01-21 11:12:12 +01:00
|
|
|
const char *ptr = from;
|
2008-12-02 12:01:29 +01:00
|
|
|
while (ptr < to && *ptr != '=') {
|
|
|
|
|
//qDebug() << "adding" << QChar(*ptr) << "to name";
|
|
|
|
|
++ptr;
|
|
|
|
|
}
|
|
|
|
|
m_name = QByteArray(from, ptr - from);
|
|
|
|
|
from = ptr;
|
|
|
|
|
if (from < to && *from == '=') {
|
|
|
|
|
++from;
|
|
|
|
|
parseValue(from, to);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 11:12:12 +01:00
|
|
|
QByteArray GdbMi::parseCString(const char *&from, const char *to)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
QByteArray result;
|
2009-05-18 14:15:25 +02:00
|
|
|
//qDebug() << "parseCString: " << QByteArray(from, to - from);
|
2008-12-02 12:01:29 +01:00
|
|
|
if (*from != '"') {
|
|
|
|
|
qDebug() << "MI Parse Error, double quote expected";
|
2009-05-04 20:49:40 +02:00
|
|
|
++from; // So we don't hang
|
2008-12-02 12:01:29 +01:00
|
|
|
return QByteArray();
|
|
|
|
|
}
|
2009-01-21 11:12:12 +01:00
|
|
|
const char *ptr = from;
|
2008-12-02 12:01:29 +01:00
|
|
|
++ptr;
|
|
|
|
|
while (ptr < to) {
|
|
|
|
|
if (*ptr == '"') {
|
|
|
|
|
++ptr;
|
|
|
|
|
result = QByteArray(from + 1, ptr - from - 2);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-05-04 20:49:40 +02:00
|
|
|
if (*ptr == '\\') {
|
2008-12-02 12:01:29 +01:00
|
|
|
++ptr;
|
2009-05-04 20:49:40 +02:00
|
|
|
if (ptr == to) {
|
|
|
|
|
qDebug() << "MI Parse Error, unterminated backslash escape";
|
|
|
|
|
from = ptr; // So we don't hang
|
|
|
|
|
return QByteArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
++ptr;
|
|
|
|
|
}
|
2009-05-04 20:49:40 +02:00
|
|
|
from = ptr;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-05-04 20:49:40 +02:00
|
|
|
int idx = result.indexOf('\\');
|
|
|
|
|
if (idx >= 0) {
|
|
|
|
|
char *dst = result.data() + idx;
|
|
|
|
|
const char *src = dst + 1, *end = result.data() + result.length();
|
|
|
|
|
do {
|
|
|
|
|
char c = *src++;
|
|
|
|
|
switch (c) {
|
|
|
|
|
case 'a': *dst++ = '\a'; break;
|
|
|
|
|
case 'b': *dst++ = '\b'; break;
|
|
|
|
|
case 'f': *dst++ = '\f'; break;
|
|
|
|
|
case 'n': *dst++ = '\n'; break;
|
|
|
|
|
case 'r': *dst++ = '\r'; break;
|
|
|
|
|
case 't': *dst++ = '\t'; break;
|
|
|
|
|
case 'v': *dst++ = '\v'; break;
|
|
|
|
|
case '"': *dst++ = '"'; break;
|
|
|
|
|
case '\\': *dst++ = '\\'; break;
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
int chars = 0;
|
|
|
|
|
uchar prod = 0;
|
|
|
|
|
forever {
|
|
|
|
|
if (c < '0' || c > '7') {
|
|
|
|
|
--src;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
prod = prod * 8 + c - '0';
|
|
|
|
|
if (++chars == 3 || src == end)
|
|
|
|
|
break;
|
|
|
|
|
c = *src++;
|
|
|
|
|
}
|
|
|
|
|
if (!chars) {
|
|
|
|
|
qDebug() << "MI Parse Error, unrecognized backslash escape";
|
|
|
|
|
return QByteArray();
|
|
|
|
|
}
|
|
|
|
|
*dst++ = prod;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (src != end) {
|
|
|
|
|
char c = *src++;
|
|
|
|
|
if (c == '\\')
|
|
|
|
|
break;
|
|
|
|
|
*dst++ = c;
|
|
|
|
|
}
|
|
|
|
|
} while (src != end);
|
|
|
|
|
*dst = 0;
|
|
|
|
|
result.truncate(dst - result.data());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 11:12:12 +01:00
|
|
|
void GdbMi::parseValue(const char *&from, const char *to)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-05-18 14:15:25 +02:00
|
|
|
//qDebug() << "parseValue: " << QByteArray(from, to - from);
|
2008-12-02 12:01:29 +01:00
|
|
|
switch (*from) {
|
2009-04-06 14:33:45 +02:00
|
|
|
case '{':
|
|
|
|
|
parseTuple(from, to);
|
|
|
|
|
break;
|
|
|
|
|
case '[':
|
|
|
|
|
parseList(from, to);
|
|
|
|
|
break;
|
|
|
|
|
case '"':
|
|
|
|
|
m_type = Const;
|
|
|
|
|
m_data = parseCString(from, to);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-01-21 11:12:12 +01:00
|
|
|
void GdbMi::parseTuple(const char *&from, const char *to)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-05-18 14:15:25 +02:00
|
|
|
//qDebug() << "parseTuple: " << QByteArray(from, to - from);
|
2008-12-09 12:08:56 +01:00
|
|
|
QTC_ASSERT(*from == '{', /**/);
|
2008-12-02 12:01:29 +01:00
|
|
|
++from;
|
|
|
|
|
parseTuple_helper(from, to);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 11:12:12 +01:00
|
|
|
void GdbMi::parseTuple_helper(const char *&from, const char *to)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-05-18 14:15:25 +02:00
|
|
|
//qDebug() << "parseTuple_helper: " << QByteArray(from, to - from);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_type = Tuple;
|
|
|
|
|
while (from < to) {
|
|
|
|
|
if (*from == '}') {
|
|
|
|
|
++from;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
GdbMi child;
|
|
|
|
|
child.parseResultOrValue(from, to);
|
|
|
|
|
//qDebug() << "\n=======\n" << qPrintable(child.toString()) << "\n========\n";
|
|
|
|
|
if (!child.isValid())
|
|
|
|
|
return;
|
|
|
|
|
m_children += child;
|
|
|
|
|
if (*from == ',')
|
|
|
|
|
++from;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 11:12:12 +01:00
|
|
|
void GdbMi::parseList(const char *&from, const char *to)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-05-18 14:15:25 +02:00
|
|
|
//qDebug() << "parseList: " << QByteArray(from, to - from);
|
2008-12-09 12:08:56 +01:00
|
|
|
QTC_ASSERT(*from == '[', /**/);
|
2008-12-02 12:01:29 +01:00
|
|
|
++from;
|
|
|
|
|
m_type = List;
|
|
|
|
|
while (from < to) {
|
|
|
|
|
if (*from == ']') {
|
|
|
|
|
++from;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
GdbMi child;
|
|
|
|
|
child.parseResultOrValue(from, to);
|
|
|
|
|
if (child.isValid())
|
|
|
|
|
m_children += child;
|
|
|
|
|
if (*from == ',')
|
|
|
|
|
++from;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GdbMi::setStreamOutput(const QByteArray &name, const QByteArray &content)
|
|
|
|
|
{
|
|
|
|
|
if (content.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
GdbMi child;
|
|
|
|
|
child.m_type = Const;
|
|
|
|
|
child.m_name = name;
|
|
|
|
|
child.m_data = content;
|
|
|
|
|
m_children += child;
|
|
|
|
|
if (m_type == Invalid)
|
|
|
|
|
m_type = Tuple;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QByteArray ind(int indent)
|
|
|
|
|
{
|
|
|
|
|
return QByteArray(2 * indent, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GdbMi::dumpChildren(QByteArray * str, bool multiline, int indent) const
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < m_children.size(); ++i) {
|
|
|
|
|
if (i != 0) {
|
|
|
|
|
*str += ',';
|
|
|
|
|
if (multiline)
|
|
|
|
|
*str += '\n';
|
|
|
|
|
}
|
|
|
|
|
if (multiline)
|
|
|
|
|
*str += ind(indent);
|
|
|
|
|
*str += m_children.at(i).toString(multiline, indent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-05 15:53:17 +02:00
|
|
|
class MyString : public QString {
|
|
|
|
|
public:
|
|
|
|
|
ushort at(int i) const { return constData()[i].unicode(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class ST, typename CT>
|
|
|
|
|
inline ST escapeCStringTpl(const ST &ba)
|
2009-04-06 14:47:15 +02:00
|
|
|
{
|
2009-05-05 15:53:17 +02:00
|
|
|
ST ret;
|
2009-05-04 20:49:40 +02:00
|
|
|
ret.reserve(ba.length() * 2);
|
|
|
|
|
for (int i = 0; i < ba.length(); ++i) {
|
2009-05-05 15:53:17 +02:00
|
|
|
CT c = ba.at(i);
|
2009-05-04 20:49:40 +02:00
|
|
|
switch (c) {
|
|
|
|
|
case '\\': ret += "\\\\"; break;
|
|
|
|
|
case '\a': ret += "\\a"; break;
|
|
|
|
|
case '\b': ret += "\\b"; break;
|
|
|
|
|
case '\f': ret += "\\f"; break;
|
|
|
|
|
case '\n': ret += "\\n"; break;
|
|
|
|
|
case '\r': ret += "\\r"; break;
|
|
|
|
|
case '\t': ret += "\\t"; break;
|
|
|
|
|
case '\v': ret += "\\v"; break;
|
|
|
|
|
case '"': ret += "\\\""; break;
|
|
|
|
|
default:
|
|
|
|
|
if (c < 32 || c == 127) {
|
|
|
|
|
ret += '\\';
|
|
|
|
|
ret += '0' + (c >> 6);
|
|
|
|
|
ret += '0' + ((c >> 3) & 7);
|
|
|
|
|
ret += '0' + (c & 7);
|
|
|
|
|
} else {
|
|
|
|
|
ret += c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2009-04-06 14:47:15 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-05 15:53:17 +02:00
|
|
|
QString GdbMi::escapeCString(const QString &ba)
|
|
|
|
|
{
|
|
|
|
|
return escapeCStringTpl<MyString, ushort>(static_cast<const MyString &>(ba));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray GdbMi::escapeCString(const QByteArray &ba)
|
|
|
|
|
{
|
|
|
|
|
return escapeCStringTpl<QByteArray, uchar>(ba);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QByteArray GdbMi::toString(bool multiline, int indent) const
|
|
|
|
|
{
|
|
|
|
|
QByteArray result;
|
|
|
|
|
switch (m_type) {
|
2009-04-06 14:33:45 +02:00
|
|
|
case Invalid:
|
|
|
|
|
if (multiline)
|
|
|
|
|
result += ind(indent) + "Invalid\n";
|
|
|
|
|
else
|
|
|
|
|
result += "Invalid";
|
|
|
|
|
break;
|
2009-04-06 14:47:15 +02:00
|
|
|
case Const:
|
2009-04-06 14:33:45 +02:00
|
|
|
if (!m_name.isEmpty())
|
|
|
|
|
result += m_name + "=";
|
2009-05-04 20:49:40 +02:00
|
|
|
result += "\"" + escapeCString(m_data) + "\"";
|
2009-04-06 14:33:45 +02:00
|
|
|
break;
|
|
|
|
|
case Tuple:
|
|
|
|
|
if (!m_name.isEmpty())
|
|
|
|
|
result += m_name + "=";
|
|
|
|
|
if (multiline) {
|
|
|
|
|
result += "{\n";
|
|
|
|
|
dumpChildren(&result, multiline, indent + 1);
|
|
|
|
|
result += '\n' + ind(indent) + "}";
|
|
|
|
|
} else {
|
|
|
|
|
result += "{";
|
|
|
|
|
dumpChildren(&result, multiline, indent + 1);
|
|
|
|
|
result += "}";
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case List:
|
|
|
|
|
if (!m_name.isEmpty())
|
|
|
|
|
result += m_name + "=";
|
|
|
|
|
if (multiline) {
|
|
|
|
|
result += "[\n";
|
|
|
|
|
dumpChildren(&result, multiline, indent + 1);
|
|
|
|
|
result += '\n' + ind(indent) + "]";
|
|
|
|
|
} else {
|
|
|
|
|
result += "[";
|
|
|
|
|
dumpChildren(&result, multiline, indent + 1);
|
|
|
|
|
result += "]";
|
|
|
|
|
}
|
|
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GdbMi::fromString(const QByteArray &ba)
|
|
|
|
|
{
|
2009-01-21 11:12:12 +01:00
|
|
|
const char *from = ba.constBegin();
|
|
|
|
|
const char *to = ba.constEnd();
|
2008-12-02 12:01:29 +01:00
|
|
|
parseResultOrValue(from, to);
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-30 22:43:57 +02:00
|
|
|
GdbMi GdbMi::findChild(const char *name) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
for (int i = 0; i < m_children.size(); ++i)
|
|
|
|
|
if (m_children.at(i).m_name == name)
|
|
|
|
|
return m_children.at(i);
|
|
|
|
|
return GdbMi();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2009-09-24 11:16:00 +02:00
|
|
|
// GdbResponse
|
2008-12-02 12:01:29 +01:00
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2009-10-12 12:00:07 +02:00
|
|
|
QByteArray GdbResponse::stringFromResultClass(GdbResultClass resultClass)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
switch (resultClass) {
|
|
|
|
|
case GdbResultDone: return "done";
|
|
|
|
|
case GdbResultRunning: return "running";
|
|
|
|
|
case GdbResultConnected: return "connected";
|
|
|
|
|
case GdbResultError: return "error";
|
|
|
|
|
case GdbResultExit: return "exit";
|
|
|
|
|
default: return "unknown";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-09-24 11:16:00 +02:00
|
|
|
QByteArray GdbResponse::toString() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
QByteArray result;
|
|
|
|
|
if (token != -1)
|
|
|
|
|
result = QByteArray::number(token);
|
|
|
|
|
result += '^';
|
|
|
|
|
result += stringFromResultClass(resultClass);
|
|
|
|
|
if (data.isValid())
|
|
|
|
|
result += ',' + data.toString();
|
|
|
|
|
result += '\n';
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Debugger
|