xref: /aosp_15_r20/external/perfetto/ui/src/components/sql_utils/args.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 {Engine} from '../../trace_processor/engine';
16import {
17  LONG_NULL,
18  NUM,
19  NUM_NULL,
20  STR,
21  STR_NULL,
22} from '../../trace_processor/query_result';
23import {ArgSetId, ArgsId, asArgId} from './core_types';
24
25export type ArgValue = bigint | string | number | boolean | null;
26type ArgValueType =
27  | 'int'
28  | 'uint'
29  | 'pointer'
30  | 'string'
31  | 'bool'
32  | 'real'
33  | 'null';
34
35export interface Arg {
36  id: ArgsId;
37  type: string;
38  flatKey: string;
39  key: string;
40  value: ArgValue;
41  displayValue: string;
42}
43
44export async function getArgs(
45  engine: Engine,
46  argSetId: ArgSetId,
47): Promise<Arg[]> {
48  const query = await engine.query(`
49    SELECT
50      id,
51      type,
52      flat_key as flatKey,
53      key,
54      int_value as intValue,
55      string_value as stringValue,
56      real_value as realValue,
57      value_type as valueType,
58      display_value as displayValue
59    FROM args
60    WHERE arg_set_id = ${argSetId}
61    ORDER BY id`);
62  const it = query.iter({
63    id: NUM,
64    type: STR,
65    flatKey: STR,
66    key: STR,
67    intValue: LONG_NULL,
68    stringValue: STR_NULL,
69    realValue: NUM_NULL,
70    valueType: STR,
71    displayValue: STR_NULL,
72  });
73
74  const result: Arg[] = [];
75  for (; it.valid(); it.next()) {
76    const value = parseValue(it.valueType as ArgValueType, it);
77    result.push({
78      id: asArgId(it.id),
79      type: it.type,
80      flatKey: it.flatKey,
81      key: it.key,
82      value,
83      displayValue: it.displayValue ?? 'NULL',
84    });
85  }
86
87  return result;
88}
89
90function parseValue(
91  valueType: ArgValueType,
92  value: {
93    intValue: bigint | null;
94    stringValue: string | null;
95    realValue: number | null;
96  },
97): ArgValue {
98  switch (valueType) {
99    case 'int':
100    case 'uint':
101      return value.intValue;
102    case 'pointer':
103      return value.intValue === null
104        ? null
105        : `0x${value.intValue.toString(16)}`;
106    case 'string':
107      return value.stringValue;
108    case 'bool':
109      // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
110      return !!value.intValue;
111    case 'real':
112      return value.realValue;
113    case 'null':
114      return null;
115    default:
116      const x: number = valueType;
117      throw new Error(`Unable to process arg of type ${x}`);
118  }
119}
120