| author | shrivatsa |
| Mon, 27 Sep 2010 14:51:17 +0530 | |
| changeset 12 | a0eee409ff14 |
| parent 3 | ebe3f8f03b59 |
| permissions | -rw-r--r-- |
| 0 | 1 |
/* |
2 |
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: Utility functions for getting data via 3rd party tools |
|
15 |
* |
|
16 |
*/ |
|
17 |
||
18 |
||
19 |
#include "la.hpp" |
|
20 |
||
21 |
// ---------------------------------------------------------------------------------------------------------- |
|
22 |
// TIP: Use The Regex Coach (http://weitz.de/regex-coach/) to check that the regular expressions are working correctly |
|
23 |
// Note that in C/C++ code \ has been replaced with \\ and " with \". |
|
24 |
// ---------------------------------------------------------------------------------------------------------- |
|
25 |
||
26 |
void GetSymbolTableWithNM(const string& nm_location, const string& lib_name, vector<string>& symbol_table) |
|
27 |
{
|
|
28 |
symbol_table.clear(); |
|
29 |
vector<string> tempVector; |
|
30 |
vector<ordinal> ordinals; |
|
31 |
||
32 |
// execute nm |
|
33 |
string cmd = nm_location + " --demangle \"" + lib_name + "\""; |
|
34 |
ExecuteCommand(cmd, tempVector); |
|
35 |
||
36 |
||
37 |
// parse the results of the command |
|
38 |
for (unsigned int j=0; j<tempVector.size(); j++) |
|
39 |
{
|
|
40 |
// first check if we have found the beginning of a block |
|
41 |
||
42 |
boost::cmatch matches1; |
|
43 |
boost::cmatch matches2; |
|
44 |
boost::cmatch matches3; |
|
45 |
||
46 |
bool match1 = false; |
|
47 |
bool match2 = false; |
|
48 |
bool match3 = false; |
|
49 |
||
50 |
// Symbian OS 6.1 LIB file, example: ds00001.o: |
|
51 |
boost::regex re1("^ds(\\d+)\\.o\\:");
|
|
52 |
match1 = boost::regex_match(tempVector.at(j).c_str(), matches1, re1); |
|
53 |
||
54 |
if (!match1) |
|
55 |
{
|
|
56 |
// Symbian OS 7.x-8.x LIB file, example: C:/DOCUME~1/mattlait/LOCALS~1/Temp/1/d1000s_00001.o: |
|
57 |
boost::regex re2("^\\S*s_(\\d+)\\.o\\:");
|
|
58 |
match2 = boost::regex_match(tempVector.at(j).c_str(), matches2, re2); |
|
59 |
||
60 |
if (!match2) |
|
61 |
{
|
|
62 |
// Symbian OS 9.x LIB file, example: AGENTDIALOG{000a0000}-14.o:
|
|
63 |
boost::regex re3("^\\S*\\{000a0000\\}-(\\d+)\\.o\\:");
|
|
64 |
match3 = boost::regex_match(tempVector.at(j).c_str(), matches3, re3); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
if (match1 || match2 || match3) |
|
69 |
{
|
|
70 |
// now get the ordinal number |
|
71 |
string ordNum; |
|
72 |
||
73 |
if (match1) |
|
74 |
{
|
|
75 |
string ms(matches1[1].first, matches1[1].second); |
|
76 |
ordNum = ms; |
|
77 |
} |
|
78 |
else if (match2) |
|
79 |
{
|
|
80 |
string ms(matches2[1].first, matches2[1].second); |
|
81 |
ordNum = ms; |
|
82 |
} |
|
83 |
else if (match3) |
|
84 |
{
|
|
85 |
string ms(matches3[1].first, matches3[1].second); |
|
86 |
ordNum = ms; |
|
87 |
} |
|
88 |
||
89 |
// now start looking for the line with the export name |
|
90 |
// eg: 00000000 T CUserActivityManager::RunL(void) |
|
91 |
while (j<tempVector.size()-1) |
|
92 |
{
|
|
93 |
j++; |
|
94 |
||
95 |
boost::regex re4("^\\d+\\sT\\s(.*)$");
|
|
96 |
boost::cmatch matches4; |
|
97 |
||
98 |
if (boost::regex_match(tempVector.at(j).c_str(), matches4, re4)) |
|
99 |
{
|
|
100 |
// now we have a full entry |
|
101 |
string ms(matches4[1].first, matches4[1].second); |
|
102 |
||
103 |
// append to the ordinal list |
|
104 |
ordinals.push_back(ordinal(Str2Int(ordNum), ms)); |
|
105 |
||
106 |
break; |
|
107 |
} |
|
108 |
} |
|
109 |
} // (match1 || match2) |
|
110 |
} // for (int j=0; j<tempVector.size(); j++) |
|
111 |
||
112 |
// convert the ordinal list into a symbol table |
|
113 |
ConvertOrdinalListIntoSymbolTable(ordinals, symbol_table, lib_name); |
|
114 |
||
115 |
||
116 |
//for (int k=0; k<symbol_table.size(); k++) |
|
117 |
//{
|
|
118 |
// cerr << symbol_table.at(k) << endl; |
|
119 |
//} |
|
120 |
||
121 |
//if (symbol_table.size() == 0) |
|
122 |
//{
|
|
123 |
// cerr << "Warning: No ordinals in " << lib_directory << lib_name << endl; |
|
124 |
//} |
|
125 |
||
126 |
||
127 |
} |
|
128 |
||
129 |
// ---------------------------------------------------------------------------------------------------------- |
|
130 |
||
131 |
void GetSymbolTableWithReadelf(const string& readelf_location, const string& cfilt_location, const string& lib_name, vector<string>& symbol_table) |
|
132 |
{
|
|
133 |
symbol_table.clear(); |
|
134 |
vector<string> tempVector; |
|
135 |
vector<ordinal> ordinals; |
|
136 |
||
137 |
// execute readelf |
|
138 |
// note: 2>NUL is used here to redirect standard error output to NULL since readelf seems to output lots of unwanted warning messages |
|
139 |
string cmd = readelf_location + " -s -W \"" + lib_name + "\" " + CERR_TO_NULL; |
|
140 |
ExecuteCommand(cmd, tempVector); |
|
141 |
||
142 |
||
143 |
// parse the results of the command |
|
144 |
for (unsigned int j=0; j<tempVector.size(); j++) |
|
145 |
{
|
|
146 |
boost::cmatch matches1; |
|
147 |
||
148 |
// example: |
|
149 |
// 1: 00000000 4 NOTYPE GLOBAL DEFAULT 1 _ZN13CSpdiaControl10DrawShadowER9CWindowGcRK5TSize@@SpdCtrl{000a0000}[10005986].dll
|
|
150 |
boost::regex re1("^\\s*(\\d+)\\:.+GLOBAL.+\\d+\\s+(.*)\\@\\@.*");
|
|
151 |
||
152 |
if (boost::regex_match(tempVector.at(j).c_str(), matches1, re1)) |
|
153 |
{
|
|
154 |
// match found |
|
155 |
string ms1(matches1[1].first, matches1[1].second); |
|
156 |
string ms2(matches1[2].first, matches1[2].second); |
|
157 |
||
158 |
// append to the ordinal list |
|
159 |
ordinals.push_back(ordinal(Str2Int(ms1), ms2)); |
|
160 |
} |
|
161 |
||
162 |
} // for (int j=0; j<tempVector.size(); j++) |
|
163 |
||
164 |
// convert the ordinal list into a symbol table |
|
165 |
ConvertOrdinalListIntoSymbolTable(ordinals, symbol_table, lib_name); |
|
166 |
||
167 |
// finally demangle all function names since it's not done in this case automatically |
|
168 |
DemangleOrdinalsInSymbolTable(cfilt_location, symbol_table); |
|
169 |
||
170 |
//for (int k=0; k<symbol_table.size(); k++) |
|
171 |
//{
|
|
172 |
// cerr << symbol_table.at(k) << endl; |
|
173 |
//} |
|
174 |
||
175 |
//if (symbol_table.size() == 0) |
|
176 |
//{
|
|
177 |
// cerr << "Warning: No ordinals in " << lib_directory << lib_name << endl; |
|
178 |
//} |
|
179 |
||
180 |
} |
|
181 |
||
182 |
// ---------------------------------------------------------------------------------------------------------- |
|
183 |
||
184 |
void GetSymbolTableWithArmar(const string& armar_location, const string& cfilt_location, const string& lib_name, vector<string>& symbol_table) |
|
185 |
{
|
|
186 |
symbol_table.clear(); |
|
187 |
vector<string> tempVector; |
|
188 |
vector<ordinal> ordinals; |
|
189 |
||
190 |
// execute armar |
|
191 |
string cmd = armar_location + " --zs \"" + lib_name + "\""; |
|
192 |
ExecuteCommand(cmd, tempVector); |
|
193 |
||
194 |
||
195 |
// parse the results of the command |
|
196 |
for (unsigned int j=0; j<tempVector.size(); j++) |
|
197 |
{
|
|
198 |
// find the entries, example: |
|
199 |
// _ZN13TAgnWeeklyRptC1Ev from AGNMODEL{000a0000}-187.o at offset 158366
|
|
200 |
boost::regex re1("(\\S*)\\s+from\\s.*-(\\d+)\\.o.*");
|
|
201 |
boost::cmatch matches1; |
|
202 |
||
203 |
if (boost::regex_match(tempVector.at(j).c_str(), matches1, re1)) |
|
204 |
{
|
|
205 |
// match found |
|
206 |
string ms1(matches1[2].first, matches1[2].second); |
|
207 |
string ms2(matches1[1].first, matches1[1].second); |
|
208 |
||
209 |
// append to the ordinal list |
|
210 |
ordinals.push_back(ordinal(Str2Int(ms1), ms2)); |
|
211 |
} |
|
212 |
||
213 |
} // for (int j=0; j<tempVector.size(); j++) |
|
214 |
||
215 |
// convert the ordinal list into a symbol table |
|
216 |
ConvertOrdinalListIntoSymbolTable(ordinals, symbol_table, lib_name); |
|
217 |
||
218 |
// finally demangle all function names since it's not done in this case automatically |
|
219 |
DemangleOrdinalsInSymbolTable(cfilt_location, symbol_table); |
|
220 |
||
221 |
||
222 |
//for (int k=0; k<symbol_table.size(); k++) |
|
223 |
//{
|
|
224 |
// cerr << symbol_table.at(k) << endl; |
|
225 |
//} |
|
226 |
||
227 |
//if (symbol_table.size() == 0) |
|
228 |
//{
|
|
229 |
// cerr << "Warning: No ordinals in " << lib_directory << lib_name << endl; |
|
230 |
//} |
|
231 |
||
232 |
} |
|
233 |
||
234 |
// ---------------------------------------------------------------------------------------------------------- |
|
235 |
||
236 |
void GetSymbolTableWithFromelf(const string& fromelf_location, const string& cfilt_location, const string& lib_name, vector<string>& symbol_table) |
|
237 |
{
|
|
238 |
symbol_table.clear(); |
|
239 |
vector<string> tempVector; |
|
240 |
vector<ordinal> ordinals; |
|
241 |
||
242 |
// execute fromelf |
|
243 |
string cmd = fromelf_location + " -s \"" + lib_name + "\""; |
|
244 |
ExecuteCommand(cmd, tempVector); |
|
245 |
||
246 |
// parse the results of the command |
|
247 |
for (unsigned int j=0; j<tempVector.size(); j++) |
|
248 |
{
|
|
249 |
// first find the start of the symbol table |
|
250 |
// ** Section #5 '.version' (SHT_GNU_versym) |
|
251 |
boost::regex re1("^.*(SHT_GNU_versym).*");
|
|
252 |
boost::cmatch matches1; |
|
253 |
||
254 |
if (boost::regex_match(tempVector.at(j).c_str(), matches1, re1)) |
|
255 |
{
|
|
256 |
//int previous_ordinal = 0; |
|
257 |
||
258 |
while (j<tempVector.size()-1) |
|
259 |
{
|
|
260 |
j++; |
|
261 |
||
262 |
//cout << tempVector.at(j) << endl; |
|
263 |
||
264 |
// now find the entries, examples: |
|
265 |
// 7 _ZNK17CPbkContactEngine9FsSessionEv 2 PbkEng{000a0000}[101f4cce].dll
|
|
266 |
// 8 _ZN17CPbkContactEngine19CreateEmptyContactLEv |
|
267 |
// 2 PbkEng{000a0000}[101f4cce].dll
|
|
268 |
// notice that line can be spread to two lines so make sure we don't accidentally get a wrong line |
|
269 |
||
270 |
// first parse out any unwanted lines |
|
271 |
boost::regex re2("^\\s*\\d+\\s+\\S+\\.\\w+$");
|
|
272 |
boost::cmatch matches2; |
|
273 |
if (boost::regex_match(tempVector.at(j).c_str(), matches2, re2)) |
|
274 |
{
|
|
275 |
//cout << "skipping" << endl; |
|
276 |
continue; |
|
277 |
} |
|
278 |
||
279 |
// now it should be the wanted line |
|
280 |
boost::regex re3("^\\s*(\\d+)\\s*(\\S*).*");
|
|
281 |
boost::cmatch matches3; |
|
282 |
||
283 |
if (boost::regex_match(tempVector.at(j).c_str(), matches3, re3)) |
|
284 |
{
|
|
285 |
// cout << tempVector.at(j) << endl; |
|
286 |
||
287 |
// match found |
|
288 |
string ms1(matches3[1].first, matches3[1].second); |
|
289 |
string ms2(matches3[2].first, matches3[2].second); |
|
290 |
||
291 |
// append to the ordinal list |
|
292 |
ordinals.push_back(ordinal(Str2Int(ms1), ms2)); |
|
293 |
||
294 |
//cout << "match " << ms1 << " " << ms2 << endl; |
|
295 |
||
296 |
// with fromelf, all entries are in order |
|
297 |
// check that we don't miss any ordinals |
|
298 |
//if (previous_ordinal + 1 != Str2Int(ms1)) |
|
299 |
//{
|
|
300 |
// cerr << endl << "Aborting because of Fromelf ordinal order check error in " << lib_directory << lib_name << " in line:" << endl << tempVector.at(j) << endl; |
|
301 |
// exit(11); |
|
302 |
//} |
|
303 |
//previous_ordinal = Str2Int(ms1); |
|
304 |
} |
|
305 |
||
|
3
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
306 |
//break from the loop before section6 begins this chage is introduced for rvct4 |
|
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
307 |
boost::regex re4("^.*(SHT_STRTAB).*");
|
|
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
308 |
boost::cmatch matches1; |
|
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
309 |
if (boost::regex_match(tempVector.at(j).c_str(), matches3, re4)) |
|
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
310 |
{
|
|
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
311 |
break; |
|
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
312 |
} |
|
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
313 |
|
|
ebe3f8f03b59
Compatibility Analyser updated to version 2.8.4. Support for Qt code analysis added.
noe\swadi
parents:
0
diff
changeset
|
314 |
|
| 0 | 315 |
} //while (j<tempVector.size()) |
316 |
||
317 |
||
318 |
} //if (boost::regex_match(tempVector.at(j).c_str(), matches1, re1)) |
|
319 |
||
320 |
} // for (int j=0; j<tempVector.size(); j++) |
|
321 |
||
322 |
// convert the ordinal list into a symbol table |
|
323 |
ConvertOrdinalListIntoSymbolTable(ordinals, symbol_table, lib_name); |
|
324 |
||
325 |
// finally demangle all function names since it's not done in this case automatically |
|
326 |
DemangleOrdinalsInSymbolTable(cfilt_location, symbol_table); |
|
327 |
||
328 |
//for (int k=0; k<symbol_table.size(); k++) |
|
329 |
//{
|
|
330 |
// cerr << symbol_table.at(k) << endl; |
|
331 |
//} |
|
332 |
||
333 |
//if (symbol_table.size() == 0) |
|
334 |
//{
|
|
335 |
// cerr << "Warning: No ordinals in " << lib_directory << lib_name << endl; |
|
336 |
//} |
|
337 |
||
338 |
} |
|
339 |
||
340 |
// ---------------------------------------------------------------------------------------------------------- |
|
341 |
||
342 |
void ConvertOrdinalListIntoSymbolTable(const vector<ordinal>& ordinals, vector<string>& symbol_table, const string& lib_path) |
|
343 |
{
|
|
344 |
// remove any invalid ordinals from the list |
|
345 |
vector<ordinal> ordinalVectorCopy; |
|
346 |
ordinalVectorCopy.reserve(ordinals.size()); |
|
347 |
||
348 |
for (unsigned int i=0; i<ordinals.size(); i++) |
|
349 |
{
|
|
350 |
if (ordinals.at(i).funcpos <= 0 && ordinals.at(i).funcpos > 32000) |
|
351 |
{
|
|
352 |
cerr << "Error: Invalid ordinal " << ordinals.at(i).funcname << " @ " << Int2Str(ordinals.at(i).funcpos) << endl; |
|
353 |
} |
|
354 |
else |
|
355 |
{
|
|
356 |
ordinalVectorCopy.push_back(ordinals.at(i)); |
|
357 |
} |
|
358 |
} |
|
359 |
||
360 |
// sort the ordinal list |
|
361 |
sort(ordinalVectorCopy.begin(), ordinalVectorCopy.end(), OrdinalCompare); |
|
362 |
||
363 |
// now check that there are no missing ordinals in the list |
|
364 |
unsigned int previous_ordnumber = 0; |
|
365 |
unsigned int current_ordnumber = 1; |
|
366 |
for (unsigned int i=0; i<ordinalVectorCopy.size(); i++) |
|
367 |
{
|
|
368 |
// get the current ordinal number |
|
369 |
current_ordnumber = ordinalVectorCopy.at(i).funcpos; |
|
370 |
//cout << "CurOrdNum: " << Int2Str(current_ordnumber) << endl; |
|
371 |
||
372 |
// the current ordinal number obviously should be one bigger than the previous one |
|
373 |
if ( current_ordnumber != previous_ordnumber+1 ) |
|
374 |
{
|
|
375 |
// append a dummy ordinal to the list |
|
376 |
ordinalVectorCopy.insert(ordinalVectorCopy.begin()+i, ordinal(i+1, "UnknownOrdinal-"+Int2Str(i+1))); |
|
377 |
current_ordnumber = i+1; |
|
378 |
//cout << "Appended a dummy ordinal at pos " << Int2Str(i+1) << endl; |
|
379 |
} |
|
380 |
||
381 |
// remember the previous ordinal number |
|
382 |
previous_ordnumber = current_ordnumber; |
|
383 |
||
384 |
// if the ordinal list is corrupted, it may lead to an infinite loop |
|
385 |
if (i>25000) |
|
386 |
{
|
|
387 |
cerr << endl << "Something went horribly wrong when trying to parse " << lib_path << ". Aborting." << endl; |
|
388 |
exit(10); |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
// finally copy data from the ordinal list to the symbol table |
|
393 |
if (symbol_table.size() < ordinalVectorCopy.size()) |
|
394 |
{
|
|
395 |
symbol_table.reserve(ordinalVectorCopy.size()); |
|
396 |
} |
|
397 |
for (unsigned int i=0; i<ordinalVectorCopy.size(); i++) |
|
398 |
{
|
|
399 |
symbol_table.push_back( ordinalVectorCopy.at(i).funcname ); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
// ---------------------------------------------------------------------------------------------------------- |
|
404 |
||
405 |
void DemangleOrdinalsInSymbolTable(const string& cfilt_location, vector<string>& symbol_table) |
|
406 |
{
|
|
407 |
ofstream f(_tempfile_location.c_str(), ios::trunc); |
|
408 |
if (f.is_open()) |
|
409 |
{
|
|
410 |
// create a temp file which contain all the function names |
|
411 |
for (unsigned int k=0; k<symbol_table.size(); k++) |
|
412 |
{
|
|
413 |
f << symbol_table.at(k) << endl; |
|
414 |
} |
|
415 |
f.close(); |
|
416 |
||
417 |
// execute cfilt |
|
418 |
vector<string> cfilt_result_set; |
|
419 |
string tempfile_loc = _tempfile_location; |
|
420 |
InsertQuotesToFilePath(tempfile_loc); |
|
421 |
string cmd = cfilt_location + " < " + tempfile_loc; |
|
422 |
ExecuteCommand(cmd, cfilt_result_set); |
|
423 |
||
424 |
// check that all functions exist and then replace the symbol table with demangled functions |
|
425 |
if (cfilt_result_set.size() == symbol_table.size()) |
|
426 |
{
|
|
427 |
symbol_table = cfilt_result_set; |
|
428 |
} |
|
429 |
} |
|
430 |
else |
|
431 |
{
|
|
432 |
f.close(); |
|
433 |
} |
|
434 |
} |
|
435 |
||
436 |
// ---------------------------------------------------------------------------------------------------------- |
|
437 |
||
438 |
bool OrdinalCompare(const ordinal& left, const ordinal& right) |
|
439 |
{
|
|
440 |
return left.funcpos < right.funcpos; |
|
441 |
} |
|
442 |
||
443 |
// ---------------------------------------------------------------------------------------------------------- |
|
444 |
||
445 |