blob: c1cfc256b4fadf364c6689a3ef593701357a113b [file] [log] [blame]
mark.lam@apple.coma33834a2015-08-28 22:58:48 +00001/*
mark.lam@apple.com3663bcd2017-04-10 19:38:44 +00002 * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
mark.lam@apple.coma33834a2015-08-28 22:58:48 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "MacroAssemblerPrinter.h"
28
29#if ENABLE(MASM_PROBE)
30
31#include "MacroAssembler.h"
32
33namespace JSC {
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000034namespace Printer {
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000035
36using CPUState = MacroAssembler::CPUState;
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000037using RegisterID = MacroAssembler::RegisterID;
38using FPRegisterID = MacroAssembler::FPRegisterID;
39
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000040void printAllRegisters(PrintStream& out, Context& context)
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000041{
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000042 auto& cpu = context.probeContext.cpu;
43 unsigned charsToIndent = context.data.as<unsigned>();
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000044
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000045 auto indent = [&] () {
46 for (unsigned i = 0; i < charsToIndent; ++i)
47 out.print(" ");
48 };
49#define INDENT indent()
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000050
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000051 INDENT, out.print("cpu: {\n");
52
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000053#if USE(JSVALUE32_64)
54 #define INTPTR_HEX_VALUE_FORMAT "0x%08lx"
55#else
56 #define INTPTR_HEX_VALUE_FORMAT "0x%016lx"
57#endif
58
59 #define PRINT_GPREGISTER(_type, _regName) { \
60 intptr_t value = reinterpret_cast<intptr_t>(cpu._regName); \
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000061 INDENT, out.printf(" %6s: " INTPTR_HEX_VALUE_FORMAT " %ld\n", #_regName, value, value) ; \
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000062 }
63 FOR_EACH_CPU_GPREGISTER(PRINT_GPREGISTER)
64 FOR_EACH_CPU_SPECIAL_REGISTER(PRINT_GPREGISTER)
65 #undef PRINT_GPREGISTER
66 #undef INTPTR_HEX_VALUE_FORMAT
67
68 #define PRINT_FPREGISTER(_type, _regName) { \
69 uint64_t* u = reinterpret_cast<uint64_t*>(&cpu._regName); \
70 double* d = reinterpret_cast<double*>(&cpu._regName); \
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000071 INDENT, out.printf(" %6s: 0x%016llx %.13g\n", #_regName, *u, *d); \
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000072 }
73 FOR_EACH_CPU_FPREGISTER(PRINT_FPREGISTER)
74 #undef PRINT_FPREGISTER
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000075
76 INDENT, out.print("}\n");
77#undef INDENT
78
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000079}
80
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000081void printPCRegister(PrintStream& out, Context& context)
mark.lam@apple.comcae93e02015-10-28 17:48:10 +000082{
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000083 auto cpu = context.probeContext.cpu;
84 void* value;
mark.lam@apple.comcae93e02015-10-28 17:48:10 +000085#if CPU(X86) || CPU(X86_64)
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000086 value = cpu.eip;
mark.lam@apple.comcae93e02015-10-28 17:48:10 +000087#elif CPU(ARM_TRADITIONAL) || CPU(ARM_THUMB2) || CPU(ARM64)
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000088 value = cpu.pc;
mark.lam@apple.comcae93e02015-10-28 17:48:10 +000089#else
90#error "Unsupported CPU"
91#endif
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000092 out.printf("pc:<%p %ld>", value, bitwise_cast<intptr_t>(value));
mark.lam@apple.comcae93e02015-10-28 17:48:10 +000093}
94
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000095void printRegisterID(PrintStream& out, Context& context)
mark.lam@apple.coma33834a2015-08-28 22:58:48 +000096{
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000097 RegisterID regID = context.data.as<RegisterID>();
mark.lam@apple.comc882c032015-10-15 18:37:38 +000098 const char* name = CPUState::gprName(regID);
mark.lam@apple.coma787fc72017-04-21 21:42:24 +000099 void* value = context.probeContext.gpr(regID);
100 out.printf("%s:<%p %ld>", name, value, bitwise_cast<intptr_t>(value));
mark.lam@apple.coma33834a2015-08-28 22:58:48 +0000101}
102
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000103void printFPRegisterID(PrintStream& out, Context& context)
mark.lam@apple.coma33834a2015-08-28 22:58:48 +0000104{
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000105 FPRegisterID regID = context.data.as<FPRegisterID>();
mark.lam@apple.comc882c032015-10-15 18:37:38 +0000106 const char* name = CPUState::fprName(regID);
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000107 double value = context.probeContext.fpr(regID);
108 out.printf("%s:<0x%016llx %.13g>", name, bitwise_cast<uint64_t>(value), value);
mark.lam@apple.coma33834a2015-08-28 22:58:48 +0000109}
110
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000111void printAddress(PrintStream& out, Context& context)
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000112{
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000113 MacroAssembler::Address address = context.data.as<MacroAssembler::Address>();
114 RegisterID regID = address.base;
115 const char* name = CPUState::gprName(regID);
116 void* value = context.probeContext.gpr(regID);
117 out.printf("Address{base:%s:<%p %ld>, offset:<0x%x %d>", name, value, bitwise_cast<intptr_t>(value), address.offset, address.offset);
118}
119
120void printMemory(PrintStream& out, Context& context)
121{
122 const Memory& memory = context.data.as<Memory>();
123
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000124 uint8_t* ptr = nullptr;
125 switch (memory.addressType) {
126 case Memory::AddressType::Address: {
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000127 ptr = reinterpret_cast<uint8_t*>(context.probeContext.gpr(memory.u.address.base));
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000128 ptr += memory.u.address.offset;
129 break;
130 }
131 case Memory::AddressType::AbsoluteAddress: {
132 ptr = reinterpret_cast<uint8_t*>(const_cast<void*>(memory.u.absoluteAddress.m_ptr));
133 break;
134 }
135 }
136
137 if (memory.dumpStyle == Memory::SingleWordDump) {
138 if (memory.numBytes == sizeof(int8_t)) {
139 auto p = reinterpret_cast<int8_t*>(ptr);
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000140 out.printf("%p:<0x%02x %d>", p, *p, *p);
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000141 return;
142 }
143 if (memory.numBytes == sizeof(int16_t)) {
144 auto p = reinterpret_cast<int16_t*>(ptr);
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000145 out.printf("%p:<0x%04x %d>", p, *p, *p);
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000146 return;
147 }
148 if (memory.numBytes == sizeof(int32_t)) {
149 auto p = reinterpret_cast<int32_t*>(ptr);
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000150 out.printf("%p:<0x%08x %d>", p, *p, *p);
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000151 return;
152 }
153 if (memory.numBytes == sizeof(int64_t)) {
154 auto p = reinterpret_cast<int64_t*>(ptr);
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000155 out.printf("%p:<0x%016llx %lld>", p, *p, *p);
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000156 return;
157 }
158 // Else, unknown word size. Fall thru and dump in the generic way.
159 }
160
161 // Generic dump: dump rows of 16 bytes in 4 byte groupings.
162 size_t numBytes = memory.numBytes;
163 for (size_t i = 0; i < numBytes; i++) {
164 if (!(i % 16))
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000165 out.printf("%p: ", &ptr[i]);
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000166 else if (!(i % 4))
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000167 out.printf(" ");
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000168
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000169 out.printf("%02x", ptr[i]);
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000170
171 if (i % 16 == 15)
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000172 out.print("\n");
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000173 }
174 if (numBytes % 16 < 15)
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000175 out.print("\n");
mark.lam@apple.comb3817cc2015-08-29 00:10:07 +0000176}
177
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000178void printCallback(ProbeContext* probeContext)
mark.lam@apple.coma33834a2015-08-28 22:58:48 +0000179{
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000180 auto& out = WTF::dataFile();
181 PrintRecordList& list = *reinterpret_cast<PrintRecordList*>(probeContext->arg);
182 for (size_t i = 0; i < list.size(); i++) {
183 auto& record = list[i];
184 Context context(*probeContext, record.data);
185 record.printer(out, context);
mark.lam@apple.coma33834a2015-08-28 22:58:48 +0000186 }
187}
188
mark.lam@apple.coma787fc72017-04-21 21:42:24 +0000189} // namespace Printer
mark.lam@apple.coma33834a2015-08-28 22:58:48 +0000190} // namespace JSC
191
192#endif // ENABLE(MASM_PROBE)