Skip to content

Commit 9c341d4

Browse files
f0alexsam-f0
authored andcommitted
Improve code clarity in non-Go files and review comments for Go code
1 parent b70341a commit 9c341d4

20 files changed

+613
-489
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ Settings are stored in a file `.llef` located in your home directory formatted a
7373
| rebase_addresses | Boolean | Enable/disable address rebase output |
7474
| rebase_offset | Int | Set the rebase offset (default 0x100000) |
7575
| show_all_registers | Boolean | Enable/disable extended register output |
76-
| enable_darwin_heap_scan | Boolean | Enable/disable more accurate heap scanning for Darwin-based platforms. Uses the Darwin malloc introspection API, executing code in the address space of the target application using LLDB's evaluation engine. |
76+
| enable_darwin_heap_scan | Boolean | Enable/disable more accurate heap scanning for Darwin-based platforms. Uses the Darwin malloc introspection API, executing code in the address space of the target application using LLDB's evaluation engine |
7777
| max_trace_length | Int | Set the maximum length of the call stack backtrace to display |
7878
| stack_view_size | Int | Set the number of entries in the stack read to display |
7979
| max_disassembly_length | Int | Set the maximum number of instructions to disassemble and display around the current PC |
80-
| go_support_level | String | Control Golang-specific analysis. `disable` / `auto` (default) / `force`. Go support in Windows binaries requires `force`. |
81-
| go_confidence_threshold | String | Set the confidence threshold (`low` / `medium` / `high`) for Go objects to be shown in the context view. |
80+
| go_support_level | String | Control Golang-specific analysis. `disable` / `auto` (default) / `force`. For performance reasons, Go support in Windows binaries requires `force`. |
81+
| go_confidence_threshold | String | Set the confidence threshold (`low` / `medium` / `high`) for Go objects to be shown in the context view |
8282

8383
#### llefcolorsettings
8484
Allows setting LLEF GUI colors:
@@ -116,6 +116,7 @@ Supported colors: BLUE, GREEN, YELLOW, RED, PINK, CYAN, GREY
116116
| dereferenced_register_color |
117117
| frame_argument_name_color |
118118
| read_memory_address_color |
119+
| go_type_color |
119120

120121
#### Hexdump
121122
View memory contents with:

arch/x86_64.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ class X86_64(BaseArch):
1717
"rbx",
1818
"rcx",
1919
"rdx",
20-
"rsp",
21-
"rbp",
22-
"rsi",
2320
"rdi",
24-
"rip",
21+
"rsi",
2522
"r8",
2623
"r9",
2724
"r10",
@@ -30,6 +27,9 @@ class X86_64(BaseArch):
3027
"r13",
3128
"r14",
3229
"r15",
30+
"rsp",
31+
"rbp",
32+
"rip",
3333
]
3434

3535
gpr_key = "general purpose"

