|
| 1 | +/* |
| 2 | +Copyright 2020-2024 Vector 35 Inc. |
| 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 | + |
| 17 | +#include "renderlayer.h" |
| 18 | +#include "debuggerapi.h" |
| 19 | + |
| 20 | +using namespace BinaryNinja; |
| 21 | +using namespace BinaryNinjaDebuggerAPI; |
| 22 | + |
| 23 | +DebuggerRenderLayer::DebuggerRenderLayer(): RenderLayer("Debugger") |
| 24 | +{ |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +void DebuggerRenderLayer::ApplyToBlock(Ref<BasicBlock> block, std::vector<DisassemblyTextLine>& lines) |
| 30 | +{ |
| 31 | + Ref<BinaryView> bv = block->GetFunction()->GetView(); |
| 32 | + DbgRef<DebuggerController> controller = DebuggerController::GetController(bv); |
| 33 | + if (!controller) |
| 34 | + return; |
| 35 | + |
| 36 | + uint64_t ipAddr = controller->IP(); |
| 37 | + bool paused = controller->GetTargetStatus() == DebugAdapterPausedStatus; |
| 38 | + |
| 39 | + for (auto& line : lines) |
| 40 | + { |
| 41 | + // Do not draw the tags on an empty line, e.g., those separating the basic blocks in the linear view |
| 42 | + if (line.tokens.empty() || (line.tokens[0].type == CommentToken)) |
| 43 | + continue; |
| 44 | + |
| 45 | + bool hasPC = line.addr == ipAddr && paused; |
| 46 | + bool hasBreakpoint = controller->ContainsBreakpoint(line.addr); |
| 47 | + |
| 48 | + if (hasPC && hasBreakpoint) |
| 49 | + { |
| 50 | + InstructionTextToken indicator(BNInstructionTextTokenType::TagToken, "🛑➞"); |
| 51 | + line.tokens.insert(line.tokens.begin(), indicator); |
| 52 | + |
| 53 | + line.highlight.style = StandardHighlightColor; |
| 54 | + line.highlight.color = MagentaHighlightColor; |
| 55 | + line.highlight.mixColor = NoHighlightColor; |
| 56 | + line.highlight.mix = 0; |
| 57 | + line.highlight.r = 0; |
| 58 | + line.highlight.g = 0; |
| 59 | + line.highlight.b = 0; |
| 60 | + line.highlight.alpha = 255; |
| 61 | + } |
| 62 | + else if (hasPC) |
| 63 | + { |
| 64 | + InstructionTextToken indicator(BNInstructionTextTokenType::TagToken, "➞"); |
| 65 | + line.tokens.insert(line.tokens.begin(), indicator); |
| 66 | + |
| 67 | + line.highlight.style = StandardHighlightColor; |
| 68 | + line.highlight.color = BlueHighlightColor; |
| 69 | + line.highlight.mixColor = NoHighlightColor; |
| 70 | + line.highlight.mix = 0; |
| 71 | + line.highlight.r = 0; |
| 72 | + line.highlight.g = 0; |
| 73 | + line.highlight.b = 0; |
| 74 | + line.highlight.alpha = 255; |
| 75 | + } |
| 76 | + else if (hasBreakpoint) |
| 77 | + { |
| 78 | + InstructionTextToken indicator(BNInstructionTextTokenType::TagToken, "🛑"); |
| 79 | + line.tokens.insert(line.tokens.begin(), indicator); |
| 80 | + |
| 81 | + line.highlight.style = StandardHighlightColor; |
| 82 | + line.highlight.color = RedHighlightColor; |
| 83 | + line.highlight.mixColor = NoHighlightColor; |
| 84 | + line.highlight.mix = 0; |
| 85 | + line.highlight.r = 0; |
| 86 | + line.highlight.g = 0; |
| 87 | + line.highlight.b = 0; |
| 88 | + line.highlight.alpha = 255; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +void DebuggerRenderLayer::ApplyToHighLevelILBody(Ref<Function> function, std::vector<LinearDisassemblyLine>& lines) |
| 95 | +{ |
| 96 | + Ref<BinaryView> bv = function->GetView(); |
| 97 | + DbgRef<DebuggerController> controller = DebuggerController::GetController(bv); |
| 98 | + if (!controller) |
| 99 | + return; |
| 100 | + |
| 101 | + uint64_t ipAddr = controller->IP(); |
| 102 | + |
| 103 | + for (auto& linearLine : lines) |
| 104 | + { |
| 105 | + DisassemblyTextLine& line = linearLine.contents; |
| 106 | + bool hasPC = line.addr == ipAddr; |
| 107 | + bool hasBreakpoint = controller->ContainsBreakpoint(line.addr); |
| 108 | + |
| 109 | + if (hasPC && hasBreakpoint) |
| 110 | + { |
| 111 | + InstructionTextToken indicator(BNInstructionTextTokenType::TagToken, "🛑➞"); |
| 112 | + line.tokens.insert(line.tokens.begin(), indicator); |
| 113 | + |
| 114 | + line.highlight.style = StandardHighlightColor; |
| 115 | + line.highlight.color = MagentaHighlightColor; |
| 116 | + line.highlight.mixColor = NoHighlightColor; |
| 117 | + line.highlight.mix = 0; |
| 118 | + line.highlight.r = 0; |
| 119 | + line.highlight.g = 0; |
| 120 | + line.highlight.b = 0; |
| 121 | + line.highlight.alpha = 255; |
| 122 | + } |
| 123 | + else if (hasPC) |
| 124 | + { |
| 125 | + InstructionTextToken indicator(BNInstructionTextTokenType::TagToken, "➞"); |
| 126 | + line.tokens.insert(line.tokens.begin(), indicator); |
| 127 | + |
| 128 | + line.highlight.style = StandardHighlightColor; |
| 129 | + line.highlight.color = BlueHighlightColor; |
| 130 | + line.highlight.mixColor = NoHighlightColor; |
| 131 | + line.highlight.mix = 0; |
| 132 | + line.highlight.r = 0; |
| 133 | + line.highlight.g = 0; |
| 134 | + line.highlight.b = 0; |
| 135 | + line.highlight.alpha = 255; |
| 136 | + } |
| 137 | + else if (hasBreakpoint) |
| 138 | + { |
| 139 | + InstructionTextToken indicator(BNInstructionTextTokenType::TagToken, "🛑"); |
| 140 | + line.tokens.insert(line.tokens.begin(), indicator); |
| 141 | + |
| 142 | + line.highlight.style = StandardHighlightColor; |
| 143 | + line.highlight.color = RedHighlightColor; |
| 144 | + line.highlight.mixColor = NoHighlightColor; |
| 145 | + line.highlight.mix = 0; |
| 146 | + line.highlight.r = 0; |
| 147 | + line.highlight.g = 0; |
| 148 | + line.highlight.b = 0; |
| 149 | + line.highlight.alpha = 255; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +void RegisterRenderLayers() |
| 156 | +{ |
| 157 | + static DebuggerRenderLayer* g_debuggerRenderLayer = new DebuggerRenderLayer(); |
| 158 | + RenderLayer::Register(g_debuggerRenderLayer, BNRenderLayerDefaultEnableState::AlwaysEnabledRenderLayerDefaultEnableState); |
| 159 | +} |
0 commit comments