blob: fc3572c3478d1a1e91e150c3128190941ecd02d0 [file] [log] [blame]
Jamie Gennis6eea6fb2012-12-07 22:03:071/*
2 * Copyright (C) 2012 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
Paul Lawrence2cd93cc2017-01-17 17:50:1817#define LOG_TAG "atrace"
John Reck40b26b42016-03-30 16:44:3618
Jamie Gennis6eea6fb2012-12-07 22:03:0719#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
Mark Salyzyn92dc3fc2014-03-12 20:12:4422#include <inttypes.h>
Jamie Gennis6eea6fb2012-12-07 22:03:0723#include <signal.h>
24#include <stdarg.h>
25#include <stdbool.h>
26#include <stdio.h>
27#include <stdlib.h>
Elliott Hughes3da5d232015-01-25 16:35:2028#include <string.h>
Jamie Gennis6eea6fb2012-12-07 22:03:0729#include <time.h>
Martijn Coenend9535872015-11-26 09:00:5530#include <unistd.h>
Jamie Gennis6eea6fb2012-12-07 22:03:0731#include <zlib.h>
32
Carmen Jacksonea826792017-05-05 18:42:3233#include <fstream>
Elliott Hughesa252f4d2016-07-22 00:12:1534#include <memory>
35
Jamie Gennis6eea6fb2012-12-07 22:03:0736#include <binder/IBinder.h>
37#include <binder/IServiceManager.h>
38#include <binder/Parcel.h>
39
Wei Wang16a63a42018-09-21 22:47:4540#include <android/hardware/atrace/1.0/IAtraceDevice.h>
Martijn Coenenee9b97e2016-11-16 15:00:2641#include <android/hidl/manager/1.0/IServiceManager.h>
42#include <hidl/ServiceManagement.h>
Jamie Gennis6eea6fb2012-12-07 22:03:0743
Corey Tabakaa6c0a722017-05-31 23:37:4044#include <pdx/default_transport/service_utility.h>
Jamie Gennis6eea6fb2012-12-07 22:03:0745#include <utils/String8.h>
John Reck469a1942015-03-26 22:31:3546#include <utils/Timers.h>
Yasuhiro Matsuda46c51fb2015-06-29 10:20:3947#include <utils/Tokenizer.h>
Jamie Gennis6eea6fb2012-12-07 22:03:0748#include <utils/Trace.h>
Stephane Gasparinid8419c22016-03-02 12:45:1549#include <android-base/file.h>
Elliott Hughes5fd6ff62017-03-28 21:55:3150#include <android-base/macros.h>
51#include <android-base/properties.h>
52#include <android-base/stringprintf.h>
Jamie Gennis6eea6fb2012-12-07 22:03:0753
54using namespace android;
Corey Tabakaa6c0a722017-05-31 23:37:4055using pdx::default_transport::ServiceUtility;
Wei Wang16a63a42018-09-21 22:47:4556using hardware::hidl_vec;
57using hardware::hidl_string;
58using hardware::Return;
59using hardware::atrace::V1_0::IAtraceDevice;
60using hardware::atrace::V1_0::Status;
61using hardware::atrace::V1_0::toString;
Jamie Gennis6eea6fb2012-12-07 22:03:0762
Martijn Coenenee9b97e2016-11-16 15:00:2663using std::string;
Jamie Gennis6eea6fb2012-12-07 22:03:0764
Carmen Jackson63ea2912019-04-26 17:41:0065#define MAX_SYS_FILES 11
Jamie Gennis6eea6fb2012-12-07 22:03:0766
67const char* k_traceTagsProperty = "debug.atrace.tags.enableflags";
Carmen Jackson65ecfbb2018-05-22 17:29:4768const char* k_userInitiatedTraceProperty = "debug.atrace.user_initiated";
sergeyv4144eff2016-04-28 18:40:0469
70const char* k_traceAppsNumberProperty = "debug.atrace.app_number";
71const char* k_traceAppsPropertyTemplate = "debug.atrace.app_%d";
sergeyvdb404152016-05-03 02:26:0772const char* k_coreServiceCategory = "core_services";
Corey Tabakaa6c0a722017-05-31 23:37:4073const char* k_pdxServiceCategory = "pdx";
sergeyvdb404152016-05-03 02:26:0774const char* k_coreServicesProp = "ro.atrace.core.services";
Jamie Gennis6eea6fb2012-12-07 22:03:0775
76typedef enum { OPT, REQ } requiredness ;
77
78struct TracingCategory {
79 // The name identifying the category.
80 const char* name;
81
82 // A longer description of the category.
83 const char* longname;
84
85 // The userland tracing tags that the category enables.
86 uint64_t tags;
87
88 // The fname==NULL terminated list of /sys/ files that the category
89 // enables.
90 struct {
91 // Whether the file must be writable in order to enable the tracing
92 // category.
93 requiredness required;
94
95 // The path to the enable file.
96 const char* path;
97 } sysfiles[MAX_SYS_FILES];
98};
99
100/* Tracing categories */
101static const TracingCategory k_categories[] = {
MÃ¥rten Kongstad2d4f89b2018-12-03 11:58:56102 { "gfx", "Graphics", ATRACE_TAG_GRAPHICS, { } },
103 { "input", "Input", ATRACE_TAG_INPUT, { } },
104 { "view", "View System", ATRACE_TAG_VIEW, { } },
105 { "webview", "WebView", ATRACE_TAG_WEBVIEW, { } },
106 { "wm", "Window Manager", ATRACE_TAG_WINDOW_MANAGER, { } },
107 { "am", "Activity Manager", ATRACE_TAG_ACTIVITY_MANAGER, { } },
108 { "sm", "Sync Manager", ATRACE_TAG_SYNC_MANAGER, { } },
109 { "audio", "Audio", ATRACE_TAG_AUDIO, { } },
110 { "video", "Video", ATRACE_TAG_VIDEO, { } },
111 { "camera", "Camera", ATRACE_TAG_CAMERA, { } },
112 { "hal", "Hardware Modules", ATRACE_TAG_HAL, { } },
113 { "res", "Resource Loading", ATRACE_TAG_RESOURCES, { } },
114 { "dalvik", "Dalvik VM", ATRACE_TAG_DALVIK, { } },
115 { "rs", "RenderScript", ATRACE_TAG_RS, { } },
116 { "bionic", "Bionic C Library", ATRACE_TAG_BIONIC, { } },
117 { "power", "Power Management", ATRACE_TAG_POWER, { } },
118 { "pm", "Package Manager", ATRACE_TAG_PACKAGE_MANAGER, { } },
119 { "ss", "System Server", ATRACE_TAG_SYSTEM_SERVER, { } },
120 { "database", "Database", ATRACE_TAG_DATABASE, { } },
121 { "network", "Network", ATRACE_TAG_NETWORK, { } },
122 { "adb", "ADB", ATRACE_TAG_ADB, { } },
123 { "vibrator", "Vibrator", ATRACE_TAG_VIBRATOR, { } },
124 { "aidl", "AIDL calls", ATRACE_TAG_AIDL, { } },
125 { "nnapi", "NNAPI", ATRACE_TAG_NNAPI, { } },
126 { "rro", "Runtime Resource Overlay", ATRACE_TAG_RRO, { } },
sergeyvdb404152016-05-03 02:26:07127 { k_coreServiceCategory, "Core services", 0, { } },
Corey Tabakaa6c0a722017-05-31 23:37:40128 { k_pdxServiceCategory, "PDX services", 0, { } },
Jamie Gennisb2a89e32013-03-12 02:37:53129 { "sched", "CPU Scheduling", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18130 { REQ, "events/sched/sched_switch/enable" },
131 { REQ, "events/sched/sched_wakeup/enable" },
Joel Fernandesee593e22017-06-04 20:05:59132 { OPT, "events/sched/sched_waking/enable" },
Paul Lawrence2cd93cc2017-01-17 17:50:18133 { OPT, "events/sched/sched_blocked_reason/enable" },
134 { OPT, "events/sched/sched_cpu_hotplug/enable" },
Wei Wangca49dfc2018-05-03 22:45:20135 { OPT, "events/sched/sched_pi_setprio/enable" },
Carmen Jackson63ea2912019-04-26 17:41:00136 { OPT, "events/sched/sched_process_exit/enable" },
Joel Fernandes4dfca7c2017-06-15 23:53:52137 { OPT, "events/cgroup/enable" },
Carmen Jackson63ea2912019-04-26 17:41:00138 { OPT, "events/oom/oom_score_adj_update/enable" },
139 { OPT, "events/task/task_rename/enable" },
140 { OPT, "events/task/task_newtask/enable" },
Jamie Gennis6eea6fb2012-12-07 22:03:07141 } },
Dan Willemsenf440d392014-04-11 22:44:09142 { "irq", "IRQ Events", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18143 { REQ, "events/irq/enable" },
144 { OPT, "events/ipi/enable" },
Dan Willemsenf440d392014-04-11 22:44:09145 } },
Joel Fernandes6ccad102017-08-31 15:35:05146 { "irqoff", "IRQ-disabled code section tracing", 0, {
147 { REQ, "events/preemptirq/irq_enable/enable" },
148 { REQ, "events/preemptirq/irq_disable/enable" },
149 } },
150 { "preemptoff", "Preempt-disabled code section tracing", 0, {
151 { REQ, "events/preemptirq/preempt_enable/enable" },
152 { REQ, "events/preemptirq/preempt_disable/enable" },
153 } },
Michael Wright43fb6782016-08-18 18:56:43154 { "i2c", "I2C Events", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18155 { REQ, "events/i2c/enable" },
156 { REQ, "events/i2c/i2c_read/enable" },
157 { REQ, "events/i2c/i2c_write/enable" },
158 { REQ, "events/i2c/i2c_result/enable" },
159 { REQ, "events/i2c/i2c_reply/enable" },
160 { OPT, "events/i2c/smbus_read/enable" },
161 { OPT, "events/i2c/smbus_write/enable" },
162 { OPT, "events/i2c/smbus_result/enable" },
163 { OPT, "events/i2c/smbus_reply/enable" },
Michael Wright43fb6782016-08-18 18:56:43164 } },
Jamie Gennisb2a89e32013-03-12 02:37:53165 { "freq", "CPU Frequency", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18166 { REQ, "events/power/cpu_frequency/enable" },
167 { OPT, "events/power/clock_set_rate/enable" },
Kevin DuBois062e0b92017-11-03 22:44:08168 { OPT, "events/power/clock_disable/enable" },
169 { OPT, "events/power/clock_enable/enable" },
Wei Wang53305dd2018-02-22 19:40:07170 { OPT, "events/clk/clk_set_rate/enable" },
171 { OPT, "events/clk/clk_disable/enable" },
172 { OPT, "events/clk/clk_enable/enable" },
Paul Lawrence2cd93cc2017-01-17 17:50:18173 { OPT, "events/power/cpu_frequency_limits/enable" },
Carmen Jackson5ab84322019-08-08 21:35:47174 { OPT, "events/power/suspend_resume/enable" },
Jamie Gennis6eea6fb2012-12-07 22:03:07175 } },
Jamie Gennisb2a89e32013-03-12 02:37:53176 { "membus", "Memory Bus Utilization", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18177 { REQ, "events/memory_bus/enable" },
Jamie Gennis6eea6fb2012-12-07 22:03:07178 } },
Jamie Gennisb2a89e32013-03-12 02:37:53179 { "idle", "CPU Idle", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18180 { REQ, "events/power/cpu_idle/enable" },
Jamie Gennis6eea6fb2012-12-07 22:03:07181 } },
Jamie Gennisb2a89e32013-03-12 02:37:53182 { "disk", "Disk I/O", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18183 { OPT, "events/f2fs/f2fs_sync_file_enter/enable" },
184 { OPT, "events/f2fs/f2fs_sync_file_exit/enable" },
185 { OPT, "events/f2fs/f2fs_write_begin/enable" },
186 { OPT, "events/f2fs/f2fs_write_end/enable" },
187 { OPT, "events/ext4/ext4_da_write_begin/enable" },
188 { OPT, "events/ext4/ext4_da_write_end/enable" },
189 { OPT, "events/ext4/ext4_sync_file_enter/enable" },
190 { OPT, "events/ext4/ext4_sync_file_exit/enable" },
191 { REQ, "events/block/block_rq_issue/enable" },
192 { REQ, "events/block/block_rq_complete/enable" },
Jamie Gennis6eea6fb2012-12-07 22:03:07193 } },
Ken Sumralld3fa5612013-07-03 19:32:03194 { "mmc", "eMMC commands", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18195 { REQ, "events/mmc/enable" },
Ken Sumralld3fa5612013-07-03 19:32:03196 } },
Jamie Gennisb2a89e32013-03-12 02:37:53197 { "load", "CPU Load", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18198 { REQ, "events/cpufreq_interactive/enable" },
Jamie Gennis6eea6fb2012-12-07 22:03:07199 } },
Jamie Gennisb2a89e32013-03-12 02:37:53200 { "sync", "Synchronization", 0, {
Jesse Halla978cef2019-02-26 00:24:10201 // linux kernel < 4.9
Jesse Hallae001a32018-05-15 19:02:15202 { OPT, "events/sync/enable" },
Jesse Halla978cef2019-02-26 00:24:10203 // linux kernel == 4.9.x
Jesse Hallae001a32018-05-15 19:02:15204 { OPT, "events/fence/enable" },
Jesse Halla978cef2019-02-26 00:24:10205 // linux kernel > 4.9
206 { OPT, "events/dma_fence/enable" },
Jamie Gennis6eea6fb2012-12-07 22:03:07207 } },
Jamie Gennisb2a89e32013-03-12 02:37:53208 { "workq", "Kernel Workqueues", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18209 { REQ, "events/workqueue/enable" },
Jamie Gennis6eea6fb2012-12-07 22:03:07210 } },
Colin Cross580407f2014-08-18 22:22:13211 { "memreclaim", "Kernel Memory Reclaim", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18212 { REQ, "events/vmscan/mm_vmscan_direct_reclaim_begin/enable" },
213 { REQ, "events/vmscan/mm_vmscan_direct_reclaim_end/enable" },
214 { REQ, "events/vmscan/mm_vmscan_kswapd_wake/enable" },
215 { REQ, "events/vmscan/mm_vmscan_kswapd_sleep/enable" },
Carmen Jackson7a23eb72018-07-02 20:14:56216 { OPT, "events/lowmemorykiller/enable" },
Colin Cross580407f2014-08-18 22:22:13217 } },
Aaron Schulmanc2c6ecd2015-02-25 16:37:09218 { "regulators", "Voltage and Current Regulators", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18219 { REQ, "events/regulator/enable" },
Aaron Schulmanc2c6ecd2015-02-25 16:37:09220 } },
Scott Bauerae473362015-06-08 23:32:36221 { "binder_driver", "Binder Kernel driver", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18222 { REQ, "events/binder/binder_transaction/enable" },
223 { REQ, "events/binder/binder_transaction_received/enable" },
Mika Raento80c3e5d2018-06-25 15:47:24224 { REQ, "events/binder/binder_transaction_alloc_buf/enable" },
Martijn Coenen7f3d7a22017-05-26 16:50:55225 { OPT, "events/binder/binder_set_priority/enable" },
Scott Bauerae473362015-06-08 23:32:36226 } },
227 { "binder_lock", "Binder global lock trace", 0, {
Howard Cheneb8acbf2017-05-10 08:32:11228 { OPT, "events/binder/binder_lock/enable" },
229 { OPT, "events/binder/binder_locked/enable" },
230 { OPT, "events/binder/binder_unlock/enable" },
Scott Bauerae473362015-06-08 23:32:36231 } },
Martijn Coenen70481612015-10-23 11:57:05232 { "pagecache", "Page cache", 0, {
Paul Lawrence2cd93cc2017-01-17 17:50:18233 { REQ, "events/filemap/enable" },
Martijn Coenen70481612015-10-23 11:57:05234 } },
Carmen Jackson4f42f212018-10-17 01:25:52235 { "memory", "Memory", 0, {
Collin Fijalkovich08e3f4a2020-03-30 23:20:24236 { OPT, "events/mm_event/mm_event_record/enable" },
Carmen Jackson4f42f212018-10-17 01:25:52237 { OPT, "events/kmem/rss_stat/enable" },
238 { OPT, "events/kmem/ion_heap_grow/enable" },
239 { OPT, "events/kmem/ion_heap_shrink/enable" },
Ioannis Ilkos88a85f32020-04-23 21:43:40240 { OPT, "events/ion/ion_stat/enable" },
Carmen Jackson4f42f212018-10-17 01:25:52241 } },
Wei Wange3079a92020-07-08 22:33:20242 { "thermal", "Thermal event", 0, {
243 { REQ, "events/thermal/thermal_temperature/enable" },
244 { OPT, "events/thermal/cdev_update/enable" },
245 } },
Jamie Gennis6eea6fb2012-12-07 22:03:07246};
247
Wei Wang16a63a42018-09-21 22:47:45248struct TracingVendorCategory {
249 // The name identifying the category.
250 std::string name;
251
252 // A longer description of the category.
253 std::string description;
254
255 // If the category is enabled through command.
256 bool enabled;
257
258 TracingVendorCategory(string &&name, string &&description, bool enabled)
259 : name(std::move(name))
260 , description(std::move(description))
261 , enabled(enabled)
262 {}
263};
264
Jamie Gennis6eea6fb2012-12-07 22:03:07265/* Command line options */
266static int g_traceDurationSeconds = 5;
267static bool g_traceOverwrite = false;
268static int g_traceBufferSizeKB = 2048;
269static bool g_compress = false;
270static bool g_nohup = false;
271static int g_initialSleepSecs = 0;
Yi Kong19d5c002018-07-20 20:39:55272static const char* g_categoriesFile = nullptr;
273static const char* g_kernelTraceFuncs = nullptr;
Jamie Gennisf7f29c82013-03-27 22:50:58274static const char* g_debugAppCmdLine = "";
John Reck40b26b42016-03-30 16:44:36275static const char* g_outputFile = nullptr;
Jamie Gennis6eea6fb2012-12-07 22:03:07276
277/* Global state */
Corey Tabakaa6c0a722017-05-31 23:37:40278static bool g_tracePdx = false;
Jamie Gennis6eea6fb2012-12-07 22:03:07279static bool g_traceAborted = false;
Elliott Hughes5fd6ff62017-03-28 21:55:31280static bool g_categoryEnables[arraysize(k_categories)] = {};
Paul Lawrence2cd93cc2017-01-17 17:50:18281static std::string g_traceFolder;
Wei Wang16a63a42018-09-21 22:47:45282static sp<IAtraceDevice> g_atraceHal;
283static std::vector<TracingVendorCategory> g_vendorCategories;
Jamie Gennis6eea6fb2012-12-07 22:03:07284
285/* Sys file paths */
286static const char* k_traceClockPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18287 "trace_clock";
Jamie Gennis6eea6fb2012-12-07 22:03:07288
289static const char* k_traceBufferSizePath =
Paul Lawrence2cd93cc2017-01-17 17:50:18290 "buffer_size_kb";
Jamie Gennis6eea6fb2012-12-07 22:03:07291
Chih-Hung Hsieh734e3782017-10-05 20:44:13292#if 0
293// TODO: Re-enable after stabilization
Joel Fernandesed80bd02017-06-02 17:19:28294static const char* k_traceCmdlineSizePath =
295 "saved_cmdlines_size";
Chih-Hung Hsieh734e3782017-10-05 20:44:13296#endif
Joel Fernandesed80bd02017-06-02 17:19:28297
Jamie Gennis6eea6fb2012-12-07 22:03:07298static const char* k_tracingOverwriteEnablePath =
Paul Lawrence2cd93cc2017-01-17 17:50:18299 "options/overwrite";
Jamie Gennis6eea6fb2012-12-07 22:03:07300
Jamie Gennise9b8cfb2013-03-12 23:00:10301static const char* k_currentTracerPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18302 "current_tracer";
Jamie Gennise9b8cfb2013-03-12 23:00:10303
304static const char* k_printTgidPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18305 "options/print-tgid";
Jamie Gennise9b8cfb2013-03-12 23:00:10306
John Recke757b1c2018-06-28 19:24:33307static const char* k_recordTgidPath =
308 "options/record-tgid";
309
Jamie Gennise9b8cfb2013-03-12 23:00:10310static const char* k_funcgraphAbsTimePath =
Paul Lawrence2cd93cc2017-01-17 17:50:18311 "options/funcgraph-abstime";
Jamie Gennise9b8cfb2013-03-12 23:00:10312
313static const char* k_funcgraphCpuPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18314 "options/funcgraph-cpu";
Jamie Gennise9b8cfb2013-03-12 23:00:10315
316static const char* k_funcgraphProcPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18317 "options/funcgraph-proc";
Jamie Gennise9b8cfb2013-03-12 23:00:10318
319static const char* k_funcgraphFlatPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18320 "options/funcgraph-flat";
Jamie Gennise9b8cfb2013-03-12 23:00:10321
Jamie Gennise9b8cfb2013-03-12 23:00:10322static const char* k_ftraceFilterPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18323 "set_ftrace_filter";
Jamie Gennise9b8cfb2013-03-12 23:00:10324
Jamie Gennis6eea6fb2012-12-07 22:03:07325static const char* k_tracingOnPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18326 "tracing_on";
Jamie Gennis6eea6fb2012-12-07 22:03:07327
328static const char* k_tracePath =
Paul Lawrence2cd93cc2017-01-17 17:50:18329 "trace";
Jamie Gennis6eea6fb2012-12-07 22:03:07330
Martijn Coenend9535872015-11-26 09:00:55331static const char* k_traceStreamPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18332 "trace_pipe";
Martijn Coenend9535872015-11-26 09:00:55333
John Reck469a1942015-03-26 22:31:35334static const char* k_traceMarkerPath =
Paul Lawrence2cd93cc2017-01-17 17:50:18335 "trace_marker";
John Reck469a1942015-03-26 22:31:35336
Jamie Gennis6eea6fb2012-12-07 22:03:07337// Check whether a file exists.
338static bool fileExists(const char* filename) {
Paul Lawrence2cd93cc2017-01-17 17:50:18339 return access((g_traceFolder + filename).c_str(), F_OK) != -1;
Jamie Gennis6eea6fb2012-12-07 22:03:07340}
341
342// Check whether a file is writable.
343static bool fileIsWritable(const char* filename) {
Paul Lawrence2cd93cc2017-01-17 17:50:18344 return access((g_traceFolder + filename).c_str(), W_OK) != -1;
Jamie Gennis6eea6fb2012-12-07 22:03:07345}
346
Jamie Gennise9b8cfb2013-03-12 23:00:10347// Truncate a file.
348static bool truncateFile(const char* path)
Jamie Gennis6eea6fb2012-12-07 22:03:07349{
Jamie Gennis43122e72013-03-21 21:06:31350 // This uses creat rather than truncate because some of the debug kernel
351 // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by
352 // calls to truncate, but they are cleared by calls to creat.
Paul Lawrence2cd93cc2017-01-17 17:50:18353 int traceFD = creat((g_traceFolder + path).c_str(), 0);
Jamie Gennis43122e72013-03-21 21:06:31354 if (traceFD == -1) {
Paul Lawrence2cd93cc2017-01-17 17:50:18355 fprintf(stderr, "error truncating %s: %s (%d)\n", (g_traceFolder + path).c_str(),
Jamie Gennis43122e72013-03-21 21:06:31356 strerror(errno), errno);
Jamie Gennise9b8cfb2013-03-12 23:00:10357 return false;
358 }
359
Jamie Gennis43122e72013-03-21 21:06:31360 close(traceFD);
361
Jamie Gennise9b8cfb2013-03-12 23:00:10362 return true;
363}
364
365static bool _writeStr(const char* filename, const char* str, int flags)
366{
Paul Lawrence2cd93cc2017-01-17 17:50:18367 std::string fullFilename = g_traceFolder + filename;
368 int fd = open(fullFilename.c_str(), flags);
Jamie Gennis6eea6fb2012-12-07 22:03:07369 if (fd == -1) {
Paul Lawrence2cd93cc2017-01-17 17:50:18370 fprintf(stderr, "error opening %s: %s (%d)\n", fullFilename.c_str(),
Jamie Gennis6eea6fb2012-12-07 22:03:07371 strerror(errno), errno);
372 return false;
373 }
374
375 bool ok = true;
376 ssize_t len = strlen(str);
377 if (write(fd, str, len) != len) {
Paul Lawrence2cd93cc2017-01-17 17:50:18378 fprintf(stderr, "error writing to %s: %s (%d)\n", fullFilename.c_str(),
Jamie Gennis6eea6fb2012-12-07 22:03:07379 strerror(errno), errno);
380 ok = false;
381 }
382
383 close(fd);
384
385 return ok;
386}
387
Jamie Gennise9b8cfb2013-03-12 23:00:10388// Write a string to a file, returning true if the write was successful.
389static bool writeStr(const char* filename, const char* str)
390{
391 return _writeStr(filename, str, O_WRONLY);
392}
393
394// Append a string to a file, returning true if the write was successful.
395static bool appendStr(const char* filename, const char* str)
396{
397 return _writeStr(filename, str, O_APPEND|O_WRONLY);
398}
399
John Reck469a1942015-03-26 22:31:35400static void writeClockSyncMarker()
401{
402 char buffer[128];
Martijn Coenen0bcd97a2015-07-15 12:25:23403 int len = 0;
Paul Lawrence2cd93cc2017-01-17 17:50:18404 int fd = open((g_traceFolder + k_traceMarkerPath).c_str(), O_WRONLY);
Martijn Coenen0bcd97a2015-07-15 12:25:23405 if (fd == -1) {
406 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceMarkerPath,
407 strerror(errno), errno);
408 return;
409 }
John Reck469a1942015-03-26 22:31:35410 float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f;
Martijn Coenen0bcd97a2015-07-15 12:25:23411
412 len = snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
413 if (write(fd, buffer, len) != len) {
414 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
415 }
416
417 int64_t realtime_in_ms = systemTime(CLOCK_REALTIME) / 1000000;
418 len = snprintf(buffer, 128, "trace_event_clock_sync: realtime_ts=%" PRId64 "\n", realtime_in_ms);
419 if (write(fd, buffer, len) != len) {
420 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
421 }
422
423 close(fd);
John Reck469a1942015-03-26 22:31:35424}
425
Jamie Gennis6eea6fb2012-12-07 22:03:07426// Enable or disable a kernel option by writing a "1" or a "0" into a /sys
427// file.
428static bool setKernelOptionEnable(const char* filename, bool enable)
429{
430 return writeStr(filename, enable ? "1" : "0");
431}
432
433// Check whether the category is supported on the device with the current
434// rootness. A category is supported only if all its required /sys/ files are
435// writable and if enabling the category will enable one or more tracing tags
436// or /sys/ files.
437static bool isCategorySupported(const TracingCategory& category)
438{
sergeyvdb404152016-05-03 02:26:07439 if (strcmp(category.name, k_coreServiceCategory) == 0) {
Elliott Hughes5fd6ff62017-03-28 21:55:31440 return !android::base::GetProperty(k_coreServicesProp, "").empty();
sergeyvdb404152016-05-03 02:26:07441 }
442
Corey Tabakaa6c0a722017-05-31 23:37:40443 if (strcmp(category.name, k_pdxServiceCategory) == 0) {
444 return true;
445 }
446
Jamie Gennis6eea6fb2012-12-07 22:03:07447 bool ok = category.tags != 0;
448 for (int i = 0; i < MAX_SYS_FILES; i++) {
449 const char* path = category.sysfiles[i].path;
450 bool req = category.sysfiles[i].required == REQ;
Yi Kong19d5c002018-07-20 20:39:55451 if (path != nullptr) {
Carmen Jackson5bbc9a22018-12-06 21:47:18452 if (fileIsWritable(path)) {
Howard Cheneb8acbf2017-05-10 08:32:11453 ok = true;
Carmen Jackson5bbc9a22018-12-06 21:47:18454 } else if (req) {
455 return false;
Jamie Gennis6eea6fb2012-12-07 22:03:07456 }
457 }
458 }
459 return ok;
460}
461
462// Check whether the category would be supported on the device if the user
463// were root. This function assumes that root is able to write to any file
464// that exists. It performs the same logic as isCategorySupported, but it
Fabien Sanglardb5c95472016-06-08 18:40:12465// uses file existence rather than writability in the /sys/ file checks.
Jamie Gennis6eea6fb2012-12-07 22:03:07466static bool isCategorySupportedForRoot(const TracingCategory& category)
467{
468 bool ok = category.tags != 0;
469 for (int i = 0; i < MAX_SYS_FILES; i++) {
470 const char* path = category.sysfiles[i].path;
471 bool req = category.sysfiles[i].required == REQ;
Yi Kong19d5c002018-07-20 20:39:55472 if (path != nullptr) {
Jamie Gennis6eea6fb2012-12-07 22:03:07473 if (req) {
474 if (!fileExists(path)) {
475 return false;
476 } else {
477 ok = true;
478 }
479 } else {
480 ok |= fileExists(path);
481 }
482 }
483 }
484 return ok;
485}
486
487// Enable or disable overwriting of the kernel trace buffers. Disabling this
488// will cause tracing to stop once the trace buffers have filled up.
489static bool setTraceOverwriteEnable(bool enable)
490{
491 return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable);
492}
493
Carmen Jackson65ecfbb2018-05-22 17:29:47494// Set the user initiated trace property
495static bool setUserInitiatedTraceProperty(bool enable)
496{
497 if (!android::base::SetProperty(k_userInitiatedTraceProperty, enable ? "1" : "")) {
498 fprintf(stderr, "error setting user initiated strace system property\n");
499 return false;
500 }
501 return true;
502}
503
Jamie Gennis6eea6fb2012-12-07 22:03:07504// Enable or disable kernel tracing.
505static bool setTracingEnabled(bool enable)
506{
507 return setKernelOptionEnable(k_tracingOnPath, enable);
508}
509
510// Clear the contents of the kernel trace.
511static bool clearTrace()
512{
Jamie Gennise9b8cfb2013-03-12 23:00:10513 return truncateFile(k_tracePath);
Jamie Gennis6eea6fb2012-12-07 22:03:07514}
515
516// Set the size of the kernel's trace buffer in kilobytes.
517static bool setTraceBufferSizeKB(int size)
518{
519 char str[32] = "1";
Jamie Gennis6eea6fb2012-12-07 22:03:07520 if (size < 1) {
521 size = 1;
522 }
523 snprintf(str, 32, "%d", size);
524 return writeStr(k_traceBufferSizePath, str);
525}
526
Chih-Hung Hsieh734e3782017-10-05 20:44:13527#if 0
528// TODO: Re-enable after stabilization
Joel Fernandesed80bd02017-06-02 17:19:28529// Set the default size of cmdline hashtable
530static bool setCmdlineSize()
531{
Joel Fernandesce964f22017-06-06 19:20:29532 if (fileExists(k_traceCmdlineSizePath)) {
533 return writeStr(k_traceCmdlineSizePath, "8192");
534 }
535 return true;
Joel Fernandesed80bd02017-06-02 17:19:28536}
Chih-Hung Hsieh734e3782017-10-05 20:44:13537#endif
Joel Fernandesed80bd02017-06-02 17:19:28538
Carmen Jacksonea826792017-05-05 18:42:32539// Set the clock to the best available option while tracing. Use 'boot' if it's
540// available; otherwise, use 'mono'. If neither are available use 'global'.
Colin Crossb1ce49b2014-08-20 21:28:47541// Any write to the trace_clock sysfs file will reset the buffer, so only
542// update it if the requested value is not the current value.
Carmen Jacksonea826792017-05-05 18:42:32543static bool setClock()
Jamie Gennis6eea6fb2012-12-07 22:03:07544{
Carmen Jacksonea826792017-05-05 18:42:32545 std::ifstream clockFile((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
546 std::string clockStr((std::istreambuf_iterator<char>(clockFile)),
547 std::istreambuf_iterator<char>());
Colin Crossb1ce49b2014-08-20 21:28:47548
Carmen Jacksonea826792017-05-05 18:42:32549 std::string newClock;
550 if (clockStr.find("boot") != std::string::npos) {
551 newClock = "boot";
552 } else if (clockStr.find("mono") != std::string::npos) {
553 newClock = "mono";
554 } else {
555 newClock = "global";
Colin Crossb1ce49b2014-08-20 21:28:47556 }
557
Chih-Hung Hsiehcb057c22017-08-03 22:48:25558 size_t begin = clockStr.find('[') + 1;
559 size_t end = clockStr.find(']');
Carmen Jacksonea826792017-05-05 18:42:32560 if (newClock.compare(0, std::string::npos, clockStr, begin, end-begin) == 0) {
561 return true;
562 }
563 return writeStr(k_traceClockPath, newClock.c_str());
Jamie Gennis6eea6fb2012-12-07 22:03:07564}
565
Jamie Gennise9b8cfb2013-03-12 23:00:10566static bool setPrintTgidEnableIfPresent(bool enable)
567{
John Reckedbf9ec2018-06-29 18:15:17568 // Pre-4.13 this was options/print-tgid as an android-specific option.
569 // In 4.13+ this is an upstream option called options/record-tgid
570 // Both options produce the same ftrace format change
Jamie Gennise9b8cfb2013-03-12 23:00:10571 if (fileExists(k_printTgidPath)) {
572 return setKernelOptionEnable(k_printTgidPath, enable);
573 }
John Recke757b1c2018-06-28 19:24:33574 if (fileExists(k_recordTgidPath)) {
575 return setKernelOptionEnable(k_recordTgidPath, enable);
576 }
Jamie Gennise9b8cfb2013-03-12 23:00:10577 return true;
578}
579
Jamie Gennis6eea6fb2012-12-07 22:03:07580// Set the trace tags that userland tracing uses, and poke the running
581// processes to pick up the new value.
582static bool setTagsProperty(uint64_t tags)
583{
Elliott Hughes5fd6ff62017-03-28 21:55:31584 std::string value = android::base::StringPrintf("%#" PRIx64, tags);
585 if (!android::base::SetProperty(k_traceTagsProperty, value)) {
Jamie Gennis6eea6fb2012-12-07 22:03:07586 fprintf(stderr, "error setting trace tags system property\n");
587 return false;
588 }
Jamie Gennisf7f29c82013-03-27 22:50:58589 return true;
590}
591
sergeyv4144eff2016-04-28 18:40:04592static void clearAppProperties()
593{
Elliott Hughes5fd6ff62017-03-28 21:55:31594 if (!android::base::SetProperty(k_traceAppsNumberProperty, "")) {
sergeyv4144eff2016-04-28 18:40:04595 fprintf(stderr, "failed to clear system property: %s",
596 k_traceAppsNumberProperty);
597 }
598}
599
Jamie Gennisf7f29c82013-03-27 22:50:58600// Set the system property that indicates which apps should perform
601// application-level tracing.
Dan Austin09a79872016-05-31 20:27:03602static bool setAppCmdlineProperty(char* cmdline)
Jamie Gennisf7f29c82013-03-27 22:50:58603{
sergeyv4144eff2016-04-28 18:40:04604 int i = 0;
Dan Austin09a79872016-05-31 20:27:03605 char* start = cmdline;
Yi Kong19d5c002018-07-20 20:39:55606 while (start != nullptr) {
sergeyv4144eff2016-04-28 18:40:04607 char* end = strchr(start, ',');
Yi Kong19d5c002018-07-20 20:39:55608 if (end != nullptr) {
sergeyv4144eff2016-04-28 18:40:04609 *end = '\0';
610 end++;
611 }
Elliott Hughes5fd6ff62017-03-28 21:55:31612 std::string key = android::base::StringPrintf(k_traceAppsPropertyTemplate, i);
613 if (!android::base::SetProperty(key, start)) {
614 fprintf(stderr, "error setting trace app %d property to %s\n", i, key.c_str());
sergeyv4144eff2016-04-28 18:40:04615 clearAppProperties();
616 return false;
617 }
618 start = end;
619 i++;
620 }
621
Elliott Hughes5fd6ff62017-03-28 21:55:31622 std::string value = android::base::StringPrintf("%d", i);
623 if (!android::base::SetProperty(k_traceAppsNumberProperty, value)) {
624 fprintf(stderr, "error setting trace app number property to %s\n", value.c_str());
sergeyv4144eff2016-04-28 18:40:04625 clearAppProperties();
Jamie Gennisf7f29c82013-03-27 22:50:58626 return false;
627 }
628 return true;
Jamie Gennis6eea6fb2012-12-07 22:03:07629}
630
631// Disable all /sys/ enable files.
632static bool disableKernelTraceEvents() {
633 bool ok = true;
Elliott Hughes5fd6ff62017-03-28 21:55:31634 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 22:03:07635 const TracingCategory &c = k_categories[i];
636 for (int j = 0; j < MAX_SYS_FILES; j++) {
637 const char* path = c.sysfiles[j].path;
Yi Kong19d5c002018-07-20 20:39:55638 if (path != nullptr && fileIsWritable(path)) {
Jamie Gennis6eea6fb2012-12-07 22:03:07639 ok &= setKernelOptionEnable(path, false);
640 }
641 }
642 }
643 return ok;
644}
645
Jamie Gennise9b8cfb2013-03-12 23:00:10646// Verify that the comma separated list of functions are being traced by the
647// kernel.
648static bool verifyKernelTraceFuncs(const char* funcs)
649{
Stephane Gasparinid8419c22016-03-02 12:45:15650 std::string buf;
Paul Lawrence2cd93cc2017-01-17 17:50:18651 if (!android::base::ReadFileToString(g_traceFolder + k_ftraceFilterPath, &buf)) {
Stephane Gasparinid8419c22016-03-02 12:45:15652 fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath,
Jamie Gennise9b8cfb2013-03-12 23:00:10653 strerror(errno), errno);
Stephane Gasparinid8419c22016-03-02 12:45:15654 return false;
Jamie Gennise9b8cfb2013-03-12 23:00:10655 }
656
Stephane Gasparinid8419c22016-03-02 12:45:15657 String8 funcList = String8::format("\n%s",buf.c_str());
Jamie Gennise9b8cfb2013-03-12 23:00:10658
659 // Make sure that every function listed in funcs is in the list we just
Thomas Buhota2c22872016-01-27 08:44:31660 // read from the kernel, except for wildcard inputs.
Jamie Gennise9b8cfb2013-03-12 23:00:10661 bool ok = true;
662 char* myFuncs = strdup(funcs);
663 char* func = strtok(myFuncs, ",");
664 while (func) {
Thomas Buhota2c22872016-01-27 08:44:31665 if (!strchr(func, '*')) {
666 String8 fancyFunc = String8::format("\n%s\n", func);
667 bool found = funcList.find(fancyFunc.string(), 0) >= 0;
668 if (!found || func[0] == '\0') {
669 fprintf(stderr, "error: \"%s\" is not a valid kernel function "
670 "to trace.\n", func);
671 ok = false;
672 }
Jamie Gennise9b8cfb2013-03-12 23:00:10673 }
Yi Kong19d5c002018-07-20 20:39:55674 func = strtok(nullptr, ",");
Jamie Gennise9b8cfb2013-03-12 23:00:10675 }
676 free(myFuncs);
Jamie Gennise9b8cfb2013-03-12 23:00:10677 return ok;
678}
679
680// Set the comma separated list of functions that the kernel is to trace.
681static bool setKernelTraceFuncs(const char* funcs)
682{
683 bool ok = true;
684
Yi Kong19d5c002018-07-20 20:39:55685 if (funcs == nullptr || funcs[0] == '\0') {
Jamie Gennise9b8cfb2013-03-12 23:00:10686 // Disable kernel function tracing.
Jamie Gennis6f6f3f72013-03-27 22:50:30687 if (fileIsWritable(k_currentTracerPath)) {
688 ok &= writeStr(k_currentTracerPath, "nop");
689 }
690 if (fileIsWritable(k_ftraceFilterPath)) {
Jamie Gennise9b8cfb2013-03-12 23:00:10691 ok &= truncateFile(k_ftraceFilterPath);
692 }
693 } else {
694 // Enable kernel function tracing.
695 ok &= writeStr(k_currentTracerPath, "function_graph");
696 ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true);
697 ok &= setKernelOptionEnable(k_funcgraphCpuPath, true);
698 ok &= setKernelOptionEnable(k_funcgraphProcPath, true);
699 ok &= setKernelOptionEnable(k_funcgraphFlatPath, true);
700
701 // Set the requested filter functions.
702 ok &= truncateFile(k_ftraceFilterPath);
703 char* myFuncs = strdup(funcs);
704 char* func = strtok(myFuncs, ",");
705 while (func) {
706 ok &= appendStr(k_ftraceFilterPath, func);
Yi Kong19d5c002018-07-20 20:39:55707 func = strtok(nullptr, ",");
Jamie Gennise9b8cfb2013-03-12 23:00:10708 }
709 free(myFuncs);
710
711 // Verify that the set functions are being traced.
712 if (ok) {
713 ok &= verifyKernelTraceFuncs(funcs);
714 }
715 }
716
717 return ok;
718}
719
Wei Wang16a63a42018-09-21 22:47:45720static bool setCategoryEnable(const char* name)
Yasuhiro Matsuda46c51fb2015-06-29 10:20:39721{
Wei Wang16a63a42018-09-21 22:47:45722 bool vendor_found = false;
723 for (auto &c : g_vendorCategories) {
724 if (strcmp(name, c.name.c_str()) == 0) {
725 c.enabled = true;
726 vendor_found = true;
727 }
728 }
Elliott Hughes5fd6ff62017-03-28 21:55:31729 for (size_t i = 0; i < arraysize(k_categories); i++) {
Yasuhiro Matsuda46c51fb2015-06-29 10:20:39730 const TracingCategory& c = k_categories[i];
731 if (strcmp(name, c.name) == 0) {
732 if (isCategorySupported(c)) {
Wei Wang16a63a42018-09-21 22:47:45733 g_categoryEnables[i] = true;
Yasuhiro Matsuda46c51fb2015-06-29 10:20:39734 return true;
735 } else {
736 if (isCategorySupportedForRoot(c)) {
737 fprintf(stderr, "error: category \"%s\" requires root "
738 "privileges.\n", name);
739 } else {
740 fprintf(stderr, "error: category \"%s\" is not supported "
741 "on this device.\n", name);
742 }
743 return false;
744 }
745 }
746 }
Wei Wang16a63a42018-09-21 22:47:45747 if (vendor_found) {
748 return true;
749 }
Yasuhiro Matsuda46c51fb2015-06-29 10:20:39750 fprintf(stderr, "error: unknown tracing category \"%s\"\n", name);
751 return false;
752}
753
754static bool setCategoriesEnableFromFile(const char* categories_file)
755{
756 if (!categories_file) {
757 return true;
758 }
Yi Kong19d5c002018-07-20 20:39:55759 Tokenizer* tokenizer = nullptr;
Yasuhiro Matsuda46c51fb2015-06-29 10:20:39760 if (Tokenizer::open(String8(categories_file), &tokenizer) != NO_ERROR) {
761 return false;
762 }
763 bool ok = true;
764 while (!tokenizer->isEol()) {
765 String8 token = tokenizer->nextToken(" ");
766 if (token.isEmpty()) {
767 tokenizer->skipDelimiters(" ");
768 continue;
769 }
Wei Wang16a63a42018-09-21 22:47:45770 ok &= setCategoryEnable(token.string());
Yasuhiro Matsuda46c51fb2015-06-29 10:20:39771 }
772 delete tokenizer;
773 return ok;
774}
775
Hector Dearman11845782018-03-08 14:35:44776static bool setUpUserspaceTracing()
Jamie Gennis6eea6fb2012-12-07 22:03:07777{
778 bool ok = true;
779
Jamie Gennis6eea6fb2012-12-07 22:03:07780 // Set up the tags property.
781 uint64_t tags = 0;
Elliott Hughes5fd6ff62017-03-28 21:55:31782 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 22:03:07783 if (g_categoryEnables[i]) {
784 const TracingCategory &c = k_categories[i];
785 tags |= c.tags;
786 }
787 }
sergeyvdb404152016-05-03 02:26:07788
789 bool coreServicesTagEnabled = false;
Elliott Hughes5fd6ff62017-03-28 21:55:31790 for (size_t i = 0; i < arraysize(k_categories); i++) {
sergeyvdb404152016-05-03 02:26:07791 if (strcmp(k_categories[i].name, k_coreServiceCategory) == 0) {
792 coreServicesTagEnabled = g_categoryEnables[i];
793 }
Corey Tabakaa6c0a722017-05-31 23:37:40794
795 // Set whether to poke PDX services in this session.
796 if (strcmp(k_categories[i].name, k_pdxServiceCategory) == 0) {
797 g_tracePdx = g_categoryEnables[i];
798 }
sergeyvdb404152016-05-03 02:26:07799 }
800
801 std::string packageList(g_debugAppCmdLine);
802 if (coreServicesTagEnabled) {
sergeyvdb404152016-05-03 02:26:07803 if (!packageList.empty()) {
804 packageList += ",";
805 }
Elliott Hughes5fd6ff62017-03-28 21:55:31806 packageList += android::base::GetProperty(k_coreServicesProp, "");
sergeyvdb404152016-05-03 02:26:07807 }
Dan Austin09a79872016-05-31 20:27:03808 ok &= setAppCmdlineProperty(&packageList[0]);
Florian Mayer6263b582019-12-10 13:28:45809 ok &= setTagsProperty(tags);
Corey Tabakaa6c0a722017-05-31 23:37:40810 if (g_tracePdx) {
811 ok &= ServiceUtility::PokeServices();
812 }
813
Hector Dearman11845782018-03-08 14:35:44814 return ok;
815}
816
817static void cleanUpUserspaceTracing()
818{
819 setTagsProperty(0);
820 clearAppProperties();
Hector Dearman11845782018-03-08 14:35:44821
822 if (g_tracePdx) {
823 ServiceUtility::PokeServices();
824 }
825}
826
827
828// Set all the kernel tracing settings to the desired state for this trace
829// capture.
830static bool setUpKernelTracing()
831{
832 bool ok = true;
833
Carmen Jackson65ecfbb2018-05-22 17:29:47834 ok &= setUserInitiatedTraceProperty(true);
835
Hector Dearman11845782018-03-08 14:35:44836 // Set up the tracing options.
837 ok &= setCategoriesEnableFromFile(g_categoriesFile);
838 ok &= setTraceOverwriteEnable(g_traceOverwrite);
839 ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
840 // TODO: Re-enable after stabilization
841 //ok &= setCmdlineSize();
842 ok &= setClock();
843 ok &= setPrintTgidEnableIfPresent(true);
844 ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
845
Jamie Gennis6eea6fb2012-12-07 22:03:07846 // Disable all the sysfs enables. This is done as a separate loop from
847 // the enables to allow the same enable to exist in multiple categories.
848 ok &= disableKernelTraceEvents();
849
850 // Enable all the sysfs enables that are in an enabled category.
Elliott Hughes5fd6ff62017-03-28 21:55:31851 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 22:03:07852 if (g_categoryEnables[i]) {
853 const TracingCategory &c = k_categories[i];
854 for (int j = 0; j < MAX_SYS_FILES; j++) {
855 const char* path = c.sysfiles[j].path;
856 bool required = c.sysfiles[j].required == REQ;
Yi Kong19d5c002018-07-20 20:39:55857 if (path != nullptr) {
Jamie Gennis6eea6fb2012-12-07 22:03:07858 if (fileIsWritable(path)) {
859 ok &= setKernelOptionEnable(path, true);
860 } else if (required) {
861 fprintf(stderr, "error writing file %s\n", path);
862 ok = false;
863 }
864 }
865 }
866 }
867 }
868
Jamie Gennis6eea6fb2012-12-07 22:03:07869 return ok;
870}
871
Jamie Gennise9b8cfb2013-03-12 23:00:10872// Reset all the kernel tracing settings to their default state.
Hector Dearman11845782018-03-08 14:35:44873static void cleanUpKernelTracing()
Jamie Gennis6eea6fb2012-12-07 22:03:07874{
Jamie Gennis6eea6fb2012-12-07 22:03:07875 // Disable all tracing that we're able to.
876 disableKernelTraceEvents();
877
Jamie Gennis6eea6fb2012-12-07 22:03:07878 // Set the options back to their defaults.
879 setTraceOverwriteEnable(true);
Jamie Gennise9b8cfb2013-03-12 23:00:10880 setTraceBufferSizeKB(1);
Jamie Gennise9b8cfb2013-03-12 23:00:10881 setPrintTgidEnableIfPresent(false);
Yi Kong19d5c002018-07-20 20:39:55882 setKernelTraceFuncs(nullptr);
Carmen Jackson65ecfbb2018-05-22 17:29:47883 setUserInitiatedTraceProperty(false);
Jamie Gennise9b8cfb2013-03-12 23:00:10884}
Jamie Gennis6eea6fb2012-12-07 22:03:07885
Jamie Gennise9b8cfb2013-03-12 23:00:10886// Enable tracing in the kernel.
887static bool startTrace()
888{
889 return setTracingEnabled(true);
890}
891
892// Disable tracing in the kernel.
893static void stopTrace()
894{
895 setTracingEnabled(false);
Jamie Gennis6eea6fb2012-12-07 22:03:07896}
897
Martijn Coenend9535872015-11-26 09:00:55898// Read data from the tracing pipe and forward to stdout
899static void streamTrace()
900{
901 char trace_data[4096];
Paul Lawrence2cd93cc2017-01-17 17:50:18902 int traceFD = open((g_traceFolder + k_traceStreamPath).c_str(), O_RDWR);
Martijn Coenend9535872015-11-26 09:00:55903 if (traceFD == -1) {
904 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceStreamPath,
905 strerror(errno), errno);
906 return;
907 }
908 while (!g_traceAborted) {
909 ssize_t bytes_read = read(traceFD, trace_data, 4096);
910 if (bytes_read > 0) {
911 write(STDOUT_FILENO, trace_data, bytes_read);
912 fflush(stdout);
913 } else {
914 if (!g_traceAborted) {
915 fprintf(stderr, "read returned %zd bytes err %d (%s)\n",
916 bytes_read, errno, strerror(errno));
917 }
918 break;
919 }
920 }
921}
922
Jamie Gennis6eea6fb2012-12-07 22:03:07923// Read the current kernel trace and write it to stdout.
John Reck40b26b42016-03-30 16:44:36924static void dumpTrace(int outFd)
Jamie Gennis6eea6fb2012-12-07 22:03:07925{
John Reck6c8ac922016-03-28 18:25:30926 ALOGI("Dumping trace");
Paul Lawrence2cd93cc2017-01-17 17:50:18927 int traceFD = open((g_traceFolder + k_tracePath).c_str(), O_RDWR);
Jamie Gennis6eea6fb2012-12-07 22:03:07928 if (traceFD == -1) {
929 fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath,
930 strerror(errno), errno);
931 return;
932 }
933
934 if (g_compress) {
935 z_stream zs;
Elliott Hughes3da5d232015-01-25 16:35:20936 memset(&zs, 0, sizeof(zs));
Elliott Hughesa252f4d2016-07-22 00:12:15937
938 int result = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
Jamie Gennis6eea6fb2012-12-07 22:03:07939 if (result != Z_OK) {
940 fprintf(stderr, "error initializing zlib: %d\n", result);
941 close(traceFD);
942 return;
943 }
944
Elliott Hughesa252f4d2016-07-22 00:12:15945 constexpr size_t bufSize = 64*1024;
946 std::unique_ptr<uint8_t> in(new uint8_t[bufSize]);
947 std::unique_ptr<uint8_t> out(new uint8_t[bufSize]);
948 if (!in || !out) {
949 fprintf(stderr, "couldn't allocate buffers\n");
950 close(traceFD);
951 return;
952 }
Jamie Gennis6eea6fb2012-12-07 22:03:07953
Elliott Hughesa252f4d2016-07-22 00:12:15954 int flush = Z_NO_FLUSH;
955
956 zs.next_out = reinterpret_cast<Bytef*>(out.get());
Jamie Gennis6eea6fb2012-12-07 22:03:07957 zs.avail_out = bufSize;
958
959 do {
960
961 if (zs.avail_in == 0) {
962 // More input is needed.
Elliott Hughesa252f4d2016-07-22 00:12:15963 result = read(traceFD, in.get(), bufSize);
Jamie Gennis6eea6fb2012-12-07 22:03:07964 if (result < 0) {
965 fprintf(stderr, "error reading trace: %s (%d)\n",
966 strerror(errno), errno);
967 result = Z_STREAM_END;
968 break;
969 } else if (result == 0) {
970 flush = Z_FINISH;
971 } else {
Elliott Hughesa252f4d2016-07-22 00:12:15972 zs.next_in = reinterpret_cast<Bytef*>(in.get());
Jamie Gennis6eea6fb2012-12-07 22:03:07973 zs.avail_in = result;
974 }
975 }
976
977 if (zs.avail_out == 0) {
978 // Need to write the output.
Elliott Hughesb59e2962016-07-22 16:00:59979 result = write(outFd, out.get(), bufSize);
Jamie Gennis6eea6fb2012-12-07 22:03:07980 if ((size_t)result < bufSize) {
981 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
982 strerror(errno), errno);
983 result = Z_STREAM_END; // skip deflate error message
984 zs.avail_out = bufSize; // skip the final write
985 break;
986 }
Elliott Hughesa252f4d2016-07-22 00:12:15987 zs.next_out = reinterpret_cast<Bytef*>(out.get());
Jamie Gennis6eea6fb2012-12-07 22:03:07988 zs.avail_out = bufSize;
989 }
990
991 } while ((result = deflate(&zs, flush)) == Z_OK);
992
993 if (result != Z_STREAM_END) {
994 fprintf(stderr, "error deflating trace: %s\n", zs.msg);
995 }
996
997 if (zs.avail_out < bufSize) {
998 size_t bytes = bufSize - zs.avail_out;
Elliott Hughesb59e2962016-07-22 16:00:59999 result = write(outFd, out.get(), bytes);
Jamie Gennis6eea6fb2012-12-07 22:03:071000 if ((size_t)result < bytes) {
1001 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
1002 strerror(errno), errno);
1003 }
1004 }
1005
1006 result = deflateEnd(&zs);
1007 if (result != Z_OK) {
1008 fprintf(stderr, "error cleaning up zlib: %d\n", result);
1009 }
Jamie Gennis6eea6fb2012-12-07 22:03:071010 } else {
Josh Gaod3d36e72017-04-11 22:21:131011 char buf[4096];
1012 ssize_t rc;
1013 while ((rc = TEMP_FAILURE_RETRY(read(traceFD, buf, sizeof(buf)))) > 0) {
1014 if (!android::base::WriteFully(outFd, buf, rc)) {
1015 fprintf(stderr, "error writing trace: %s\n", strerror(errno));
1016 break;
1017 }
1018 }
1019 if (rc == -1) {
1020 fprintf(stderr, "error dumping trace: %s\n", strerror(errno));
Jamie Gennis6eea6fb2012-12-07 22:03:071021 }
1022 }
1023
1024 close(traceFD);
1025}
1026
Mark Salyzyn92dc3fc2014-03-12 20:12:441027static void handleSignal(int /*signo*/)
Jamie Gennis6eea6fb2012-12-07 22:03:071028{
1029 if (!g_nohup) {
1030 g_traceAborted = true;
1031 }
1032}
1033
1034static void registerSigHandler()
1035{
1036 struct sigaction sa;
1037 sigemptyset(&sa.sa_mask);
1038 sa.sa_flags = 0;
1039 sa.sa_handler = handleSignal;
Yi Kong19d5c002018-07-20 20:39:551040 sigaction(SIGHUP, &sa, nullptr);
1041 sigaction(SIGINT, &sa, nullptr);
1042 sigaction(SIGQUIT, &sa, nullptr);
1043 sigaction(SIGTERM, &sa, nullptr);
Jamie Gennis6eea6fb2012-12-07 22:03:071044}
1045
Jamie Gennis6eea6fb2012-12-07 22:03:071046static void listSupportedCategories()
1047{
Elliott Hughes5fd6ff62017-03-28 21:55:311048 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 22:03:071049 const TracingCategory& c = k_categories[i];
1050 if (isCategorySupported(c)) {
1051 printf(" %10s - %s\n", c.name, c.longname);
1052 }
1053 }
Wei Wang16a63a42018-09-21 22:47:451054 for (const auto &c : g_vendorCategories) {
1055 printf(" %10s - %s (HAL)\n", c.name.c_str(), c.description.c_str());
1056 }
Jamie Gennis6eea6fb2012-12-07 22:03:071057}
1058
1059// Print the command usage help to stderr.
1060static void showHelp(const char *cmd)
1061{
1062 fprintf(stderr, "usage: %s [options] [categories...]\n", cmd);
1063 fprintf(stderr, "options include:\n"
Jamie Gennisf7f29c82013-03-27 22:50:581064 " -a appname enable app-level tracing for a comma "
Daniel Colascione519a0792018-02-10 04:05:391065 "separated list of cmdlines; * is a wildcard matching any process\n"
Jamie Gennis6eea6fb2012-12-07 22:03:071066 " -b N use a trace buffer size of N KB\n"
1067 " -c trace into a circular buffer\n"
Yasuhiro Matsuda46c51fb2015-06-29 10:20:391068 " -f filename use the categories written in a file as space-separated\n"
1069 " values in a line\n"
Jamie Gennise9b8cfb2013-03-12 23:00:101070 " -k fname,... trace the listed kernel functions\n"
Jamie Gennis6eea6fb2012-12-07 22:03:071071 " -n ignore signals\n"
1072 " -s N sleep for N seconds before tracing [default 0]\n"
Fabien Sanglardb5c95472016-06-08 18:40:121073 " -t N trace for N seconds [default 5]\n"
Jamie Gennis6eea6fb2012-12-07 22:03:071074 " -z compress the trace dump\n"
Fabien Sanglardb5c95472016-06-08 18:40:121075 " --async_start start circular trace and return immediately\n"
Jamie Gennis6eea6fb2012-12-07 22:03:071076 " --async_dump dump the current contents of circular trace buffer\n"
1077 " --async_stop stop tracing and dump the current contents of circular\n"
1078 " trace buffer\n"
Martijn Coenend9535872015-11-26 09:00:551079 " --stream stream trace to stdout as it enters the trace buffer\n"
1080 " Note: this can take significant CPU time, and is best\n"
1081 " used for measuring things that are not affected by\n"
1082 " CPU performance, like pagecache usage.\n"
Jamie Gennis92573f12012-12-08 00:29:031083 " --list_categories\n"
1084 " list the available tracing categories\n"
John Reck40b26b42016-03-30 16:44:361085 " -o filename write the trace to the specified file instead\n"
1086 " of stdout.\n"
Jamie Gennis6eea6fb2012-12-07 22:03:071087 );
1088}
1089
Paul Lawrence2cd93cc2017-01-17 17:50:181090bool findTraceFiles()
1091{
1092 static const std::string debugfs_path = "/sys/kernel/debug/tracing/";
1093 static const std::string tracefs_path = "/sys/kernel/tracing/";
1094 static const std::string trace_file = "trace_marker";
1095
1096 bool tracefs = access((tracefs_path + trace_file).c_str(), F_OK) != -1;
1097 bool debugfs = access((debugfs_path + trace_file).c_str(), F_OK) != -1;
1098
1099 if (!tracefs && !debugfs) {
1100 fprintf(stderr, "Error: Did not find trace folder\n");
1101 return false;
1102 }
1103
1104 if (tracefs) {
1105 g_traceFolder = tracefs_path;
1106 } else {
1107 g_traceFolder = debugfs_path;
1108 }
1109
1110 return true;
1111}
1112
Wei Wang16a63a42018-09-21 22:47:451113void initVendorCategories()
1114{
1115 g_atraceHal = IAtraceDevice::getService();
1116
1117 if (g_atraceHal == nullptr) {
1118 // No atrace HAL
1119 return;
1120 }
1121
1122 Return<void> ret = g_atraceHal->listCategories(
1123 [](const auto& list) {
1124 g_vendorCategories.reserve(list.size());
1125 for (const auto& category : list) {
1126 g_vendorCategories.emplace_back(category.name, category.description, false);
1127 }
1128 });
1129 if (!ret.isOk()) {
1130 fprintf(stderr, "calling atrace HAL failed: %s\n", ret.description().c_str());
1131 }
1132}
1133
1134static bool setUpVendorTracing()
1135{
1136 if (g_atraceHal == nullptr) {
1137 // No atrace HAL
1138 return true;
1139 }
1140
1141 std::vector<hidl_string> categories;
1142 for (const auto &c : g_vendorCategories) {
1143 if (c.enabled) {
1144 categories.emplace_back(c.name);
1145 }
1146 }
1147
1148 if (!categories.size()) {
1149 return true;
1150 }
1151
1152 auto ret = g_atraceHal->enableCategories(categories);
1153 if (!ret.isOk()) {
1154 fprintf(stderr, "calling atrace HAL failed: %s\n", ret.description().c_str());
1155 return false;
1156 } else if (ret != Status::SUCCESS) {
1157 fprintf(stderr, "calling atrace HAL failed: %s\n", toString(ret).c_str());
1158 return false;
1159 }
1160 return true;
1161}
1162
1163static bool cleanUpVendorTracing()
1164{
1165 if (g_atraceHal == nullptr) {
1166 // No atrace HAL
1167 return true;
1168 }
1169
1170 if (!g_vendorCategories.size()) {
1171 // No vendor categories
1172 return true;
1173 }
1174
1175 auto ret = g_atraceHal->disableAllCategories();
1176 if (!ret.isOk()) {
1177 fprintf(stderr, "calling atrace HAL failed: %s\n", ret.description().c_str());
1178 return false;
1179 } else if (ret != Status::SUCCESS) {
1180 fprintf(stderr, "calling atrace HAL failed: %s\n", toString(ret).c_str());
1181 return false;
1182 }
1183 return true;
1184}
1185
Jamie Gennis6eea6fb2012-12-07 22:03:071186int main(int argc, char **argv)
1187{
1188 bool async = false;
1189 bool traceStart = true;
1190 bool traceStop = true;
1191 bool traceDump = true;
Martijn Coenend9535872015-11-26 09:00:551192 bool traceStream = false;
Hector Dearman11845782018-03-08 14:35:441193 bool onlyUserspace = false;
Jamie Gennis6eea6fb2012-12-07 22:03:071194
1195 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
1196 showHelp(argv[0]);
1197 exit(0);
1198 }
1199
Paul Lawrence2cd93cc2017-01-17 17:50:181200 if (!findTraceFiles()) {
1201 fprintf(stderr, "No trace folder found\n");
1202 exit(-1);
1203 }
1204
Wei Wang16a63a42018-09-21 22:47:451205 initVendorCategories();
1206
Jamie Gennis6eea6fb2012-12-07 22:03:071207 for (;;) {
1208 int ret;
1209 int option_index = 0;
1210 static struct option long_options[] = {
Yi Kong19d5c002018-07-20 20:39:551211 {"async_start", no_argument, nullptr, 0 },
1212 {"async_stop", no_argument, nullptr, 0 },
1213 {"async_dump", no_argument, nullptr, 0 },
1214 {"only_userspace", no_argument, nullptr, 0 },
1215 {"list_categories", no_argument, nullptr, 0 },
1216 {"stream", no_argument, nullptr, 0 },
1217 {nullptr, 0, nullptr, 0 }
Jamie Gennis6eea6fb2012-12-07 22:03:071218 };
1219
John Reck40b26b42016-03-30 16:44:361220 ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:zo:",
Jamie Gennis6eea6fb2012-12-07 22:03:071221 long_options, &option_index);
1222
1223 if (ret < 0) {
1224 for (int i = optind; i < argc; i++) {
Wei Wang16a63a42018-09-21 22:47:451225 if (!setCategoryEnable(argv[i])) {
Jamie Gennis6eea6fb2012-12-07 22:03:071226 fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]);
1227 exit(1);
1228 }
1229 }
1230 break;
1231 }
1232
1233 switch(ret) {
Jamie Gennisf7f29c82013-03-27 22:50:581234 case 'a':
1235 g_debugAppCmdLine = optarg;
1236 break;
1237
Jamie Gennis6eea6fb2012-12-07 22:03:071238 case 'b':
1239 g_traceBufferSizeKB = atoi(optarg);
1240 break;
1241
1242 case 'c':
1243 g_traceOverwrite = true;
1244 break;
1245
Yasuhiro Matsuda46c51fb2015-06-29 10:20:391246 case 'f':
1247 g_categoriesFile = optarg;
1248 break;
1249
Jamie Gennise9b8cfb2013-03-12 23:00:101250 case 'k':
1251 g_kernelTraceFuncs = optarg;
Jamie Gennis6f6f3f72013-03-27 22:50:301252 break;
Jamie Gennise9b8cfb2013-03-12 23:00:101253
Jamie Gennis6eea6fb2012-12-07 22:03:071254 case 'n':
1255 g_nohup = true;
Jamie Gennis6f6f3f72013-03-27 22:50:301256 break;
Jamie Gennis6eea6fb2012-12-07 22:03:071257
1258 case 's':
1259 g_initialSleepSecs = atoi(optarg);
1260 break;
1261
1262 case 't':
1263 g_traceDurationSeconds = atoi(optarg);
1264 break;
1265
1266 case 'z':
1267 g_compress = true;
1268 break;
1269
John Reck40b26b42016-03-30 16:44:361270 case 'o':
1271 g_outputFile = optarg;
1272 break;
1273
Jamie Gennis6eea6fb2012-12-07 22:03:071274 case 0:
1275 if (!strcmp(long_options[option_index].name, "async_start")) {
1276 async = true;
1277 traceStop = false;
1278 traceDump = false;
1279 g_traceOverwrite = true;
1280 } else if (!strcmp(long_options[option_index].name, "async_stop")) {
1281 async = true;
John Reck4ba2b632015-05-15 17:00:341282 traceStart = false;
Jamie Gennis6eea6fb2012-12-07 22:03:071283 } else if (!strcmp(long_options[option_index].name, "async_dump")) {
1284 async = true;
1285 traceStart = false;
1286 traceStop = false;
Hector Dearman11845782018-03-08 14:35:441287 } else if (!strcmp(long_options[option_index].name, "only_userspace")) {
1288 onlyUserspace = true;
Martijn Coenend9535872015-11-26 09:00:551289 } else if (!strcmp(long_options[option_index].name, "stream")) {
1290 traceStream = true;
1291 traceDump = false;
Jamie Gennis6eea6fb2012-12-07 22:03:071292 } else if (!strcmp(long_options[option_index].name, "list_categories")) {
1293 listSupportedCategories();
1294 exit(0);
1295 }
Jamie Gennis6f6f3f72013-03-27 22:50:301296 break;
Jamie Gennis6eea6fb2012-12-07 22:03:071297
1298 default:
1299 fprintf(stderr, "\n");
1300 showHelp(argv[0]);
1301 exit(-1);
1302 break;
1303 }
1304 }
1305
Hector Dearman11845782018-03-08 14:35:441306 if (onlyUserspace) {
1307 if (!async || !(traceStart || traceStop)) {
1308 fprintf(stderr, "--only_userspace can only be used with "
1309 "--async_start or --async_stop\n");
1310 exit(1);
1311 }
1312 }
1313
Jamie Gennis6eea6fb2012-12-07 22:03:071314 registerSigHandler();
1315
1316 if (g_initialSleepSecs > 0) {
1317 sleep(g_initialSleepSecs);
1318 }
1319
Jamie Gennise9b8cfb2013-03-12 23:00:101320 bool ok = true;
Hector Dearman11845782018-03-08 14:35:441321
Wei Wang5b73b742018-03-08 21:33:121322 if (traceStart) {
Hector Dearman11845782018-03-08 14:35:441323 ok &= setUpUserspaceTracing();
1324 }
1325
1326 if (ok && traceStart && !onlyUserspace) {
1327 ok &= setUpKernelTracing();
Wei Wang16a63a42018-09-21 22:47:451328 ok &= setUpVendorTracing();
Wei Wang5b73b742018-03-08 21:33:121329 ok &= startTrace();
1330 }
Jamie Gennis6eea6fb2012-12-07 22:03:071331
1332 if (ok && traceStart) {
Hector Dearman11845782018-03-08 14:35:441333
1334 if (!traceStream && !onlyUserspace) {
Carmen Jacksonac53e732017-05-02 23:55:331335 printf("capturing trace...");
Martijn Coenend9535872015-11-26 09:00:551336 fflush(stdout);
1337 }
Jamie Gennis6eea6fb2012-12-07 22:03:071338
1339 // We clear the trace after starting it because tracing gets enabled for
1340 // each CPU individually in the kernel. Having the beginning of the trace
1341 // contain entries from only one CPU can cause "begin" entries without a
1342 // matching "end" entry to show up if a task gets migrated from one CPU to
1343 // another.
Hector Dearman11845782018-03-08 14:35:441344 if (!onlyUserspace)
1345 ok = clearTrace();
Jamie Gennis6eea6fb2012-12-07 22:03:071346
Martijn Coenen0bcd97a2015-07-15 12:25:231347 writeClockSyncMarker();
Martijn Coenend9535872015-11-26 09:00:551348 if (ok && !async && !traceStream) {
Jamie Gennis6eea6fb2012-12-07 22:03:071349 // Sleep to allow the trace to be captured.
1350 struct timespec timeLeft;
1351 timeLeft.tv_sec = g_traceDurationSeconds;
1352 timeLeft.tv_nsec = 0;
1353 do {
1354 if (g_traceAborted) {
1355 break;
1356 }
1357 } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR);
1358 }
Martijn Coenend9535872015-11-26 09:00:551359
1360 if (traceStream) {
1361 streamTrace();
1362 }
Jamie Gennis6eea6fb2012-12-07 22:03:071363 }
1364
1365 // Stop the trace and restore the default settings.
Hector Dearman11845782018-03-08 14:35:441366 if (traceStop && !onlyUserspace)
Jamie Gennis6eea6fb2012-12-07 22:03:071367 stopTrace();
1368
Hector Dearman11845782018-03-08 14:35:441369 if (ok && traceDump && !onlyUserspace) {
Jamie Gennis6eea6fb2012-12-07 22:03:071370 if (!g_traceAborted) {
John Reck40b26b42016-03-30 16:44:361371 printf(" done\n");
Jamie Gennis6eea6fb2012-12-07 22:03:071372 fflush(stdout);
John Reck40b26b42016-03-30 16:44:361373 int outFd = STDOUT_FILENO;
1374 if (g_outputFile) {
Martijn Coenenc5791982017-02-22 08:25:311375 outFd = open(g_outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
John Reck40b26b42016-03-30 16:44:361376 }
1377 if (outFd == -1) {
1378 printf("Failed to open '%s', err=%d", g_outputFile, errno);
1379 } else {
1380 dprintf(outFd, "TRACE:\n");
1381 dumpTrace(outFd);
1382 if (g_outputFile) {
1383 close(outFd);
1384 }
1385 }
Jamie Gennis6eea6fb2012-12-07 22:03:071386 } else {
1387 printf("\ntrace aborted.\n");
1388 fflush(stdout);
1389 }
1390 clearTrace();
1391 } else if (!ok) {
1392 fprintf(stderr, "unable to start tracing\n");
1393 }
1394
1395 // Reset the trace buffer size to 1.
Hector Dearman11845782018-03-08 14:35:441396 if (traceStop) {
1397 cleanUpUserspaceTracing();
Hector Dearmanada0a4a2019-04-09 13:13:141398 if (!onlyUserspace) {
1399 cleanUpVendorTracing();
Hector Dearman11845782018-03-08 14:35:441400 cleanUpKernelTracing();
Hector Dearmanada0a4a2019-04-09 13:13:141401 }
Hector Dearman11845782018-03-08 14:35:441402 }
Jamie Gennis6eea6fb2012-12-07 22:03:071403
1404 return g_traceAborted ? 1 : 0;
1405}