Skip to content

Commit 7ed185a

Browse files
authored
[lldb] Expose language plugin commands based based on language of current frame (#136766)
Use the current frame's language to lookup commands provided by language plugins. This means commands like `language {objc,cplusplus} <command>` can be used directly, without using the `language <lang>` prefix. For example, when stopped on a C++ frame, `demangle _Z1fv` will run `language cplusplus demangle _Z1fv`. rdar://149882520
1 parent 31f47dd commit 7ed185a

File tree

8 files changed

+148
-1
lines changed

8 files changed

+148
-1
lines changed

lldb/include/lldb/Interpreter/CommandInterpreter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,12 @@ class CommandInterpreter : public Broadcaster,
730730
bool EchoCommandNonInteractive(llvm::StringRef line,
731731
const Flags &io_handler_flags) const;
732732

733+
/// Return the language specific command object for the current frame.
734+
///
735+
/// For example, when stopped on a C++ frame, this returns the command object
736+
/// for "language cplusplus" (`CommandObjectMultiwordItaniumABI`).
737+
lldb::CommandObjectSP GetFrameLanguageCommand() const;
738+
733739
// A very simple state machine which models the command handling transitions
734740
enum class CommandHandlingState {
735741
eIdle,

lldb/source/Commands/CommandObjectLanguage.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ CommandObjectLanguage::CommandObjectLanguage(CommandInterpreter &interpreter)
2121
"language <language-name> <subcommand> [<subcommand-options>]") {
2222
// Let the LanguageRuntime populates this command with subcommands
2323
LanguageRuntime::InitializeCommands(this);
24+
SetHelpLong(
25+
R"(
26+
Language specific subcommands may be used directly (without the `language
27+
<language-name>` prefix), when stopped on a frame written in that language. For
28+
example, from a C++ frame, users may run `demangle` directly, instead of
29+
`language cplusplus demangle`.
30+
31+
Language specific subcommands are only available when the command name cannot be
32+
misinterpreted. Take the `demangle` command for example, if a Python command
33+
named `demangle-tree` were loaded, then the invocation `demangle` would run
34+
`demangle-tree`, not `language cplusplus demangle`.
35+
)");
2436
}
2537

2638
CommandObjectLanguage::~CommandObjectLanguage() = default;

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,28 @@ CommandInterpreter::VerifyUserMultiwordCmdPath(Args &path, bool leaf_is_command,
10181018
return cur_as_multi;
10191019
}
10201020

1021+
CommandObjectSP CommandInterpreter::GetFrameLanguageCommand() const {
1022+
auto frame_sp = GetExecutionContext().GetFrameSP();
1023+
if (!frame_sp)
1024+
return {};
1025+
auto frame_language =
1026+
Language::GetPrimaryLanguage(frame_sp->GuessLanguage().AsLanguageType());
1027+
1028+
auto it = m_command_dict.find("language");
1029+
if (it == m_command_dict.end())
1030+
return {};
1031+
// The root "language" command.
1032+
CommandObjectSP language_cmd_sp = it->second;
1033+
1034+
auto *plugin = Language::FindPlugin(frame_language);
1035+
if (!plugin)
1036+
return {};
1037+
// "cplusplus", "objc", etc.
1038+
auto lang_name = plugin->GetPluginName();
1039+
1040+
return language_cmd_sp->GetSubcommandSPExact(lang_name);
1041+
}
1042+
10211043
CommandObjectSP
10221044
CommandInterpreter::GetCommandSP(llvm::StringRef cmd_str, bool include_aliases,
10231045
bool exact, StringList *matches,
@@ -1136,7 +1158,34 @@ CommandInterpreter::GetCommandSP(llvm::StringRef cmd_str, bool include_aliases,
11361158
else
11371159
return user_match_sp;
11381160
}
1139-
} else if (matches && command_sp) {
1161+
}
1162+
1163+
// When no single match is found, attempt to resolve the command as a language
1164+
// plugin subcommand.
1165+
if (!command_sp) {
1166+
// The `language` subcommand ("language objc", "language cplusplus", etc).
1167+
CommandObjectMultiword *lang_subcmd = nullptr;
1168+
if (auto lang_subcmd_sp = GetFrameLanguageCommand()) {
1169+
lang_subcmd = lang_subcmd_sp->GetAsMultiwordCommand();
1170+
command_sp = lang_subcmd_sp->GetSubcommandSPExact(cmd_str);
1171+
}
1172+
1173+
if (!command_sp && !exact && lang_subcmd) {
1174+
StringList lang_matches;
1175+
AddNamesMatchingPartialString(lang_subcmd->GetSubcommandDictionary(),
1176+
cmd_str, lang_matches, descriptions);
1177+
if (matches)
1178+
matches->AppendList(lang_matches);
1179+
if (lang_matches.GetSize() == 1) {
1180+
const auto &lang_dict = lang_subcmd->GetSubcommandDictionary();
1181+
auto pos = lang_dict.find(lang_matches[0]);
1182+
if (pos != lang_dict.end())
1183+
return pos->second;
1184+
}
1185+
}
1186+
}
1187+
1188+
if (matches && command_sp) {
11401189
matches->AppendString(cmd_str);
11411190
if (descriptions)
11421191
descriptions->AppendString(command_sp->GetHelp());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
OBJCXX_SOURCES := main.mm
2+
CXX_SOURCES := lib.cpp
3+
LD_EXTRAS := -lobjc
4+
include Makefile.rules
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test.decorators import *
4+
import lldbsuite.test.lldbutil as lldbutil
5+
6+
7+
class TestCase(TestBase):
8+
def test(self):
9+
self.build()
10+
_, _, thread, _ = lldbutil.run_to_source_breakpoint(
11+
self, "break here", lldb.SBFileSpec("lib.cpp")
12+
)
13+
14+
frame = thread.selected_frame
15+
self.assertEqual(frame.GuessLanguage(), lldb.eLanguageTypeC_plus_plus_11)
16+
self.assertEqual(frame.name, "f()")
17+
18+
# Test `help`.
19+
self.expect(
20+
"help demangle",
21+
substrs=[
22+
"Demangle a C++ mangled name.",
23+
"Syntax: language cplusplus demangle [<mangled-name> ...]",
24+
],
25+
)
26+
27+
# Run a `language cplusplus` command.
28+
self.expect("demangle _Z1fv", startstr="_Z1fv ---> f()")
29+
# Test prefix matching.
30+
self.expect("dem _Z1fv", startstr="_Z1fv ---> f()")
31+
32+
# Select the objc caller.
33+
self.runCmd("up")
34+
frame = thread.selected_frame
35+
self.assertEqual(frame.GuessLanguage(), lldb.eLanguageTypeObjC_plus_plus)
36+
self.assertEqual(frame.name, "main")
37+
38+
# Ensure `demangle` doesn't resolve from the objc frame.
39+
self.expect("help demangle", error=True)
40+
41+
# Run a `language objc` command.
42+
self.expect(
43+
"tagged-pointer",
44+
substrs=[
45+
"Commands for operating on Objective-C tagged pointers.",
46+
"Syntax: tagged-pointer <subcommand> [<subcommand-options>]",
47+
"The following subcommands are supported:",
48+
"info -- Dump information on a tagged pointer.",
49+
],
50+
)
51+
52+
# To ensure compatability with existing scripts, a language specific
53+
# command must not be invoked if another command (such as a python
54+
# command) has the language specific command name as its prefix.
55+
#
56+
# For example, this test loads a `tagged-pointer-collision` command. A
57+
# script could exist that invokes this command using its prefix
58+
# `tagged-pointer`, under the assumption that "tagged-pointer" uniquely
59+
# identifies the python command `tagged-pointer-collision`.
60+
self.runCmd("command script import commands.py")
61+
self.expect("tagged-pointer", startstr="ran tagged-pointer-collision")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import lldb
2+
3+
4+
@lldb.command("tagged-pointer-collision")
5+
def noop(dbg, cmdstr, ctx, result, _):
6+
print("ran tagged-pointer-collision", file=result)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <stdio.h>
2+
extern void f();
3+
void f() { puts("break here"); }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern void f();
2+
3+
int main() {
4+
f();
5+
return 0;
6+
}

0 commit comments

Comments
 (0)