1dda28197Spatrick //===-- RenderScriptScriptGroup.cpp ---------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Breakpoint/StoppointCallbackContext.h"
10061da546Spatrick #include "lldb/Core/Debugger.h"
11061da546Spatrick #include "lldb/Core/PluginManager.h"
12061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
13061da546Spatrick #include "lldb/Interpreter/CommandObjectMultiword.h"
14061da546Spatrick #include "lldb/Interpreter/CommandReturnObject.h"
15061da546Spatrick #include "lldb/Interpreter/Options.h"
16061da546Spatrick #include "lldb/Symbol/Symbol.h"
17061da546Spatrick #include "lldb/Symbol/Type.h"
18061da546Spatrick #include "lldb/Symbol/VariableList.h"
19061da546Spatrick #include "lldb/Target/Process.h"
20061da546Spatrick #include "lldb/Target/Target.h"
21061da546Spatrick #include "lldb/Utility/Args.h"
22061da546Spatrick #include "lldb/Utility/ConstString.h"
23061da546Spatrick #include "lldb/Utility/Log.h"
24061da546Spatrick #include "lldb/Utility/Status.h"
25061da546Spatrick 
26061da546Spatrick #include "RenderScriptRuntime.h"
27061da546Spatrick #include "RenderScriptScriptGroup.h"
28061da546Spatrick 
29061da546Spatrick using namespace lldb;
30061da546Spatrick using namespace lldb_private;
31061da546Spatrick using namespace lldb_renderscript;
32061da546Spatrick 
33061da546Spatrick class CommandObjectRenderScriptScriptGroupBreakpointSet
34061da546Spatrick     : public CommandObjectParsed {
35061da546Spatrick public:
CommandObjectRenderScriptScriptGroupBreakpointSet(CommandInterpreter & interpreter)36061da546Spatrick   CommandObjectRenderScriptScriptGroupBreakpointSet(
37061da546Spatrick       CommandInterpreter &interpreter)
38061da546Spatrick       : CommandObjectParsed(
39061da546Spatrick             interpreter, "renderscript scriptgroup breakpoint set",
40061da546Spatrick             "Place a breakpoint on all kernels forming a script group.",
41061da546Spatrick             "renderscript scriptgroup breakpoint set <group_name>",
42*f6aab3d8Srobert             eCommandRequiresProcess | eCommandProcessMustBeLaunched) {
43*f6aab3d8Srobert     CommandArgumentData name_arg{eArgTypeName, eArgRepeatPlus};
44*f6aab3d8Srobert     m_arguments.push_back({name_arg});
45*f6aab3d8Srobert   }
46061da546Spatrick 
47061da546Spatrick   ~CommandObjectRenderScriptScriptGroupBreakpointSet() override = default;
48061da546Spatrick 
DoExecute(Args & command,CommandReturnObject & result)49061da546Spatrick   bool DoExecute(Args &command, CommandReturnObject &result) override {
50061da546Spatrick     Stream &stream = result.GetOutputStream();
51061da546Spatrick     RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>(
52061da546Spatrick         m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(
53061da546Spatrick             eLanguageTypeExtRenderScript));
54061da546Spatrick     assert(runtime);
55061da546Spatrick     auto &target = m_exe_ctx.GetTargetSP();
56061da546Spatrick     bool stop_on_all = false;
57061da546Spatrick     const llvm::StringRef long_stop_all("--stop-on-all"), short_stop_all("-a");
58061da546Spatrick     std::vector<ConstString> sites;
59061da546Spatrick     sites.reserve(command.GetArgumentCount());
60061da546Spatrick     for (size_t i = 0; i < command.GetArgumentCount(); ++i) {
61061da546Spatrick       const auto arg = command.GetArgumentAtIndex(i);
62061da546Spatrick       if (long_stop_all == arg || short_stop_all == arg)
63061da546Spatrick         stop_on_all = true;
64061da546Spatrick       else
65061da546Spatrick         sites.push_back(ConstString(arg));
66061da546Spatrick     }
67061da546Spatrick     for (const auto &name : sites) {
68061da546Spatrick       runtime->PlaceBreakpointOnScriptGroup(target, stream, name, stop_on_all);
69061da546Spatrick     }
70061da546Spatrick     result.SetStatus(eReturnStatusSuccessFinishResult);
71061da546Spatrick     return true;
72061da546Spatrick   }
73061da546Spatrick };
74061da546Spatrick 
75061da546Spatrick class CommandObjectRenderScriptScriptGroupBreakpoint
76061da546Spatrick     : public CommandObjectMultiword {
77061da546Spatrick public:
CommandObjectRenderScriptScriptGroupBreakpoint(CommandInterpreter & interpreter)78061da546Spatrick   CommandObjectRenderScriptScriptGroupBreakpoint(
79061da546Spatrick       CommandInterpreter &interpreter)
80061da546Spatrick       : CommandObjectMultiword(
81061da546Spatrick             interpreter, "renderscript scriptgroup breakpoint",
82061da546Spatrick             "Renderscript scriptgroup breakpoint interaction.",
83061da546Spatrick             "renderscript scriptgroup breakpoint set [--stop-on-all/-a]"
84061da546Spatrick             "<scriptgroup name> ...",
85061da546Spatrick             eCommandRequiresProcess | eCommandProcessMustBeLaunched) {
86061da546Spatrick     LoadSubCommand(
87061da546Spatrick         "set",
88061da546Spatrick         CommandObjectSP(new CommandObjectRenderScriptScriptGroupBreakpointSet(
89061da546Spatrick             interpreter)));
90061da546Spatrick   }
91061da546Spatrick 
92061da546Spatrick   ~CommandObjectRenderScriptScriptGroupBreakpoint() override = default;
93061da546Spatrick };
94061da546Spatrick 
95061da546Spatrick class CommandObjectRenderScriptScriptGroupList : public CommandObjectParsed {
96061da546Spatrick public:
CommandObjectRenderScriptScriptGroupList(CommandInterpreter & interpreter)97061da546Spatrick   CommandObjectRenderScriptScriptGroupList(CommandInterpreter &interpreter)
98061da546Spatrick       : CommandObjectParsed(interpreter, "renderscript scriptgroup list",
99061da546Spatrick                             "List all currently discovered script groups.",
100061da546Spatrick                             "renderscript scriptgroup list",
101061da546Spatrick                             eCommandRequiresProcess |
102061da546Spatrick                                 eCommandProcessMustBeLaunched) {}
103061da546Spatrick 
104061da546Spatrick   ~CommandObjectRenderScriptScriptGroupList() override = default;
105061da546Spatrick 
DoExecute(Args & command,CommandReturnObject & result)106061da546Spatrick   bool DoExecute(Args &command, CommandReturnObject &result) override {
107061da546Spatrick     Stream &stream = result.GetOutputStream();
108061da546Spatrick     RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>(
109061da546Spatrick         m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(
110061da546Spatrick             eLanguageTypeExtRenderScript));
111061da546Spatrick     assert(runtime);
112061da546Spatrick     const RSScriptGroupList &groups = runtime->GetScriptGroups();
113061da546Spatrick     // print script group count
114061da546Spatrick     stream.Printf("%" PRIu64 " script %s", uint64_t(groups.size()),
115061da546Spatrick                   (groups.size() == 1) ? "group" : "groups");
116061da546Spatrick     stream.EOL();
117061da546Spatrick     // print script group details
118061da546Spatrick     stream.IndentMore();
119061da546Spatrick     for (const RSScriptGroupDescriptorSP &g : groups) {
120061da546Spatrick       if (g) {
121061da546Spatrick         stream.Indent();
122061da546Spatrick         // script group name
123061da546Spatrick         stream.Printf("%s", g->m_name.AsCString());
124061da546Spatrick         stream.EOL();
125061da546Spatrick         // print out the kernels
126061da546Spatrick         stream.IndentMore();
127061da546Spatrick         for (const auto &k : g->m_kernels) {
128061da546Spatrick           stream.Indent();
129061da546Spatrick           stream.Printf(". %s", k.m_name.AsCString());
130061da546Spatrick           stream.EOL();
131061da546Spatrick         }
132061da546Spatrick         stream.IndentLess();
133061da546Spatrick       }
134061da546Spatrick     }
135061da546Spatrick     stream.IndentLess();
136061da546Spatrick     result.SetStatus(eReturnStatusSuccessFinishResult);
137061da546Spatrick     return true;
138061da546Spatrick   }
139061da546Spatrick };
140061da546Spatrick 
141061da546Spatrick class CommandObjectRenderScriptScriptGroup : public CommandObjectMultiword {
142061da546Spatrick public:
CommandObjectRenderScriptScriptGroup(CommandInterpreter & interpreter)143061da546Spatrick   CommandObjectRenderScriptScriptGroup(CommandInterpreter &interpreter)
144061da546Spatrick       : CommandObjectMultiword(interpreter, "renderscript scriptgroup",
145061da546Spatrick                                "Command set for interacting with scriptgroups.",
146061da546Spatrick                                nullptr, eCommandRequiresProcess |
147061da546Spatrick                                             eCommandProcessMustBeLaunched) {
148061da546Spatrick     LoadSubCommand(
149061da546Spatrick         "breakpoint",
150061da546Spatrick         CommandObjectSP(
151061da546Spatrick             new CommandObjectRenderScriptScriptGroupBreakpoint(interpreter)));
152061da546Spatrick     LoadSubCommand(
153061da546Spatrick         "list", CommandObjectSP(
154061da546Spatrick                     new CommandObjectRenderScriptScriptGroupList(interpreter)));
155061da546Spatrick   }
156061da546Spatrick 
157061da546Spatrick   ~CommandObjectRenderScriptScriptGroup() override = default;
158061da546Spatrick };
159061da546Spatrick 
NewCommandObjectRenderScriptScriptGroup(lldb_private::CommandInterpreter & interpreter)160061da546Spatrick lldb::CommandObjectSP NewCommandObjectRenderScriptScriptGroup(
161061da546Spatrick     lldb_private::CommandInterpreter &interpreter) {
162061da546Spatrick   return CommandObjectSP(new CommandObjectRenderScriptScriptGroup(interpreter));
163061da546Spatrick }
164