Skip to content

[lldb] Also show session history in fzf_history #128986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kastiglione
Copy link
Contributor

@kastiglione kastiglione commented Feb 27, 2025

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.

@llvmbot
Copy link
Member

llvmbot commented Feb 27, 2025

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/128986.diff

1 Files Affected:

  • (modified) lldb/examples/python/fzf_history.py (+36-3)
diff --git a/lldb/examples/python/fzf_history.py b/lldb/examples/python/fzf_history.py
index 546edb2ed2712..d70789c8c0259 100644
--- a/lldb/examples/python/fzf_history.py
+++ b/lldb/examples/python/fzf_history.py
@@ -14,7 +14,7 @@ def fzf_history(debugger, cmdstr, ctx, result, _):
     if not os.path.exists(history_file):
         result.SetError("history file does not exist")
         return
-    history = _load_history(history_file)
+    history = _load_history(debugger, history_file)
 
     if sys.platform != "darwin":
         # The ability to integrate fzf's result into lldb uses copy and paste.
@@ -79,16 +79,49 @@ def _handle_command(debugger, command):
         debugger.HandleCommand(command)
 
 
-def _load_history(history_file):
-    """Load, decode, parse, and prepare an lldb history file for fzf."""
+# `session history` example formatting:
+#    1: first command
+#    2: penultimate command
+#    3: latest command
+_HISTORY_PREFIX = re.compile(r"^\s+\d+:\s+")
+
+
+def _load_session_history(debugger):
+    """Load and parse lldb session history."""
+    result = lldb.SBCommandReturnObject()
+    interp = debugger.GetCommandInterpreter()
+    interp.HandleCommand("session history", result)
+    history = result.GetOutput()
+    commands = []
+    for line in history.splitlines():
+        # Strip the prefix.
+        command = _HISTORY_PREFIX.sub("", line)
+        commands.append(command)
+    return commands
+
+
+def _load_persisted_history(history_file):
+    """Load and decode lldb persisted history."""
     with open(history_file) as f:
         history_contents = f.read()
 
+    # Some characters (ex spaces and newlines) are encoded as octal values, but
+    # as _characters_ (not bytes). Space is the string r"\\040".
     history_decoded = re.sub(r"\\0([0-7][0-7])", _decode_char, history_contents)
     history_lines = history_decoded.splitlines()
 
     # Skip the header line (_HiStOrY_V2_)
     del history_lines[0]
+    return history_lines
+
+
+def _load_history(debugger, history_file):
+    """Load, decode, parse, and prepare lldb history for fzf."""
+    # Persisted history is older (earlier).
+    history_lines = _load_persisted_history(history_file)
+    # Session history is newer (later).
+    history_lines.extend(_load_session_history(debugger))
+
     # Reverse to show latest first.
     history_lines.reverse()
 

@kastiglione kastiglione merged commit 354eb88 into llvm:main Feb 27, 2025
12 checks passed
@kastiglione kastiglione deleted the lldb-Also-show-session-history-in-fzf_history branch February 27, 2025 03:15
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Mar 3, 2025
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants