OSDN Git Service

1fb0d932ddf6ce47c3ea377eda13940d6d05ca74
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / gdb / gdbmi.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "gdbmi.h"
35
36 #include <utils/qtcassert.h>
37
38 #include <QtCore/QByteArray>
39 #include <QtCore/QRegExp>
40 #include <QtCore/QTextStream>
41
42 #include <ctype.h>
43
44 namespace Debugger {
45 namespace Internal {
46
47 void skipCommas(const char *&from, const char *to)
48 {
49     while (*from == ',' && from != to)
50         ++from;
51 }
52
53 QTextStream &operator<<(QTextStream &os, const GdbMi &mi)
54 {
55     return os << mi.toString();
56 }
57
58 void GdbMi::parseResultOrValue(const char *&from, const char *to)
59 {
60     while (from != to && isspace(*from))
61         ++from;
62
63     //qDebug() << "parseResultOrValue: " << QByteArray(from, to - from);
64     parseValue(from, to);
65     if (isValid()) {
66         //qDebug() << "no valid result in " << QByteArray(from, to - from);
67         return;
68     }
69     if (from == to || *from == '(')
70         return;
71     const char *ptr = from;
72     while (ptr < to && *ptr != '=') {
73         //qDebug() << "adding" << QChar(*ptr) << "to name";
74         ++ptr;
75     }
76     m_name = QByteArray(from, ptr - from);
77     from = ptr;
78     if (from < to && *from == '=') {
79         ++from;
80         parseValue(from, to);
81     }
82 }
83
84 QByteArray GdbMi::parseCString(const char *&from, const char *to)
85 {
86     QByteArray result;
87     //qDebug() << "parseCString: " << QByteArray(from, to - from);
88     if (*from != '"') {
89         qDebug() << "MI Parse Error, double quote expected";
90         ++from; // So we don't hang
91         return QByteArray();
92     }
93     const char *ptr = from;
94     ++ptr;
95     while (ptr < to) {
96         if (*ptr == '"') {
97             ++ptr;
98             result = QByteArray(from + 1, ptr - from - 2);
99             break;
100         }
101         if (*ptr == '\\') {
102             ++ptr;
103             if (ptr == to) {
104                 qDebug() << "MI Parse Error, unterminated backslash escape";
105                 from = ptr; // So we don't hang
106                 return QByteArray();
107             }
108         }
109         ++ptr;
110     }
111     from = ptr;
112
113     int idx = result.indexOf('\\');
114     if (idx >= 0) {
115         char *dst = result.data() + idx;
116         const char *src = dst + 1, *end = result.data() + result.length();
117         do {
118             char c = *src++;
119             switch (c) {
120                 case 'a': *dst++ = '\a'; break;
121                 case 'b': *dst++ = '\b'; break;
122                 case 'f': *dst++ = '\f'; break;
123                 case 'n': *dst++ = '\n'; break;
124                 case 'r': *dst++ = '\r'; break;
125                 case 't': *dst++ = '\t'; break;
126                 case 'v': *dst++ = '\v'; break;
127                 case '"': *dst++ = '"'; break;
128                 case '\\': *dst++ = '\\'; break;
129                 default:
130                     {
131                         int chars = 0;
132                         uchar prod = 0;
133                         forever {
134                             if (c < '0' || c > '7') {
135                                 --src;
136                                 break;
137                             }
138                             prod = prod * 8 + c - '0';
139                             if (++chars == 3 || src == end)
140                                 break;
141                             c = *src++;
142                         }
143                         if (!chars) {
144                             qDebug() << "MI Parse Error, unrecognized backslash escape";
145                             return QByteArray();
146                         }
147                         *dst++ = prod;
148                     }
149             }
150             while (src != end) {
151                 char c = *src++;
152                 if (c == '\\')
153                     break;
154                 *dst++ = c;
155             }
156         } while (src != end);
157         *dst = 0;
158         result.truncate(dst - result.data());
159     }
160
161     return result;
162 }
163
164 void GdbMi::parseValue(const char *&from, const char *to)
165 {
166     //qDebug() << "parseValue: " << QByteArray(from, to - from);
167     switch (*from) {
168         case '{':
169             parseTuple(from, to);
170             break;
171         case '[':
172             parseList(from, to);
173             break;
174         case '"':
175             m_type = Const;
176             m_data = parseCString(from, to);
177             break;
178         default:
179             break;
180     }
181 }
182
183
184 void GdbMi::parseTuple(const char *&from, const char *to)
185 {
186     //qDebug() << "parseTuple: " << QByteArray(from, to - from);
187     QTC_ASSERT(*from == '{', /**/);
188     ++from;
189     parseTuple_helper(from, to);
190 }
191
192 void GdbMi::parseTuple_helper(const char *&from, const char *to)
193 {
194     skipCommas(from, to);
195     //qDebug() << "parseTuple_helper: " << QByteArray(from, to - from);
196     m_type = Tuple;
197     while (from < to) {
198         if (*from == '}') {
199             ++from;
200             break;
201         }
202         GdbMi child;
203         child.parseResultOrValue(from, to);
204         //qDebug() << "\n=======\n" << qPrintable(child.toString()) << "\n========\n";
205         if (!child.isValid())
206             return;
207         m_children += child;
208         skipCommas(from, to);
209     }
210 }
211
212 void GdbMi::parseList(const char *&from, const char *to)
213 {
214     //qDebug() << "parseList: " << QByteArray(from, to - from);
215     QTC_ASSERT(*from == '[', /**/);
216     ++from;
217     m_type = List;
218     skipCommas(from, to);
219     while (from < to) {
220         if (*from == ']') {
221             ++from;
222             break;
223         }
224         GdbMi child;
225         child.parseResultOrValue(from, to);
226         if (child.isValid())
227             m_children += child;
228         skipCommas(from, to);
229     }
230 }
231
232 void GdbMi::setStreamOutput(const QByteArray &name, const QByteArray &content)
233 {
234     if (content.isEmpty())
235         return;
236     GdbMi child;
237     child.m_type = Const;
238     child.m_name = name;
239     child.m_data = content;
240     m_children += child;
241     if (m_type == Invalid)
242         m_type = Tuple;
243 }
244
245 static QByteArray ind(int indent)
246 {
247     return QByteArray(2 * indent, ' ');
248 }
249
250 void GdbMi::dumpChildren(QByteArray * str, bool multiline, int indent) const
251 {
252     for (int i = 0; i < m_children.size(); ++i) {
253         if (i != 0) {
254             *str += ',';
255             if (multiline)
256                 *str += '\n';
257         }
258         if (multiline)
259             *str += ind(indent);
260         *str += m_children.at(i).toString(multiline, indent);
261     }
262 }
263
264 class MyString : public QString {
265 public:
266     ushort at(int i) const { return constData()[i].unicode(); }
267 };
268
269 template<class ST, typename CT>
270 inline ST escapeCStringTpl(const ST &ba)
271 {
272     ST ret;
273     ret.reserve(ba.length() * 2);
274     for (int i = 0; i < ba.length(); ++i) {
275         CT c = ba.at(i);
276         switch (c) {
277             case '\\': ret += "\\\\"; break;
278             case '\a': ret += "\\a"; break;
279             case '\b': ret += "\\b"; break;
280             case '\f': ret += "\\f"; break;
281             case '\n': ret += "\\n"; break;
282             case '\r': ret += "\\r"; break;
283             case '\t': ret += "\\t"; break;
284             case '\v': ret += "\\v"; break;
285             case '"': ret += "\\\""; break;
286             default:
287                 if (c < 32 || c == 127) {
288                     ret += '\\';
289                     ret += '0' + (c >> 6);
290                     ret += '0' + ((c >> 3) & 7);
291                     ret += '0' + (c & 7);
292                 } else {
293                     ret += c;
294                 }
295         }
296     }
297     return ret;
298 }
299
300 QString GdbMi::escapeCString(const QString &ba)
301 {
302     return escapeCStringTpl<MyString, ushort>(static_cast<const MyString &>(ba));
303 }
304
305 QByteArray GdbMi::escapeCString(const QByteArray &ba)
306 {
307     return escapeCStringTpl<QByteArray, uchar>(ba);
308 }
309
310 QByteArray GdbMi::toString(bool multiline, int indent) const
311 {
312     QByteArray result;
313     switch (m_type) {
314         case Invalid:
315             if (multiline)
316                 result += ind(indent) + "Invalid\n";
317             else
318                 result += "Invalid";
319             break;
320         case Const:
321             if (!m_name.isEmpty())
322                 result += m_name + '=';
323             result += '"' + escapeCString(m_data) + '"';
324             break;
325         case Tuple:
326             if (!m_name.isEmpty())
327                 result += m_name + '=';
328             if (multiline) {
329                 result += "{\n";
330                 dumpChildren(&result, multiline, indent + 1);
331                 result += '\n' + ind(indent) + '}';
332             } else {
333                 result += '{';
334                 dumpChildren(&result, multiline, indent + 1);
335                 result += '}';
336             }
337             break;
338         case List:
339             if (!m_name.isEmpty())
340                 result += m_name + '=';
341             if (multiline) {
342                 result += "[\n";
343                 dumpChildren(&result, multiline, indent + 1);
344                 result += '\n' + ind(indent) + ']';
345             } else {
346                 result += '[';
347                 dumpChildren(&result, multiline, indent + 1);
348                 result += ']';
349             }
350             break;
351     }
352     return result;
353 }
354
355 void GdbMi::fromString(const QByteArray &ba)
356 {
357     const char *from = ba.constBegin();
358     const char *to = ba.constEnd();
359     parseResultOrValue(from, to);
360 }
361
362 void GdbMi::fromStringMultiple(const QByteArray &ba)
363 {
364     const char *from = ba.constBegin();
365     const char *to = ba.constEnd();
366     parseTuple_helper(from, to);
367 }
368
369 GdbMi GdbMi::findChild(const char *name) const
370 {
371     for (int i = 0; i < m_children.size(); ++i)
372         if (m_children.at(i).m_name == name)
373             return m_children.at(i);
374     return GdbMi();
375 }
376
377 //////////////////////////////////////////////////////////////////////////////////
378 //
379 // GdbResponse
380 //
381 //////////////////////////////////////////////////////////////////////////////////
382
383 QByteArray GdbResponse::stringFromResultClass(GdbResultClass resultClass)
384 {
385     switch (resultClass) {
386         case GdbResultDone: return "done";
387         case GdbResultRunning: return "running";
388         case GdbResultConnected: return "connected";
389         case GdbResultError: return "error";
390         case GdbResultExit: return "exit";
391         default: return "unknown";
392     }
393 }
394
395 QByteArray GdbResponse::toString() const
396 {
397     QByteArray result;
398     if (token != -1)
399         result = QByteArray::number(token);
400     result += '^';
401     result += stringFromResultClass(resultClass);
402     if (data.isValid())
403         result += ',' + data.toString();
404     result += '\n';
405     return result;
406 }
407
408
409 //////////////////////////////////////////////////////////////////////////////////
410 //
411 // GdbResponse
412 //
413 //////////////////////////////////////////////////////////////////////////////////
414
415 void extractGdbVersion(const QString &msg,
416     int *gdbVersion, int *gdbBuildVersion, bool *isMacGdb)
417 {
418     const QChar dot(QLatin1Char('.'));
419
420     QString cleaned;
421     QString build;
422     bool inClean = true;
423     foreach (QChar c, msg) {
424         if (inClean && !cleaned.isEmpty() && c != dot && (c.isPunct() || c.isSpace()))
425             inClean = false;
426         if (inClean) {
427             if (c.isDigit())
428                 cleaned.append(c);
429             else if (!cleaned.isEmpty() && !cleaned.endsWith(dot))
430                 cleaned.append(dot);
431         } else {
432             if (c.isDigit())
433                 build.append(c);
434             else if (!build.isEmpty() && !build.endsWith(dot))
435                 build.append(dot);
436         }
437     }
438
439     *isMacGdb = msg.contains(QLatin1String("Apple version"));
440
441     *gdbVersion = 10000 * cleaned.section(dot, 0, 0).toInt()
442                   + 100 * cleaned.section(dot, 1, 1).toInt()
443                     + 1 * cleaned.section(dot, 2, 2).toInt();
444     if (cleaned.count(dot) >= 3)
445         *gdbBuildVersion = cleaned.section(dot, 3, 3).toInt();
446     else
447         *gdbBuildVersion = build.section(dot, 0, 0).toInt();
448
449     if (*isMacGdb)
450         *gdbBuildVersion = build.section(dot, 1, 1).toInt();
451 }
452
453 } // namespace Internal
454 } // namespace Debugger