mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | 3663bcd | 2017-04-10 19:38:44 +0000 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 Apple Inc. All rights reserved. |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 3 | * |
| 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 | |
| 33 | namespace JSC { |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 34 | namespace Printer { |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 35 | |
| 36 | using CPUState = MacroAssembler::CPUState; |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 37 | using RegisterID = MacroAssembler::RegisterID; |
| 38 | using FPRegisterID = MacroAssembler::FPRegisterID; |
| 39 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 40 | void printAllRegisters(PrintStream& out, Context& context) |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 41 | { |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 42 | auto& cpu = context.probeContext.cpu; |
| 43 | unsigned charsToIndent = context.data.as<unsigned>(); |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 44 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 45 | auto indent = [&] () { |
| 46 | for (unsigned i = 0; i < charsToIndent; ++i) |
| 47 | out.print(" "); |
| 48 | }; |
| 49 | #define INDENT indent() |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 50 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 51 | INDENT, out.print("cpu: {\n"); |
| 52 | |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 53 | #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.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 61 | INDENT, out.printf(" %6s: " INTPTR_HEX_VALUE_FORMAT " %ld\n", #_regName, value, value) ; \ |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 62 | } |
| 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.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 71 | INDENT, out.printf(" %6s: 0x%016llx %.13g\n", #_regName, *u, *d); \ |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 72 | } |
| 73 | FOR_EACH_CPU_FPREGISTER(PRINT_FPREGISTER) |
| 74 | #undef PRINT_FPREGISTER |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 75 | |
| 76 | INDENT, out.print("}\n"); |
| 77 | #undef INDENT |
| 78 | |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 79 | } |
| 80 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 81 | void printPCRegister(PrintStream& out, Context& context) |
mark.lam@apple.com | cae93e0 | 2015-10-28 17:48:10 +0000 | [diff] [blame] | 82 | { |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 83 | auto cpu = context.probeContext.cpu; |
| 84 | void* value; |
mark.lam@apple.com | cae93e0 | 2015-10-28 17:48:10 +0000 | [diff] [blame] | 85 | #if CPU(X86) || CPU(X86_64) |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 86 | value = cpu.eip; |
mark.lam@apple.com | cae93e0 | 2015-10-28 17:48:10 +0000 | [diff] [blame] | 87 | #elif CPU(ARM_TRADITIONAL) || CPU(ARM_THUMB2) || CPU(ARM64) |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 88 | value = cpu.pc; |
mark.lam@apple.com | cae93e0 | 2015-10-28 17:48:10 +0000 | [diff] [blame] | 89 | #else |
| 90 | #error "Unsupported CPU" |
| 91 | #endif |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 92 | out.printf("pc:<%p %ld>", value, bitwise_cast<intptr_t>(value)); |
mark.lam@apple.com | cae93e0 | 2015-10-28 17:48:10 +0000 | [diff] [blame] | 93 | } |
| 94 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 95 | void printRegisterID(PrintStream& out, Context& context) |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 96 | { |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 97 | RegisterID regID = context.data.as<RegisterID>(); |
mark.lam@apple.com | c882c03 | 2015-10-15 18:37:38 +0000 | [diff] [blame] | 98 | const char* name = CPUState::gprName(regID); |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 99 | void* value = context.probeContext.gpr(regID); |
| 100 | out.printf("%s:<%p %ld>", name, value, bitwise_cast<intptr_t>(value)); |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 101 | } |
| 102 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 103 | void printFPRegisterID(PrintStream& out, Context& context) |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 104 | { |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 105 | FPRegisterID regID = context.data.as<FPRegisterID>(); |
mark.lam@apple.com | c882c03 | 2015-10-15 18:37:38 +0000 | [diff] [blame] | 106 | const char* name = CPUState::fprName(regID); |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 107 | double value = context.probeContext.fpr(regID); |
| 108 | out.printf("%s:<0x%016llx %.13g>", name, bitwise_cast<uint64_t>(value), value); |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 109 | } |
| 110 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 111 | void printAddress(PrintStream& out, Context& context) |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 112 | { |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 113 | 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 | |
| 120 | void printMemory(PrintStream& out, Context& context) |
| 121 | { |
| 122 | const Memory& memory = context.data.as<Memory>(); |
| 123 | |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 124 | uint8_t* ptr = nullptr; |
| 125 | switch (memory.addressType) { |
| 126 | case Memory::AddressType::Address: { |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 127 | ptr = reinterpret_cast<uint8_t*>(context.probeContext.gpr(memory.u.address.base)); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 128 | 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.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 140 | out.printf("%p:<0x%02x %d>", p, *p, *p); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | if (memory.numBytes == sizeof(int16_t)) { |
| 144 | auto p = reinterpret_cast<int16_t*>(ptr); |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 145 | out.printf("%p:<0x%04x %d>", p, *p, *p); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | if (memory.numBytes == sizeof(int32_t)) { |
| 149 | auto p = reinterpret_cast<int32_t*>(ptr); |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 150 | out.printf("%p:<0x%08x %d>", p, *p, *p); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | if (memory.numBytes == sizeof(int64_t)) { |
| 154 | auto p = reinterpret_cast<int64_t*>(ptr); |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 155 | out.printf("%p:<0x%016llx %lld>", p, *p, *p); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 156 | 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.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 165 | out.printf("%p: ", &ptr[i]); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 166 | else if (!(i % 4)) |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 167 | out.printf(" "); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 168 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 169 | out.printf("%02x", ptr[i]); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 170 | |
| 171 | if (i % 16 == 15) |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 172 | out.print("\n"); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 173 | } |
| 174 | if (numBytes % 16 < 15) |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 175 | out.print("\n"); |
mark.lam@apple.com | b3817cc | 2015-08-29 00:10:07 +0000 | [diff] [blame] | 176 | } |
| 177 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 178 | void printCallback(ProbeContext* probeContext) |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 179 | { |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 180 | 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.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
mark.lam@apple.com | a787fc7 | 2017-04-21 21:42:24 +0000 | [diff] [blame] | 189 | } // namespace Printer |
mark.lam@apple.com | a33834a | 2015-08-28 22:58:48 +0000 | [diff] [blame] | 190 | } // namespace JSC |
| 191 | |
| 192 | #endif // ENABLE(MASM_PROBE) |