xref: /openbsd-src/gnu/llvm/lldb/examples/plugins/commands/fooplugin.cpp (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1*061da546Spatrick //===-- fooplugin.cpp -------------------------------------------*- C++ -*-===//
2*061da546Spatrick //
3*061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*061da546Spatrick //
7*061da546Spatrick //===----------------------------------------------------------------------===//
8*061da546Spatrick 
9*061da546Spatrick /*
10*061da546Spatrick An example plugin for LLDB that provides a new foo command with a child
11*061da546Spatrick subcommand
12*061da546Spatrick Compile this into a dylib foo.dylib and load by placing in appropriate locations
13*061da546Spatrick on disk or
14*061da546Spatrick by typing plugin load foo.dylib at the LLDB command line
15*061da546Spatrick */
16*061da546Spatrick 
17*061da546Spatrick #include <LLDB/SBCommandInterpreter.h>
18*061da546Spatrick #include <LLDB/SBCommandReturnObject.h>
19*061da546Spatrick #include <LLDB/SBDebugger.h>
20*061da546Spatrick 
21*061da546Spatrick namespace lldb {
22*061da546Spatrick bool PluginInitialize(lldb::SBDebugger debugger);
23*061da546Spatrick }
24*061da546Spatrick 
25*061da546Spatrick class ChildCommand : public lldb::SBCommandPluginInterface {
26*061da546Spatrick public:
DoExecute(lldb::SBDebugger debugger,char ** command,lldb::SBCommandReturnObject & result)27*061da546Spatrick   virtual bool DoExecute(lldb::SBDebugger debugger, char **command,
28*061da546Spatrick                          lldb::SBCommandReturnObject &result) {
29*061da546Spatrick     if (command) {
30*061da546Spatrick       const char *arg = *command;
31*061da546Spatrick       while (arg) {
32*061da546Spatrick         result.Printf("%s\n", arg);
33*061da546Spatrick         arg = *(++command);
34*061da546Spatrick       }
35*061da546Spatrick       return true;
36*061da546Spatrick     }
37*061da546Spatrick     return false;
38*061da546Spatrick   }
39*061da546Spatrick };
40*061da546Spatrick 
PluginInitialize(lldb::SBDebugger debugger)41*061da546Spatrick bool lldb::PluginInitialize(lldb::SBDebugger debugger) {
42*061da546Spatrick   lldb::SBCommandInterpreter interpreter = debugger.GetCommandInterpreter();
43*061da546Spatrick   lldb::SBCommand foo = interpreter.AddMultiwordCommand("foo", NULL);
44*061da546Spatrick   foo.AddCommand("child", new ChildCommand(), "a child of foo");
45*061da546Spatrick   return true;
46*061da546Spatrick }
47