-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput.cpp
991 lines (675 loc) · 24.7 KB
/
output.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
#include <ctype.h>
#include "config.h"
#include "debug.h"
#include "ihpp.h"
using namespace std;
#include "benchmark.h"
#include "output.h"
#include "tracingFuncs.h"
#define OUTPUT_LINE "=============================================================================================\n"
#define OUTPUT_LINE2 "=============================================================================================\n\n"
#define OUTPUT_PLINE "---------------------------------------------------------------------------------------------\n"
#define OUTPUT_PLINE2 "---------------------------------------------------------------------------------------------\n\n"
#define TSP " "
// TODO: drop this to_string() once Intel PIN starts supporting C++11
template <class T>
static inline string to_string(const T& val)
{
stringstream ss;
ss << val;
return ss.str();
}
inline void openTag(const char *tag, bool ret=false, ostream &s = globalSharedContext->OutFile) {
s << "<" << tag << (ret ? ">\n" : ">");
}
inline void closeTag(const char *tag, ostream &s = globalSharedContext->OutFile) {
s << "</" << tag << ">\n";
}
inline void openAttr(const char *tag, ostream &s = globalSharedContext->OutFile) {
s << tag << "=\"";
}
inline void closeAttr(const char *tag, ostream &s = globalSharedContext->OutFile) {
s << "\" ";
}
inline void benchmark_dump_before_kCCF(GlobalContext *globalCtx, BenchmarkObj *ctx)
{
#if IHPP_BENCHMARK
globalCtx->OutFile << endl;
globalCtx->OutFile << "Before creation of k-CCF\n";
globalCtx->OutFile << "Nodes created (valid): " << ctx->nodes_created << endl;
globalCtx->OutFile << "Nodes created (empty): " << ctx->empty_nodes_created << endl;
globalCtx->OutFile << "Nodes copied: " << ctx->nodes_copied << endl;
globalCtx->OutFile << endl;
#endif
}
inline void benchmark_dump_after_kCCF(GlobalContext *globalCtx, BenchmarkObj *ctx)
{
#if IHPP_BENCHMARK
globalCtx->OutFile << "After the creation of k-CCF\n";
globalCtx->OutFile << "Nodes created (valid): " << ctx->nodes_created << endl;
globalCtx->OutFile << "Nodes created (empty): " << ctx->empty_nodes_created << endl;
globalCtx->OutFile << "Nodes copied: " << ctx->nodes_copied << endl;
globalCtx->OutFile << "Forests copied: " << ctx->forests_copied << endl << endl;
#endif
}
template <typename T>
inline void print_title(GlobalContext *globalCtx, T str)
{
globalCtx->OutFile << OUTPUT_PLINE;
globalCtx->OutFile << str << "\n";
globalCtx->OutFile << OUTPUT_PLINE2;
}
template <typename T>
inline void print_thread_id(GlobalContext *ctx, T str)
{
ctx->OutFile << OUTPUT_LINE;
ctx->OutFile << OUTPUT_LINE;
ctx->OutFile << "Thread ID: " << str << "\n";
ctx->OutFile << OUTPUT_LINE;
ctx->OutFile << OUTPUT_LINE2;
}
template <typename T>
inline void print_openThread(T th)
{
if (globalSharedContext->options.xmloutput) {
openTag("thread", true);
openTag("id");
if (!globalSharedContext->options.joinThreads)
globalSharedContext->OutFile << th;
closeTag("id");
} else {
if (!globalSharedContext->options.joinThreads)
print_thread_id(globalSharedContext, th);
}
}
inline void print_closeThread()
{
if (globalSharedContext->options.xmloutput)
closeTag("thread");
}
template <typename T>
inline void print_func_name(GlobalContext *ctx, T str)
{
ctx->OutFile << OUTPUT_LINE;
ctx->OutFile << "Function: " << str << "()\n";
ctx->OutFile << OUTPUT_LINE;
}
static bool isStringPrintable(const char *str) {
const char *ptr = str;
if (!str)
return false;
while (*ptr)
if (!isprint(*ptr++))
return false;
return true;
}
static void dumpXmlNode(ihppNode &n, int ident=0) {
ostream &o = globalSharedContext->OutFile;
openTag("objAddr");
o << "0x" << hex << (size_t)n.getKey() << dec;
closeTag("objAddr");
openTag("counter");
o << n.getCounter();
closeTag("counter");
if (n.childrenCount()) {
openTag("children",true);
for (auto& child : n) {
openTag("child",true);
dumpXmlNode(child, ident+1);
closeTag("child");
}
closeTag("children");
}
}
static void dumpXmlForest(ihppForest& forest) {
for (auto& tree : forest) {
openTag("tree",true);
dumpXmlNode(tree);
closeTag("tree");
}
}
static void printContextInfo(GlobalContext *globalCtx, GenericTraceContext *ctx) {
ihppForest kccf;
ihppForest *kSFCopy = nullptr;
//Operations in this copy shouldn't be counted in benchmark
//because they are computed only for a visualization propouse:
//the K slab forest need to copied in order to save it's counter values
//resetted by kSlabForestKLevelCountersClear() before forest inversion operation
if (globalCtx->options.showkSF) {
BENCHMARK_OFF
kSFCopy = new ihppForest;
*kSFCopy=ctx->kSlabForest;
BENCHMARK_ON
}
if (!globalCtx->options.xmloutput) {
globalCtx->OutFile
<< "Nodes count of k slab forest: "
<< ctx->kSlabForest.recursiveAllNodesCount()
<< endl;
benchmark_dump_before_kCCF(globalCtx, ctx);
}
if (globalCtx->options.showkCCF) {
kSlabForestKLevelCountersClear(ctx->kSlabForest, ctx->rootKey, globalCtx->kval());
kccf = ctx->kSlabForest.inverseK(globalCtx->kval());
if (!globalCtx->options.xmloutput) {
benchmark_dump_after_kCCF(globalCtx, ctx);
globalCtx->OutFile
<< "Nodes count of kCCF: "
<< kccf.recursiveAllNodesCount()
<< endl
<< endl;
}
}
if (globalCtx->options.showkSF) {
if (!globalCtx->options.xmloutput) {
print_title(globalCtx, "DUMP of K-SF");
dump(*kSFCopy, globalCtx->OutFile);
} else {
openTag("kSF",true);
dumpXmlForest(*kSFCopy);
closeTag("kSF");
}
delete kSFCopy;
}
if (globalCtx->options.showkSF2) {
if (!globalCtx->options.xmloutput) {
print_title(globalCtx, "DUMP of K-SF with resetted counters");
dump(ctx->kSlabForest, globalCtx->OutFile);
}
}
if (!globalCtx->options.xmloutput)
globalCtx->OutFile << "\n\n\n";
if (globalCtx->options.showkCCF) {
if (!globalCtx->options.xmloutput) {
print_title(globalCtx, "DUMP of K-CCF");
dump(kccf, globalCtx->OutFile);
} else {
openTag("kCCF",true);
dumpXmlForest(kccf);
closeTag("kCCF");
}
}
}
static void printThreadContextInfo(GlobalContext *globalCtx, ThreadContext *ctx)
{
if (globalCtx->WorkingMode() == WM_FuncMode || globalCtx->WorkingMode() == WM_InterProcMode) {
printContextInfo(globalCtx, ctx);
return;
}
if (globalCtx->options.xmloutput) {
openTag("intraMode_ctx",true);
}
for (const auto& it : ctx->intraModeContexts) {
IntraModeContext *intraCtx = it.second;
ADDRINT funcAddr = intraCtx->getFunctionAddr();
FunctionObj *fc = globalCtx->allFuncs[funcAddr];
if (!globalCtx->options.xmloutput)
print_func_name(globalCtx, fc->functionName());
BENCHMARK_SET_FUNC(funcAddr);
if (globalCtx->options.xmloutput) {
openTag("func_ctx",true);
openTag("funcAddr");
globalCtx->OutFile << "0x" << hex << (size_t)fc->functionAddress() << dec;
closeTag("funcAddr");
}
printContextInfo(globalCtx, intraCtx);
if (globalCtx->options.xmloutput)
closeTag("func_ctx");
}
if (globalCtx->options.xmloutput)
closeTag("intraMode_ctx");
}
static void blockFuncMode_joinThreads(GlobalContext *globalCtx) {
ThreadContext *thCtx = globalCtx->threadContexts[0];
ThreadContext *thCtx2;
ihppForest *forest = &thCtx->kSlabForest;
for (unsigned i=1; i < globalCtx->threadContexts.size(); i++) {
thCtx2 = globalCtx->threadContexts[i];
forest->join(thCtx2->kSlabForest);
#if IHPP_BENCHMARK
thCtx->_BM_sumBenchmarkInfo(thCtx2);
#endif
}
if (globalCtx->threadContexts.size() > 1) {
globalCtx->threadContexts.erase(
globalCtx->threadContexts.begin() + 1,
globalCtx->threadContexts.end()
);
}
}
static void intraMode_joinThreads(GlobalContext *globalCtx) {
ThreadContext *th0Ctx = globalCtx->threadContexts[0];
ThreadContext *thCtx2;
IntraModeContext *intraCtx;
ihppForest *forest;
for (const auto& funcIt : globalCtx->allFuncs) {
if (!globalCtx->hasToTrace(funcIt.second->functionAddress()))
continue;
intraCtx = th0Ctx->getFunctionCtx(funcIt.first);
forest = &intraCtx->kSlabForest;
for (unsigned i=1; i < globalCtx->threadContexts.size(); i++) {
thCtx2 = globalCtx->threadContexts[i];
map<ADDRINT, IntraModeContext *>::iterator it
= thCtx2->intraModeContexts.find(funcIt.first);
if (it != thCtx2->intraModeContexts.end()) {
forest->join(it->second->kSlabForest);
#if IHPP_BENCHMARK
intraCtx->_BM_sumBenchmarkInfo(it->second);
#endif
}
}
}
if (globalCtx->threadContexts.size() > 1) {
globalCtx->threadContexts.erase(
globalCtx->threadContexts.begin() + 1,
globalCtx->threadContexts.end()
);
}
}
static string getInsName(const string& ins) {
size_t space_ch;
for (space_ch=0; space_ch < ins.size(); space_ch++)
if (ins[space_ch] == ' ')
break;
if (space_ch == ins.size())
return string();
return ins.substr(0, space_ch);
}
static string makeHumanJump(const insInfo& insData) {
GlobalContext *globalCtx = globalSharedContext;
string ins = insData.ins_text;
const string& ins_name = getInsName(ins);
if (!ins_name.size())
return ins;
ADDRINT addr = insData.targetAddr;
if (insData.externFuncName) {
ADDRINT diff = insData.targetAddr - insData.targetFuncAddr;
string diffStr;
if (diff > 0)
diffStr = to_string(diff);
ins = ins_name + string(" ") + string(insData.externFuncName) + string("+") + diffStr;
} else {
FuncsMapIt it3 = globalCtx->allFuncs.find(addr);
if (insData.isCall) {
if (it3 != globalCtx->allFuncs.end()) {
ins = ins_name + string(" ") + it3->second->functionName();
} else {
FuncsMapIt it3 = globalCtx->allFuncs.find(insData.targetFuncAddr);
if (it3 == globalCtx->allFuncs.end())
return ins;
ADDRINT diff = addr - insData.targetFuncAddr;
ins = ins_name + string(" ") + it3->second->functionName() + string("+") + to_string(diff);
const insInfo& targetIns = it3->second->instructions[addr];
if (targetIns.ins_text.size() && targetIns.isDirectBranchOrCall())
ins += string(" --> ") + makeHumanJump(targetIns);
}
} else {
BlocksMapIt it4 = globalCtx->allBlocks.find(addr);
if (it4 != globalCtx->allBlocks.end()) {
ins = ins_name + " " + (string)*it4->second;
return ins;
}
if (it3 != globalCtx->allFuncs.end()) {
ins = ins_name + string(" ") + it3->second->functionName();
return ins;
}
it3 = globalCtx->allFuncs.find(insData.targetFuncAddr);
ADDRINT diff = insData.targetAddr - insData.targetFuncAddr;
if (it3 != globalCtx->allFuncs.end())
ins = ins_name + string(" ") + it3->second->functionName() + string("+") + to_string(diff);
}
}
return ins;
}
static void makeHumanDisasm() {
GlobalContext *globalCtx = globalSharedContext;
for (const auto& funcIt : globalCtx->allFuncs) {
for (auto& it : funcIt.second->instructions) {
if (!it.second.isDirectBranchOrCall())
continue;
it.second.ins_text = makeHumanJump(it.second);
}
}
}
static void writeXmlConfig() {
GlobalContext *ctx = globalSharedContext;
ostream &o = globalSharedContext->OutFile;
openTag("configuration",true);
openTag("WorkingMode");
o << WorkingModesString[ctx->WorkingMode()];
closeTag("WorkingMode");
openTag("joinThreads");
o << ctx->options.joinThreads;
closeTag("joinThreads");
openTag("rollLoops");
o << ctx->options.rollLoops;
closeTag("rollLoops");
openTag("kvalue");
o << ctx->kval();
closeTag("kvalue");
openTag("insTracing");
o << ENABLE_INS_TRACING;
closeTag("insTracing");
openTag("subCallCheck");
o << ENABLE_SUBCALL_CHECK;
closeTag("subCallCheck");
openTag("relyOnSpCheck");
o << ENABLE_RELY_ON_SP_CHECK;
closeTag("relyOnSpCheck");
openTag("insTracingFwdJmpRec");
o << ENABLE_INS_FORWARD_JMP_RECOGNITION;
closeTag("insTracingFwdJmpRec");
openTag("windows");
o << USING_WINDOWS;
closeTag("windows");
openTag("archbits");
o << sizeof(void*)*8;
closeTag("archbits");
openTag("winFullTracingLongjmpTracing");
o << ENABLE_WIN32_FULLTRACE_TARGET_TRACING_LONGJMP;
closeTag("winFullTracingLongjmpTracing");
openTag("winMainAlignment");
o << ENABLE_WIN32_MAIN_ALIGNMENT;
closeTag("winMainAlignment");
closeTag("configuration");
}
static void print_outputInit() {
GlobalContext *ctx = globalSharedContext;
if (!ctx->options.xmloutput) {
if (ctx->options.blocksDisasm || ctx->options.funcsDisasm)
makeHumanDisasm();
ctx->OutFile << endl << endl << endl;
#if DEBUG
ctx->OutFile << "Size of a node: " << sizeof(ihppNode) << endl;
#endif
if (!ctx->options.kinf) {
ctx->OutFile << "K value: " << ctx->kval() << endl;
} else {
ctx->OutFile << "K value: INFINITE" << endl;
}
if (ctx->WorkingMode() != WM_FuncMode) {
ctx->OutFile << "Basic blocks count: " << ctx->allBlocks.size() << endl;
} else {
ctx->OutFile << "Functions count: " << ctx->allFuncs.size() << endl;
}
ctx->OutFile << "Maximum number of different threads: " << ctx->threadContexts.size() << endl;
ctx->OutFile << endl << endl;
} else {
ctx->OutFile << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl;
openTag("output",true);
writeXmlConfig();
}
}
static size_t getMaxFuncLen() {
GlobalContext *ctx = globalSharedContext;
size_t maxFuncLen = 0;
if (ctx->options.showFuncs || (ctx->options.showBlocks && !ctx->funcsToTrace.size())) {
for (const auto& it : ctx->allFuncs) {
FunctionObj *fc = it.second;
if (fc->functionName().size() > maxFuncLen)
maxFuncLen = fc->functionName().size();
}
}
return maxFuncLen;
}
static void print_ins(ADDRINT addr, const insInfo& info) {
GlobalContext *ctx = globalSharedContext;
openAttr("address");
ctx->OutFile << "0x" << hex << (size_t)addr << dec;
closeAttr("address");
openAttr("isDirectBranch");
ctx->OutFile << (info.isDirectBranchOrCall() && !info.isCall);
closeAttr("isDirectBranch");
openAttr("isDirectCall");
ctx->OutFile << (info.isDirectBranchOrCall() && info.isCall);
closeAttr("isDirectCall");
if (info.targetAddr) {
openAttr("targetAddr");
ctx->OutFile << "0x" << hex << (size_t)info.targetAddr << dec;
closeAttr("targetAddr");
}
if (info.targetFuncAddr) {
openAttr("targetAddrFunc");
ctx->OutFile << "0x" << hex << (size_t)info.targetFuncAddr << dec;
closeAttr("targetAddrFunc");
}
if (isStringPrintable(info.externFuncName)) {
openAttr("externFuncName");
ctx->OutFile << info.externFuncName;
closeAttr("externFuncName");
}
ctx->OutFile << ">\n";
openTag("disasm");
ctx->OutFile << info.ins_text;
closeTag("disasm");
if (info.externFuncName)
return;
if (!info.isCall)
return;
FuncsMapIt it = ctx->allFuncs.find(addr);
if (it != ctx->allFuncs.end())
return;
FuncsMapIt it2 = ctx->allFuncs.find(info.targetFuncAddr);
if (it2 == ctx->allFuncs.end())
return;
insInfo targetIns = it2->second->instructions[info.targetAddr];
if ((targetIns.ins_text).size() && targetIns.isDirectBranchOrCall()) {
openTag("twoStepCallIns", true);
print_ins(info.targetAddr, targetIns);
closeTag("twoStepCallIns");
}
}
static void print_showBlocks(size_t maxFuncLen) {
GlobalContext *ctx = globalSharedContext;
size_t maxLen=0;
if (!ctx->options.xmloutput) {
if (ctx->funcsToTrace.size()) {
for (const string& s : ctx->funcsToTrace)
if (s.size() > maxLen)
maxLen = s.size();
} else {
maxLen = maxFuncLen;
}
ctx->OutFile << "\n\n\n";
print_title(ctx, "All basic blocks");
} else {
openTag("basicblocks",true);
}
for (const auto& it : ctx->allBlocks) {
BasicBlock& bb = *it.second;
if (!ctx->options.xmloutput) {
ctx->OutFile << "block: ";
ctx->OutFile.width(maxLen+12);
ctx->OutFile << left << bb;
ctx->OutFile << " addr: 0x";
ctx->OutFile << hex << (size_t)bb.getKey() << dec;
ctx->OutFile << " simpleCounter: ";
ctx->OutFile.width(6);
ctx->OutFile << bb.getSimpleCounter();
} else {
ctx->OutFile << "<bb ";
openAttr("firstInsAddr");
ctx->OutFile << "0x" << hex << (size_t)bb.blockAddress() << dec;
closeAttr("firstInsAddr");
openAttr("lastInsAddr");
ctx->OutFile << "0x" << hex << (size_t)bb.blockEndAddress() << dec;
closeAttr("lastInsAddr");
openAttr("funcAddr");
ctx->OutFile << "0x" << hex << (size_t)bb.functionAddr() << dec;
closeAttr("funcAddr");
openAttr("simpleCounter");
ctx->OutFile << bb.getSimpleCounter();
closeAttr("simpleCounter");
openAttr("line");
ctx->OutFile << bb.firstLine();
closeAttr("line");
openAttr("col");
ctx->OutFile << bb.firstCh();
closeAttr("col");
}
if (ctx->options.blocksDisasm)
{
if (!ctx->options.xmloutput) {
//ctx->OutFile << TSP;
ctx->OutFile << "Disassembly: \n";
} else {
ctx->OutFile << ">\n";
openTag("instructions",true);
}
map<ADDRINT, insInfo>::iterator insIt;
for (insIt = bb.functionPtr->instructions.begin(); insIt != bb.functionPtr->instructions.end(); insIt++)
if (insIt->first == bb.blockAddress())
break;
for (; insIt != bb.functionPtr->instructions.end(); insIt++)
{
if (insIt->first > bb.blockEndAddress())
break;
if (!ctx->options.xmloutput) {
const string& ins = insIt->second.ins_text;
ctx->OutFile.width(maxLen+12);
ctx->OutFile << TSP << TSP << TSP << TSP;
ctx->OutFile << TSP << TSP << TSP << TSP;
ctx->OutFile << ins << endl;
} else {
ctx->OutFile << "<ins ";
print_ins(insIt->first, insIt->second);
closeTag("ins");
}
}
if (!ctx->options.xmloutput) {
ctx->OutFile << endl;
} else {
closeTag("instructions");
closeTag("bb");
}
} else {
if (ctx->options.xmloutput)
ctx->OutFile << "/>" << endl;
}
if (!ctx->options.blocksDisasm)
ctx->OutFile << endl;
}
if (ctx->options.xmloutput)
closeTag("basicblocks");
}
static void print_showFuncs(size_t maxFuncLen) {
GlobalContext *ctx = globalSharedContext;
if (!ctx->options.xmloutput) {
ctx->OutFile << "\n\n\n";
print_title(ctx, "All functions");
} else {
openTag("functions",true);
}
for (const auto& it : ctx->allFuncs) {
FunctionObj& fc = *it.second;
if (!ctx->options.xmloutput) {
ctx->OutFile << "function: ";
ctx->OutFile.width(maxFuncLen+4);
ctx->OutFile << left << fc;
ctx->OutFile << " addr: 0x" << hex << (size_t)fc.getKey() << dec;
ctx->OutFile << " simpleCounter: ";
ctx->OutFile.width(6);
ctx->OutFile << fc.getSimpleCounter() << endl;
if (ctx->options.funcsDisasm) {
ctx->OutFile << endl;
for (const auto& insIt : fc.instructions) {
ctx->OutFile << "\t\t";
ctx->OutFile << hex << (size_t)insIt.first << dec << " ";
ctx->OutFile << insIt.second.ins_text << endl;
}
ctx->OutFile << endl;
}
} else {
openTag("func",true);
openTag("name");
ctx->OutFile << fc.functionName();
closeTag("name");
openTag("address");
ctx->OutFile << "0x" << hex << (size_t)fc.functionAddress() << dec;
closeTag("address");
openTag("simpleCounter");
ctx->OutFile << fc.getSimpleCounter();
closeTag("simpleCounter");
if (fc.fileName().size()) {
openTag("fileName");
ctx->OutFile << fc.fileName();
closeTag("fileName");
}
if (fc.instructions.size() && ctx->options.funcsDisasm) {
openTag("instructions",true);
for (const auto& insIt : fc.instructions) {
ctx->OutFile << "<ins ";
print_ins(insIt.first, insIt.second);
closeTag("ins");
}
closeTag("instructions");
}
closeTag("func");
}
}
if (ctx->options.xmloutput)
closeTag("functions");
}
static void freeMemory() {
GlobalContext *ctx = globalSharedContext;
for (const auto& it : ctx->allFuncs) {
FunctionObj& fc = *it.second;
for (const auto& insIt : fc.instructions) {
if (insIt.second.externFuncName)
delete [] insIt.second.externFuncName;
}
}
}
void Fini(INT32 code, void *)
{
GlobalContext *ctx = globalSharedContext;
double diff = getMilliseconds() - ctx->timer;
fprintf(stderr, "[ IHPP ] Program running time: %.3f sec\n", diff/1000.0);
print_outputInit();
if (ctx->options.joinThreads) {
if (ctx->WorkingMode() != WM_IntraMode)
blockFuncMode_joinThreads(ctx);
else
intraMode_joinThreads(ctx);
}
if (ctx->options.xmloutput)
openTag("threads", true);
for (ThreadContext *threadCtx : ctx->threadContexts) {
print_openThread(threadCtx->getThreadID());
//Operations in printContextInfo (inversion of the K slab forest and others)
//have to counted as operations made by thread "threadID" and not by current thread.
BENCHMARK_SET_THREAD(threadCtx);
//cerr << "Computing kCCF for thread " << threadCtx->threadID << "...\n";
printThreadContextInfo(ctx, threadCtx);
print_closeThread();
}
if (ctx->options.xmloutput)
closeTag("threads");
size_t maxFuncLen = getMaxFuncLen();
if (ctx->options.showBlocks)
print_showBlocks(maxFuncLen);
if (ctx->options.showFuncs)
print_showFuncs(maxFuncLen);
if (ctx->options.xmloutput)
closeTag("output");
/*
********************************
Finalization
********************************
*/
ctx->OutFile.close();
freeMemory();
delete ctx;
}
void funcTraceDebugDump(GlobalContext *globalCtx, FunctionObj *fc,
ThreadContext *ctx, ADDRINT reg_sp,
ihppNode *treeTop, ihppNode *treeBottom)
{
for (unsigned int i=0; i < ctx->shadowStack.size(); i++)
globalCtx->OutFile << " ";
if (globalCtx->threadContexts.size() > 1)
globalCtx->OutFile << "[ Thread: " << PIN_ThreadUid() << " ] " << fc->functionName() << endl;
else
globalCtx->OutFile << fc->functionName() << endl;
globalCtx->OutFile << OUTPUT_PLINE;
}