library: added dp_*{h,cc} files as libbarrydp library ('.debug' parser)
[barry.git] / src / dp_codinfo.cc
blobeb6d64073578bf000491ec29f98de1784ffef005
1 /**
2 * @file dp_codinfo.cc
3 * @author Nicolas VIVIEN
4 * @date 2009-08-01
6 * @note CopyRight Nicolas VIVIEN
8 * @brief COD debug file parser
9 * RIM's JDE generates several files when you build a COD application.
10 * Indeed, with the COD files for the device, we have a ".debug" file.
11 * This file is usefull to debug an application from JVM.
12 * This tool is a parser to understand these ".debug" files.
14 * @par Modifications
15 * - 2009/08/01 : N. VIVIEN
16 * - First release
18 * @par Licences
19 * Copyright (C) 2009-2010, Nicolas VIVIEN
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
30 * See the GNU General Public License in the COPYING file at the
31 * root directory of this project for more details.
35 #include <iostream>
36 #include <sstream>
37 #include <fstream>
38 #include <iomanip>
40 #include <sys/types.h>
41 #include <dirent.h>
42 #include <string.h>
44 #include "dp_parser.h"
45 #include "dp_codinfo.h"
48 using namespace std;
51 namespace Barry {
53 namespace JDG {
56 // Public API
57 //------------
59 #define DEBUG_FILE_EXT ".debug"
62 void searchDebugFile(JDGDebugFileList &list)
64 DIR *path;
65 struct dirent *entry;
67 path = opendir(".");
69 while( (entry = readdir(path)) ) {
70 int offset;
72 if (strlen(entry->d_name) < strlen(DEBUG_FILE_EXT))
73 continue;
75 offset = strlen(entry->d_name) - strlen(DEBUG_FILE_EXT);
77 if (!strcmp(entry->d_name + offset, DEBUG_FILE_EXT)) {
78 ifstream file(entry->d_name);
80 JDGCodInfo info;
82 // Parse header section
83 info.parseHeaderSection(file);
85 // Add element to list
86 list.AddElement(info.getUniqueId(), info.getAppName(), entry->d_name);
90 closedir(path);
94 bool loadDebugInfo(JDGDebugFileList &list, const char *filename, JDGCodInfo &info)
96 if (filename == NULL)
97 return false;
99 vector<JDGDebugFileEntry>::iterator b = list.begin();
101 for( ; b != list.end(); b++ ) {
102 JDGDebugFileEntry entry = (*b);
104 if (entry.fileName == string(filename)) {
105 info.loadDebugFile(filename);
106 return true;
110 return false;
114 bool loadDebugInfo(JDGDebugFileList &list, const uint32_t uniqueId, const std::string module, JDGCodInfo &info)
116 vector<JDGDebugFileEntry>::iterator b = list.begin();
118 for( ; b != list.end(); b++ ) {
119 JDGDebugFileEntry entry = (*b);
121 if ((entry.uniqueId == uniqueId) && (entry.appName == module)) {
122 info.loadDebugFile(entry.fileName.c_str());
123 return true;
127 return false;
131 // JDGDebugFileList class
132 //------------------------
134 void JDGDebugFileList::AddElement(uint32_t uniqueid, std::string appname, std::string filename)
136 JDGDebugFileEntry entry;
138 entry.uniqueId = uniqueid;
139 entry.appName = appname;
140 entry.fileName = filename;
142 push_back(entry);
146 void JDGDebugFileList::Dump(std::ostream &os) const
148 const_iterator i = begin(), e = end();
150 os << " UniqueID " << "|";
151 os << " Module Name " << "|";
152 os << " File Name " << endl;
154 os << "------------+";
155 os << "--------------------------+";
156 os << "--------------------------";
157 os << endl;
159 for( ; i != e; ++i ) {
160 (*i).Dump(os);
165 void JDGDebugFileEntry::Dump(std::ostream &os) const
167 os << " 0x" << setfill('0') << setw(8) << hex << uniqueId << " |";
168 os << " " << appName << setfill(' ') << setw(24) << " |";
169 os << " " << fileName << endl;
173 // JDGClassList class
174 //---------------------------
177 void JDGClassList::createDefaultEntries() {
178 JDGClassEntry entry;
180 // 1
181 entry.classPath = "com.rim.resources";
182 entry.className = "net_rim_rimsecuridlibRIMResources";
183 push_back(entry);
185 // 2
186 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
187 entry.className = "RimSecurIDLib";
188 push_back(entry);
190 // 3
191 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
192 entry.className = "RimDatabaseFullException";
193 push_back(entry);
195 // 4
196 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
197 entry.className = "RimDecryptFailException";
198 push_back(entry);
200 // 5
201 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
202 entry.className = "RimDuplicateNameException";
203 push_back(entry);
205 // 6
206 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
207 entry.className = "RimDuplicateTokenException";
208 push_back(entry);
210 // 7
211 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
212 entry.className = "RimInvalidParamException";
213 push_back(entry);
215 // 8
216 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
217 entry.className = "RimSecurIDLib";
218 push_back(entry);
220 // 9
221 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
222 entry.className = "RimWrongDeviceIDException";
223 push_back(entry);
225 // 10
226 entry.classPath = "net.rim.device.cldc.impl.softtoken.rimsecuridlib";
227 entry.className = "RimWrongFormFactorException";
228 push_back(entry);
232 // JDGCodInfo class
233 //------------------------
235 bool JDGCodInfo::loadDebugFile(const char *filename)
237 if (filename == NULL)
238 return false;
240 ifstream file(filename);
242 // Parse header file
243 parseHeaderSection(file);
245 // Parse type area zone
246 parseTypeSection(file);
248 return true;
252 uint32_t JDGCodInfo::getUniqueId()
254 return uniqueId;
258 string JDGCodInfo::getAppName()
260 return appName;
264 // Private API - Section parsing
265 //-------------------------------
267 void JDGCodInfo::parseHeaderSection(ifstream &input)
269 uint32_t type;
271 type = parseNextHeaderField(input);
273 if (type != COD_DEBUG_UNIQUEID_HEADERFIELD)
274 return;
276 type = parseNextHeaderField(input);
278 if (type != COD_DEBUG_APPNAME_HEADERFIELD)
279 return;
283 void JDGCodInfo::parseTypeSection(ifstream &input)
285 uint32_t type;
286 uint32_t count;
287 uint32_t nbr, check;
289 // Read number of declared type content into this section
290 nbr = ParseInteger(input);
292 // Read each object
293 count = 0;
295 while (!input.eof()) {
296 type = parseNextTypeField(input);
298 if (type == COD_DEBUG_NONE_FIELD)
299 break;
301 count++;
304 // Read again number of declared type content into this section
305 // We have to find the same value
306 check = ParseInteger(input);
308 // Checking...
309 cout << "Nbr = " << dec << nbr << " / Count = " << dec << count << " / check = " << check << endl;
313 uint32_t JDGCodInfo::parseNextHeaderField(ifstream &input)
315 uint32_t type = ParseInteger(input);
317 switch (type) {
318 case COD_DEBUG_UNIQUEID_HEADERFIELD:
319 parseUniqueId(input);
320 break;
322 case COD_DEBUG_APPNAME_HEADERFIELD:
323 parseAppName(input);
324 break;
326 default:
327 type = 0xFFFFFFFF;
330 return type;
334 uint32_t JDGCodInfo::parseNextTypeField(ifstream &input)
336 uint32_t type = ParseInteger(input);
338 switch (type) {
339 case COD_DEBUG_NONE_FIELD:
340 break;
342 case COD_DEBUG_BOOLEAN_FIELD:
343 parseBoolean(input);
344 break;
346 case COD_DEBUG_BYTE_FIELD:
347 parseByte(input);
348 break;
350 case COD_DEBUG_CHAR_FIELD:
351 parseChar(input);
352 break;
354 case COD_DEBUG_SHORT_FIELD:
355 parseShort(input);
356 break;
358 case COD_DEBUG_INT_FIELD:
359 parseInt(input);
360 break;
362 case COD_DEBUG_LONG_FIELD:
363 parseLong(input);
364 break;
366 case COD_DEBUG_CLASS_FIELD:
367 parseClass(input);
368 break;
370 case COD_DEBUG_ARRAY_FIELD:
371 parseArray(input);
372 break;
374 case COD_DEBUG_VOID_FIELD:
375 parseVoid(input);
376 break;
378 case COD_DEBUG_DOUBLE_FIELD:
379 parseDouble(input);
380 break;
382 default:
383 cout << "Type unknown ! " << hex << type << endl;
384 type = 0xFFFFFFFF;
387 return type;
391 void JDGCodInfo::parseUniqueId(ifstream &input)
393 uniqueId = ParseInteger(input);
397 void JDGCodInfo::parseAppName(ifstream &input)
399 uint32_t len = ParseInteger(input);
401 appName = ParseString(input, len);
405 void JDGCodInfo::parseBoolean(ifstream &input)
407 uint32_t len = ParseInteger(input);
409 string str = ParseString(input, len);
411 cout << "JDGCodInfo::parseBoolean" << endl;
412 cout << " name : " << str << endl;
416 void JDGCodInfo::parseByte(ifstream &input)
418 uint32_t len = ParseInteger(input);
420 string str = ParseString(input, len);
422 cout << "JDGCodInfo::parseByte" << endl;
423 cout << " name : " << str << endl;
427 void JDGCodInfo::parseChar(ifstream &input)
429 uint32_t len = ParseInteger(input);
431 string str = ParseString(input, len);
433 cout << "JDGCodInfo::parseChar" << endl;
434 cout << " name : " << str << endl;
438 void JDGCodInfo::parseShort(ifstream &input)
440 uint32_t len = ParseInteger(input);
442 string str = ParseString(input, len);
444 cout << "JDGCodInfo::parseShort" << endl;
445 cout << " name : " << str << endl;
449 void JDGCodInfo::parseInt(ifstream &input)
451 uint32_t len = ParseInteger(input);
453 string str = ParseString(input, len);
455 cout << "JDGCodInfo::parseInt" << endl;
456 cout << " name : " << str << endl;
460 void JDGCodInfo::parseLong(ifstream &input)
462 uint32_t len = ParseInteger(input);
464 string str = ParseString(input, len);
466 cout << "JDGCodInfo::parseLong" << endl;
467 cout << " name : " << str << endl;
471 void JDGCodInfo::parseClass(ifstream &input)
473 uint32_t len;
475 JDGClassEntry object;
477 cout << "JDGCodInfo::parseClass" << endl;
479 len = ParseInteger(input);
481 object.className = ParseString(input, len);
483 object.type = ParseInteger(input);
484 object.unknown02 = ParseInteger(input);
485 object.unknown03 = ParseInteger(input);
486 object.id = ParseInteger(input);
488 len = ParseInteger(input);
490 if (len == 0)
491 object.classPath = "com.barry." + appName;
492 else if (len != 0xFFFFFF)
493 object.classPath = ParseString(input, len);
495 len = ParseInteger(input);
497 object.sourceFile = ParseString(input, len);
499 object.unknown05 = ParseInteger(input);
500 object.unknown06 = ParseInteger(input);
501 object.unknown07 = ParseInteger(input);
502 object.unknown08 = ParseInteger(input);
504 classList.push_back(object);
506 cout << " name : " << object.className << endl;
507 cout << " path : " << object.classPath << endl;
508 cout << " type : " << hex << object.type << endl;
509 cout << " unknown02 : " << hex << object.unknown02 << endl;
510 cout << " unknown03 : " << hex << object.unknown03 << endl;
511 cout << " id : " << hex << object.id << endl;
512 cout << " source file : " << object.sourceFile << endl;
513 cout << " unknown05 : " << hex << object.unknown05 << endl;
514 cout << " unknown06 : " << hex << object.unknown06 << endl;
515 cout << " unknown07 : " << hex << object.unknown07 << endl;
516 cout << " unknown08 : " << hex << object.unknown08 << endl;
520 void JDGCodInfo::parseArray(ifstream &input)
522 uint32_t len = ParseInteger(input);
524 string str = ParseString(input, len);
526 cout << "JDGCodInfo::parseArray" << endl;
527 cout << " name : " << str << endl;
531 void JDGCodInfo::parseVoid(ifstream &input)
533 uint32_t len = ParseInteger(input);
535 string str = ParseString(input, len);
537 cout << "JDGCodInfo::parseVoid" << endl;
538 cout << " name : " << str << endl;
542 void JDGCodInfo::parseDouble(ifstream &input)
544 uint32_t len = ParseInteger(input);
546 string str = ParseString(input, len);
548 cout << "JDGCodInfo::parseDouble" << endl;
549 cout << " name : " << str << endl;
553 void JDGCodInfo::parseType2(ifstream &input) {
554 uint32_t value;
555 uint32_t len = ParseInteger(input);
557 string str = ParseString(input, len);
559 cout << "Type2 : " << str << endl;
561 value = ParseInteger(input);
562 value = ParseInteger(input);
563 value = ParseInteger(input);
564 value = ParseInteger(input);
565 value = ParseInteger(input);
566 value = ParseInteger(input);
569 } // namespace JDG
571 } // namespace Barry