xref: /aosp_15_r20/external/perfetto/ui/src/plugins/dev.perfetto.Debug/index.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {
16  addDebugCounterTrack,
17  addDebugSliceTrack,
18} from '../../components/tracks/debug_tracks';
19import {Trace} from '../../public/trace';
20import {PerfettoPlugin} from '../../public/plugin';
21import {exists} from '../../base/utils';
22
23export default class implements PerfettoPlugin {
24  static readonly id = 'dev.perfetto.DebugTracks';
25  async onTraceLoad(ctx: Trace): Promise<void> {
26    ctx.commands.registerCommand({
27      id: 'perfetto.DebugTracks#addDebugSliceTrack',
28      name: 'Add debug slice track',
29      callback: async (arg: unknown) => {
30        // This command takes a query and creates a debug track out of it The
31        // query can be passed in using the first arg, or if this is not defined
32        // or is the wrong type, we prompt the user for it.
33        const query = await getStringFromArgOrPrompt(ctx, arg);
34        if (exists(query)) {
35          await addDebugSliceTrack({
36            trace: ctx,
37            data: {
38              sqlSource: query,
39            },
40            title: 'Debug slice track',
41          });
42        }
43      },
44    });
45
46    ctx.commands.registerCommand({
47      id: 'perfetto.DebugTracks#addDebugCounterTrack',
48      name: 'Add debug counter track',
49      callback: async (arg: unknown) => {
50        const query = await getStringFromArgOrPrompt(ctx, arg);
51        if (exists(query)) {
52          await addDebugCounterTrack({
53            trace: ctx,
54            data: {
55              sqlSource: query,
56            },
57            title: 'Debug slice track',
58          });
59        }
60      },
61    });
62  }
63}
64
65// If arg is a string, return it, otherwise prompt the user for a string. An
66// exception is thrown if the prompt is cancelled, so this function handles this
67// and returns undefined in this case.
68async function getStringFromArgOrPrompt(
69  ctx: Trace,
70  arg: unknown,
71): Promise<string | undefined> {
72  if (typeof arg === 'string') {
73    return arg;
74  } else {
75    return await ctx.omnibox.prompt('Enter a query...');
76  }
77}
78