使用Intel Pin 动态追踪函数运行调用过程

准备

1
2
Intel Pin 下载 编译 运行
略.详见 Intel Pin README.

源码

debugtrace.c

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
/*
* Copyright (C) 2004-2021 Intel Corporation.
* SPDX-License-Identifier: MIT
*/

/*! @file
* This file contains a tool that generates instructions traces with values.
* It is designed to help debugging.
*/

#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <unistd.h>
#include "pin.H"
#include "instlib.H"
#include "control_manager.H"
#include "regvalue_utils.h"

using namespace CONTROLLER;
using namespace INSTLIB;

struct kv_t
{
const char *function;
size_t address;
};

/* ===================================================================== */
/* Commandline Switches */
/* ===================================================================== */

KNOB< string > KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "debugtrace.out", "trace file");
KNOB< BOOL > KnobPid(KNOB_MODE_WRITEONCE, "pintool", "i", "0", "append pid to output");
KNOB< THREADID > KnobWatchThread(KNOB_MODE_WRITEONCE, "pintool", "watch_thread", "-1", "thread to watch, -1 for all");
KNOB< BOOL > KnobFlush(KNOB_MODE_WRITEONCE, "pintool", "flush", "0", "Flush output after every instruction");
KNOB< BOOL > KnobSymbols(KNOB_MODE_WRITEONCE, "pintool", "symbols", "1", "Include symbol information");
KNOB< BOOL > KnobLines(KNOB_MODE_WRITEONCE, "pintool", "lines", "0", "Include line number information");
KNOB< BOOL > KnobTraceInstructions(KNOB_MODE_WRITEONCE, "pintool", "instruction", "0", "Trace instructions");
KNOB< BOOL > KnobTraceCalls(KNOB_MODE_WRITEONCE, "pintool", "call", "1", "Trace calls");
KNOB< BOOL > KnobTraceMemory(KNOB_MODE_WRITEONCE, "pintool", "memory", "0", "Trace memory");
KNOB< BOOL > KnobSilent(KNOB_MODE_WRITEONCE, "pintool", "silent", "0", "Do everything but write file (for debugging).");
KNOB< BOOL > KnobEarlyOut(KNOB_MODE_WRITEONCE, "pintool", "early_out", "0", "Exit after tracing the first region.");

/* ===================================================================== */

INT32 Usage()
{
cerr << "This pin tool collects an instruction trace for debugging\n"
"\n";

cerr << KNOB_BASE::StringKnobSummary();

cerr << endl;

return -1;
}

/* ===================================================================== */
/* Global Variables */
/* ===================================================================== */

static std::ofstream out;

static INT32 enabled = 0;

static FILTER filter;

static ICOUNT icount;

static BOOL Emit(THREADID threadid)
{
if (!enabled || KnobSilent || (KnobWatchThread != static_cast< THREADID >(-1) && KnobWatchThread != threadid)) return false;
return true;
}

static VOID Flush()
{
if (KnobFlush) out << flush;
}

/* ===================================================================== */

static VOID Fini(int, VOID* v);

static VOID Handler(EVENT_TYPE ev, VOID*, CONTEXT* ctxt, VOID*, THREADID, bool bcast)
{
switch (ev)
{
case EVENT_START:
enabled = 1;
PIN_RemoveInstrumentation();
#if defined(TARGET_IA32) || defined(TARGET_IA32E)
// So that the rest of the current trace is re-instrumented.
if (ctxt) PIN_ExecuteAt(ctxt);
#endif
break;

case EVENT_STOP:
enabled = 0;
PIN_RemoveInstrumentation();
if (KnobEarlyOut)
{
cerr << "Exiting due to -early_out" << endl;
Fini(0, NULL);
exit(0);
}
#if defined(TARGET_IA32) || defined(TARGET_IA32E)
// So that the rest of the current trace is re-instrumented.
if (ctxt) PIN_ExecuteAt(ctxt);
#endif
break;

default:
ASSERTX(false);
}
}

