-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCallTracerPlugin.cpp
More file actions
136 lines (104 loc) · 3.16 KB
/
Copy pathCallTracerPlugin.cpp
File metadata and controls
136 lines (104 loc) · 3.16 KB
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
/*
Copyright 2014 F-Secure
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ASString.h"
#include "ASMethodInfo.h"
#include "CallTracerPlugin.h"
#include "Logger.h"
namespace WINDOWS
{
#include <windows.h>
}
// Commandline options for call tracer plugin
extern KNOB<bool> KnobCallTracerEarlyTracing;
extern KNOB<string> KnobCallTracerTraceFile;
CallTracerPlugin::CallTracerPlugin() :
m_traceLoggingEnabled(KnobCallTracerEarlyTracing.Value()),
m_callDepth(0)
{
m_traceFile.open(KnobCallTracerTraceFile.Value().c_str());
}
CallTracerPlugin::~CallTracerPlugin()
{
m_traceFile.close();
}
bool CallTracerPlugin::beforeMethodCall(ASMethodInfo* mi, string methodName, UINT32 argc, UINT32* argv)
{
// Save this so we can check the return value type
m_mi = mi;
m_traceFile << format("\n%s%s ()\n", string(m_callDepth, '\t').c_str(), methodName.c_str());
// Is call trace logging (already) enabled?
if (m_traceLoggingEnabled)
{
m_traceFile << format("%sthis: 0x%x\n", string(m_callDepth+1, '\t').c_str(), argv[0]);
// Get parameter types
const vector<string>* parameterTypes = mi->getParameterTypes();
string paramType;
// Print the actual arguments
for (UINT32 i=0; i < argc; i++)
{
if (i >= parameterTypes->size())
{
paramType = "null";
}
else
{
paramType = parameterTypes->at(i);
}
if (paramType == "String")
{
ASString* s = ASString::create((UINT32*)argv[i+1]);
if (s != NULL)
{
m_traceFile << format("%sarg %d: \"%s\"\n", string(m_callDepth+1, '\t').c_str(), i, s->getString().c_str());
continue;
}
}
m_traceFile << format("%sarg %d: 0x%x : %s\n", string(m_callDepth+1, '\t').c_str(), i, argv[i+1], paramType.c_str());
}
}
else if (!m_traceLoggingEnabled && methodName == "flash.display::Sprite/constructChildren")
{
// We can start logging from the next method call
m_traceLoggingEnabled = true;
}
m_callDepth++;
return false;
}
void CallTracerPlugin::afterMethodCall(UINT32 retVal)
{
m_callDepth--;
string type = m_mi->getReturnValueType();
if (type == "String")
{
ASString* s = ASString::create((UINT32*)retVal);
if (s != NULL)
{
m_traceFile << format("%sReturned: \"%s\" (call depth: %d)\n", string(m_callDepth, '\t').c_str(), s->getString().c_str(), m_callDepth);
return;
}
}
m_traceFile << format("%sReturned: 0x%x : %s (call depth: %d)\n", string(m_callDepth, '\t').c_str(), retVal, type.c_str(), m_callDepth);
}
string CallTracerPlugin::format(char* fmt, ...)
{
va_list args;
va_start(args, fmt);
int len = vsnprintf(NULL, 0, fmt, args) + 1;
char* buf = (char*)malloc(len);
vsnprintf(buf, len-1, fmt, args);
buf[len-1] = 0;
string s(buf);
free(buf);
va_end(args);
return s;
}