blob: 996b06da0dcdded84f4b65695a827510f79dddd8 [file] [log] [blame]
Josh Gaocbe70cb2016-10-18 18:17:52 -07001/*
2 * Copyright 2016, 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
Narayan Kamath2d377cd2017-05-10 10:58:59 +010017#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070018
Elliott Hughesa660cb32020-07-23 15:26:10 -070019#include <time.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070020
Christopher Ferrisb999b822022-02-09 17:57:21 -080021#include <functional>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070022#include <string>
Josh Gaocbe70cb2016-10-18 18:17:52 -070023#include <utility>
24
Josh Gao2b2ae0c2017-08-21 14:31:17 -070025#include <android-base/file.h>
Inseob Kimf337dee2025-06-16 15:27:26 +090026#include <android-base/properties.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070027#include <android-base/stringprintf.h>
28#include <android-base/strings.h>
Narayan Kamath2d377cd2017-05-10 10:58:59 +010029#include "protocol.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070030
repo sync -j5ede69992025-04-17 22:24:57 +000031constexpr const char kUnknown[] = "<unknown>";
32
Josh Gao31348a72021-03-29 21:53:42 -070033std::vector<std::string> get_command_line(pid_t pid) {
34 std::vector<std::string> result;
35
36 std::string cmdline;
37 android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid), &cmdline);
38
39 auto it = cmdline.cbegin();
40 while (it != cmdline.cend()) {
41 // string::iterator is a wrapped type, not a raw char*.
42 auto terminator = std::find(it, cmdline.cend(), '\0');
43 result.emplace_back(it, terminator);
44 it = std::find_if(terminator, cmdline.cend(), [](char c) { return c != '\0'; });
45 }
46 if (result.empty()) {
repo sync -j5ede69992025-04-17 22:24:57 +000047 return {kUnknown};
Josh Gao31348a72021-03-29 21:53:42 -070048 }
49
50 return result;
51}
52
Josh Gao2b2ae0c2017-08-21 14:31:17 -070053std::string get_process_name(pid_t pid) {
repo sync -j5ede69992025-04-17 22:24:57 +000054 std::string result(kUnknown);
Josh Gao2b2ae0c2017-08-21 14:31:17 -070055 android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid), &result);
Josh Gao31348a72021-03-29 21:53:42 -070056 // We only want the name, not the whole command line, so truncate at the first NUL.
57 return result.c_str();
Josh Gao2b2ae0c2017-08-21 14:31:17 -070058}
59
60std::string get_thread_name(pid_t tid) {
repo sync -j5ede69992025-04-17 22:24:57 +000061 std::string result(kUnknown);
Josh Gao2b2ae0c2017-08-21 14:31:17 -070062 android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/comm", tid), &result);
63 return android::base::Trim(result);
64}
Elliott Hughesa660cb32020-07-23 15:26:10 -070065
repo sync -j5ede69992025-04-17 22:24:57 +000066std::string get_executable_name(pid_t pid) {
67 std::string result;
68 if (!android::base::Readlink(android::base::StringPrintf("/proc/%d/exe", pid), &result)) {
69 return kUnknown;
70 }
71 return result;
72}
73
Elliott Hughesa660cb32020-07-23 15:26:10 -070074std::string get_timestamp() {
75 timespec ts;
76 clock_gettime(CLOCK_REALTIME, &ts);
77
78 tm tm;
79 localtime_r(&ts.tv_sec, &tm);
80
81 char buf[strlen("1970-01-01 00:00:00.123456789+0830") + 1];
82 char* s = buf;
83 size_t sz = sizeof(buf), n;
84 n = strftime(s, sz, "%F %H:%M", &tm), s += n, sz -= n;
85 n = snprintf(s, sz, ":%02d.%09ld", tm.tm_sec, ts.tv_nsec), s += n, sz -= n;
86 n = strftime(s, sz, "%z", &tm), s += n, sz -= n;
87 return buf;
88}
Christopher Ferrisb999b822022-02-09 17:57:21 -080089
90bool iterate_tids(pid_t pid, std::function<void(pid_t)> callback) {
91 char buf[BUFSIZ];
92 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
93 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(buf), closedir);
94 if (dir == nullptr) {
95 return false;
96 }
97
98 struct dirent* entry;
99 while ((entry = readdir(dir.get())) != nullptr) {
100 pid_t tid = atoi(entry->d_name);
101 if (tid == 0) {
102 continue;
103 }
Christopher Ferris7c2e7e32022-05-27 12:57:58 -0700104 callback(tid);
Christopher Ferrisb999b822022-02-09 17:57:21 -0800105 }
106 return true;
107}
Inseob Kimf337dee2025-06-16 15:27:26 +0900108
109bool is_microdroid() {
110 return android::base::GetProperty("ro.hardware", "") == "microdroid";
111}