blob: 86793ccb190172c45176a213377e71b51668290e [file] [log] [blame]
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers02ed4c02013-09-06 13:10:04 -070017#ifndef ART_DISASSEMBLER_DISASSEMBLER_H_
18#define ART_DISASSEMBLER_DISASSEMBLER_H_
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080019
Elliott Hughes0f3c5532012-03-30 14:51:51 -070020#include <stdint.h>
21
22#include <iosfwd>
23
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/macros.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080026
27namespace art {
28
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070029class DisassemblerOptions {
30 public:
Andreas Gampe372f3a32016-08-19 10:49:06 -070031 using ThreadOffsetNameFunction = void (*)(std::ostream& os, uint32_t offset);
32
33 ThreadOffsetNameFunction thread_offset_name_function_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070034
Aart Bikd3059e72016-05-11 10:30:47 -070035 // Base address for calculating relative code offsets when absolute_addresses_ is false.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070036 const uint8_t* const base_address_;
37
Aart Bikd3059e72016-05-11 10:30:47 -070038 // End address (exclusive);
39 const uint8_t* const end_address_;
40
Andreas Gampe372f3a32016-08-19 10:49:06 -070041 // Should the disassembler print absolute or relative addresses.
42 const bool absolute_addresses_;
43
Alexandre Ramesa37d9252014-10-27 11:28:14 +000044 // If set, the disassembler is allowed to look at load targets in literal
45 // pools.
46 const bool can_read_literals_;
47
Aart Bikd3059e72016-05-11 10:30:47 -070048 DisassemblerOptions(bool absolute_addresses,
49 const uint8_t* base_address,
50 const uint8_t* end_address,
Andreas Gampe372f3a32016-08-19 10:49:06 -070051 bool can_read_literals,
52 ThreadOffsetNameFunction fn)
53 : thread_offset_name_function_(fn),
Aart Bikd3059e72016-05-11 10:30:47 -070054 base_address_(base_address),
55 end_address_(end_address),
Andreas Gampe372f3a32016-08-19 10:49:06 -070056 absolute_addresses_(absolute_addresses),
Alexandre Ramesa37d9252014-10-27 11:28:14 +000057 can_read_literals_(can_read_literals) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070058
59 private:
60 DISALLOW_COPY_AND_ASSIGN(DisassemblerOptions);
61};
62
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080063class Disassembler {
64 public:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070065 // Creates a Disassembler for the given InstructionSet with the
66 // non-null DisassemblerOptions which become owned by the
67 // Disassembler.
68 static Disassembler* Create(InstructionSet instruction_set, DisassemblerOptions* options);
69
70 virtual ~Disassembler() {
71 delete disassembler_options_;
72 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080073
Ian Rogersb23a7722012-10-09 16:54:26 -070074 // Dump a single instruction returning the length of that instruction.
75 virtual size_t Dump(std::ostream& os, const uint8_t* begin) = 0;
76 // Dump instructions within a range.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080077 virtual void Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) = 0;
Elliott Hughes105afd22012-04-10 15:04:25 -070078
Alexandre Rameseb7b7392015-06-19 14:47:01 +010079 const DisassemblerOptions* GetDisassemblerOptions() const {
80 return disassembler_options_;
81 }
82
Elliott Hughes105afd22012-04-10 15:04:25 -070083 protected:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070084 explicit Disassembler(DisassemblerOptions* disassembler_options)
85 : disassembler_options_(disassembler_options) {
86 CHECK(disassembler_options_ != nullptr);
87 }
88
89 std::string FormatInstructionPointer(const uint8_t* begin);
Elliott Hughes105afd22012-04-10 15:04:25 -070090
91 private:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070092 DisassemblerOptions* disassembler_options_;
Elliott Hughes105afd22012-04-10 15:04:25 -070093 DISALLOW_COPY_AND_ASSIGN(Disassembler);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080094};
95
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010096static inline bool HasBitSet(uint32_t value, uint32_t bit) {
97 return (value & (1 << bit)) != 0;
98}
99
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100100extern "C"
101Disassembler* create_disassembler(InstructionSet instruction_set, DisassemblerOptions* options);
102
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800103} // namespace art
104
Ian Rogers02ed4c02013-09-06 13:10:04 -0700105#endif // ART_DISASSEMBLER_DISASSEMBLER_H_