2 Test defining commands using the lldb command definitions
7 from lldb
.plugins
.parsed_cmd
import ParsedCommand
10 class ReportingCmd(ParsedCommand
):
11 def __init__(self
, debugger
, unused
):
12 super().__init
__(debugger
, unused
)
14 def __call__(self
, debugger
, args_array
, exe_ctx
, result
):
15 opt_def
= self
.get_options_definition()
17 result
.AppendMessage("Options:\n")
18 for long_option
, elem
in opt_def
.items():
21 f
"{long_option} (set: {elem['_value_set']}): {object.__getattribute__(self.get_parser(), dest)}\n"
24 result
.AppendMessage("No options\n")
26 num_args
= args_array
.GetSize()
28 result
.AppendMessage(f
"{num_args} arguments:")
29 for idx
in range(0, num_args
):
31 f
"{idx}: {args_array.GetItemAtIndex(idx).GetStringValue(10000)}\n"
34 # Use these to make sure that get_repeat_command sends the right
40 class NoArgsCommand(ReportingCmd
):
43 def __init__(self
, debugger
, unused
):
44 super().__init
__(debugger
, unused
)
47 def register_lldb_command(cls
, debugger
, module_name
):
48 ParsedCommand
.do_register_cmd(cls
, debugger
, module_name
)
50 def setup_command_definition(self
):
51 ov_parser
= self
.get_parser()
55 "a boolean arg, defaults to True",
56 value_type
=lldb
.eArgTypeBoolean
,
65 "A shared library name.",
66 value_type
=lldb
.eArgTypeShlibName
,
75 "An on disk filename",
76 value_type
=lldb
.eArgTypeFilename
,
77 dest
="disk_file_name",
85 value_type
=lldb
.eArgTypeLineNum
,
94 "An enum, doesn't actually do anything",
96 ["foo", "does foo things"],
97 ["bar", "does bar things"],
98 ["baz", "does baz things"],
105 def get_repeat_command(self
, command
):
107 global no_args_repeat
108 no_args_repeat
= command
111 def get_short_help(self
):
112 return "Example command for use in debugging"
114 def get_long_help(self
):
115 return self
.help_string
118 class OneArgCommandNoOptions(ReportingCmd
):
119 program
= "one-arg-no-opt"
121 def __init__(self
, debugger
, unused
):
122 super().__init
__(debugger
, unused
)
125 def register_lldb_command(cls
, debugger
, module_name
):
126 ParsedCommand
.do_register_cmd(cls
, debugger
, module_name
)
128 def setup_command_definition(self
):
129 ov_parser
= self
.get_parser()
130 ov_parser
.add_argument_set(
131 [ov_parser
.make_argument_element(lldb
.eArgTypeSourceFile
, "plain")]
134 def get_repeat_command(self
, command
):
135 # Repeat the current command
136 global one_arg_repeat
137 one_arg_repeat
= command
140 def get_short_help(self
):
141 return "Example command for use in debugging"
143 def get_long_help(self
):
144 return self
.help_string
147 class TwoArgGroupsCommand(ReportingCmd
):
150 def __init__(self
, debugger
, unused
):
151 super().__init
__(debugger
, unused
)
154 def register_lldb_command(cls
, debugger
, module_name
):
155 ParsedCommand
.do_register_cmd(cls
, debugger
, module_name
)
157 def setup_command_definition(self
):
158 ov_parser
= self
.get_parser()
159 ov_parser
.add_option(
162 "language defaults to None",
163 value_type
=lldb
.eArgTypeLanguage
,
169 ov_parser
.add_option(
172 "log channel - defaults to lldb",
173 value_type
=lldb
.eArgTypeLogChannel
,
179 ov_parser
.add_option(
182 "A process name, defaults to None",
183 value_type
=lldb
.eArgTypeProcessName
,
188 ov_parser
.add_argument_set(
190 ov_parser
.make_argument_element(
191 lldb
.eArgTypeClassName
, "plain", [1, 2]
193 ov_parser
.make_argument_element(
194 lldb
.eArgTypeOffset
, "optional", [1, 2]
199 ov_parser
.add_argument_set(
201 ov_parser
.make_argument_element(
202 lldb
.eArgTypePythonClass
, "plain", [3, 4]
204 ov_parser
.make_argument_element(lldb
.eArgTypePid
, "optional", [3, 4]),
208 def get_repeat_command(self
, command
):
209 global two_arg_repeat
210 two_arg_repeat
= command
211 return command
+ " THIRD_ARG"
213 def handle_option_argument_completion(self
, long_option
, cursor_pos
):
214 ov_parser
= self
.get_parser()
215 value
= ov_parser
.dest_for_option(long_option
)[0 : cursor_pos
+ 1]
216 proc_value
= ov_parser
.proc_name
217 if proc_value
!= None:
218 new_str
= value
+ proc_value
219 ret_arr
= {"completion": new_str
, "mode": "partial"}
222 ret_arr
= {"values": [value
+ "nice", value
+ "not_nice", value
+ "mediocre"]}
225 def handle_argument_completion(self
, args
, arg_pos
, cursor_pos
):
226 ov_parser
= self
.get_parser()
227 orig_arg
= args
[arg_pos
][0:cursor_pos
]
228 if orig_arg
== "correct_":
229 ret_arr
= {"completion": "correct_answer"}
232 if ov_parser
.was_set("process-name"):
233 # No completions if proc_name was set.
237 "values": [orig_arg
+ "cool", orig_arg
+ "yuck"],
238 "descriptions": ["good idea", "bad idea"],
242 def get_short_help(self
):
243 return "This is my short help string"
245 def get_long_help(self
):
246 return self
.help_string
249 def __lldb_init_module(debugger
, dict):
250 # Register all classes that have a register_lldb_command method
251 for _name
, cls
in inspect
.getmembers(sys
.modules
[__name__
]):
252 if inspect
.isclass(cls
) and callable(
253 getattr(cls
, "register_lldb_command", None)
255 cls
.register_lldb_command(debugger
, __name__
)