/* ===================================================================== */

VOID EmitNoValues(THREADID threadid, string* str)
{
if (!Emit(threadid)) return;

out << *str << endl;

Flush();
}

VOID Emit1Values(THREADID threadid, string* str, string* reg1str, ADDRINT reg1val)
{
if (!Emit(threadid)) return;

out << *str << " | " << *reg1str << " = " << reg1val << endl;

Flush();
}

VOID Emit2Values(THREADID threadid, string* str, string* reg1str, ADDRINT reg1val, string* reg2str, ADDRINT reg2val)
{
if (!Emit(threadid)) return;

out << *str << " | " << *reg1str << " = " << reg1val << ", " << *reg2str << " = " << reg2val << endl;

Flush();
}

VOID Emit3Values(THREADID threadid, string* str, string* reg1str, ADDRINT reg1val, string* reg2str, ADDRINT reg2val,
string* reg3str, ADDRINT reg3val)
{
if (!Emit(threadid)) return;

out << *str << " | " << *reg1str << " = " << reg1val << ", " << *reg2str << " = " << reg2val << ", " << *reg3str << " = "
<< reg3val << endl;

Flush();
}

VOID Emit4Values(THREADID threadid, string* str, string* reg1str, ADDRINT reg1val, string* reg2str, ADDRINT reg2val,
string* reg3str, ADDRINT reg3val, string* reg4str, ADDRINT reg4val)
{
if (!Emit(threadid)) return;

out << *str << " | " << *reg1str << " = " << reg1val << ", " << *reg2str << " = " << reg2val << ", " << *reg3str << " = "
<< reg3val << ", " << *reg4str << " = " << reg4val << endl;

Flush();
}

const UINT32 MaxEmitArgs = 4;

AFUNPTR emitFuns[] = {AFUNPTR(EmitNoValues), AFUNPTR(Emit1Values), AFUNPTR(Emit2Values), AFUNPTR(Emit3Values),
AFUNPTR(Emit4Values)};

/* ===================================================================== */

VOID EmitXMM(THREADID threadid, UINT32 regno, PINTOOL_REGISTER* xmm)
{
if (!Emit(threadid)) return;
out << "\t\t\tXMM" << dec << regno << " := " << setfill('0') << hex;
out.unsetf(ios::showbase);
for (int i = 0; i < 16; i++)
{
if (i == 4 || i == 8 || i == 12) out << "_";
out << setw(2) << (int)xmm->byte[15 - i]; // msb on the left as in registers
}
out << setfill(' ') << endl;
out.setf(ios::showbase);
Flush();
}

VOID AddXMMEmit(INS ins, IPOINT point, REG xmm_dst)
{
INS_InsertCall(ins, point, AFUNPTR(EmitXMM), IARG_THREAD_ID, IARG_UINT32, xmm_dst - REG_XMM0, IARG_REG_CONST_REFERENCE,
xmm_dst, IARG_END);
}

VOID AddEmit(INS ins, IPOINT point, string& traceString, UINT32 regCount, REG regs[])
{
if (regCount > MaxEmitArgs) regCount = MaxEmitArgs;

IARGLIST args = IARGLIST_Alloc();
for (UINT32 i = 0; i < regCount; i++)
{
IARGLIST_AddArguments(args, IARG_PTR, new string(REG_StringShort(regs[i])), IARG_REG_VALUE, regs[i], IARG_END);
}

INS_InsertCall(ins, point, emitFuns[regCount], IARG_THREAD_ID, IARG_PTR, new string(traceString), IARG_IARGLIST, args,
IARG_END);
IARGLIST_Free(args);
}

static VOID* WriteEa[PIN_MAX_THREADS];

VOID CaptureWriteEa(THREADID threadid, VOID* addr) { WriteEa[threadid] = addr; }

