1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "codamessage.h"
36 #include <QtCore/QString>
37 #include <QtCore/QTextStream>
39 // Names matching the enum
40 static const char *serviceNamesC[] =
41 { "Locator", "RunControl", "Processes", "Memory", "Settings", "Breakpoints",
42 "Registers", "Logging", "FileSystem", "SymbianInstall", "SymbianOSData",
47 SYMBIANUTILS_EXPORT QString joinByteArrays(const QVector<QByteArray> &a, char sep)
50 const int count = a.size();
51 for (int i = 0; i < count; i++) {
53 rc += QLatin1Char(sep);
54 rc += QString::fromUtf8(a.at(i));
59 static inline bool jsonToBool(const JsonValue& js)
61 return js.type() == JsonValue::Boolean && js.data() == "true";
64 SYMBIANUTILS_EXPORT const char *serviceName(Services s)
66 return serviceNamesC[s];
69 SYMBIANUTILS_EXPORT Services serviceFromName(const char *n)
71 const int count = sizeof(serviceNamesC)/sizeof(char *);
72 for (int i = 0; i < count; i++)
73 if (!qstrcmp(serviceNamesC[i], n))
74 return static_cast<Services>(i);
75 return UnknownService;
78 SYMBIANUTILS_EXPORT QString formatData(const QByteArray &a)
80 const int columns = 16;
83 str.setIntegerBase(16);
84 str.setPadChar(QLatin1Char('0'));
85 const unsigned char *start = reinterpret_cast<const unsigned char *>(a.constData());
86 const unsigned char *end = start + a.size();
87 for (const unsigned char *p = start; p < end ; ) {
95 for ( ; c < columns && p < end; c++, p++) {
96 const unsigned u = *p;
103 asc += QLatin1String("\\n");
106 asc += QLatin1String("\\r");
109 asc += QLatin1String("\\t");
112 if (u >= 32 && u < 128) {
113 asc += QLatin1Char(' ');
114 asc += QLatin1Char(u);
116 asc += QLatin1String(" .");
121 if (const int remainder = columns - c)
122 str << QString(3 * remainder, QLatin1Char(' '));
123 str << ' ' << asc << '\n';
128 // ----------- RunControlContext
129 RunControlContext::RunControlContext() :
130 flags(0), resumeFlags(0)
134 void RunControlContext::clear()
143 RunControlContext::Type RunControlContext::typeFromTcfId(const QByteArray &id)
145 // "p12" or "p12.t34"?
146 return id.contains(".t") ? Thread : Process;
149 unsigned RunControlContext::processId() const
151 return processIdFromTcdfId(id);
154 unsigned RunControlContext::threadId() const
156 return threadIdFromTcdfId(id);
159 unsigned RunControlContext::processIdFromTcdfId(const QByteArray &id)
161 // Cut out process id from "p12" or "p12.t34"?
162 if (!id.startsWith('p'))
164 const int dotPos = id.indexOf('.');
165 const int pLen = dotPos == -1 ? id.size() : dotPos;
166 return id.mid(1, pLen - 1).toUInt();
169 unsigned RunControlContext::threadIdFromTcdfId(const QByteArray &id)
171 const int tPos = id.indexOf(".t");
172 return tPos != -1 ? id.mid(tPos + 2).toUInt() : uint(0);
175 QByteArray RunControlContext::tcfId(unsigned processId, unsigned threadId /* = 0 */)
178 rc += QByteArray::number(processId);
181 rc += QByteArray::number(threadId);
186 RunControlContext::Type RunControlContext::type() const
188 return RunControlContext::typeFromTcfId(id);
191 bool RunControlContext::parse(const JsonValue &val)
194 if (val.type() != JsonValue::Object)
196 foreach(const JsonValue &c, val.children()) {
197 if (c.name() == "ID") {
199 } else if (c.name() == "OSID") {
201 } else if (c.name() == "ParentID") {
203 } else if (c.name() == "IsContainer") {
206 } else if (c.name() == "CanTerminate") {
208 flags |= CanTerminate;
209 } else if (c.name() == "CanResume") {
210 resumeFlags = c.data().toUInt();
211 } else if (c.name() == "HasState") {
214 } else if (c.name() == "CanSuspend") {
222 QString RunControlContext::toString() const
225 QTextStream str(&rc);
230 void RunControlContext::format(QTextStream &str) const
232 str << " id='" << id << "' osid='" << osid
233 << "' parentId='" << parentId <<"' ";
234 if (flags & Container)
235 str << "[container] ";
236 if (flags & HasState)
237 str << "[has state] ";
238 if (flags & CanSuspend)
239 str << "[can suspend] ";
240 if (flags & CanSuspend)
241 str << "[can terminate] ";
242 str.setIntegerBase(16);
243 str << " resume_flags: 0x" << resumeFlags;
244 str.setIntegerBase(10);
247 // ------ ModuleLoadEventInfo
248 ModuleLoadEventInfo::ModuleLoadEventInfo() :
249 loaded(false), codeAddress(0), dataAddress(0), requireResume(false)
253 void ModuleLoadEventInfo::clear()
255 loaded = requireResume = false;
256 codeAddress = dataAddress =0;
259 bool ModuleLoadEventInfo::parse(const JsonValue &val)
262 if (val.type() != JsonValue::Object)
264 foreach(const JsonValue &c, val.children()) {
265 if (c.name() == "Name") {
267 } else if (c.name() == "File") {
269 } else if (c.name() == "CodeAddress") {
270 codeAddress = c.data().toULongLong();
271 } else if (c.name() == "DataAddress") {
272 dataAddress = c.data().toULongLong();
273 } else if (c.name() == "Loaded") {
274 loaded = jsonToBool(c);
275 } else if (c.name() == "RequireResume") {
276 requireResume =jsonToBool(c);
281 void ModuleLoadEventInfo::format(QTextStream &str) const
283 str << "name='" << name << "' file='" << file << "' " <<
284 (loaded ? "[loaded] " : "[not loaded] ");
286 str << "[requires resume] ";
287 str.setIntegerBase(16);
288 str << " code: 0x" << codeAddress << " data: 0x" << dataAddress;
289 str.setIntegerBase(10);
292 // ---------------------- Breakpoint
294 // Types matching enum
295 static const char *breakPointTypesC[] = {"Software", "Hardware", "Auto"};
297 Breakpoint::Breakpoint(quint64 loc) :
298 type(Auto), enabled(true), ignoreCount(0), location(loc), size(1), thumb(true)
301 id = idFromLocation(location);
304 void Breakpoint::setContextId(unsigned processId, unsigned threadId)
306 contextIds = QVector<QByteArray>(1, RunControlContext::tcfId(processId, threadId));
309 QByteArray Breakpoint::idFromLocation(quint64 loc)
311 return QByteArray("BP_0x") + QByteArray::number(loc, 16);
314 QString Breakpoint::toString() const
317 QTextStream str(&rc);
318 str.setIntegerBase(16);
319 str << "Breakpoint '" << id << "' " << breakPointTypesC[type] << " for contexts '"
320 << joinByteArrays(contextIds, ',') << "' at 0x" << location;
321 str.setIntegerBase(10);
322 str << " size " << size;
328 str << " IgnoreCount " << ignoreCount;
332 JsonInputStream &operator<<(JsonInputStream &str, const Breakpoint &b)
334 if (b.contextIds.isEmpty())
335 qWarning("Coda::Breakpoint: No context ids specified");
337 str << '{' << "ID" << ':' << QString::fromUtf8(b.id) << ','
338 << "BreakpointType" << ':' << breakPointTypesC[b.type] << ','
339 << "Enabled" << ':' << b.enabled << ','
340 << "IgnoreCount" << ':' << b.ignoreCount << ','
341 << "ContextIds" << ':' << b.contextIds << ','
342 << "Location" << ':' << QString::number(b.location) << ','
343 << "Size" << ':' << b.size << ','
344 << "THUMB_BREAKPOINT" << ':' << b.thumb
350 CodaEvent::CodaEvent(Type type) : m_type(type)
354 CodaEvent::~CodaEvent()
358 CodaEvent::Type CodaEvent::type() const
363 QString CodaEvent::toString() const
368 static const char sharedLibrarySuspendReasonC[] = "Shared Library";
370 CodaEvent *CodaEvent::parseEvent(Services s, const QByteArray &nameBA, const QVector<JsonValue> &values)
374 if (nameBA == "Hello" && values.size() == 1 && values.front().type() == JsonValue::Array) {
375 QStringList services;
376 foreach (const JsonValue &jv, values.front().children())
377 services.push_back(QString::fromUtf8(jv.data()));
378 return new CodaLocatorHelloEvent(services);
381 case RunControlService:
384 // "id/PC/Reason/Data"
385 if (nameBA == "contextSuspended" && values.size() == 4) {
386 const QByteArray idBA = values.at(0).data();
387 const quint64 pc = values.at(1).data().toULongLong();
388 const QByteArray reasonBA = values.at(2).data();
389 QByteArray messageBA;
390 // Module load: Special
391 if (reasonBA == sharedLibrarySuspendReasonC) {
392 ModuleLoadEventInfo info;
393 if (!info.parse(values.at(3)))
395 return new CodaRunControlModuleLoadContextSuspendedEvent(idBA, reasonBA, pc, info);
397 // hash containing a 'message'-key with a verbose crash message.
398 if (values.at(3).type() == JsonValue::Object && values.at(3).childCount()
399 && values.at(3).children().at(0).type() == JsonValue::String)
400 messageBA = values.at(3).children().at(0).data();
402 return new CodaRunControlContextSuspendedEvent(idBA, reasonBA, messageBA, pc);
403 } // "contextSuspended"
404 if (nameBA == "contextAdded")
405 return CodaRunControlContextAddedEvent::parseEvent(values);
406 if (nameBA == "contextRemoved" && values.front().type() == JsonValue::Array) {
407 QVector<QByteArray> ids;
408 foreach(const JsonValue &c, values.front().children())
409 ids.push_back(c.data());
410 return new CodaRunControlContextRemovedEvent(ids);
414 if ((nameBA == "writeln" || nameBA == "write" /*not yet used*/) && values.size() >= 2)
415 return new CodaLoggingWriteEvent(values.at(0).data(), values.at(1).data());
423 // -------------- CodaServiceHelloEvent
424 CodaLocatorHelloEvent::CodaLocatorHelloEvent(const QStringList &s) :
425 CodaEvent(LocatorHello),
430 QString CodaLocatorHelloEvent::toString() const
432 return QLatin1String("ServiceHello: ") + m_services.join(QLatin1String(", "));
435 // -------------- Logging event
437 CodaLoggingWriteEvent::CodaLoggingWriteEvent(const QByteArray &console, const QByteArray &message) :
438 CodaEvent(LoggingWriteEvent), m_console(console), m_message(message)
442 QString CodaLoggingWriteEvent::toString() const
444 QByteArray msgBA = m_console;
447 return QString::fromUtf8(msgBA);
450 // -------------- CodaIdEvent
451 CodaIdEvent::CodaIdEvent(Type t, const QByteArray &id) :
452 CodaEvent(t), m_id(id)
456 // ---------- CodaIdsEvent
457 CodaIdsEvent::CodaIdsEvent(Type t, const QVector<QByteArray> &ids) :
458 CodaEvent(t), m_ids(ids)
462 QString CodaIdsEvent::joinedIdString(const char sep) const
464 return joinByteArrays(m_ids, sep);
467 // ---------------- CodaRunControlContextAddedEvent
468 CodaRunControlContextAddedEvent::CodaRunControlContextAddedEvent(const RunControlContexts &c) :
469 CodaEvent(RunControlContextAdded), m_contexts(c)
473 CodaRunControlContextAddedEvent
474 *CodaRunControlContextAddedEvent::parseEvent(const QVector<JsonValue> &values)
476 // Parse array of contexts
477 if (values.size() < 1 || values.front().type() != JsonValue::Array)
480 RunControlContexts contexts;
481 foreach (const JsonValue &v, values.front().children()) {
482 RunControlContext context;
483 if (context.parse(v))
484 contexts.push_back(context);
486 return new CodaRunControlContextAddedEvent(contexts);
489 QString CodaRunControlContextAddedEvent::toString() const
492 QTextStream str(&rc);
493 str << "RunControl: " << m_contexts.size() << " context(s) "
494 << (type() == RunControlContextAdded ? "added" : "removed")
496 foreach (const RunControlContext &c, m_contexts) {
503 // --------------- CodaRunControlContextRemovedEvent
504 CodaRunControlContextRemovedEvent::CodaRunControlContextRemovedEvent(const QVector<QByteArray> &ids) :
505 CodaIdsEvent(RunControlContextRemoved, ids)
509 QString CodaRunControlContextRemovedEvent::toString() const
511 return QLatin1String("RunControl: Removed contexts '") + joinedIdString() + ("'.");
514 // --------------- CodaRunControlContextSuspendedEvent
515 CodaRunControlContextSuspendedEvent::CodaRunControlContextSuspendedEvent(const QByteArray &id,
516 const QByteArray &reason,
517 const QByteArray &message,
519 CodaIdEvent(RunControlSuspended, id), m_pc(pc), m_reason(reason), m_message(message)
523 CodaRunControlContextSuspendedEvent::CodaRunControlContextSuspendedEvent(Type t,
524 const QByteArray &id,
525 const QByteArray &reason,
527 CodaIdEvent(t, id), m_pc(pc), m_reason(reason)
531 void CodaRunControlContextSuspendedEvent::format(QTextStream &str) const
533 str.setIntegerBase(16);
534 str << "RunControl: '" << idString() << "' suspended at 0x"
535 << m_pc << ": '" << m_reason << "'.";
536 str.setIntegerBase(10);
537 if (!m_message.isEmpty())
538 str << " (" <<m_message << ')';
541 QString CodaRunControlContextSuspendedEvent::toString() const
544 QTextStream str(&rc);
549 CodaRunControlContextSuspendedEvent::Reason CodaRunControlContextSuspendedEvent::reason() const
551 if (m_reason == sharedLibrarySuspendReasonC)
553 if (m_reason == "Breakpoint")
555 // 'Data abort exception'/'Thread has panicked' ... unfortunately somewhat unspecific.
556 if (m_reason.contains("Exception") || m_reason.contains("panick"))
561 CodaRunControlModuleLoadContextSuspendedEvent::CodaRunControlModuleLoadContextSuspendedEvent(const QByteArray &id,
562 const QByteArray &reason,
564 const ModuleLoadEventInfo &mi) :
565 CodaRunControlContextSuspendedEvent(RunControlModuleLoadSuspended, id, reason, pc),
570 QString CodaRunControlModuleLoadContextSuspendedEvent::toString() const
573 QTextStream str(&rc);
574 CodaRunControlContextSuspendedEvent::format(str);