1-- 2-- Copyright 2019 The Android Open Source Project 3-- 4-- Licensed under the Apache License, Version 2.0 (the "License"); 5-- you may not use this file except in compliance with the License. 6-- You may obtain a copy of the License at 7-- 8-- https://www.apache.org/licenses/LICENSE-2.0 9-- 10-- Unless required by applicable law or agreed to in writing, software 11-- distributed under the License is distributed on an "AS IS" BASIS, 12-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13-- See the License for the specific language governing permissions and 14-- limitations under the License. 15-- 16 17-- Count packages by package UID. 18CREATE PERFETTO TABLE _uid_package_count AS 19SELECT uid, COUNT(1) AS cnt 20FROM package_list 21GROUP BY 1; 22 23CREATE PERFETTO FUNCTION _android_package_for_process( 24 uid INT, 25 uid_count INT, 26 process_name STRING 27) 28RETURNS TABLE( 29 package_name STRING, 30 version_code INT, 31 debuggable BOOL 32) 33AS 34WITH min_distance AS ( 35 SELECT 36 -- SQLite allows omitting the group-by for the MIN: the other columns 37 -- will match the row with the minimum value. 38 MIN(LENGTH($process_name) - LENGTH(package_name)), 39 package_name, 40 version_code, 41 debuggable 42 FROM package_list 43 WHERE ( 44 ( 45 $uid = uid 46 AND ( 47 -- unique match 48 $uid_count = 1 49 -- or process name is a prefix the package name 50 OR $process_name GLOB package_name || '*' 51 ) 52 ) 53 OR 54 ( 55 -- isolated processes can only be matched based on the name 56 $uid >= 90000 AND $uid < 100000 57 AND STR_SPLIT($process_name, ':', 0) GLOB package_name || '*' 58 ) 59 ) 60) 61SELECT package_name, version_code, debuggable 62FROM min_distance; 63 64-- Data about packages running on the process. 65CREATE PERFETTO TABLE android_process_metadata( 66 -- Process upid. 67 upid INT, 68 -- Process name. 69 process_name STRING, 70 -- Android app UID. 71 uid INT, 72 -- Whether the UID is shared by multiple packages. 73 shared_uid BOOL, 74 -- Name of the packages running in this process. 75 package_name STRING, 76 -- Package version code. 77 version_code INT, 78 -- Whether package is debuggable. 79 debuggable INT 80) AS 81SELECT 82 process.upid, 83 -- workaround for b/169226092: the bug has been fixed it Android T, but 84 -- we support ingesting traces from older Android versions. 85 CASE 86 -- cmdline gets rewritten after fork, if these are still there we must 87 -- have seen a racy capture. 88 WHEN length(process.name) = 15 AND ( 89 process.cmdline IN ('zygote', 'zygote64', '<pre-initialized>') 90 OR process.cmdline GLOB '*' || process.name) 91 THEN process.cmdline 92 ELSE process.name 93 END AS process_name, 94 process.android_appid AS uid, 95 CASE WHEN _uid_package_count.cnt > 1 THEN TRUE ELSE NULL END AS shared_uid, 96 plist.package_name, 97 plist.version_code, 98 plist.debuggable 99FROM process 100LEFT JOIN _uid_package_count ON process.android_appid = _uid_package_count.uid 101LEFT JOIN _android_package_for_process( 102 process.android_appid, _uid_package_count.cnt, process.name 103) AS plist; 104