The Pedigree Project 0.1
DisassembleCommand.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "pedigree/kernel/debugger/commands/DisassembleCommand.h"
21#include "pedigree/kernel/linker/KernelElf.h"
22#include "pedigree/kernel/processor/Disassembler.h"
23#include "pedigree/kernel/processor/state.h"
24#include "pedigree/kernel/processor/types.h"
25#include "pedigree/kernel/utilities/demangle.h"
26
27DisassembleCommand::DisassembleCommand() {}
28
29DisassembleCommand::~DisassembleCommand() {}
30
32 // TODO: add symbols.
33 output = "<address>";
34}
35
37 InterruptState& state, DebuggerIO* screen) {
38 // This command can take either an address or a symbol name (or nothing).
39 uintptr_t address;
40
41 // If we see just "disassemble", no parameters were matched.
42 if (input == "disassemble")
43 address = state.getInstructionPointer();
44 else {
45 // Is it an address?
46 address = input.uintptrValue();
47
48 if (address == 0) {
49 // No, try a symbol name.
50 // TODO.
51 output = "Not a valid address or symbol name: `";
52 output += input;
53 output += "'.\n";
54 return true;
55 }
56 }
57
58 // Dissassemble around address.
59 size_t nInstructions = 10;
60
62 Disassembler disassembler;
63#if BITS_64
64 disassembler.setMode(64);
65#endif
66 disassembler.setLocation(address);
67
68 for (size_t i = 0; i < nInstructions; i++) {
69 text.clear();
70 uintptr_t location = disassembler.getLocation();
71 disassembler.disassemble(text);
72
73 // What symbol are we in?
74 // TODO grep the memory map for the right ELF to look at.
75 uintptr_t symStart = 0;
76 const char* pSym = KernelElf::instance().globalLookupSymbol(location, &symStart);
77 if (location == symStart) {
78#if BITS_32
79 output.append(location, 16, 8, '0');
80#endif
81#if BITS_64
82 output.append(location, 16, 16, '0');
83#endif
84 output += ": <";
86 demangle_full(LargeStaticString(pSym), sym);
87 output += sym;
88 output += ">:\n";
89 }
90
91#if BITS_32
92 output.append(location, 16, 8, ' ');
93#endif
94#if BITS_64
95 output.append(location, 16, 16, ' ');
96#endif
97 output += ": ";
98 output += text;
99 output += '\n';
100 }
101
102 return true;
103}
104
const NormalStaticString getString()
bool execute(const HugeStaticString &input, HugeStaticString &output, InterruptState &state, DebuggerIO *screen)
void autocomplete(const HugeStaticString &input, HugeStaticString &output)
static KernelElf & instance()
Definition KernelElf.h:135
uintptr_t globalLookupSymbol(const char *pName)
void setLocation(uintptr_t location)
void disassemble(LargeStaticString &text)
void setMode(size_t mode)