xref: /aosp_15_r20/system/logging/logcat/process_names.cpp (revision 598139dc91b21518d67c408eaea2644226490971)
1 /*
2  * Copyright (C) 2023 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  *      http://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 #include "process_names.h"
18 
19 #include "android-base/file.h"
20 
ReadCmdline(uint64_t pid)21 const std::string ProcessNames::ReadCmdline(uint64_t pid) {
22     const std::string path = std::string("/proc/") + std::to_string(pid) + "/cmdline";
23 
24     std::string cmdline;
25     if (!android::base::ReadFileToString(path, &cmdline)) {
26         return "";
27     }
28 
29     // We need to remove anything that would be part of an absolute path for the executable
30     // but also the parameters. e.g.:
31     // Input : /path/to/myProgram -D --ooo
32     // Output: myProgram
33     return android::base::Basename(cmdline.c_str());
34 }
35 
ReadComm(uint64_t pid)36 const std::string ProcessNames::ReadComm(uint64_t pid) {
37     const std::string path = std::string("/proc/") + std::to_string(pid) + "/comm";
38     std::string cmdline;
39     bool success = android::base::ReadFileToString(path, &cmdline);
40     if (!success) {
41         return "";
42     }
43     return cmdline;
44 }
45 
Resolve(uint64_t pid)46 const std::string ProcessNames::Resolve(uint64_t pid) {
47     std::string name = ReadCmdline(pid);
48     if (!name.empty()) {
49         return name;
50     }
51 
52     // Kernel threads do not have anything in /proc/PID/cmdline. e.g.:
53     // migration/0
54     // cpuhp/0
55     // kworker/7:1-events
56     //
57     // To still have a somewhat relevant name, we check /proc/PID/comm, even though
58     // the max length is 16 characters.
59     name = ReadComm(pid);
60     if (!name.empty()) {
61         return name;
62     }
63 
64     return "";
65 }
66 
Get(uint64_t pid)67 std::string ProcessNames::Get(uint64_t pid) {
68     // Cache hit!
69     const std::string& cached = cache.get(pid);
70     if (!cached.empty()) {
71         return cached;
72     }
73 
74     // Cache miss!
75     std::string name = Resolve(pid);
76     // When an app starts, after it forks from zygote process, the process name remains
77     // zygote/zygote64, then <pre-initialized>, then the package/process name is set.
78     // We don't cache until we know the final value.
79     if (name != "<pre-initialized>" && !name.starts_with("zygote")) {
80         cache.put(pid, name);
81     }
82     return name;
83 }
84