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
|
/*
* Copyright (C) 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MacroAssemblerPrinter_h
#define MacroAssemblerPrinter_h
#if ENABLE(MASM_PROBE)
#include "MacroAssembler.h"
namespace JSC {
// What is MacroAssembler::print()?
// ===============================
// The MacroAsssembler::print() makes it easy to add print logging
// from JIT compiled code, and can be used to print all types of values
// at runtime e.g. CPU register values being operated on by the compiled
// code.
//
// print() is built on top of MacroAsssembler::probe(), and hence
// inserting logging in JIT compiled code will not perturb register values.
// The only register value that is perturbed is the PC (program counter)
// since there is now more compiled code to do the printing.
//
// How to use the MacroAssembler print()?
// =====================================
// 1. #include "MacroAssemblerPrinter.h" in the JIT file where you want to use print().
//
// 2. Add print() calls like these in your JIT code:
//
// jit.print("Hello world\n"); // Emits code to print the string.
//
// CodeBlock* cb = ...;
// jit.print(cb, "\n"); // Emits code to print the pointer value.
//
// RegisterID regID = ...;
// jit.print(regID, "\n"); // Emits code to print the register value (not the id).
//
// // Emits code to print all registers. Unlike other items, this prints
// // multiple lines as follows:
// // cpu {
// // eax: 0x123456789
// // ebx: 0x000000abc
// // ...
// // }
// jit.print(AllRegisters());
//
// jit.print(MemWord<uint8_t>(regID), "\n"); // Emits code to print a byte pointed to by the register.
// jit.print(MemWord<uint32_t>(regID), "\n"); // Emits code to print a 32-bit word pointed to by the register.
//
// jit.print(MemWord<uint8_t>(Address(regID, 23), "\n"); // Emits code to print a byte at the address.
// jit.print(MemWord<intptr_t>(AbsoluteAddress(&cb), "\n"); // Emits code to print an intptr_t sized word at the address.
//
// jit.print(Memory(reg, 100), "\n"); // Emits code to print a 100 bytes at the address pointed by the register.
// jit.print(Memory(Address(reg, 4), 100), "\n"); // Emits code to print a 100 bytes at the address.
//
// // Print multiple things at once. This incurs the probe overhead only once
// // to print all the items.
// jit.print("cb:", cb, " regID:", regID, " cpu:\n", AllRegisters());
//
// The type of values that can be printed is encapsulated in the PrintArg struct below.
//
// Note: print() does not automatically insert a '\n' at the end of the line.
// If you want a '\n', you'll have to add it explicitly (as in the examples above).
// This is a marker type only used with MacroAssemblerPrinter::print().
// See MacroAssemblerPrinter::print() below for details.
struct AllRegisters { };
struct PCRegister { };
struct Memory {
using Address = MacroAssembler::Address;
using AbsoluteAddress = MacroAssembler::AbsoluteAddress;
using RegisterID = MacroAssembler::RegisterID;
enum class AddressType {
Address,
AbsoluteAddress,
};
enum DumpStyle {
SingleWordDump,
GenericDump,
};
Memory(RegisterID& reg, size_t bytes, DumpStyle style = GenericDump)
: addressType(AddressType::Address)
, dumpStyle(style)
, numBytes(bytes)
{
u.address = Address(reg, 0);
}
Memory(const Address& address, size_t bytes, DumpStyle style = GenericDump)
: addressType(AddressType::Address)
, dumpStyle(style)
, numBytes(bytes)
{
u.address = address;
}
Memory(const AbsoluteAddress& address, size_t bytes, DumpStyle style = GenericDump)
: addressType(AddressType::AbsoluteAddress)
, dumpStyle(style)
, numBytes(bytes)
{
u.absoluteAddress = address;
}
AddressType addressType;
DumpStyle dumpStyle;
size_t numBytes;
union UnionedAddress {
UnionedAddress() { }
Address address;
AbsoluteAddress absoluteAddress;
} u;
};
template <typename IntType>
struct MemWord : public Memory {
MemWord(RegisterID& reg)
: Memory(reg, sizeof(IntType), Memory::SingleWordDump)
{ }
MemWord(const Address& address)
: Memory(address, sizeof(IntType), Memory::SingleWordDump)
{ }
MemWord(const AbsoluteAddress& address)
: Memory(address, sizeof(IntType), Memory::SingleWordDump)
{ }
};
class MacroAssemblerPrinter {
using CPUState = MacroAssembler::CPUState;
using ProbeContext = MacroAssembler::ProbeContext;
using RegisterID = MacroAssembler::RegisterID;
using FPRegisterID = MacroAssembler::FPRegisterID;
public:
template<typename... Arguments>
static void print(MacroAssembler* masm, Arguments... args)
{
auto argsList = std::make_unique<PrintArgsList>();
appendPrintArg(argsList.get(), args...);
masm->probe(printCallback, argsList.release(), 0);
}
private:
struct PrintArg {
enum class Type {
AllRegisters,
PCRegister,
RegisterID,
FPRegisterID,
Memory,
ConstCharPtr,
ConstVoidPtr,
IntptrValue,
UintptrValue,
};
PrintArg(AllRegisters&)
: type(Type::AllRegisters)
{
}
PrintArg(PCRegister&)
: type(Type::PCRegister)
{
}
PrintArg(RegisterID regID)
: type(Type::RegisterID)
{
u.gpRegisterID = regID;
}
PrintArg(FPRegisterID regID)
: type(Type::FPRegisterID)
{
u.fpRegisterID = regID;
}
PrintArg(const Memory& memory)
: type(Type::Memory)
{
u.memory = memory;
}
PrintArg(const char* ptr)
: type(Type::ConstCharPtr)
{
u.constCharPtr = ptr;
}
PrintArg(const void* ptr)
: type(Type::ConstVoidPtr)
{
u.constVoidPtr = ptr;
}
PrintArg(int value)
: type(Type::IntptrValue)
{
u.intptrValue = value;
}
PrintArg(unsigned value)
: type(Type::UintptrValue)
{
u.intptrValue = value;
}
PrintArg(intptr_t value)
: type(Type::IntptrValue)
{
u.intptrValue = value;
}
PrintArg(uintptr_t value)
: type(Type::UintptrValue)
{
u.uintptrValue = value;
}
Type type;
union Value {
Value() { }
RegisterID gpRegisterID;
FPRegisterID fpRegisterID;
Memory memory;
const char* constCharPtr;
const void* constVoidPtr;
intptr_t intptrValue;
uintptr_t uintptrValue;
} u;
};
typedef Vector<PrintArg> PrintArgsList;
template<typename FirstArg, typename... Arguments>
static void appendPrintArg(PrintArgsList* argsList, FirstArg& firstArg, Arguments... otherArgs)
{
argsList->append(PrintArg(firstArg));
appendPrintArg(argsList, otherArgs...);
}
static void appendPrintArg(PrintArgsList*) { }
private:
static void printCallback(ProbeContext*);
};
template<typename... Arguments>
void MacroAssembler::print(Arguments... args)
{
MacroAssemblerPrinter::print(this, args...);
}
// These printers will print a block of information. That block may be
// indented with the specified indentation.
void printCPU(MacroAssembler::CPUState&, int indentation = 0);
void printCPURegisters(MacroAssembler::CPUState&, int indentation = 0);
// These printers will print the specified information in line in the
// print stream. Hence, no indentation will be applied.
void printRegister(MacroAssembler::CPUState&, MacroAssembler::RegisterID);
void printRegister(MacroAssembler::CPUState&, MacroAssembler::FPRegisterID);
void printMemory(MacroAssembler::CPUState&, const Memory&);
} // namespace JSC
#endif // ENABLE(MASM_PROBE)
#endif // MacroAssemblerPrinter_h
|