commands/golang.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
from commands.base_container import BaseContainer
1212
from common.constants import MSG_TYPE
1313
from common.context_handler import ContextHandler
14-
from common.golang.constants import GO_TUNE_DEFAULT_UNPACK_DEPTH
14+
from common.golang.analysis import go_get_backtrace
15+
from common.golang.constants import GO_DEFAULT_UNPACK_DEPTH
1516
from common.golang.data import GoDataBad
16-
from common.golang.improvements import go_improve_backtrace
1717
from common.golang.state import GoState
1818
from common.golang.static import setup_go
1919
from common.golang.type_getter import TypeGetter
2020
from common.golang.types import ExtractInfo
21-
from common.golang.util import go_calculate_bp, go_find_func, perform_go_functions
21+
from common.golang.util import go_calculate_base_pointer, go_context_analysis, go_find_func
2222
from common.output_util import output_line, print_message
2323
from common.settings import LLEFSettings
2424
from common.state import LLEFState
@@ -103,7 +103,7 @@ def __call__(
103103

104104
if self.settings.go_support_level == "disable":
105105
print_message(MSG_TYPE.ERROR, GO_DISABLED_MSG)
106-
elif not perform_go_functions(self.settings):
106+
elif not go_context_analysis(self.settings):
107107
print_message(MSG_TYPE.ERROR, "The binary does not appear to be a Go binary.")
108108
else:
109109

@@ -117,9 +117,9 @@ def __call__(
117117
try:
118118
# User has typed in a numeric address
119119
address = int(address_or_name, 0)
120-
record = go_find_func(address)
121-
if record is not None:
122-
(entry, gofunc) = record
120+
function_mapping = go_find_func(address)
121+
if function_mapping is not None:
122+
(entry, gofunc) = function_mapping
123123
output_line(f"{hex(entry)} - {gofunc.name} (file address = {hex(gofunc.file_addr)})")
124124
else:
125125
print_message(MSG_TYPE.ERROR, f"Could not find function containing address {hex(address)}")
@@ -129,9 +129,9 @@ def __call__(
129129
name = address_or_name
130130

131131
success = False
132-
for entry, f in LLEFState.go_state.pclntab_info.func_mapping:
133-
if f.name == name:
134-
output_line(f"{hex(entry)} - {name} (file address = {hex(f.file_addr)})")
132+
for entry, gofunc in LLEFState.go_state.pclntab_info.func_mapping:
133+
if name in gofunc.name:
134+
output_line(f"{hex(entry)} - {gofunc.name} (file address = {hex(gofunc.file_addr)})")
135135
success = True
136136
# Don't break: there are potentially multiple matches.
137137

@@ -164,8 +164,8 @@ def get_command_parser(cls) -> argparse.ArgumentParser:
164164
"-d",
165165
"--depth",
166166
type=positive_int,
167-
default=GO_TUNE_DEFAULT_UNPACK_DEPTH,
168-
help=f"Depth to unpack child types, default is {GO_TUNE_DEFAULT_UNPACK_DEPTH}",
167+
default=GO_DEFAULT_UNPACK_DEPTH,
168+
help=f"Depth to unpack child types, default is {GO_DEFAULT_UNPACK_DEPTH}",
169169
)
170170

171171
return parser
@@ -184,7 +184,7 @@ def get_long_help() -> str:
184184
+ "If not given an argument, prints a table of all known types."
185185
+ os.linesep
186186
+ "The depth argument specifies how deeply to follow and unpack child types. "
187-
+ f"It defaults to {GO_TUNE_DEFAULT_UNPACK_DEPTH}"
187+
+ f"It defaults to {GO_DEFAULT_UNPACK_DEPTH}"
188188
+ os.linesep
189189
+ GolangGetTypeCommand.get_command_parser().format_help()
190190
)
@@ -214,7 +214,7 @@ def __call__(
214214

215215
if self.settings.go_support_level == "disable":
216216
print_message(MSG_TYPE.ERROR, GO_DISABLED_MSG)
217-
elif not perform_go_functions(self.settings):
217+
elif not go_context_analysis(self.settings):
218218
print_message(MSG_TYPE.ERROR, "The binary does not appear to be a Go binary.")
219219
elif LLEFState.go_state.moduledata_info is None:
220220
print_message(MSG_TYPE.ERROR, "No type information available in this Go binary.")
@@ -257,6 +257,14 @@ def __call__(
257257
else:
258258
print_message(MSG_TYPE.ERROR, f"Could not parse type '{name}'")
259259

260+
found = False
261+
for ptr, type_struct in LLEFState.go_state.moduledata_info.type_structs.items():
262+
if name in type_struct.header.name:
263+
if found is False:
264+
print_message(MSG_TYPE.ERROR, "Did you mean:")
265+
found = True
266+
output_line(f"{hex(ptr)} - {type_struct.header.name}")
267+
260268

261269
class GolangUnpackTypeCommand(BaseCommand):
262270
"""Implements the 'unpack-type' subcommand"""
@@ -286,8 +294,8 @@ def get_command_parser(cls) -> argparse.ArgumentParser:
286294
"-d",
287295
"--depth",
288296
type=positive_int,
289-
default=GO_TUNE_DEFAULT_UNPACK_DEPTH,
290-
help=f"Depth to unpack child objects, default is {GO_TUNE_DEFAULT_UNPACK_DEPTH}",
297+
default=GO_DEFAULT_UNPACK_DEPTH,
298+
help=f"Depth to unpack child objects, default is {GO_DEFAULT_UNPACK_DEPTH}",
291299
)
292300
return parser
293301

@@ -303,7 +311,7 @@ def get_long_help() -> str:
303311
+ "The type can either be a string or a pointer to a type information structure."
304312
+ os.linesep
305313
+ "The depth argument specifies how deeply to follow and unpack child objects. "
306-
+ f"It defaults to {GO_TUNE_DEFAULT_UNPACK_DEPTH}"
314+
+ f"It defaults to {GO_DEFAULT_UNPACK_DEPTH}"
307315
+ os.linesep
308316
+ GolangUnpackTypeCommand.get_command_parser().format_help()
309317
)
@@ -334,7 +342,7 @@ def __call__(
334342

335343
if self.settings.go_support_level == "disable":
336344
print_message(MSG_TYPE.ERROR, GO_DISABLED_MSG)
337-
elif not perform_go_functions(self.settings):
345+
elif not go_context_analysis(self.settings):
338346
print_message(MSG_TYPE.ERROR, "The binary does not appear to be a Go binary.")
339347
elif LLEFState.go_state.moduledata_info is None:
340348
print_message(MSG_TYPE.ERROR, "No type information available in this Go binary.")
@@ -438,22 +446,25 @@ def __call__(
438446

439447
if self.settings.go_support_level == "disable":
440448
print_message(MSG_TYPE.ERROR, GO_DISABLED_MSG)
441-
elif not perform_go_functions(self.settings):
449+
elif not go_context_analysis(self.settings):
442450
print_message(MSG_TYPE.ERROR, "The binary does not appear to be a Go binary.")
443451
else:
444452
# At this point, we're good to go with running the command.
445453

446-
bt = go_improve_backtrace(
454+
backtrace = go_get_backtrace(
447455
exe_ctx.GetProcess(),
448456
exe_ctx.GetFrame(),
449457
self.context_handler.arch,
450458
self.context_handler.color_settings,
451459
depth,
452460
)
453-
if bt is not None:
454-
output_line(bt)
461+
if backtrace is not None:
462+
output_line(backtrace)
455463
else:
456-
print_message(MSG_TYPE.ERROR, "Go traceback failed. Try using LLDB's `bt` command.")
464+
print_message(
465+
MSG_TYPE.ERROR,
466+
"Go traceback failed (only supported on x86 and x86_64). Try using LLDB's `bt` command.",
467+
)
457468

458469

459470
class GolangReanalyseCommand(BaseCommand):
@@ -511,5 +522,5 @@ def __call__(
511522
else:
512523
LLEFState.go_state = GoState()
513524
setup_go(exe_ctx.GetProcess(), exe_ctx.GetTarget(), self.settings)
514-
go_calculate_bp.cache_clear()
525+
go_calculate_base_pointer.cache_clear()
515526
go_find_func.cache_clear()

common/color_settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def read_memory_address_color(self) -> str:
9898
def address_operand_color(self) -> str:
9999
return self._RAW_CONFIG.get(self.GLOBAL_SECTION, "address_operand_color", fallback="RED").upper()
100100

101+
@property
102+
def go_type_color(self) -> str:
103+
return self._RAW_CONFIG.get(self.GLOBAL_SECTION, "go_type_color", fallback="CYAN").upper()
104+
101105
def __init__(self) -> None:
102106
self.supported_colors = [color.name for color in TERM_COLORS]
103107
self.supported_colors.remove(TERM_COLORS.ENDC.name)

common/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
from enum import Enum, IntEnum
44

5+
# Pointers are stored using int, however, this can lead to confusion between an offset, an absolute address,
6+
# and just a plain old integer value. The llef_pointer alias is intended to improve readability.
7+
pointer = int
8+
59

610
class TERM_COLORS(Enum):
711
"""Used to colorify terminal output."""

0 commit comments

Comments
 (0)