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 
27 DisassembleCommand::DisassembleCommand()
28 {
29 }
30 
31 DisassembleCommand::~DisassembleCommand()
32 {
33 }
34 
36  const HugeStaticString &input, HugeStaticString &output)
37 {
38  // TODO: add symbols.
39  output = "<address>";
40 }
41 
43  const HugeStaticString &input, HugeStaticString &output,
44  InterruptState &state, DebuggerIO *screen)
45 {
46  // This command can take either an address or a symbol name (or nothing).
47  uintptr_t address;
48 
49  // If we see just "disassemble", no parameters were matched.
50  if (input == "disassemble")
51  address = state.getInstructionPointer();
52  else
53  {
54  // Is it an address?
55  address = input.uintptrValue();
56 
57  if (address == 0)
58  {
59  // No, try a symbol name.
60  // TODO.
61  output = "Not a valid address or symbol name: `";
62  output += input;
63  output += "'.\n";
64  return true;
65  }
66  }
67 
68  // Dissassemble around address.
69  size_t nInstructions = 10;
70 
71  LargeStaticString text;
72  Disassembler disassembler;
73 #ifdef BITS_64
74  disassembler.setMode(64);
75 #endif
76  disassembler.setLocation(address);
77 
78  for (size_t i = 0; i < nInstructions; i++)
79  {
80  text.clear();
81  uintptr_t location = disassembler.getLocation();
82  disassembler.disassemble(text);
83 
84  // What symbol are we in?
85  // TODO grep the memory map for the right ELF to look at.
86  uintptr_t symStart = 0;
87  const char *pSym =
88  KernelElf::instance().globalLookupSymbol(location, &symStart);
89  if (location == symStart)
90  {
91 #ifdef BITS_32
92  output.append(location, 16, 8, '0');
93 #endif
94 #ifdef BITS_64
95  output.append(location, 16, 16, '0');
96 #endif
97  output += ": <";
99  demangle_full(LargeStaticString(pSym), sym);
100  output += sym;
101  output += ">:\n";
102  }
103 
104 #ifdef BITS_32
105  output.append(location, 16, 8, ' ');
106 #endif
107 #ifdef BITS_64
108  output.append(location, 16, 16, ' ');
109 #endif
110  output += ": ";
111  output += text;
112  output += '\n';
113  }
114 
115  return true;
116 }
117 
119 {
120  return NormalStaticString("disassemble");
121 }
static KernelElf & instance()
Definition: KernelElf.h:129
bool execute(const HugeStaticString &input, HugeStaticString &output, InterruptState &state, DebuggerIO *screen)
void autocomplete(const HugeStaticString &input, HugeStaticString &output)
uintptr_t globalLookupSymbol(const char *pName)
Definition: KernelElf.cc:1031
const NormalStaticString getString()
void setLocation(uintptr_t nLocation)
void disassemble(LargeStaticString &text)