Skip to content

Commit 354eb88

Browse files
authored
[lldb] Also show session history in fzf_history (#128986)
lldb's history log file is written to at the end of a debugging session. As a result, the log does not contain commands run during the current session. This extends the `fzf_history` to include the output of `session history`.
1 parent 4c9f6a7 commit 354eb88

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

lldb/examples/python/fzf_history.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def fzf_history(debugger, cmdstr, ctx, result, _):
1414
if not os.path.exists(history_file):
1515
result.SetError("history file does not exist")
1616
return
17-
history = _load_history(history_file)
17+
history = _load_history(debugger, history_file)
1818

1919
if sys.platform != "darwin":
2020
# The ability to integrate fzf's result into lldb uses copy and paste.
@@ -79,16 +79,49 @@ def _handle_command(debugger, command):
7979
debugger.HandleCommand(command)
8080

8181

82-
def _load_history(history_file):
83-
"""Load, decode, parse, and prepare an lldb history file for fzf."""
82+
# `session history` example formatting:
83+
# 1: first command
84+
# 2: penultimate command
85+
# 3: latest command
86+
_HISTORY_PREFIX = re.compile(r"^\s+\d+:\s+")
87+
88+
89+
def _load_session_history(debugger):
90+
"""Load and parse lldb session history."""
91+
result = lldb.SBCommandReturnObject()
92+
interp = debugger.GetCommandInterpreter()
93+
interp.HandleCommand("session history", result)
94+
history = result.GetOutput()
95+
commands = []
96+
for line in history.splitlines():
97+
# Strip the prefix.
98+
command = _HISTORY_PREFIX.sub("", line)
99+
commands.append(command)
100+
return commands
101+
102+
103+
def _load_persisted_history(history_file):
104+
"""Load and decode lldb persisted history."""
84105
with open(history_file) as f:
85106
history_contents = f.read()
86107

108+
# Some characters (ex spaces and newlines) are encoded as octal values, but
109+
# as _characters_ (not bytes). Space is the string r"\\040".
87110
history_decoded = re.sub(r"\\0([0-7][0-7])", _decode_char, history_contents)
88111
history_lines = history_decoded.splitlines()
89112

90113
# Skip the header line (_HiStOrY_V2_)
91114
del history_lines[0]
115+
return history_lines
116+
117+
118+
def _load_history(debugger, history_file):
119+
"""Load, decode, parse, and prepare lldb history for fzf."""
120+
# Persisted history is older (earlier).
121+
history_lines = _load_persisted_history(history_file)
122+
# Session history is newer (later).
123+
history_lines.extend(_load_session_history(debugger))
124+
92125
# Reverse to show latest first.
93126
history_lines.reverse()
94127

0 commit comments

Comments
 (0)