1435933ddSDimitry Andric //===-- RenderScriptScriptGroup.cpp -----------------------------*- C++ -*-===//
2435933ddSDimitry Andric //
3435933ddSDimitry Andric //                     The LLVM Compiler Infrastructure
4435933ddSDimitry Andric //
5435933ddSDimitry Andric // This file is distributed under the University of Illinois Open Source
6435933ddSDimitry Andric // License. See LICENSE.TXT for details.
7435933ddSDimitry Andric //
8435933ddSDimitry Andric //===----------------------------------------------------------------------===//
9435933ddSDimitry Andric 
10435933ddSDimitry Andric #include "lldb/Breakpoint/StoppointCallbackContext.h"
11435933ddSDimitry Andric #include "lldb/Core/Debugger.h"
12435933ddSDimitry Andric #include "lldb/Core/PluginManager.h"
13435933ddSDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
14435933ddSDimitry Andric #include "lldb/Interpreter/CommandObjectMultiword.h"
15435933ddSDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
16435933ddSDimitry Andric #include "lldb/Interpreter/Options.h"
17435933ddSDimitry Andric #include "lldb/Symbol/Symbol.h"
18435933ddSDimitry Andric #include "lldb/Symbol/Type.h"
19435933ddSDimitry Andric #include "lldb/Symbol/VariableList.h"
20435933ddSDimitry Andric #include "lldb/Target/Process.h"
21435933ddSDimitry Andric #include "lldb/Target/Target.h"
22*4ba319b5SDimitry Andric #include "lldb/Utility/Args.h"
23f678e45dSDimitry Andric #include "lldb/Utility/ConstString.h"
24f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
255517e702SDimitry Andric #include "lldb/Utility/Status.h"
26435933ddSDimitry Andric 
27435933ddSDimitry Andric #include "RenderScriptRuntime.h"
28435933ddSDimitry Andric #include "RenderScriptScriptGroup.h"
29435933ddSDimitry Andric 
30435933ddSDimitry Andric using namespace lldb;
31435933ddSDimitry Andric using namespace lldb_private;
32435933ddSDimitry Andric using namespace lldb_renderscript;
33435933ddSDimitry Andric 
34435933ddSDimitry Andric class CommandObjectRenderScriptScriptGroupBreakpointSet
35435933ddSDimitry Andric     : public CommandObjectParsed {
36435933ddSDimitry Andric public:
CommandObjectRenderScriptScriptGroupBreakpointSet(CommandInterpreter & interpreter)37435933ddSDimitry Andric   CommandObjectRenderScriptScriptGroupBreakpointSet(
38435933ddSDimitry Andric       CommandInterpreter &interpreter)
39435933ddSDimitry Andric       : CommandObjectParsed(
40435933ddSDimitry Andric             interpreter, "renderscript scriptgroup breakpoint set",
41435933ddSDimitry Andric             "Place a breakpoint on all kernels forming a script group.",
42435933ddSDimitry Andric             "renderscript scriptgroup breakpoint set <group_name>",
43435933ddSDimitry Andric             eCommandRequiresProcess | eCommandProcessMustBeLaunched) {}
44435933ddSDimitry Andric 
45435933ddSDimitry Andric   ~CommandObjectRenderScriptScriptGroupBreakpointSet() override = default;
46435933ddSDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)47435933ddSDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
48435933ddSDimitry Andric     Stream &stream = result.GetOutputStream();
49435933ddSDimitry Andric     RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>(
50435933ddSDimitry Andric         m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(
51435933ddSDimitry Andric             eLanguageTypeExtRenderScript));
52435933ddSDimitry Andric     assert(runtime);
53435933ddSDimitry Andric     auto &target = m_exe_ctx.GetTargetSP();
54435933ddSDimitry Andric     bool stop_on_all = false;
55435933ddSDimitry Andric     const llvm::StringRef long_stop_all("--stop-on-all"), short_stop_all("-a");
56435933ddSDimitry Andric     std::vector<ConstString> sites;
57435933ddSDimitry Andric     sites.reserve(command.GetArgumentCount());
58435933ddSDimitry Andric     for (size_t i = 0; i < command.GetArgumentCount(); ++i) {
59435933ddSDimitry Andric       const auto arg = command.GetArgumentAtIndex(i);
60435933ddSDimitry Andric       if (long_stop_all == arg || short_stop_all == arg)
61435933ddSDimitry Andric         stop_on_all = true;
62435933ddSDimitry Andric       else
63435933ddSDimitry Andric         sites.push_back(ConstString(arg));
64435933ddSDimitry Andric     }
65435933ddSDimitry Andric     for (const auto &name : sites) {
66435933ddSDimitry Andric       runtime->PlaceBreakpointOnScriptGroup(target, stream, name, stop_on_all);
67435933ddSDimitry Andric     }
68435933ddSDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
69435933ddSDimitry Andric     return true;
70435933ddSDimitry Andric   }
71435933ddSDimitry Andric };
72435933ddSDimitry Andric 
73435933ddSDimitry Andric class CommandObjectRenderScriptScriptGroupBreakpoint
74435933ddSDimitry Andric     : public CommandObjectMultiword {
75435933ddSDimitry Andric public:
CommandObjectRenderScriptScriptGroupBreakpoint(CommandInterpreter & interpreter)76435933ddSDimitry Andric   CommandObjectRenderScriptScriptGroupBreakpoint(
77435933ddSDimitry Andric       CommandInterpreter &interpreter)
78435933ddSDimitry Andric       : CommandObjectMultiword(
79435933ddSDimitry Andric             interpreter, "renderscript scriptgroup breakpoint",
80435933ddSDimitry Andric             "Renderscript scriptgroup breakpoint interaction.",
81435933ddSDimitry Andric             "renderscript scriptgroup breakpoint set [--stop-on-all/-a]"
82435933ddSDimitry Andric             "<scriptgroup name> ...",
83435933ddSDimitry Andric             eCommandRequiresProcess | eCommandProcessMustBeLaunched) {
84435933ddSDimitry Andric     LoadSubCommand(
85435933ddSDimitry Andric         "set",
86435933ddSDimitry Andric         CommandObjectSP(new CommandObjectRenderScriptScriptGroupBreakpointSet(
87435933ddSDimitry Andric             interpreter)));
88435933ddSDimitry Andric   }
89435933ddSDimitry Andric 
90435933ddSDimitry Andric   ~CommandObjectRenderScriptScriptGroupBreakpoint() override = default;
91435933ddSDimitry Andric };
92435933ddSDimitry Andric 
93435933ddSDimitry Andric class CommandObjectRenderScriptScriptGroupList : public CommandObjectParsed {
94435933ddSDimitry Andric public:
CommandObjectRenderScriptScriptGroupList(CommandInterpreter & interpreter)95435933ddSDimitry Andric   CommandObjectRenderScriptScriptGroupList(CommandInterpreter &interpreter)
96435933ddSDimitry Andric       : CommandObjectParsed(interpreter, "renderscript scriptgroup list",
97435933ddSDimitry Andric                             "List all currently discovered script groups.",
98435933ddSDimitry Andric                             "renderscript scriptgroup list",
99435933ddSDimitry Andric                             eCommandRequiresProcess |
100435933ddSDimitry Andric                                 eCommandProcessMustBeLaunched) {}
101435933ddSDimitry Andric 
102435933ddSDimitry Andric   ~CommandObjectRenderScriptScriptGroupList() override = default;
103435933ddSDimitry Andric 
DoExecute(Args & command,CommandReturnObject & result)104435933ddSDimitry Andric   bool DoExecute(Args &command, CommandReturnObject &result) override {
105435933ddSDimitry Andric     Stream &stream = result.GetOutputStream();
106435933ddSDimitry Andric     RenderScriptRuntime *runtime = static_cast<RenderScriptRuntime *>(
107435933ddSDimitry Andric         m_exe_ctx.GetProcessPtr()->GetLanguageRuntime(
108435933ddSDimitry Andric             eLanguageTypeExtRenderScript));
109435933ddSDimitry Andric     assert(runtime);
110435933ddSDimitry Andric     const RSScriptGroupList &groups = runtime->GetScriptGroups();
111435933ddSDimitry Andric     // print script group count
112435933ddSDimitry Andric     stream.Printf("%" PRIu64 " script %s", uint64_t(groups.size()),
113435933ddSDimitry Andric                   (groups.size() == 1) ? "group" : "groups");
114435933ddSDimitry Andric     stream.EOL();
115435933ddSDimitry Andric     // print script group details
116435933ddSDimitry Andric     stream.IndentMore();
117435933ddSDimitry Andric     for (const RSScriptGroupDescriptorSP &g : groups) {
118435933ddSDimitry Andric       if (g) {
119435933ddSDimitry Andric         stream.Indent();
120435933ddSDimitry Andric         // script group name
121435933ddSDimitry Andric         stream.Printf("%s", g->m_name.AsCString());
122435933ddSDimitry Andric         stream.EOL();
123435933ddSDimitry Andric         // print out the kernels
124435933ddSDimitry Andric         stream.IndentMore();
125435933ddSDimitry Andric         for (const auto &k : g->m_kernels) {
126435933ddSDimitry Andric           stream.Indent();
127435933ddSDimitry Andric           stream.Printf(". %s", k.m_name.AsCString());
128435933ddSDimitry Andric           stream.EOL();
129435933ddSDimitry Andric         }
130435933ddSDimitry Andric         stream.IndentLess();
131435933ddSDimitry Andric       }
132435933ddSDimitry Andric     }
133435933ddSDimitry Andric     stream.IndentLess();
134435933ddSDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
135435933ddSDimitry Andric     return true;
136435933ddSDimitry Andric   }
137435933ddSDimitry Andric };
138435933ddSDimitry Andric 
139435933ddSDimitry Andric class CommandObjectRenderScriptScriptGroup : public CommandObjectMultiword {
140435933ddSDimitry Andric public:
CommandObjectRenderScriptScriptGroup(CommandInterpreter & interpreter)141435933ddSDimitry Andric   CommandObjectRenderScriptScriptGroup(CommandInterpreter &interpreter)
142435933ddSDimitry Andric       : CommandObjectMultiword(interpreter, "renderscript scriptgroup",
143435933ddSDimitry Andric                                "Command set for interacting with scriptgroups.",
144435933ddSDimitry Andric                                nullptr, eCommandRequiresProcess |
145435933ddSDimitry Andric                                             eCommandProcessMustBeLaunched) {
146435933ddSDimitry Andric     LoadSubCommand(
147435933ddSDimitry Andric         "breakpoint",
148435933ddSDimitry Andric         CommandObjectSP(
149435933ddSDimitry Andric             new CommandObjectRenderScriptScriptGroupBreakpoint(interpreter)));
150435933ddSDimitry Andric     LoadSubCommand(
151435933ddSDimitry Andric         "list", CommandObjectSP(
152435933ddSDimitry Andric                     new CommandObjectRenderScriptScriptGroupList(interpreter)));
153435933ddSDimitry Andric   }
154435933ddSDimitry Andric 
155435933ddSDimitry Andric   ~CommandObjectRenderScriptScriptGroup() override = default;
156435933ddSDimitry Andric };
157435933ddSDimitry Andric 
NewCommandObjectRenderScriptScriptGroup(lldb_private::CommandInterpreter & interpreter)158435933ddSDimitry Andric lldb::CommandObjectSP NewCommandObjectRenderScriptScriptGroup(
159435933ddSDimitry Andric     lldb_private::CommandInterpreter &interpreter) {
160435933ddSDimitry Andric   return CommandObjectSP(new CommandObjectRenderScriptScriptGroup(interpreter));
161435933ddSDimitry Andric }
162