VOID ShowN(UINT32 n, VOID* ea)
{
out.unsetf(ios::showbase);
// Print out the bytes in "big endian even though they are in memory little endian.
// This is most natural for 8B and 16B quantities that show up most frequently.
// The address pointed to
out << std::setfill('0');
UINT8 b[512];
UINT8* x;
if (n > 512)
x = new UINT8[n];
else
x = b;
PIN_SafeCopy(x, static_cast< UINT8* >(ea), n);
for (UINT32 i = 0; i < n; i++)
{
out << std::setw(2) << static_cast< UINT32 >(x[n - i - 1]);
if (((reinterpret_cast< ADDRINT >(ea) + n - i - 1) & 0x3) == 0 && i < n - 1) out << "_";
}
out << std::setfill(' ');
out.setf(ios::showbase);
if (n > 512) delete[] x;
}

VOID EmitWrite(THREADID threadid, UINT32 size)
{
if (!Emit(threadid)) return;

out << " Write ";

VOID* ea = WriteEa[threadid];

switch (size)
{
case 0:
out << "0 repeat count" << endl;
break;

case 1:
{
UINT8 x;
PIN_SafeCopy(&x, static_cast< UINT8* >(ea), 1);
out << "*(UINT8*)" << ea << " = " << static_cast< UINT32 >(x) << endl;
}
break;

case 2:
{
UINT16 x;
PIN_SafeCopy(&x, static_cast< UINT16* >(ea), 2);
out << "*(UINT16*)" << ea << " = " << x << endl;
}
break;

case 4:
{
UINT32 x;
PIN_SafeCopy(&x, static_cast< UINT32* >(ea), 4);
out << "*(UINT32*)" << ea << " = " << x << endl;
}
break;

case 8:
{
UINT64 x;
PIN_SafeCopy(&x, static_cast< UINT64* >(ea), 8);
out << "*(UINT64*)" << ea << " = " << x << endl;
}
break;

default:
out << "*(UINT" << dec << size * 8 << hex << ")" << ea << " = ";
ShowN(size, ea);
out << endl;
break;
}

Flush();
}

VOID EmitRead(THREADID threadid, VOID* ea, UINT32 size)
{
if (!Emit(threadid)) return;

out << " Read ";

switch (size)
{
case 0:
out << "0 repeat count" << endl;
break;

case 1:
{
UINT8 x;
PIN_SafeCopy(&x, static_cast< UINT8* >(ea), 1);
out << static_cast< UINT32 >(x) << " = *(UINT8*)" << ea << endl;
}
break;

case 2:
{
UINT16 x;
PIN_SafeCopy(&x, static_cast< UINT16* >(ea), 2);
out << x << " = *(UINT16*)" << ea << endl;
}
break;

case 4:
{
UINT32 x;
PIN_SafeCopy(&x, static_cast< UINT32* >(ea), 4);
out << x << " = *(UINT32*)" << ea << endl;
}
break;

case 8:
{
UINT64 x;
PIN_SafeCopy(&x, static_cast< UINT64* >(ea), 8);
out << x << " = *(UINT64*)" << ea << endl;
}
break;

default:
ShowN(size, ea);
out << " = *(UINT" << dec << size * 8 << hex << ")" << ea << endl;
break;
}

Flush();
}

static INT32 indent = 0;

VOID Indent()
{
for (INT32 i = 0; i < indent; i++)
{
out << "| ";
}
}

VOID EmitICount()
{
//out << setw(10) << dec << icount.Count() << hex << " ";
}

VOID EmitDirectCall(THREADID threadid, string* str, INT32 tailCall, ADDRINT arg0, ADDRINT arg1)
{
if (!Emit(threadid)) return;

EmitICount();

if (tailCall)
{
// A tail call is like an implicit return followed by an immediate call
indent--;
}

Indent();
out << *str << "(" << arg0 << ", " << arg1 << ", ...)" << endl;

indent++;

Flush();
}

