blob: f65f4f82cff13f4d63511aa227c09ca105942cf0 [file] [log] [blame]
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001/*
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 09:50:18 -080017#define LOG_TAG "atrace"
John Reck40b26b42016-03-30 09:44:36 -070018
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080019#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070022#include <inttypes.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080023#include <signal.h>
24#include <stdarg.h>
25#include <stdbool.h>
26#include <stdio.h>
27#include <stdlib.h>
Elliott Hughes3da5d232015-01-25 08:35:20 -080028#include <string.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080029#include <time.h>
Martijn Coenend9535872015-11-26 10:00:55 +010030#include <unistd.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080031#include <zlib.h>
32
Carmen Jacksonea826792017-05-05 11:42:32 -070033#include <fstream>
Elliott Hughesa252f4d2016-07-21 17:12:15 -070034#include <memory>
35
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080036#include <binder/IBinder.h>
37#include <binder/IServiceManager.h>
38#include <binder/Parcel.h>
39
Martijn Coenenee9b97e2016-11-16 16:00:26 +010040#include <android/hidl/manager/1.0/IServiceManager.h>
41#include <hidl/ServiceManagement.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080042
Corey Tabakaa6c0a722017-05-31 16:37:40 -070043#include <pdx/default_transport/service_utility.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080044#include <utils/String8.h>
John Reck469a1942015-03-26 15:31:35 -070045#include <utils/Timers.h>
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +090046#include <utils/Tokenizer.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080047#include <utils/Trace.h>
Stephane Gasparinid8419c22016-03-02 13:45:15 +010048#include <android-base/file.h>
Elliott Hughes5fd6ff62017-03-28 14:55:31 -070049#include <android-base/macros.h>
50#include <android-base/properties.h>
51#include <android-base/stringprintf.h>
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080052
53using namespace android;
Corey Tabakaa6c0a722017-05-31 16:37:40 -070054using pdx::default_transport::ServiceUtility;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080055
Martijn Coenenee9b97e2016-11-16 16:00:26 +010056using std::string;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080057
sergeyv4144eff2016-04-28 11:40:04 -070058#define MAX_SYS_FILES 10
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080059
60const char* k_traceTagsProperty = "debug.atrace.tags.enableflags";
sergeyv4144eff2016-04-28 11:40:04 -070061
62const char* k_traceAppsNumberProperty = "debug.atrace.app_number";
63const char* k_traceAppsPropertyTemplate = "debug.atrace.app_%d";
sergeyvdb404152016-05-02 19:26:07 -070064const char* k_coreServiceCategory = "core_services";
Corey Tabakaa6c0a722017-05-31 16:37:40 -070065const char* k_pdxServiceCategory = "pdx";
sergeyvdb404152016-05-02 19:26:07 -070066const char* k_coreServicesProp = "ro.atrace.core.services";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080067
68typedef enum { OPT, REQ } requiredness ;
69
70struct TracingCategory {
71 // The name identifying the category.
72 const char* name;
73
74 // A longer description of the category.
75 const char* longname;
76
77 // The userland tracing tags that the category enables.
78 uint64_t tags;
79
80 // The fname==NULL terminated list of /sys/ files that the category
81 // enables.
82 struct {
83 // Whether the file must be writable in order to enable the tracing
84 // category.
85 requiredness required;
86
87 // The path to the enable file.
88 const char* path;
89 } sysfiles[MAX_SYS_FILES];
90};
91
92/* Tracing categories */
93static const TracingCategory k_categories[] = {
John Reck732a29a2017-05-24 16:09:21 -070094 { "gfx", "Graphics", ATRACE_TAG_GRAPHICS, {
95 { OPT, "events/mdss/enable" },
Wei Wange2c10462018-02-13 10:47:23 -080096 { OPT, "events/sde/enable" },
John Reck732a29a2017-05-24 16:09:21 -070097 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -070098 { "input", "Input", ATRACE_TAG_INPUT, { } },
99 { "view", "View System", ATRACE_TAG_VIEW, { } },
100 { "webview", "WebView", ATRACE_TAG_WEBVIEW, { } },
101 { "wm", "Window Manager", ATRACE_TAG_WINDOW_MANAGER, { } },
102 { "am", "Activity Manager", ATRACE_TAG_ACTIVITY_MANAGER, { } },
Patrick Auchter70ec2942014-09-30 15:38:30 -0500103 { "sm", "Sync Manager", ATRACE_TAG_SYNC_MANAGER, { } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700104 { "audio", "Audio", ATRACE_TAG_AUDIO, { } },
105 { "video", "Video", ATRACE_TAG_VIDEO, { } },
106 { "camera", "Camera", ATRACE_TAG_CAMERA, { } },
107 { "hal", "Hardware Modules", ATRACE_TAG_HAL, { } },
Dianne Hackborn9380d782013-04-12 14:52:35 -0700108 { "res", "Resource Loading", ATRACE_TAG_RESOURCES, { } },
Jamie Genniseff2e8d2013-05-07 15:20:39 -0700109 { "dalvik", "Dalvik VM", ATRACE_TAG_DALVIK, { } },
Tim Murrayf0f28412013-05-23 14:39:42 -0700110 { "rs", "RenderScript", ATRACE_TAG_RS, { } },
Brigid Smith750aa972014-05-28 14:23:24 -0700111 { "bionic", "Bionic C Library", ATRACE_TAG_BIONIC, { } },
Jeff Brown3200b0b2014-08-14 19:24:47 -0700112 { "power", "Power Management", ATRACE_TAG_POWER, { } },
Todd Kennedy01e111b2015-07-31 14:36:20 -0700113 { "pm", "Package Manager", ATRACE_TAG_PACKAGE_MANAGER, { } },
Yasuhiro Matsuda7cc49772015-07-01 01:46:25 +0900114 { "ss", "System Server", ATRACE_TAG_SYSTEM_SERVER, { } },
Greg Hackmannbbd7d992014-12-01 14:43:34 -0800115 { "database", "Database", ATRACE_TAG_DATABASE, { } },
Felipe Leme0f97c1d2016-09-07 11:33:26 -0700116 { "network", "Network", ATRACE_TAG_NETWORK, { } },
Josh Gao468b4cb2016-11-29 10:55:21 -0800117 { "adb", "ADB", ATRACE_TAG_ADB, { } },
Martijn Coenen47791e92018-03-09 09:27:32 +0100118 { "vibrator", "Vibrator", ATRACE_TAG_VIBRATOR, { } },
119 { "aidl", "AIDL calls", ATRACE_TAG_AIDL, { } },
sergeyvdb404152016-05-02 19:26:07 -0700120 { k_coreServiceCategory, "Core services", 0, { } },
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700121 { k_pdxServiceCategory, "PDX services", 0, { } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700122 { "sched", "CPU Scheduling", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800123 { REQ, "events/sched/sched_switch/enable" },
124 { REQ, "events/sched/sched_wakeup/enable" },
Joel Fernandesee593e22017-06-04 13:05:59 -0700125 { OPT, "events/sched/sched_waking/enable" },
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800126 { OPT, "events/sched/sched_blocked_reason/enable" },
127 { OPT, "events/sched/sched_cpu_hotplug/enable" },
Joel Fernandes4dfca7c2017-06-15 16:53:52 -0700128 { OPT, "events/cgroup/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800129 } },
Dan Willemsenf440d392014-04-11 15:44:09 -0700130 { "irq", "IRQ Events", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800131 { REQ, "events/irq/enable" },
132 { OPT, "events/ipi/enable" },
Dan Willemsenf440d392014-04-11 15:44:09 -0700133 } },
Joel Fernandes0ad0f302017-08-31 08:35:05 -0700134 { "irqoff", "IRQ-disabled code section tracing", 0, {
135 { REQ, "events/preemptirq/irq_enable/enable" },
136 { REQ, "events/preemptirq/irq_disable/enable" },
137 } },
138 { "preemptoff", "Preempt-disabled code section tracing", 0, {
139 { REQ, "events/preemptirq/preempt_enable/enable" },
140 { REQ, "events/preemptirq/preempt_disable/enable" },
141 } },
Michael Wright43fb6782016-08-18 19:56:43 +0100142 { "i2c", "I2C Events", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800143 { REQ, "events/i2c/enable" },
144 { REQ, "events/i2c/i2c_read/enable" },
145 { REQ, "events/i2c/i2c_write/enable" },
146 { REQ, "events/i2c/i2c_result/enable" },
147 { REQ, "events/i2c/i2c_reply/enable" },
148 { OPT, "events/i2c/smbus_read/enable" },
149 { OPT, "events/i2c/smbus_write/enable" },
150 { OPT, "events/i2c/smbus_result/enable" },
151 { OPT, "events/i2c/smbus_reply/enable" },
Michael Wright43fb6782016-08-18 19:56:43 +0100152 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700153 { "freq", "CPU Frequency", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800154 { REQ, "events/power/cpu_frequency/enable" },
155 { OPT, "events/power/clock_set_rate/enable" },
Kevin DuBoisafc2f972017-11-03 15:44:08 -0700156 { OPT, "events/power/clock_disable/enable" },
157 { OPT, "events/power/clock_enable/enable" },
Wei Wang391efc22018-02-22 11:40:07 -0800158 { OPT, "events/clk/clk_set_rate/enable" },
159 { OPT, "events/clk/clk_disable/enable" },
160 { OPT, "events/clk/clk_enable/enable" },
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800161 { OPT, "events/power/cpu_frequency_limits/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800162 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700163 { "membus", "Memory Bus Utilization", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800164 { REQ, "events/memory_bus/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800165 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700166 { "idle", "CPU Idle", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800167 { REQ, "events/power/cpu_idle/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800168 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700169 { "disk", "Disk I/O", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800170 { OPT, "events/f2fs/f2fs_sync_file_enter/enable" },
171 { OPT, "events/f2fs/f2fs_sync_file_exit/enable" },
172 { OPT, "events/f2fs/f2fs_write_begin/enable" },
173 { OPT, "events/f2fs/f2fs_write_end/enable" },
174 { OPT, "events/ext4/ext4_da_write_begin/enable" },
175 { OPT, "events/ext4/ext4_da_write_end/enable" },
176 { OPT, "events/ext4/ext4_sync_file_enter/enable" },
177 { OPT, "events/ext4/ext4_sync_file_exit/enable" },
178 { REQ, "events/block/block_rq_issue/enable" },
179 { REQ, "events/block/block_rq_complete/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800180 } },
Ken Sumralld3fa5612013-07-03 12:32:03 -0700181 { "mmc", "eMMC commands", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800182 { REQ, "events/mmc/enable" },
Ken Sumralld3fa5612013-07-03 12:32:03 -0700183 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700184 { "load", "CPU Load", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800185 { REQ, "events/cpufreq_interactive/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800186 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700187 { "sync", "Synchronization", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800188 { REQ, "events/sync/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800189 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700190 { "workq", "Kernel Workqueues", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800191 { REQ, "events/workqueue/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800192 } },
Colin Cross580407f2014-08-18 15:22:13 -0700193 { "memreclaim", "Kernel Memory Reclaim", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800194 { REQ, "events/vmscan/mm_vmscan_direct_reclaim_begin/enable" },
195 { REQ, "events/vmscan/mm_vmscan_direct_reclaim_end/enable" },
196 { REQ, "events/vmscan/mm_vmscan_kswapd_wake/enable" },
197 { REQ, "events/vmscan/mm_vmscan_kswapd_sleep/enable" },
Marc Hittingerf1f62e32017-05-17 15:57:43 -0700198 { REQ, "events/lowmemorykiller/enable" },
Colin Cross580407f2014-08-18 15:22:13 -0700199 } },
Aaron Schulmanc2c6ecd2015-02-25 08:37:09 -0800200 { "regulators", "Voltage and Current Regulators", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800201 { REQ, "events/regulator/enable" },
Aaron Schulmanc2c6ecd2015-02-25 08:37:09 -0800202 } },
Scott Bauerae473362015-06-08 16:32:36 -0700203 { "binder_driver", "Binder Kernel driver", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800204 { REQ, "events/binder/binder_transaction/enable" },
205 { REQ, "events/binder/binder_transaction_received/enable" },
Martijn Coenen7f3d7a22017-05-26 09:50:55 -0700206 { OPT, "events/binder/binder_set_priority/enable" },
Scott Bauerae473362015-06-08 16:32:36 -0700207 } },
208 { "binder_lock", "Binder global lock trace", 0, {
Howard Cheneb8acbf2017-05-10 16:32:11 +0800209 { OPT, "events/binder/binder_lock/enable" },
210 { OPT, "events/binder/binder_locked/enable" },
211 { OPT, "events/binder/binder_unlock/enable" },
Scott Bauerae473362015-06-08 16:32:36 -0700212 } },
Martijn Coenen70481612015-10-23 13:57:05 +0200213 { "pagecache", "Page cache", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800214 { REQ, "events/filemap/enable" },
Martijn Coenen70481612015-10-23 13:57:05 +0200215 } },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800216};
217
218/* Command line options */
219static int g_traceDurationSeconds = 5;
220static bool g_traceOverwrite = false;
221static int g_traceBufferSizeKB = 2048;
222static bool g_compress = false;
223static bool g_nohup = false;
224static int g_initialSleepSecs = 0;
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900225static const char* g_categoriesFile = NULL;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700226static const char* g_kernelTraceFuncs = NULL;
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700227static const char* g_debugAppCmdLine = "";
John Reck40b26b42016-03-30 09:44:36 -0700228static const char* g_outputFile = nullptr;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800229
230/* Global state */
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700231static bool g_tracePdx = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800232static bool g_traceAborted = false;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700233static bool g_categoryEnables[arraysize(k_categories)] = {};
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800234static std::string g_traceFolder;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800235
236/* Sys file paths */
237static const char* k_traceClockPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800238 "trace_clock";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800239
240static const char* k_traceBufferSizePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800241 "buffer_size_kb";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800242
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700243#if 0
244// TODO: Re-enable after stabilization
Joel Fernandesed80bd02017-06-02 10:19:28 -0700245static const char* k_traceCmdlineSizePath =
246 "saved_cmdlines_size";
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700247#endif
Joel Fernandesed80bd02017-06-02 10:19:28 -0700248
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800249static const char* k_tracingOverwriteEnablePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800250 "options/overwrite";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800251
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700252static const char* k_currentTracerPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800253 "current_tracer";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700254
255static const char* k_printTgidPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800256 "options/print-tgid";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700257
258static const char* k_funcgraphAbsTimePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800259 "options/funcgraph-abstime";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700260
261static const char* k_funcgraphCpuPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800262 "options/funcgraph-cpu";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700263
264static const char* k_funcgraphProcPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800265 "options/funcgraph-proc";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700266
267static const char* k_funcgraphFlatPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800268 "options/funcgraph-flat";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700269
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700270static const char* k_ftraceFilterPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800271 "set_ftrace_filter";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700272
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800273static const char* k_tracingOnPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800274 "tracing_on";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800275
276static const char* k_tracePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800277 "trace";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800278
Martijn Coenend9535872015-11-26 10:00:55 +0100279static const char* k_traceStreamPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800280 "trace_pipe";
Martijn Coenend9535872015-11-26 10:00:55 +0100281
John Reck469a1942015-03-26 15:31:35 -0700282static const char* k_traceMarkerPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800283 "trace_marker";
John Reck469a1942015-03-26 15:31:35 -0700284
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800285// Check whether a file exists.
286static bool fileExists(const char* filename) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800287 return access((g_traceFolder + filename).c_str(), F_OK) != -1;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800288}
289
290// Check whether a file is writable.
291static bool fileIsWritable(const char* filename) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800292 return access((g_traceFolder + filename).c_str(), W_OK) != -1;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800293}
294
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700295// Truncate a file.
296static bool truncateFile(const char* path)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800297{
Jamie Gennis43122e72013-03-21 14:06:31 -0700298 // This uses creat rather than truncate because some of the debug kernel
299 // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by
300 // calls to truncate, but they are cleared by calls to creat.
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800301 int traceFD = creat((g_traceFolder + path).c_str(), 0);
Jamie Gennis43122e72013-03-21 14:06:31 -0700302 if (traceFD == -1) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800303 fprintf(stderr, "error truncating %s: %s (%d)\n", (g_traceFolder + path).c_str(),
Jamie Gennis43122e72013-03-21 14:06:31 -0700304 strerror(errno), errno);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700305 return false;
306 }
307
Jamie Gennis43122e72013-03-21 14:06:31 -0700308 close(traceFD);
309
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700310 return true;
311}
312
313static bool _writeStr(const char* filename, const char* str, int flags)
314{
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800315 std::string fullFilename = g_traceFolder + filename;
316 int fd = open(fullFilename.c_str(), flags);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800317 if (fd == -1) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800318 fprintf(stderr, "error opening %s: %s (%d)\n", fullFilename.c_str(),
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800319 strerror(errno), errno);
320 return false;
321 }
322
323 bool ok = true;
324 ssize_t len = strlen(str);
325 if (write(fd, str, len) != len) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800326 fprintf(stderr, "error writing to %s: %s (%d)\n", fullFilename.c_str(),
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800327 strerror(errno), errno);
328 ok = false;
329 }
330
331 close(fd);
332
333 return ok;
334}
335
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700336// Write a string to a file, returning true if the write was successful.
337static bool writeStr(const char* filename, const char* str)
338{
339 return _writeStr(filename, str, O_WRONLY);
340}
341
342// Append a string to a file, returning true if the write was successful.
343static bool appendStr(const char* filename, const char* str)
344{
345 return _writeStr(filename, str, O_APPEND|O_WRONLY);
346}
347
John Reck469a1942015-03-26 15:31:35 -0700348static void writeClockSyncMarker()
349{
350 char buffer[128];
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200351 int len = 0;
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800352 int fd = open((g_traceFolder + k_traceMarkerPath).c_str(), O_WRONLY);
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200353 if (fd == -1) {
354 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceMarkerPath,
355 strerror(errno), errno);
356 return;
357 }
John Reck469a1942015-03-26 15:31:35 -0700358 float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f;
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200359
360 len = snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
361 if (write(fd, buffer, len) != len) {
362 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
363 }
364
365 int64_t realtime_in_ms = systemTime(CLOCK_REALTIME) / 1000000;
366 len = snprintf(buffer, 128, "trace_event_clock_sync: realtime_ts=%" PRId64 "\n", realtime_in_ms);
367 if (write(fd, buffer, len) != len) {
368 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
369 }
370
371 close(fd);
John Reck469a1942015-03-26 15:31:35 -0700372}
373
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800374// Enable or disable a kernel option by writing a "1" or a "0" into a /sys
375// file.
376static bool setKernelOptionEnable(const char* filename, bool enable)
377{
378 return writeStr(filename, enable ? "1" : "0");
379}
380
381// Check whether the category is supported on the device with the current
382// rootness. A category is supported only if all its required /sys/ files are
383// writable and if enabling the category will enable one or more tracing tags
384// or /sys/ files.
385static bool isCategorySupported(const TracingCategory& category)
386{
sergeyvdb404152016-05-02 19:26:07 -0700387 if (strcmp(category.name, k_coreServiceCategory) == 0) {
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700388 return !android::base::GetProperty(k_coreServicesProp, "").empty();
sergeyvdb404152016-05-02 19:26:07 -0700389 }
390
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700391 if (strcmp(category.name, k_pdxServiceCategory) == 0) {
392 return true;
393 }
394
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800395 bool ok = category.tags != 0;
396 for (int i = 0; i < MAX_SYS_FILES; i++) {
397 const char* path = category.sysfiles[i].path;
398 bool req = category.sysfiles[i].required == REQ;
399 if (path != NULL) {
400 if (req) {
401 if (!fileIsWritable(path)) {
402 return false;
403 } else {
404 ok = true;
405 }
406 } else {
Howard Cheneb8acbf2017-05-10 16:32:11 +0800407 ok = true;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800408 }
409 }
410 }
411 return ok;
412}
413
414// Check whether the category would be supported on the device if the user
415// were root. This function assumes that root is able to write to any file
416// that exists. It performs the same logic as isCategorySupported, but it
Fabien Sanglardb5c95472016-06-08 11:40:12 -0700417// uses file existence rather than writability in the /sys/ file checks.
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800418static bool isCategorySupportedForRoot(const TracingCategory& category)
419{
420 bool ok = category.tags != 0;
421 for (int i = 0; i < MAX_SYS_FILES; i++) {
422 const char* path = category.sysfiles[i].path;
423 bool req = category.sysfiles[i].required == REQ;
424 if (path != NULL) {
425 if (req) {
426 if (!fileExists(path)) {
427 return false;
428 } else {
429 ok = true;
430 }
431 } else {
432 ok |= fileExists(path);
433 }
434 }
435 }
436 return ok;
437}
438
439// Enable or disable overwriting of the kernel trace buffers. Disabling this
440// will cause tracing to stop once the trace buffers have filled up.
441static bool setTraceOverwriteEnable(bool enable)
442{
443 return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable);
444}
445
446// Enable or disable kernel tracing.
447static bool setTracingEnabled(bool enable)
448{
449 return setKernelOptionEnable(k_tracingOnPath, enable);
450}
451
452// Clear the contents of the kernel trace.
453static bool clearTrace()
454{
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700455 return truncateFile(k_tracePath);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800456}
457
458// Set the size of the kernel's trace buffer in kilobytes.
459static bool setTraceBufferSizeKB(int size)
460{
461 char str[32] = "1";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800462 if (size < 1) {
463 size = 1;
464 }
465 snprintf(str, 32, "%d", size);
466 return writeStr(k_traceBufferSizePath, str);
467}
468
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700469#if 0
470// TODO: Re-enable after stabilization
Joel Fernandesed80bd02017-06-02 10:19:28 -0700471// Set the default size of cmdline hashtable
472static bool setCmdlineSize()
473{
Joel Fernandesce964f22017-06-06 12:20:29 -0700474 if (fileExists(k_traceCmdlineSizePath)) {
475 return writeStr(k_traceCmdlineSizePath, "8192");
476 }
477 return true;
Joel Fernandesed80bd02017-06-02 10:19:28 -0700478}
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700479#endif
Joel Fernandesed80bd02017-06-02 10:19:28 -0700480
Carmen Jacksonea826792017-05-05 11:42:32 -0700481// Set the clock to the best available option while tracing. Use 'boot' if it's
482// available; otherwise, use 'mono'. If neither are available use 'global'.
Colin Crossb1ce49b2014-08-20 14:28:47 -0700483// Any write to the trace_clock sysfs file will reset the buffer, so only
484// update it if the requested value is not the current value.
Carmen Jacksonea826792017-05-05 11:42:32 -0700485static bool setClock()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800486{
Carmen Jacksonea826792017-05-05 11:42:32 -0700487 std::ifstream clockFile((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
488 std::string clockStr((std::istreambuf_iterator<char>(clockFile)),
489 std::istreambuf_iterator<char>());
Colin Crossb1ce49b2014-08-20 14:28:47 -0700490
Carmen Jacksonea826792017-05-05 11:42:32 -0700491 std::string newClock;
492 if (clockStr.find("boot") != std::string::npos) {
493 newClock = "boot";
494 } else if (clockStr.find("mono") != std::string::npos) {
495 newClock = "mono";
496 } else {
497 newClock = "global";
Colin Crossb1ce49b2014-08-20 14:28:47 -0700498 }
499
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700500 size_t begin = clockStr.find('[') + 1;
501 size_t end = clockStr.find(']');
Carmen Jacksonea826792017-05-05 11:42:32 -0700502 if (newClock.compare(0, std::string::npos, clockStr, begin, end-begin) == 0) {
503 return true;
504 }
505 return writeStr(k_traceClockPath, newClock.c_str());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800506}
507
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700508static bool setPrintTgidEnableIfPresent(bool enable)
509{
510 if (fileExists(k_printTgidPath)) {
511 return setKernelOptionEnable(k_printTgidPath, enable);
512 }
513 return true;
514}
515
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800516// Poke all the binder-enabled processes in the system to get them to re-read
517// their system properties.
518static bool pokeBinderServices()
519{
520 sp<IServiceManager> sm = defaultServiceManager();
521 Vector<String16> services = sm->listServices();
522 for (size_t i = 0; i < services.size(); i++) {
523 sp<IBinder> obj = sm->checkService(services[i]);
524 if (obj != NULL) {
525 Parcel data;
526 if (obj->transact(IBinder::SYSPROPS_TRANSACTION, data,
527 NULL, 0) != OK) {
528 if (false) {
529 // XXX: For some reason this fails on tablets trying to
530 // poke the "phone" service. It's not clear whether some
531 // are expected to fail.
532 String8 svc(services[i]);
533 fprintf(stderr, "error poking binder service %s\n",
534 svc.string());
535 return false;
536 }
537 }
538 }
539 }
540 return true;
541}
542
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100543// Poke all the HAL processes in the system to get them to re-read
544// their system properties.
545static void pokeHalServices()
546{
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100547 using ::android::hidl::base::V1_0::IBase;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100548 using ::android::hidl::manager::V1_0::IServiceManager;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100549 using ::android::hardware::hidl_string;
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100550 using ::android::hardware::Return;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100551
552 sp<IServiceManager> sm = ::android::hardware::defaultServiceManager();
Steven Morelanda42866b2016-12-12 15:18:06 -0800553
554 if (sm == nullptr) {
555 fprintf(stderr, "failed to get IServiceManager to poke hal services\n");
556 return;
557 }
558
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800559 auto listRet = sm->list([&](const auto &interfaces) {
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100560 for (size_t i = 0; i < interfaces.size(); i++) {
561 string fqInstanceName = interfaces[i];
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700562 string::size_type n = fqInstanceName.find('/');
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100563 if (n == std::string::npos || interfaces[i].size() == n+1)
564 continue;
565 hidl_string fqInterfaceName = fqInstanceName.substr(0, n);
566 hidl_string instanceName = fqInstanceName.substr(n+1, std::string::npos);
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100567 Return<sp<IBase>> interfaceRet = sm->get(fqInterfaceName, instanceName);
568 if (!interfaceRet.isOk()) {
Martijn Coenen1e4a7fb2017-02-17 14:57:38 +0100569 // ignore
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100570 continue;
571 }
Carmen Jackson73206122017-04-18 15:37:57 -0700572
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100573 sp<IBase> interface = interfaceRet;
Carmen Jackson73206122017-04-18 15:37:57 -0700574 if (interface == nullptr) {
575 // ignore
576 continue;
577 }
578
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100579 auto notifyRet = interface->notifySyspropsChanged();
580 if (!notifyRet.isOk()) {
Martijn Coenen1e4a7fb2017-02-17 14:57:38 +0100581 // ignore
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800582 }
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100583 }
584 });
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800585 if (!listRet.isOk()) {
Martijn Coenen64d54eb2017-01-12 17:16:31 +0100586 // TODO(b/34242478) fix this when we determine the correct ACL
587 //fprintf(stderr, "failed to list services: %s\n", listRet.description().c_str());
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800588 }
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100589}
590
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800591// Set the trace tags that userland tracing uses, and poke the running
592// processes to pick up the new value.
593static bool setTagsProperty(uint64_t tags)
594{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700595 std::string value = android::base::StringPrintf("%#" PRIx64, tags);
596 if (!android::base::SetProperty(k_traceTagsProperty, value)) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800597 fprintf(stderr, "error setting trace tags system property\n");
598 return false;
599 }
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700600 return true;
601}
602
sergeyv4144eff2016-04-28 11:40:04 -0700603static void clearAppProperties()
604{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700605 if (!android::base::SetProperty(k_traceAppsNumberProperty, "")) {
sergeyv4144eff2016-04-28 11:40:04 -0700606 fprintf(stderr, "failed to clear system property: %s",
607 k_traceAppsNumberProperty);
608 }
609}
610
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700611// Set the system property that indicates which apps should perform
612// application-level tracing.
Dan Austin09a79872016-05-31 13:27:03 -0700613static bool setAppCmdlineProperty(char* cmdline)
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700614{
sergeyv4144eff2016-04-28 11:40:04 -0700615 int i = 0;
Dan Austin09a79872016-05-31 13:27:03 -0700616 char* start = cmdline;
sergeyv4144eff2016-04-28 11:40:04 -0700617 while (start != NULL) {
sergeyv4144eff2016-04-28 11:40:04 -0700618 char* end = strchr(start, ',');
619 if (end != NULL) {
620 *end = '\0';
621 end++;
622 }
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700623 std::string key = android::base::StringPrintf(k_traceAppsPropertyTemplate, i);
624 if (!android::base::SetProperty(key, start)) {
625 fprintf(stderr, "error setting trace app %d property to %s\n", i, key.c_str());
sergeyv4144eff2016-04-28 11:40:04 -0700626 clearAppProperties();
627 return false;
628 }
629 start = end;
630 i++;
631 }
632
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700633 std::string value = android::base::StringPrintf("%d", i);
634 if (!android::base::SetProperty(k_traceAppsNumberProperty, value)) {
635 fprintf(stderr, "error setting trace app number property to %s\n", value.c_str());
sergeyv4144eff2016-04-28 11:40:04 -0700636 clearAppProperties();
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700637 return false;
638 }
639 return true;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800640}
641
642// Disable all /sys/ enable files.
643static bool disableKernelTraceEvents() {
644 bool ok = true;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700645 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800646 const TracingCategory &c = k_categories[i];
647 for (int j = 0; j < MAX_SYS_FILES; j++) {
648 const char* path = c.sysfiles[j].path;
649 if (path != NULL && fileIsWritable(path)) {
650 ok &= setKernelOptionEnable(path, false);
651 }
652 }
653 }
654 return ok;
655}
656
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700657// Verify that the comma separated list of functions are being traced by the
658// kernel.
659static bool verifyKernelTraceFuncs(const char* funcs)
660{
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100661 std::string buf;
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800662 if (!android::base::ReadFileToString(g_traceFolder + k_ftraceFilterPath, &buf)) {
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100663 fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath,
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700664 strerror(errno), errno);
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100665 return false;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700666 }
667
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100668 String8 funcList = String8::format("\n%s",buf.c_str());
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700669
670 // Make sure that every function listed in funcs is in the list we just
Thomas Buhota2c22872016-01-27 09:44:31 +0100671 // read from the kernel, except for wildcard inputs.
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700672 bool ok = true;
673 char* myFuncs = strdup(funcs);
674 char* func = strtok(myFuncs, ",");
675 while (func) {
Thomas Buhota2c22872016-01-27 09:44:31 +0100676 if (!strchr(func, '*')) {
677 String8 fancyFunc = String8::format("\n%s\n", func);
678 bool found = funcList.find(fancyFunc.string(), 0) >= 0;
679 if (!found || func[0] == '\0') {
680 fprintf(stderr, "error: \"%s\" is not a valid kernel function "
681 "to trace.\n", func);
682 ok = false;
683 }
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700684 }
685 func = strtok(NULL, ",");
686 }
687 free(myFuncs);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700688 return ok;
689}
690
691// Set the comma separated list of functions that the kernel is to trace.
692static bool setKernelTraceFuncs(const char* funcs)
693{
694 bool ok = true;
695
696 if (funcs == NULL || funcs[0] == '\0') {
697 // Disable kernel function tracing.
Jamie Gennis6f6f3f72013-03-27 15:50:30 -0700698 if (fileIsWritable(k_currentTracerPath)) {
699 ok &= writeStr(k_currentTracerPath, "nop");
700 }
701 if (fileIsWritable(k_ftraceFilterPath)) {
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700702 ok &= truncateFile(k_ftraceFilterPath);
703 }
704 } else {
705 // Enable kernel function tracing.
706 ok &= writeStr(k_currentTracerPath, "function_graph");
707 ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true);
708 ok &= setKernelOptionEnable(k_funcgraphCpuPath, true);
709 ok &= setKernelOptionEnable(k_funcgraphProcPath, true);
710 ok &= setKernelOptionEnable(k_funcgraphFlatPath, true);
711
712 // Set the requested filter functions.
713 ok &= truncateFile(k_ftraceFilterPath);
714 char* myFuncs = strdup(funcs);
715 char* func = strtok(myFuncs, ",");
716 while (func) {
717 ok &= appendStr(k_ftraceFilterPath, func);
718 func = strtok(NULL, ",");
719 }
720 free(myFuncs);
721
722 // Verify that the set functions are being traced.
723 if (ok) {
724 ok &= verifyKernelTraceFuncs(funcs);
725 }
726 }
727
728 return ok;
729}
730
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900731static bool setCategoryEnable(const char* name, bool enable)
732{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700733 for (size_t i = 0; i < arraysize(k_categories); i++) {
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900734 const TracingCategory& c = k_categories[i];
735 if (strcmp(name, c.name) == 0) {
736 if (isCategorySupported(c)) {
737 g_categoryEnables[i] = enable;
738 return true;
739 } else {
740 if (isCategorySupportedForRoot(c)) {
741 fprintf(stderr, "error: category \"%s\" requires root "
742 "privileges.\n", name);
743 } else {
744 fprintf(stderr, "error: category \"%s\" is not supported "
745 "on this device.\n", name);
746 }
747 return false;
748 }
749 }
750 }
751 fprintf(stderr, "error: unknown tracing category \"%s\"\n", name);
752 return false;
753}
754
755static bool setCategoriesEnableFromFile(const char* categories_file)
756{
757 if (!categories_file) {
758 return true;
759 }
760 Tokenizer* tokenizer = NULL;
761 if (Tokenizer::open(String8(categories_file), &tokenizer) != NO_ERROR) {
762 return false;
763 }
764 bool ok = true;
765 while (!tokenizer->isEol()) {
766 String8 token = tokenizer->nextToken(" ");
767 if (token.isEmpty()) {
768 tokenizer->skipDelimiters(" ");
769 continue;
770 }
771 ok &= setCategoryEnable(token.string(), true);
772 }
773 delete tokenizer;
774 return ok;
775}
776
Hector Dearman550b18c2018-03-08 14:35:44 +0000777static bool setUpUserspaceTracing()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800778{
779 bool ok = true;
780
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800781 // Set up the tags property.
782 uint64_t tags = 0;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700783 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800784 if (g_categoryEnables[i]) {
785 const TracingCategory &c = k_categories[i];
786 tags |= c.tags;
787 }
788 }
789 ok &= setTagsProperty(tags);
sergeyvdb404152016-05-02 19:26:07 -0700790
791 bool coreServicesTagEnabled = false;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700792 for (size_t i = 0; i < arraysize(k_categories); i++) {
sergeyvdb404152016-05-02 19:26:07 -0700793 if (strcmp(k_categories[i].name, k_coreServiceCategory) == 0) {
794 coreServicesTagEnabled = g_categoryEnables[i];
795 }
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700796
797 // Set whether to poke PDX services in this session.
798 if (strcmp(k_categories[i].name, k_pdxServiceCategory) == 0) {
799 g_tracePdx = g_categoryEnables[i];
800 }
sergeyvdb404152016-05-02 19:26:07 -0700801 }
802
803 std::string packageList(g_debugAppCmdLine);
804 if (coreServicesTagEnabled) {
sergeyvdb404152016-05-02 19:26:07 -0700805 if (!packageList.empty()) {
806 packageList += ",";
807 }
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700808 packageList += android::base::GetProperty(k_coreServicesProp, "");
sergeyvdb404152016-05-02 19:26:07 -0700809 }
Dan Austin09a79872016-05-31 13:27:03 -0700810 ok &= setAppCmdlineProperty(&packageList[0]);
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700811 ok &= pokeBinderServices();
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100812 pokeHalServices();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800813
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700814 if (g_tracePdx) {
815 ok &= ServiceUtility::PokeServices();
816 }
817
Hector Dearman550b18c2018-03-08 14:35:44 +0000818 return ok;
819}
820
821static void cleanUpUserspaceTracing()
822{
823 setTagsProperty(0);
824 clearAppProperties();
825 pokeBinderServices();
826
827 if (g_tracePdx) {
828 ServiceUtility::PokeServices();
829 }
830}
831
832
833// Set all the kernel tracing settings to the desired state for this trace
834// capture.
835static bool setUpKernelTracing()
836{
837 bool ok = true;
838
839 // Set up the tracing options.
840 ok &= setCategoriesEnableFromFile(g_categoriesFile);
841 ok &= setTraceOverwriteEnable(g_traceOverwrite);
842 ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
843 // TODO: Re-enable after stabilization
844 //ok &= setCmdlineSize();
845 ok &= setClock();
846 ok &= setPrintTgidEnableIfPresent(true);
847 ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
848
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800849 // Disable all the sysfs enables. This is done as a separate loop from
850 // the enables to allow the same enable to exist in multiple categories.
851 ok &= disableKernelTraceEvents();
852
853 // Enable all the sysfs enables that are in an enabled category.
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700854 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800855 if (g_categoryEnables[i]) {
856 const TracingCategory &c = k_categories[i];
857 for (int j = 0; j < MAX_SYS_FILES; j++) {
858 const char* path = c.sysfiles[j].path;
859 bool required = c.sysfiles[j].required == REQ;
860 if (path != NULL) {
861 if (fileIsWritable(path)) {
862 ok &= setKernelOptionEnable(path, true);
863 } else if (required) {
864 fprintf(stderr, "error writing file %s\n", path);
865 ok = false;
866 }
867 }
868 }
869 }
870 }
871
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800872 return ok;
873}
874
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700875// Reset all the kernel tracing settings to their default state.
Hector Dearman550b18c2018-03-08 14:35:44 +0000876static void cleanUpKernelTracing()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800877{
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800878 // Disable all tracing that we're able to.
879 disableKernelTraceEvents();
880
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800881 // Set the options back to their defaults.
882 setTraceOverwriteEnable(true);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700883 setTraceBufferSizeKB(1);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700884 setPrintTgidEnableIfPresent(false);
885 setKernelTraceFuncs(NULL);
886}
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800887
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700888// Enable tracing in the kernel.
889static bool startTrace()
890{
891 return setTracingEnabled(true);
892}
893
894// Disable tracing in the kernel.
895static void stopTrace()
896{
897 setTracingEnabled(false);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800898}
899
Martijn Coenend9535872015-11-26 10:00:55 +0100900// Read data from the tracing pipe and forward to stdout
901static void streamTrace()
902{
903 char trace_data[4096];
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800904 int traceFD = open((g_traceFolder + k_traceStreamPath).c_str(), O_RDWR);
Martijn Coenend9535872015-11-26 10:00:55 +0100905 if (traceFD == -1) {
906 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceStreamPath,
907 strerror(errno), errno);
908 return;
909 }
910 while (!g_traceAborted) {
911 ssize_t bytes_read = read(traceFD, trace_data, 4096);
912 if (bytes_read > 0) {
913 write(STDOUT_FILENO, trace_data, bytes_read);
914 fflush(stdout);
915 } else {
916 if (!g_traceAborted) {
917 fprintf(stderr, "read returned %zd bytes err %d (%s)\n",
918 bytes_read, errno, strerror(errno));
919 }
920 break;
921 }
922 }
923}
924
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800925// Read the current kernel trace and write it to stdout.
John Reck40b26b42016-03-30 09:44:36 -0700926static void dumpTrace(int outFd)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800927{
John Reck6c8ac922016-03-28 11:25:30 -0700928 ALOGI("Dumping trace");
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800929 int traceFD = open((g_traceFolder + k_tracePath).c_str(), O_RDWR);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800930 if (traceFD == -1) {
931 fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath,
932 strerror(errno), errno);
933 return;
934 }
935
936 if (g_compress) {
937 z_stream zs;
Elliott Hughes3da5d232015-01-25 08:35:20 -0800938 memset(&zs, 0, sizeof(zs));
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700939
940 int result = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800941 if (result != Z_OK) {
942 fprintf(stderr, "error initializing zlib: %d\n", result);
943 close(traceFD);
944 return;
945 }
946
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700947 constexpr size_t bufSize = 64*1024;
948 std::unique_ptr<uint8_t> in(new uint8_t[bufSize]);
949 std::unique_ptr<uint8_t> out(new uint8_t[bufSize]);
950 if (!in || !out) {
951 fprintf(stderr, "couldn't allocate buffers\n");
952 close(traceFD);
953 return;
954 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800955
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700956 int flush = Z_NO_FLUSH;
957
958 zs.next_out = reinterpret_cast<Bytef*>(out.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800959 zs.avail_out = bufSize;
960
961 do {
962
963 if (zs.avail_in == 0) {
964 // More input is needed.
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700965 result = read(traceFD, in.get(), bufSize);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800966 if (result < 0) {
967 fprintf(stderr, "error reading trace: %s (%d)\n",
968 strerror(errno), errno);
969 result = Z_STREAM_END;
970 break;
971 } else if (result == 0) {
972 flush = Z_FINISH;
973 } else {
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700974 zs.next_in = reinterpret_cast<Bytef*>(in.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800975 zs.avail_in = result;
976 }
977 }
978
979 if (zs.avail_out == 0) {
980 // Need to write the output.
Elliott Hughesb59e2962016-07-22 09:00:59 -0700981 result = write(outFd, out.get(), bufSize);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800982 if ((size_t)result < bufSize) {
983 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
984 strerror(errno), errno);
985 result = Z_STREAM_END; // skip deflate error message
986 zs.avail_out = bufSize; // skip the final write
987 break;
988 }
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700989 zs.next_out = reinterpret_cast<Bytef*>(out.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800990 zs.avail_out = bufSize;
991 }
992
993 } while ((result = deflate(&zs, flush)) == Z_OK);
994
995 if (result != Z_STREAM_END) {
996 fprintf(stderr, "error deflating trace: %s\n", zs.msg);
997 }
998
999 if (zs.avail_out < bufSize) {
1000 size_t bytes = bufSize - zs.avail_out;
Elliott Hughesb59e2962016-07-22 09:00:59 -07001001 result = write(outFd, out.get(), bytes);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001002 if ((size_t)result < bytes) {
1003 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
1004 strerror(errno), errno);
1005 }
1006 }
1007
1008 result = deflateEnd(&zs);
1009 if (result != Z_OK) {
1010 fprintf(stderr, "error cleaning up zlib: %d\n", result);
1011 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001012 } else {
Josh Gaod3d36e72017-04-11 15:21:13 -07001013 char buf[4096];
1014 ssize_t rc;
1015 while ((rc = TEMP_FAILURE_RETRY(read(traceFD, buf, sizeof(buf)))) > 0) {
1016 if (!android::base::WriteFully(outFd, buf, rc)) {
1017 fprintf(stderr, "error writing trace: %s\n", strerror(errno));
1018 break;
1019 }
1020 }
1021 if (rc == -1) {
1022 fprintf(stderr, "error dumping trace: %s\n", strerror(errno));
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001023 }
1024 }
1025
1026 close(traceFD);
1027}
1028
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001029static void handleSignal(int /*signo*/)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001030{
1031 if (!g_nohup) {
1032 g_traceAborted = true;
1033 }
1034}
1035
1036static void registerSigHandler()
1037{
1038 struct sigaction sa;
1039 sigemptyset(&sa.sa_mask);
1040 sa.sa_flags = 0;
1041 sa.sa_handler = handleSignal;
1042 sigaction(SIGHUP, &sa, NULL);
1043 sigaction(SIGINT, &sa, NULL);
1044 sigaction(SIGQUIT, &sa, NULL);
1045 sigaction(SIGTERM, &sa, NULL);
1046}
1047
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001048static void listSupportedCategories()
1049{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -07001050 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001051 const TracingCategory& c = k_categories[i];
1052 if (isCategorySupported(c)) {
1053 printf(" %10s - %s\n", c.name, c.longname);
1054 }
1055 }
1056}
1057
1058// Print the command usage help to stderr.
1059static void showHelp(const char *cmd)
1060{
1061 fprintf(stderr, "usage: %s [options] [categories...]\n", cmd);
1062 fprintf(stderr, "options include:\n"
Jamie Gennisf7f29c82013-03-27 15:50:58 -07001063 " -a appname enable app-level tracing for a comma "
Daniel Colascione519a0792018-02-09 20:05:39 -08001064 "separated list of cmdlines; * is a wildcard matching any process\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001065 " -b N use a trace buffer size of N KB\n"
1066 " -c trace into a circular buffer\n"
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +09001067 " -f filename use the categories written in a file as space-separated\n"
1068 " values in a line\n"
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001069 " -k fname,... trace the listed kernel functions\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001070 " -n ignore signals\n"
1071 " -s N sleep for N seconds before tracing [default 0]\n"
Fabien Sanglardb5c95472016-06-08 11:40:12 -07001072 " -t N trace for N seconds [default 5]\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001073 " -z compress the trace dump\n"
Fabien Sanglardb5c95472016-06-08 11:40:12 -07001074 " --async_start start circular trace and return immediately\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001075 " --async_dump dump the current contents of circular trace buffer\n"
1076 " --async_stop stop tracing and dump the current contents of circular\n"
1077 " trace buffer\n"
Martijn Coenend9535872015-11-26 10:00:55 +01001078 " --stream stream trace to stdout as it enters the trace buffer\n"
1079 " Note: this can take significant CPU time, and is best\n"
1080 " used for measuring things that are not affected by\n"
1081 " CPU performance, like pagecache usage.\n"
Jamie Gennis92573f12012-12-07 16:29:03 -08001082 " --list_categories\n"
1083 " list the available tracing categories\n"
John Reck40b26b42016-03-30 09:44:36 -07001084 " -o filename write the trace to the specified file instead\n"
1085 " of stdout.\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001086 );
1087}
1088
Paul Lawrence2cd93cc2017-01-17 09:50:18 -08001089bool findTraceFiles()
1090{
1091 static const std::string debugfs_path = "/sys/kernel/debug/tracing/";
1092 static const std::string tracefs_path = "/sys/kernel/tracing/";
1093 static const std::string trace_file = "trace_marker";
1094
1095 bool tracefs = access((tracefs_path + trace_file).c_str(), F_OK) != -1;
1096 bool debugfs = access((debugfs_path + trace_file).c_str(), F_OK) != -1;
1097
1098 if (!tracefs && !debugfs) {
1099 fprintf(stderr, "Error: Did not find trace folder\n");
1100 return false;
1101 }
1102
1103 if (tracefs) {
1104 g_traceFolder = tracefs_path;
1105 } else {
1106 g_traceFolder = debugfs_path;
1107 }
1108
1109 return true;
1110}
1111
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001112int main(int argc, char **argv)
1113{
1114 bool async = false;
1115 bool traceStart = true;
1116 bool traceStop = true;
1117 bool traceDump = true;
Martijn Coenend9535872015-11-26 10:00:55 +01001118 bool traceStream = false;
Hector Dearman550b18c2018-03-08 14:35:44 +00001119 bool onlyUserspace = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001120
1121 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
1122 showHelp(argv[0]);
1123 exit(0);
1124 }
1125
Paul Lawrence2cd93cc2017-01-17 09:50:18 -08001126 if (!findTraceFiles()) {
1127 fprintf(stderr, "No trace folder found\n");
1128 exit(-1);
1129 }
1130
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001131 for (;;) {
1132 int ret;
1133 int option_index = 0;
1134 static struct option long_options[] = {
Hector Dearman550b18c2018-03-08 14:35:44 +00001135 {"async_start", no_argument, 0, 0 },
1136 {"async_stop", no_argument, 0, 0 },
1137 {"async_dump", no_argument, 0, 0 },
1138 {"only_userspace", no_argument, 0, 0 },
1139 {"list_categories", no_argument, 0, 0 },
1140 {"stream", no_argument, 0, 0 },
1141 { 0, 0, 0, 0 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001142 };
1143
John Reck40b26b42016-03-30 09:44:36 -07001144 ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:zo:",
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001145 long_options, &option_index);
1146
1147 if (ret < 0) {
1148 for (int i = optind; i < argc; i++) {
1149 if (!setCategoryEnable(argv[i], true)) {
1150 fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]);
1151 exit(1);
1152 }
1153 }
1154 break;
1155 }
1156
1157 switch(ret) {
Jamie Gennisf7f29c82013-03-27 15:50:58 -07001158 case 'a':
1159 g_debugAppCmdLine = optarg;
1160 break;
1161
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001162 case 'b':
1163 g_traceBufferSizeKB = atoi(optarg);
1164 break;
1165
1166 case 'c':
1167 g_traceOverwrite = true;
1168 break;
1169
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +09001170 case 'f':
1171 g_categoriesFile = optarg;
1172 break;
1173
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001174 case 'k':
1175 g_kernelTraceFuncs = optarg;
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001176 break;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001177
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001178 case 'n':
1179 g_nohup = true;
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001180 break;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001181
1182 case 's':
1183 g_initialSleepSecs = atoi(optarg);
1184 break;
1185
1186 case 't':
1187 g_traceDurationSeconds = atoi(optarg);
1188 break;
1189
1190 case 'z':
1191 g_compress = true;
1192 break;
1193
John Reck40b26b42016-03-30 09:44:36 -07001194 case 'o':
1195 g_outputFile = optarg;
1196 break;
1197
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001198 case 0:
1199 if (!strcmp(long_options[option_index].name, "async_start")) {
1200 async = true;
1201 traceStop = false;
1202 traceDump = false;
1203 g_traceOverwrite = true;
1204 } else if (!strcmp(long_options[option_index].name, "async_stop")) {
1205 async = true;
John Reck4ba2b632015-05-15 10:00:34 -07001206 traceStart = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001207 } else if (!strcmp(long_options[option_index].name, "async_dump")) {
1208 async = true;
1209 traceStart = false;
1210 traceStop = false;
Hector Dearman550b18c2018-03-08 14:35:44 +00001211 } else if (!strcmp(long_options[option_index].name, "only_userspace")) {
1212 onlyUserspace = true;
Martijn Coenend9535872015-11-26 10:00:55 +01001213 } else if (!strcmp(long_options[option_index].name, "stream")) {
1214 traceStream = true;
1215 traceDump = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001216 } else if (!strcmp(long_options[option_index].name, "list_categories")) {
1217 listSupportedCategories();
1218 exit(0);
1219 }
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001220 break;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001221
1222 default:
1223 fprintf(stderr, "\n");
1224 showHelp(argv[0]);
1225 exit(-1);
1226 break;
1227 }
1228 }
1229
Hector Dearman550b18c2018-03-08 14:35:44 +00001230 if (onlyUserspace) {
1231 if (!async || !(traceStart || traceStop)) {
1232 fprintf(stderr, "--only_userspace can only be used with "
1233 "--async_start or --async_stop\n");
1234 exit(1);
1235 }
1236 }
1237
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001238 registerSigHandler();
1239
1240 if (g_initialSleepSecs > 0) {
1241 sleep(g_initialSleepSecs);
1242 }
1243
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001244 bool ok = true;
Hector Dearman550b18c2018-03-08 14:35:44 +00001245
Wei Wang190cb7d2018-03-08 13:33:12 -08001246 if (traceStart) {
Hector Dearman550b18c2018-03-08 14:35:44 +00001247 ok &= setUpUserspaceTracing();
1248 }
1249
1250 if (ok && traceStart && !onlyUserspace) {
1251 ok &= setUpKernelTracing();
Wei Wang190cb7d2018-03-08 13:33:12 -08001252 ok &= startTrace();
1253 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001254
1255 if (ok && traceStart) {
Hector Dearman550b18c2018-03-08 14:35:44 +00001256
1257 if (!traceStream && !onlyUserspace) {
Carmen Jacksonac53e732017-05-02 16:55:33 -07001258 printf("capturing trace...");
Martijn Coenend9535872015-11-26 10:00:55 +01001259 fflush(stdout);
1260 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001261
1262 // We clear the trace after starting it because tracing gets enabled for
1263 // each CPU individually in the kernel. Having the beginning of the trace
1264 // contain entries from only one CPU can cause "begin" entries without a
1265 // matching "end" entry to show up if a task gets migrated from one CPU to
1266 // another.
Hector Dearman550b18c2018-03-08 14:35:44 +00001267 if (!onlyUserspace)
1268 ok = clearTrace();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001269
Hector Dearman550b18c2018-03-08 14:35:44 +00001270 if (!onlyUserspace)
1271 writeClockSyncMarker();
1272
Martijn Coenend9535872015-11-26 10:00:55 +01001273 if (ok && !async && !traceStream) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001274 // Sleep to allow the trace to be captured.
1275 struct timespec timeLeft;
1276 timeLeft.tv_sec = g_traceDurationSeconds;
1277 timeLeft.tv_nsec = 0;
1278 do {
1279 if (g_traceAborted) {
1280 break;
1281 }
1282 } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR);
1283 }
Martijn Coenend9535872015-11-26 10:00:55 +01001284
1285 if (traceStream) {
1286 streamTrace();
1287 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001288 }
1289
1290 // Stop the trace and restore the default settings.
Hector Dearman550b18c2018-03-08 14:35:44 +00001291 if (traceStop && !onlyUserspace)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001292 stopTrace();
1293
Hector Dearman550b18c2018-03-08 14:35:44 +00001294 if (ok && traceDump && !onlyUserspace) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001295 if (!g_traceAborted) {
John Reck40b26b42016-03-30 09:44:36 -07001296 printf(" done\n");
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001297 fflush(stdout);
John Reck40b26b42016-03-30 09:44:36 -07001298 int outFd = STDOUT_FILENO;
1299 if (g_outputFile) {
Martijn Coenenc5791982017-02-22 09:25:31 +01001300 outFd = open(g_outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
John Reck40b26b42016-03-30 09:44:36 -07001301 }
1302 if (outFd == -1) {
1303 printf("Failed to open '%s', err=%d", g_outputFile, errno);
1304 } else {
1305 dprintf(outFd, "TRACE:\n");
1306 dumpTrace(outFd);
1307 if (g_outputFile) {
1308 close(outFd);
1309 }
1310 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001311 } else {
1312 printf("\ntrace aborted.\n");
1313 fflush(stdout);
1314 }
1315 clearTrace();
1316 } else if (!ok) {
1317 fprintf(stderr, "unable to start tracing\n");
1318 }
1319
1320 // Reset the trace buffer size to 1.
Hector Dearman550b18c2018-03-08 14:35:44 +00001321 if (traceStop) {
1322 cleanUpUserspaceTracing();
1323 if (!onlyUserspace)
1324 cleanUpKernelTracing();
1325 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001326
1327 return g_traceAborted ? 1 : 0;
1328}