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