string FormatAddress(ADDRINT address, RTN rtn)
{
string s;// = StringFromAddrint(address);
if (KnobSymbols && RTN_Valid(rtn))
{
//IMG img = SEC_Img(RTN_Sec(rtn));
//s += " ";
//if (IMG_Valid(img))
//{
// s += IMG_Name(img) + ":";
//}
//s += RTN_Name(rtn);
////////// C++filt //////////////
//string api = "c++filt ";
//api += RTN_Name(rtn);
//FILE *api_file = popen(api.c_str(), "r");
//char buff[1024];
//fgets(buff, sizeof(buff), api_file);
//pclose(api_file);
//buff[strlen(buff)-1] = '\0';
//s+=buff;
////////////////////////

INT32 line;
string file;
PIN_GetSourceLocation(RTN_Address(rtn), NULL, &line, &file);
if(!file.empty())
{
char buff[1024];
snprintf(buff, sizeof(buff), "%s", file.c_str());
char *p = basename(buff);
s += p;
s += ":";
}

s += RTN_Name(rtn);

ADDRINT delta = address - RTN_Address(rtn);
if (delta != 0)
{
s += "+" + hexstr(delta, 2);
//s += "+" + delta;
}
}

if (KnobLines)
{
INT32 line;
string file;

PIN_GetSourceLocation(address, NULL, &line, &file);
if (file != "")
{
s += " (" + file + ":" + decstr(line) + ")";
}
//cout << "address "<< hex << address << " file " << file << " line "<< dec << line << endl;
}
return s;
}

VOID EmitIndirectCall(THREADID threadid, string* str, ADDRINT target, ADDRINT arg0, ADDRINT arg1)
{
if (!Emit(threadid)) return;

EmitICount();
Indent();
out << *str;

PIN_LockClient();

string s = FormatAddress(target, RTN_FindByAddress(target));

PIN_UnlockClient();

out << s << "(" << arg0 << ", " << arg1 << ", ...)" << endl;
indent++;

Flush();
}

VOID EmitReturn(THREADID threadid, string* str, ADDRINT ret0)
{
if (!Emit(threadid)) return;

EmitICount();
indent--;
if (indent < 0)
{
out << "@@@ return underflow\n";
indent = 0;
}

Indent();
out << *str << " returns: " << ret0 << endl;

Flush();
}

VOID CallTrace(TRACE trace, INS ins)
{
if (!KnobTraceCalls) return;

if (INS_IsCall(ins) && !INS_IsDirectControlFlow(ins))
{
// Indirect call
string s = "Call " + FormatAddress(INS_Address(ins), TRACE_Rtn(trace));
s += " -> ";

INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(EmitIndirectCall), IARG_THREAD_ID, IARG_PTR, new string(s),
IARG_BRANCH_TARGET_ADDR, IARG_FUNCARG_CALLSITE_VALUE, 0, IARG_FUNCARG_CALLSITE_VALUE, 1, IARG_END);
}
else if (INS_IsDirectControlFlow(ins))
{
// Is this a tail call?
RTN sourceRtn = TRACE_Rtn(trace);
RTN destRtn = RTN_FindByAddress(INS_DirectControlFlowTargetAddress(ins));

if (INS_IsCall(ins) // conventional call
|| sourceRtn != destRtn // tail call
)
{
BOOL tailcall = !INS_IsCall(ins);

string s = "";
if (tailcall)
{
s += "Tailcall ";
}
else
{
if (INS_IsProcedureCall(ins))
s += "CALL ";
else
{
s += "PcMaterialization ";
tailcall = 1;
}
}

//s += INS_Mnemonic(ins) + " ";

s += FormatAddress(INS_Address(ins), TRACE_Rtn(trace));
s += " -> ";

ADDRINT target = INS_DirectControlFlowTargetAddress(ins);

s += FormatAddress(target, RTN_FindByAddress(target));

INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(EmitDirectCall), IARG_THREAD_ID, IARG_PTR, new string(s), IARG_BOOL,
tailcall, IARG_FUNCARG_CALLSITE_VALUE, 0, IARG_FUNCARG_CALLSITE_VALUE, 1, IARG_END);
}
}
else if (INS_IsRet(ins))
{
RTN rtn = TRACE_Rtn(trace);

#if defined(TARGET_LINUX) && defined(TARGET_IA32)
// if( RTN_Name(rtn) == "_dl_debug_state") return;
if (RTN_Valid(rtn) && RTN_Name(rtn) == "_dl_runtime_resolve") return;
#endif
string tracestring = "Return " + FormatAddress(INS_Address(ins), rtn);
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(EmitReturn), IARG_THREAD_ID, IARG_PTR, new string(tracestring),
IARG_FUNCRET_EXITPOINT_VALUE, IARG_END);
}
}

