1 /* 2 * Copyright (C) 2022 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 #ifndef SRC_PROFILING_COMMON_PROC_CMDLINE_H_ 18 #define SRC_PROFILING_COMMON_PROC_CMDLINE_H_ 19 20 #include <sys/types.h> 21 22 #include <string> 23 24 namespace perfetto { 25 namespace profiling { 26 27 // TODO(rsavitski): these functions are a reimplementation of those found in 28 // proc_utils, but with a change in semantics that we intend for all profilers. 29 // Eventually this should become the single canonical file dealing with proc 30 // cmdlines. The transition will start with traced_perf and perfetto_hprof, and 31 // heapprofd will follow later. 32 namespace glob_aware { 33 34 // These functions let the profilers read a /proc/pid/cmdline, find the 35 // substrings corresponding to the argv0 as well as the binary name (e.g. 36 // "/bin/echo" and "echo" respectively), and then match it against a set of glob 37 // patterns. 38 // 39 // Example usage: 40 // std::string cmdline; 41 // bool success = ReadProcCmdlineForPID(42, &cmdline); 42 // if (!success) return false; 43 // const char* binname = FindBinaryName(cmdline.c_str(), cmdline.size()); 44 // return MatchGlobPattern("test*", cmdline.c_str(), binname); 45 46 bool ReadProcCmdlineForPID(pid_t pid, std::string* cmdline_out); 47 const char* FindBinaryName(const char* cmdline, size_t cmdline_len); 48 bool MatchGlobPattern(const char* pattern, 49 const char* cmdline, 50 const char* binname); 51 52 } // namespace glob_aware 53 } // namespace profiling 54 } // namespace perfetto 55 56 #endif // SRC_PROFILING_COMMON_PROC_CMDLINE_H_ 57