Skip to content

Commit 94e4dcb

Browse files
authored
[lldb] Expose language plugin commands based based on language of current frame (llvm#136766) (#10755)
* [lldb] Expose language plugin commands based based on language of current frame (llvm#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 (cherry picked from commit 7ed185a) * [lldb] Fix typo in tagged-pointer syntax string (NFC) (llvm#137069) (cherry-picked from commit cef9ed5) * [lldb] Run TestFrameLanguageCommands.py only on Darwin (llvm#141866) Tests using ObjC do not readily run on Linux. (cherry-picked from commit 1d48e55) * [lldb] Fix TestFrameLanguageCommands.py build (llvm#141829) The use of `-lobjc` resulted in this test failing to build on Linux. The flag is not necessary, this removes the flag with the expectation it will fix the test on Linux. (cherry-picked from commit e653dc9)
1 parent 0d47fa0 commit 94e4dcb

File tree

9 files changed

+149
-2
lines changed

9 files changed

+149
-2
lines changed

lldb/include/lldb/Interpreter/CommandInterpreter.h

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

735+
/// Return the language specific command object for the current frame.
736+
///
737+
/// For example, when stopped on a C++ frame, this returns the command object
738+
/// for "language cplusplus" (`CommandObjectMultiwordItaniumABI`).
739+
lldb::CommandObjectSP GetFrameLanguageCommand() const;
740+
735741
// A very simple state machine which models the command handling transitions
736742
enum class CommandHandlingState {
737743
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
@@ -1026,6 +1026,28 @@ CommandObjectMultiword *CommandInterpreter::VerifyUserMultiwordCmdPath(
10261026
return cur_as_multi;
10271027
}
10281028

1029+
CommandObjectSP CommandInterpreter::GetFrameLanguageCommand() const {
1030+
auto frame_sp = GetExecutionContext().GetFrameSP();
1031+
if (!frame_sp)
1032+
return {};
1033+
auto frame_language =
1034+
Language::GetPrimaryLanguage(frame_sp->GuessLanguage().AsLanguageType());
1035+
1036+
auto it = m_command_dict.find("language");
1037+
if (it == m_command_dict.end())
1038+
return {};
1039+
// The root "language" command.
1040+
CommandObjectSP language_cmd_sp = it->second;
1041+
1042+
auto *plugin = Language::FindPlugin(frame_language);
1043+
if (!plugin)
1044+
return {};
1045+
// "cplusplus", "objc", etc.
1046+
auto lang_name = plugin->GetPluginName();
1047+
1048+
return language_cmd_sp->GetSubcommandSPExact(lang_name);
1049+
}
1050+
10291051
CommandObjectSP
10301052
CommandInterpreter::GetCommandSP(llvm::StringRef cmd_str, bool include_aliases,
10311053
bool exact, StringList *matches,
@@ -1144,7 +1166,34 @@ CommandInterpreter::GetCommandSP(llvm::StringRef cmd_str, bool include_aliases,
11441166
else
11451167
return user_match_sp;
11461168
}
1147-
} else if (matches && command_sp) {
1169+
}
1170+
1171+
// When no single match is found, attempt to resolve the command as a language
1172+
// plugin subcommand.
1173+
if (!command_sp) {
1174+
// The `language` subcommand ("language objc", "language cplusplus", etc).
1175+
CommandObjectMultiword *lang_subcmd = nullptr;
1176+
if (auto lang_subcmd_sp = GetFrameLanguageCommand()) {
1177+
lang_subcmd = lang_subcmd_sp->GetAsMultiwordCommand();
1178+
command_sp = lang_subcmd_sp->GetSubcommandSPExact(cmd_str);
1179+
}
1180+
1181+
if (!command_sp && !exact && lang_subcmd) {
1182+
StringList lang_matches;
1183+
AddNamesMatchingPartialString(lang_subcmd->GetSubcommandDictionary(),
1184+
cmd_str, lang_matches, descriptions);
1185+
if (matches)
1186+
matches->AppendList(lang_matches);
1187+
if (lang_matches.GetSize() == 1) {
1188+
const auto &lang_dict = lang_subcmd->GetSubcommandDictionary();
1189+
auto pos = lang_dict.find(lang_matches[0]);
1190+
if (pos != lang_dict.end())
1191+
return pos->second;
1192+
}
1193+
}
1194+
}
1195+
1196+
if (matches && command_sp) {
11481197
matches->AppendString(cmd_str);
11491198
if (descriptions)
11501199
descriptions->AppendString(command_sp->GetHelp());

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ class CommandObjectMultiwordObjC_TaggedPointer : public CommandObjectMultiword {
11251125
: CommandObjectMultiword(
11261126
interpreter, "tagged-pointer",
11271127
"Commands for operating on Objective-C tagged pointers.",
1128-
"class-table <subcommand> [<subcommand-options>]") {
1128+
"tagged-pointer <subcommand> [<subcommand-options>]") {
11291129
LoadSubCommand(
11301130
"info",
11311131
CommandObjectSP(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
OBJCXX_SOURCES := main.mm
2+
CXX_SOURCES := lib.cpp
3+
include Makefile.rules
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
@skipUnlessDarwin
9+
def test(self):
10+
self.build()
11+
_, _, thread, _ = lldbutil.run_to_source_breakpoint(
12+
self, "break here", lldb.SBFileSpec("lib.cpp")
13+
)
14+
15+
frame = thread.selected_frame
16+
self.assertEqual(frame.GuessLanguage(), lldb.eLanguageTypeC_plus_plus_11)
17+
self.assertEqual(frame.name, "f()")
18+
19+
# Test `help`.
20+
self.expect(
21+
"help demangle",
22+
substrs=[
23+
"Demangle a C++ mangled name.",
24+
"Syntax: language cplusplus demangle [<mangled-name> ...]",
25+
],
26+
)
27+
28+
# Run a `language cplusplus` command.
29+
self.expect("demangle _Z1fv", startstr="_Z1fv ---> f()")
30+
# Test prefix matching.
31+
self.expect("dem _Z1fv", startstr="_Z1fv ---> f()")
32+
33+
# Select the objc caller.
34+
self.runCmd("up")
35+
frame = thread.selected_frame
36+
self.assertEqual(frame.GuessLanguage(), lldb.eLanguageTypeObjC_plus_plus)
37+
self.assertEqual(frame.name, "main")
38+
39+
# Ensure `demangle` doesn't resolve from the objc frame.
40+
self.expect("help demangle", error=True)
41+
42+
# Run a `language objc` command.
43+
self.expect(
44+
"tagged-pointer",
45+
substrs=[
46+
"Commands for operating on Objective-C tagged pointers.",
47+
"Syntax: tagged-pointer <subcommand> [<subcommand-options>]",
48+
"The following subcommands are supported:",
49+
"info -- Dump information on a tagged pointer.",
50+
],
51+
)
52+
53+
# To ensure compatability with existing scripts, a language specific
54+
# command must not be invoked if another command (such as a python
55+
# command) has the language specific command name as its prefix.
56+
#
57+
# For example, this test loads a `tagged-pointer-collision` command. A
58+
# script could exist that invokes this command using its prefix
59+
# `tagged-pointer`, under the assumption that "tagged-pointer" uniquely
60+
# identifies the python command `tagged-pointer-collision`.
61+
self.runCmd("command script import commands.py")
62+
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)