VOID InstructionTrace(TRACE trace, INS ins)
{
if (!KnobTraceInstructions) return;

ADDRINT addr = INS_Address(ins);
ASSERTX(addr);

// Format the string at instrumentation time
string traceString = "";
string astring = FormatAddress(INS_Address(ins), TRACE_Rtn(trace));
for (INT32 length = astring.length(); length < 30; length++)
{
traceString += " ";
}
traceString = astring + traceString;

traceString += " " + INS_Disassemble(ins);

for (INT32 length = traceString.length(); length < 80; length++)
{
traceString += " ";
}

INT32 regCount = 0;
REG regs[20];
REG xmm_dst = REG_INVALID();

for (UINT32 i = 0; i < INS_MaxNumWRegs(ins); i++)
{
REG x = REG_FullRegName(INS_RegW(ins, i));

if (REG_is_gr(x)
#if defined(TARGET_IA32)
|| x == REG_EFLAGS
#elif defined(TARGET_IA32E)
|| x == REG_RFLAGS
#endif
)
{
regs[regCount] = x;
regCount++;
}

if (REG_is_xmm(x)) xmm_dst = x;
}

if (INS_IsValidForIpointAfter(ins))
{
AddEmit(ins, IPOINT_AFTER, traceString, regCount, regs);
}
if (INS_IsValidForIpointTakenBranch(ins))
{
AddEmit(ins, IPOINT_TAKEN_BRANCH, traceString, regCount, regs);
}

if (xmm_dst != REG_INVALID())
{
if (INS_IsValidForIpointAfter(ins)) AddXMMEmit(ins, IPOINT_AFTER, xmm_dst);
if (INS_IsValidForIpointTakenBranch(ins)) AddXMMEmit(ins, IPOINT_TAKEN_BRANCH, xmm_dst);
}
}

VOID MemoryTrace(INS ins)
{
if (!KnobTraceMemory) return;

if (INS_IsMemoryWrite(ins) && INS_IsStandardMemop(ins))
{
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(CaptureWriteEa), IARG_THREAD_ID, IARG_MEMORYWRITE_EA, IARG_END);

if (INS_IsValidForIpointAfter(ins))
{
INS_InsertPredicatedCall(ins, IPOINT_AFTER, AFUNPTR(EmitWrite), IARG_THREAD_ID, IARG_MEMORYWRITE_SIZE, IARG_END);
}
if (INS_IsValidForIpointTakenBranch(ins))
{
INS_InsertPredicatedCall(ins, IPOINT_TAKEN_BRANCH, AFUNPTR(EmitWrite), IARG_THREAD_ID, IARG_MEMORYWRITE_SIZE,
IARG_END);
}
}

if (INS_HasMemoryRead2(ins) && INS_IsStandardMemop(ins))
{
INS_InsertPredicatedCall(ins, IPOINT_BEFORE, AFUNPTR(EmitRead), IARG_THREAD_ID, IARG_MEMORYREAD2_EA, IARG_MEMORYREAD_SIZE,
IARG_END);
}

