this repo has no description
at trunk 110 lines 3.8 kB view raw
1/* Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) */ 2// Copyright (c) 2013, the Dart project authors and Facebook, Inc. and its 3// affiliates. Please see the AUTHORS-Dart file for details. All rights 4// reserved. Use of this source code is governed by a BSD-style license that 5// can be found in the LICENSE-Dart file. 6 7#pragma once 8 9#include "globals.h" 10#include "handles-decl.h" 11 12namespace py { 13 14// Forward declaration. 15class CodeComments; 16 17// Disassembly formatter interface, which consumes the 18// disassembled instructions in any desired form. 19class DisassemblyFormatter { 20 public: 21 DisassemblyFormatter() {} 22 virtual ~DisassemblyFormatter() {} 23 24 // Consume the decoded instruction at the given pc. 25 virtual void consumeInstruction(char* hex_buffer, intptr_t hex_size, 26 char* human_buffer, intptr_t human_size, 27 uword pc) = 0; 28 29 // print a formatted message. 30 virtual void print(const char* format, ...) = 0; 31}; 32 33// Basic disassembly formatter that outputs the disassembled instruction 34// to stdout. 35class DisassembleToStdout : public DisassemblyFormatter { 36 public: 37 DisassembleToStdout() : DisassemblyFormatter() {} 38 ~DisassembleToStdout() {} 39 40 virtual void consumeInstruction(char* hex_buffer, intptr_t hex_size, 41 char* human_buffer, intptr_t human_size, 42 uword pc); 43 44 virtual void print(const char* format, ...); 45 46 private: 47 DISALLOW_HEAP_ALLOCATION(); 48 DISALLOW_COPY_AND_ASSIGN(DisassembleToStdout); 49}; 50 51// Basic disassembly formatter that outputs the disassembled instruction 52// to a memory buffer. This is only intended for test writing. 53class DisassembleToMemory : public DisassemblyFormatter { 54 public: 55 DisassembleToMemory(char* buffer, uintptr_t length) 56 : DisassemblyFormatter(), 57 buffer_(buffer), 58 remaining_(length), 59 overflowed_(false) {} 60 ~DisassembleToMemory() {} 61 62 virtual void consumeInstruction(char* hex_buffer, intptr_t hex_size, 63 char* human_buffer, intptr_t human_size, 64 uword pc); 65 66 virtual void print(const char* format, ...); 67 68 private: 69 char* buffer_; 70 int remaining_; 71 bool overflowed_; 72 DISALLOW_HEAP_ALLOCATION(); 73 DISALLOW_COPY_AND_ASSIGN(DisassembleToMemory); 74}; 75 76// Disassemble instructions. 77class Disassembler { 78 public: 79 // disassemble instructions between start and end. 80 // (The assumption is that start is at a valid instruction). 81 // Return true if all instructions were successfully decoded, false otherwise. 82 static void disassemble(uword start, uword end, 83 DisassemblyFormatter* formatter, 84 const CodeComments* comments = nullptr); 85 86 static void disassemble(uword start, uword end) { 87 DisassembleToStdout stdout_formatter; 88 disassemble(start, end, &stdout_formatter); 89 } 90 91 static void disassemble(uword start, uword end, char* buffer, 92 uintptr_t buffer_size) { 93 DisassembleToMemory memory_formatter(buffer, buffer_size); 94 disassemble(start, end, &memory_formatter); 95 } 96 97 // Decodes one instruction. 98 // Writes a hexadecimal representation into the hex_buffer and a 99 // human-readable representation into the human_buffer. 100 // Writes the length of the decoded instruction in bytes in out_instr_len. 101 static void decodeInstruction(char* hex_buffer, intptr_t hex_size, 102 char* human_buffer, intptr_t human_size, 103 int* out_instr_len, uword pc); 104 105 private: 106 static const int kHexadecimalBufferSize = 32; 107 static const int kUserReadableBufferSize = 256; 108}; 109 110} // namespace py