1 /* 2 * Copyright (C) 2017 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_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_ 18 #define SRC_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_ 19 20 #include <memory> 21 #include <set> 22 #include <string> 23 #include <vector> 24 25 #include "perfetto/ext/base/scoped_file.h" 26 27 namespace perfetto { 28 29 constexpr std::string_view kKretprobeDefaultMaxactives = "1024"; 30 31 class FtraceProcfs { 32 public: 33 static const char* const kTracingPaths[]; 34 35 // Tries creating an |FtraceProcfs| at the standard tracefs mount points. 36 // Takes an optional |instance_path| such as "instances/wifi/", in which case 37 // the returned object will be for that ftrace instance path. 38 static std::unique_ptr<FtraceProcfs> CreateGuessingMountPoint( 39 const std::string& instance_path = ""); 40 41 static std::unique_ptr<FtraceProcfs> Create(const std::string& root); 42 43 static int g_kmesg_fd; 44 45 explicit FtraceProcfs(const std::string& root); 46 virtual ~FtraceProcfs(); 47 48 // Set the filter for syscall events. If empty, clear the filter. 49 bool SetSyscallFilter(const std::set<size_t>& filter); 50 51 // Enable the event under with the given |group| and |name|. 52 bool EnableEvent(const std::string& group, const std::string& name); 53 54 // Create the kprobe event for the function |name|. The event will be in 55 // |group|/|name|. Depending on the value of |is_retprobe|, installs a kprobe 56 // or a kretprobe. 57 bool CreateKprobeEvent(const std::string& group, 58 const std::string& name, 59 bool is_retprobe); 60 61 // Remove kprobe event from the system 62 bool RemoveKprobeEvent(const std::string& group, const std::string& name); 63 64 // Read the "kprobe_profile" file. 65 std::string ReadKprobeStats() const; 66 67 // Disable the event under with the given |group| and |name|. 68 bool DisableEvent(const std::string& group, const std::string& name); 69 70 // Returns true if the event under the given |group| and |name| exists and its 71 // enable file is writeable. 72 bool IsEventAccessible(const std::string& group, const std::string& name); 73 74 // Disable all events by writing to the global enable file. 75 bool DisableAllEvents(); 76 77 // Read the format for event with the given |group| and |name|. 78 // virtual for testing. 79 virtual std::string ReadEventFormat(const std::string& group, 80 const std::string& name) const; 81 82 virtual std::string ReadPageHeaderFormat() const; 83 84 std::string GetCurrentTracer(); 85 // Sets the "current_tracer". Might fail with EBUSY if tracing pipes have 86 // already been opened for reading. 87 bool SetCurrentTracer(const std::string& tracer); 88 // Resets the "current_tracer" to "nop". 89 bool ResetCurrentTracer(); 90 bool AppendFunctionFilters(const std::vector<std::string>& filters); 91 bool ClearFunctionFilters(); 92 bool AppendFunctionGraphFilters(const std::vector<std::string>& filters); 93 bool ClearFunctionGraphFilters(); 94 95 // Get all triggers for event with the given |group| and |name|. 96 std::vector<std::string> ReadEventTriggers(const std::string& group, 97 const std::string& name) const; 98 99 // Create an event trigger for the given |group| and |name|. 100 bool CreateEventTrigger(const std::string& group, 101 const std::string& name, 102 const std::string& trigger); 103 104 // Remove an event trigger for the given |group| and |name|. 105 bool RemoveEventTrigger(const std::string& group, 106 const std::string& name, 107 const std::string& trigger); 108 109 // Remove all event trigger for the given |group| and |name|. 110 bool RemoveAllEventTriggers(const std::string& group, 111 const std::string& name); 112 113 // Sets up any associated event trigger before enabling the event 114 bool MaybeSetUpEventTriggers(const std::string& group, 115 const std::string& name); 116 117 // Tears down any associated event trigger after disabling the event 118 bool MaybeTearDownEventTriggers(const std::string& group, 119 const std::string& name); 120 121 // Returns true if rss_stat_throttled synthetic event is supported 122 bool SupportsRssStatThrottled(); 123 124 // Read the printk formats file. 125 std::string ReadPrintkFormats() const; 126 127 // Opens the "/per_cpu/cpuXX/stats" file for the given |cpu|. 128 base::ScopedFile OpenCpuStats(size_t cpu) const; 129 130 // Read the "/per_cpu/cpuXX/stats" file for the given |cpu|. 131 std::string ReadCpuStats(size_t cpu) const; 132 133 // Set ftrace buffer size in pages. 134 // This size is *per cpu* so for the total size you have to multiply 135 // by the number of CPUs. 136 bool SetCpuBufferSizeInPages(size_t pages); 137 138 // Returns the number of CPUs. 139 // This will match the number of tracing/per_cpu/cpuXX directories. 140 size_t virtual NumberOfCpus() const; 141 142 // Clears the trace buffers for all CPUs. Blocks until this is done. 143 void ClearTrace(); 144 145 // Clears the trace buffer for cpu. Blocks until this is done. 146 void ClearPerCpuTrace(size_t cpu); 147 148 // Writes the string |str| as an event into the trace buffer. 149 bool WriteTraceMarker(const std::string& str); 150 151 // Read tracing_on and return true if tracing_on is 1, otherwise return false. 152 bool GetTracingOn(); 153 154 // Write 1 to tracing_on if |on| is true, otherwise write 0. 155 bool SetTracingOn(bool on); 156 157 // Returns true if ftrace tracing is available. 158 // Ftrace tracing is available iff "/current_tracer" is "nop", indicates 159 // function tracing is not in use. Necessarily 160 // racy: another program could enable/disable tracing at any point. 161 bool IsTracingAvailable(); 162 163 // Set the clock. |clock_name| should be one of the names returned by 164 // AvailableClocks. Setting the clock clears the buffer. 165 bool SetClock(const std::string& clock_name); 166 167 // Get the currently set clock. 168 std::string GetClock(); 169 170 // Get all the available clocks. 171 std::set<std::string> AvailableClocks(); 172 173 uint32_t ReadBufferPercent(); 174 bool SetBufferPercent(uint32_t percent); 175 176 // Get all the enabled events. 177 virtual std::vector<std::string> ReadEnabledEvents(); 178 179 // Open the raw pipe for |cpu|. 180 virtual base::ScopedFile OpenPipeForCpu(size_t cpu); 181 182 virtual const std::set<std::string> GetEventNamesForGroup( 183 const std::string& path) const; 184 185 // Returns the |id| for event with the given |group| and |name|. Returns 0 if 186 // the event doesn't exist, or its /id file could not be read. Not typically 187 // needed if already parsing the format file. 188 uint32_t ReadEventId(const std::string& group, const std::string& name) const; 189 GetRootPath()190 std::string GetRootPath() const { return root_; } 191 192 protected: 193 // virtual and protected for testing. 194 virtual bool WriteToFile(const std::string& path, const std::string& str); 195 virtual bool AppendToFile(const std::string& path, const std::string& str); 196 virtual bool ClearFile(const std::string& path); 197 virtual bool IsFileWriteable(const std::string& path); 198 virtual char ReadOneCharFromFile(const std::string& path); 199 virtual std::string ReadFileIntoString(const std::string& path) const; 200 201 private: 202 // Checks the trace file is present at the given root path. 203 static bool CheckRootPath(const std::string& root); 204 205 bool WriteNumberToFile(const std::string& path, size_t value); 206 207 const std::string root_; 208 }; 209 210 } // namespace perfetto 211 212 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_PROCFS_H_ 213