if (INS_IsMemoryRead(ins) && !INS_IsPrefetch(ins) && INS_IsStandardMemop(ins))
{
INS_InsertPredicatedCall(ins, IPOINT_BEFORE, AFUNPTR(EmitRead), IARG_THREAD_ID, IARG_MEMORYREAD_EA, IARG_MEMORYREAD_SIZE,
IARG_END);
}
}

/* ===================================================================== */

VOID Trace(TRACE trace, VOID* v)
{
if (!filter.SelectTrace(trace)) return;

if (enabled)
{
for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
{
for (INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins))
{
InstructionTrace(trace, ins);

CallTrace(trace, ins);

MemoryTrace(ins);
}
}
}
}

/* ===================================================================== */

VOID Fini(int, VOID* v)
{
out << "# $eof" << endl;

out.close();
}

/* ===================================================================== */

static void OnSig(THREADID threadIndex, CONTEXT_CHANGE_REASON reason, const CONTEXT* ctxtFrom, CONTEXT* ctxtTo, INT32 sig,
VOID* v)
{
if (ctxtFrom != 0)
{
ADDRINT address = PIN_GetContextReg(ctxtFrom, REG_INST_PTR);
out << "SIG signal=" << sig << " on thread " << threadIndex << " at address " << hex << address << dec << " ";
}

switch (reason)
{
case CONTEXT_CHANGE_REASON_FATALSIGNAL:
out << "FATALSIG" << sig;
break;
case CONTEXT_CHANGE_REASON_SIGNAL:
out << "SIGNAL " << sig;
break;
case CONTEXT_CHANGE_REASON_SIGRETURN:
out << "SIGRET";
break;

case CONTEXT_CHANGE_REASON_APC:
out << "APC";
break;

case CONTEXT_CHANGE_REASON_EXCEPTION:
out << "EXCEPTION";
break;

case CONTEXT_CHANGE_REASON_CALLBACK:
out << "CALLBACK";
break;

default:
break;
}
out << std::endl;
}

/* ===================================================================== */

static CONTROL_MANAGER control;
static SKIPPER skipper;

/* ===================================================================== */

int main(int argc, CHAR* argv[])
{
PIN_InitSymbols();

if (PIN_Init(argc, argv))
{
return Usage();
}

string filename = KnobOutputFile.Value();

if (KnobPid)
{
filename += "." + decstr(getpid());
}

// Do this before we activate controllers
out.open(filename.c_str());
out << hex << right;
out.setf(ios::showbase);

control.RegisterHandler(Handler, 0, FALSE);
control.Activate();
skipper.CheckKnobs(0);

TRACE_AddInstrumentFunction(Trace, 0);
PIN_AddContextChangeFunction(OnSig, 0);

PIN_AddFiniFunction(Fini, 0);

filter.Activate();
icount.Activate();

// Never returns

PIN_StartProgram();

return 0;
}

/* ===================================================================== */
/* eof */
/* ===================================================================== */

test_main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
void printdisk(char x,char y) /*定义打印函数*/
{
printf("%c----->%c\n",x,y);
}
void hanoi(int n,char a,char b,char c) /*定义递归函数hanoi()完成移动*/
{
if(n==1) /*如果A针上的盘子数只剩下最后一个,移到C针上*/
printdisk(a,c);
else /*如果A针上的盘子数多于一个,执行以下语句*/
{
hanoi(n-1,a,c,b); /*将A针上的n-1个盘子借助C针先移到B针上*/
printdisk(a,c); /*将A针上剩下的一个盘子移到C针上,即打印移动方式*/
hanoi(n-1,b,a,c); /*将n-1个盘从B针借助A针移到C针上*/
}
}
int main()
{
int n = 4;
hanoi(n,'A','B','C'); /*调用hanoi()函数*/
}

编译运行

1
2
gcc -g -O0 han.c && ./a.out
pin -t ~/debugtrace.so -- ./a.out

追踪效果