blob: a87212b7653527ff4e4e9be36160820de88c5320 [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
59#define MAX_PACKAGES 16
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080060
61const char* k_traceTagsProperty = "debug.atrace.tags.enableflags";
sergeyv4144eff2016-04-28 11:40:04 -070062
63const char* k_traceAppsNumberProperty = "debug.atrace.app_number";
64const char* k_traceAppsPropertyTemplate = "debug.atrace.app_%d";
sergeyvdb404152016-05-02 19:26:07 -070065const char* k_coreServiceCategory = "core_services";
Corey Tabakaa6c0a722017-05-31 16:37:40 -070066const char* k_pdxServiceCategory = "pdx";
sergeyvdb404152016-05-02 19:26:07 -070067const char* k_coreServicesProp = "ro.atrace.core.services";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -080068
69typedef enum { OPT, REQ } requiredness ;
70
71struct TracingCategory {
72 // The name identifying the category.
73 const char* name;
74
75 // A longer description of the category.
76 const char* longname;
77
78 // The userland tracing tags that the category enables.
79 uint64_t tags;
80
81 // The fname==NULL terminated list of /sys/ files that the category
82 // enables.
83 struct {
84 // Whether the file must be writable in order to enable the tracing
85 // category.
86 requiredness required;
87
88 // The path to the enable file.
89 const char* path;
90 } sysfiles[MAX_SYS_FILES];
91};
92
93/* Tracing categories */
94static const TracingCategory k_categories[] = {
John Reck732a29a2017-05-24 16:09:21 -070095 { "gfx", "Graphics", ATRACE_TAG_GRAPHICS, {
96 { OPT, "events/mdss/enable" },
97 } },
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, { } },
Jeff Brown3200b0b2014-08-14 19:24:47 -0700108 { "app", "Application", ATRACE_TAG_APP, { } },
Dianne Hackborn9380d782013-04-12 14:52:35 -0700109 { "res", "Resource Loading", ATRACE_TAG_RESOURCES, { } },
Jamie Genniseff2e8d2013-05-07 15:20:39 -0700110 { "dalvik", "Dalvik VM", ATRACE_TAG_DALVIK, { } },
Tim Murrayf0f28412013-05-23 14:39:42 -0700111 { "rs", "RenderScript", ATRACE_TAG_RS, { } },
Brigid Smith750aa972014-05-28 14:23:24 -0700112 { "bionic", "Bionic C Library", ATRACE_TAG_BIONIC, { } },
Jeff Brown3200b0b2014-08-14 19:24:47 -0700113 { "power", "Power Management", ATRACE_TAG_POWER, { } },
Todd Kennedy01e111b2015-07-31 14:36:20 -0700114 { "pm", "Package Manager", ATRACE_TAG_PACKAGE_MANAGER, { } },
Yasuhiro Matsuda7cc49772015-07-01 01:46:25 +0900115 { "ss", "System Server", ATRACE_TAG_SYSTEM_SERVER, { } },
Greg Hackmannbbd7d992014-12-01 14:43:34 -0800116 { "database", "Database", ATRACE_TAG_DATABASE, { } },
Felipe Leme0f97c1d2016-09-07 11:33:26 -0700117 { "network", "Network", ATRACE_TAG_NETWORK, { } },
Josh Gao468b4cb2016-11-29 10:55:21 -0800118 { "adb", "ADB", ATRACE_TAG_ADB, { } },
Alexey Kuzmin808b51c2018-02-10 15:17:57 +0000119 { "vibrator", "Vibrator", ATRACE_TAG_VIBRATOR, {}},
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" },
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800158 { OPT, "events/power/cpu_frequency_limits/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800159 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700160 { "membus", "Memory Bus Utilization", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800161 { REQ, "events/memory_bus/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800162 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700163 { "idle", "CPU Idle", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800164 { REQ, "events/power/cpu_idle/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800165 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700166 { "disk", "Disk I/O", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800167 { OPT, "events/f2fs/f2fs_sync_file_enter/enable" },
168 { OPT, "events/f2fs/f2fs_sync_file_exit/enable" },
169 { OPT, "events/f2fs/f2fs_write_begin/enable" },
170 { OPT, "events/f2fs/f2fs_write_end/enable" },
171 { OPT, "events/ext4/ext4_da_write_begin/enable" },
172 { OPT, "events/ext4/ext4_da_write_end/enable" },
173 { OPT, "events/ext4/ext4_sync_file_enter/enable" },
174 { OPT, "events/ext4/ext4_sync_file_exit/enable" },
175 { REQ, "events/block/block_rq_issue/enable" },
176 { REQ, "events/block/block_rq_complete/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800177 } },
Ken Sumralld3fa5612013-07-03 12:32:03 -0700178 { "mmc", "eMMC commands", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800179 { REQ, "events/mmc/enable" },
Ken Sumralld3fa5612013-07-03 12:32:03 -0700180 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700181 { "load", "CPU Load", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800182 { REQ, "events/cpufreq_interactive/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800183 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700184 { "sync", "Synchronization", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800185 { REQ, "events/sync/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800186 } },
Jamie Gennisb2a89e32013-03-11 19:37:53 -0700187 { "workq", "Kernel Workqueues", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800188 { REQ, "events/workqueue/enable" },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800189 } },
Colin Cross580407f2014-08-18 15:22:13 -0700190 { "memreclaim", "Kernel Memory Reclaim", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800191 { REQ, "events/vmscan/mm_vmscan_direct_reclaim_begin/enable" },
192 { REQ, "events/vmscan/mm_vmscan_direct_reclaim_end/enable" },
193 { REQ, "events/vmscan/mm_vmscan_kswapd_wake/enable" },
194 { REQ, "events/vmscan/mm_vmscan_kswapd_sleep/enable" },
Marc Hittingerf1f62e32017-05-17 15:57:43 -0700195 { REQ, "events/lowmemorykiller/enable" },
Colin Cross580407f2014-08-18 15:22:13 -0700196 } },
Aaron Schulmanc2c6ecd2015-02-25 08:37:09 -0800197 { "regulators", "Voltage and Current Regulators", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800198 { REQ, "events/regulator/enable" },
Aaron Schulmanc2c6ecd2015-02-25 08:37:09 -0800199 } },
Scott Bauerae473362015-06-08 16:32:36 -0700200 { "binder_driver", "Binder Kernel driver", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800201 { REQ, "events/binder/binder_transaction/enable" },
202 { REQ, "events/binder/binder_transaction_received/enable" },
Martijn Coenen7f3d7a22017-05-26 09:50:55 -0700203 { OPT, "events/binder/binder_set_priority/enable" },
Scott Bauerae473362015-06-08 16:32:36 -0700204 } },
205 { "binder_lock", "Binder global lock trace", 0, {
Howard Cheneb8acbf2017-05-10 16:32:11 +0800206 { OPT, "events/binder/binder_lock/enable" },
207 { OPT, "events/binder/binder_locked/enable" },
208 { OPT, "events/binder/binder_unlock/enable" },
Scott Bauerae473362015-06-08 16:32:36 -0700209 } },
Martijn Coenen70481612015-10-23 13:57:05 +0200210 { "pagecache", "Page cache", 0, {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800211 { REQ, "events/filemap/enable" },
Martijn Coenen70481612015-10-23 13:57:05 +0200212 } },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800213};
214
215/* Command line options */
216static int g_traceDurationSeconds = 5;
217static bool g_traceOverwrite = false;
218static int g_traceBufferSizeKB = 2048;
219static bool g_compress = false;
220static bool g_nohup = false;
221static int g_initialSleepSecs = 0;
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900222static const char* g_categoriesFile = NULL;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700223static const char* g_kernelTraceFuncs = NULL;
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700224static const char* g_debugAppCmdLine = "";
John Reck40b26b42016-03-30 09:44:36 -0700225static const char* g_outputFile = nullptr;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800226
227/* Global state */
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700228static bool g_tracePdx = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800229static bool g_traceAborted = false;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700230static bool g_categoryEnables[arraysize(k_categories)] = {};
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800231static std::string g_traceFolder;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800232
233/* Sys file paths */
234static const char* k_traceClockPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800235 "trace_clock";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800236
237static const char* k_traceBufferSizePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800238 "buffer_size_kb";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800239
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700240#if 0
241// TODO: Re-enable after stabilization
Joel Fernandesed80bd02017-06-02 10:19:28 -0700242static const char* k_traceCmdlineSizePath =
243 "saved_cmdlines_size";
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700244#endif
Joel Fernandesed80bd02017-06-02 10:19:28 -0700245
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800246static const char* k_tracingOverwriteEnablePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800247 "options/overwrite";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800248
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700249static const char* k_currentTracerPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800250 "current_tracer";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700251
252static const char* k_printTgidPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800253 "options/print-tgid";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700254
255static const char* k_funcgraphAbsTimePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800256 "options/funcgraph-abstime";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700257
258static const char* k_funcgraphCpuPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800259 "options/funcgraph-cpu";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700260
261static const char* k_funcgraphProcPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800262 "options/funcgraph-proc";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700263
264static const char* k_funcgraphFlatPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800265 "options/funcgraph-flat";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700266
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700267static const char* k_ftraceFilterPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800268 "set_ftrace_filter";
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700269
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800270static const char* k_tracingOnPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800271 "tracing_on";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800272
273static const char* k_tracePath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800274 "trace";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800275
Martijn Coenend9535872015-11-26 10:00:55 +0100276static const char* k_traceStreamPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800277 "trace_pipe";
Martijn Coenend9535872015-11-26 10:00:55 +0100278
John Reck469a1942015-03-26 15:31:35 -0700279static const char* k_traceMarkerPath =
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800280 "trace_marker";
John Reck469a1942015-03-26 15:31:35 -0700281
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800282// Check whether a file exists.
283static bool fileExists(const char* filename) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800284 return access((g_traceFolder + filename).c_str(), F_OK) != -1;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800285}
286
287// Check whether a file is writable.
288static bool fileIsWritable(const char* filename) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800289 return access((g_traceFolder + filename).c_str(), W_OK) != -1;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800290}
291
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700292// Truncate a file.
293static bool truncateFile(const char* path)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800294{
Jamie Gennis43122e72013-03-21 14:06:31 -0700295 // This uses creat rather than truncate because some of the debug kernel
296 // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by
297 // calls to truncate, but they are cleared by calls to creat.
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800298 int traceFD = creat((g_traceFolder + path).c_str(), 0);
Jamie Gennis43122e72013-03-21 14:06:31 -0700299 if (traceFD == -1) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800300 fprintf(stderr, "error truncating %s: %s (%d)\n", (g_traceFolder + path).c_str(),
Jamie Gennis43122e72013-03-21 14:06:31 -0700301 strerror(errno), errno);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700302 return false;
303 }
304
Jamie Gennis43122e72013-03-21 14:06:31 -0700305 close(traceFD);
306
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700307 return true;
308}
309
310static bool _writeStr(const char* filename, const char* str, int flags)
311{
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800312 std::string fullFilename = g_traceFolder + filename;
313 int fd = open(fullFilename.c_str(), flags);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800314 if (fd == -1) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800315 fprintf(stderr, "error opening %s: %s (%d)\n", fullFilename.c_str(),
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800316 strerror(errno), errno);
317 return false;
318 }
319
320 bool ok = true;
321 ssize_t len = strlen(str);
322 if (write(fd, str, len) != len) {
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800323 fprintf(stderr, "error writing to %s: %s (%d)\n", fullFilename.c_str(),
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800324 strerror(errno), errno);
325 ok = false;
326 }
327
328 close(fd);
329
330 return ok;
331}
332
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700333// Write a string to a file, returning true if the write was successful.
334static bool writeStr(const char* filename, const char* str)
335{
336 return _writeStr(filename, str, O_WRONLY);
337}
338
339// Append a string to a file, returning true if the write was successful.
340static bool appendStr(const char* filename, const char* str)
341{
342 return _writeStr(filename, str, O_APPEND|O_WRONLY);
343}
344
John Reck469a1942015-03-26 15:31:35 -0700345static void writeClockSyncMarker()
346{
347 char buffer[128];
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200348 int len = 0;
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800349 int fd = open((g_traceFolder + k_traceMarkerPath).c_str(), O_WRONLY);
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200350 if (fd == -1) {
351 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceMarkerPath,
352 strerror(errno), errno);
353 return;
354 }
John Reck469a1942015-03-26 15:31:35 -0700355 float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f;
Martijn Coenen0bcd97a2015-07-15 14:25:23 +0200356
357 len = snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds);
358 if (write(fd, buffer, len) != len) {
359 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
360 }
361
362 int64_t realtime_in_ms = systemTime(CLOCK_REALTIME) / 1000000;
363 len = snprintf(buffer, 128, "trace_event_clock_sync: realtime_ts=%" PRId64 "\n", realtime_in_ms);
364 if (write(fd, buffer, len) != len) {
365 fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno);
366 }
367
368 close(fd);
John Reck469a1942015-03-26 15:31:35 -0700369}
370
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800371// Enable or disable a kernel option by writing a "1" or a "0" into a /sys
372// file.
373static bool setKernelOptionEnable(const char* filename, bool enable)
374{
375 return writeStr(filename, enable ? "1" : "0");
376}
377
378// Check whether the category is supported on the device with the current
379// rootness. A category is supported only if all its required /sys/ files are
380// writable and if enabling the category will enable one or more tracing tags
381// or /sys/ files.
382static bool isCategorySupported(const TracingCategory& category)
383{
sergeyvdb404152016-05-02 19:26:07 -0700384 if (strcmp(category.name, k_coreServiceCategory) == 0) {
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700385 return !android::base::GetProperty(k_coreServicesProp, "").empty();
sergeyvdb404152016-05-02 19:26:07 -0700386 }
387
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700388 if (strcmp(category.name, k_pdxServiceCategory) == 0) {
389 return true;
390 }
391
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800392 bool ok = category.tags != 0;
393 for (int i = 0; i < MAX_SYS_FILES; i++) {
394 const char* path = category.sysfiles[i].path;
395 bool req = category.sysfiles[i].required == REQ;
396 if (path != NULL) {
397 if (req) {
398 if (!fileIsWritable(path)) {
399 return false;
400 } else {
401 ok = true;
402 }
403 } else {
Howard Cheneb8acbf2017-05-10 16:32:11 +0800404 ok = true;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800405 }
406 }
407 }
408 return ok;
409}
410
411// Check whether the category would be supported on the device if the user
412// were root. This function assumes that root is able to write to any file
413// that exists. It performs the same logic as isCategorySupported, but it
Fabien Sanglardb5c95472016-06-08 11:40:12 -0700414// uses file existence rather than writability in the /sys/ file checks.
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800415static bool isCategorySupportedForRoot(const TracingCategory& category)
416{
417 bool ok = category.tags != 0;
418 for (int i = 0; i < MAX_SYS_FILES; i++) {
419 const char* path = category.sysfiles[i].path;
420 bool req = category.sysfiles[i].required == REQ;
421 if (path != NULL) {
422 if (req) {
423 if (!fileExists(path)) {
424 return false;
425 } else {
426 ok = true;
427 }
428 } else {
429 ok |= fileExists(path);
430 }
431 }
432 }
433 return ok;
434}
435
436// Enable or disable overwriting of the kernel trace buffers. Disabling this
437// will cause tracing to stop once the trace buffers have filled up.
438static bool setTraceOverwriteEnable(bool enable)
439{
440 return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable);
441}
442
443// Enable or disable kernel tracing.
444static bool setTracingEnabled(bool enable)
445{
446 return setKernelOptionEnable(k_tracingOnPath, enable);
447}
448
449// Clear the contents of the kernel trace.
450static bool clearTrace()
451{
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700452 return truncateFile(k_tracePath);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800453}
454
455// Set the size of the kernel's trace buffer in kilobytes.
456static bool setTraceBufferSizeKB(int size)
457{
458 char str[32] = "1";
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800459 if (size < 1) {
460 size = 1;
461 }
462 snprintf(str, 32, "%d", size);
463 return writeStr(k_traceBufferSizePath, str);
464}
465
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700466#if 0
467// TODO: Re-enable after stabilization
Joel Fernandesed80bd02017-06-02 10:19:28 -0700468// Set the default size of cmdline hashtable
469static bool setCmdlineSize()
470{
Joel Fernandesce964f22017-06-06 12:20:29 -0700471 if (fileExists(k_traceCmdlineSizePath)) {
472 return writeStr(k_traceCmdlineSizePath, "8192");
473 }
474 return true;
Joel Fernandesed80bd02017-06-02 10:19:28 -0700475}
Chih-Hung Hsieh734e3782017-10-05 13:44:13 -0700476#endif
Joel Fernandesed80bd02017-06-02 10:19:28 -0700477
Carmen Jacksonea826792017-05-05 11:42:32 -0700478// Set the clock to the best available option while tracing. Use 'boot' if it's
479// available; otherwise, use 'mono'. If neither are available use 'global'.
Colin Crossb1ce49b2014-08-20 14:28:47 -0700480// Any write to the trace_clock sysfs file will reset the buffer, so only
481// update it if the requested value is not the current value.
Carmen Jacksonea826792017-05-05 11:42:32 -0700482static bool setClock()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800483{
Carmen Jacksonea826792017-05-05 11:42:32 -0700484 std::ifstream clockFile((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
485 std::string clockStr((std::istreambuf_iterator<char>(clockFile)),
486 std::istreambuf_iterator<char>());
Colin Crossb1ce49b2014-08-20 14:28:47 -0700487
Carmen Jacksonea826792017-05-05 11:42:32 -0700488 std::string newClock;
489 if (clockStr.find("boot") != std::string::npos) {
490 newClock = "boot";
491 } else if (clockStr.find("mono") != std::string::npos) {
492 newClock = "mono";
493 } else {
494 newClock = "global";
Colin Crossb1ce49b2014-08-20 14:28:47 -0700495 }
496
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700497 size_t begin = clockStr.find('[') + 1;
498 size_t end = clockStr.find(']');
Carmen Jacksonea826792017-05-05 11:42:32 -0700499 if (newClock.compare(0, std::string::npos, clockStr, begin, end-begin) == 0) {
500 return true;
501 }
502 return writeStr(k_traceClockPath, newClock.c_str());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800503}
504
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700505static bool setPrintTgidEnableIfPresent(bool enable)
506{
507 if (fileExists(k_printTgidPath)) {
508 return setKernelOptionEnable(k_printTgidPath, enable);
509 }
510 return true;
511}
512
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800513// Poke all the binder-enabled processes in the system to get them to re-read
514// their system properties.
515static bool pokeBinderServices()
516{
517 sp<IServiceManager> sm = defaultServiceManager();
518 Vector<String16> services = sm->listServices();
519 for (size_t i = 0; i < services.size(); i++) {
520 sp<IBinder> obj = sm->checkService(services[i]);
521 if (obj != NULL) {
522 Parcel data;
523 if (obj->transact(IBinder::SYSPROPS_TRANSACTION, data,
524 NULL, 0) != OK) {
525 if (false) {
526 // XXX: For some reason this fails on tablets trying to
527 // poke the "phone" service. It's not clear whether some
528 // are expected to fail.
529 String8 svc(services[i]);
530 fprintf(stderr, "error poking binder service %s\n",
531 svc.string());
532 return false;
533 }
534 }
535 }
536 }
537 return true;
538}
539
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100540// Poke all the HAL processes in the system to get them to re-read
541// their system properties.
542static void pokeHalServices()
543{
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100544 using ::android::hidl::base::V1_0::IBase;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100545 using ::android::hidl::manager::V1_0::IServiceManager;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100546 using ::android::hardware::hidl_string;
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100547 using ::android::hardware::Return;
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100548
549 sp<IServiceManager> sm = ::android::hardware::defaultServiceManager();
Steven Morelanda42866b2016-12-12 15:18:06 -0800550
551 if (sm == nullptr) {
552 fprintf(stderr, "failed to get IServiceManager to poke hal services\n");
553 return;
554 }
555
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800556 auto listRet = sm->list([&](const auto &interfaces) {
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100557 for (size_t i = 0; i < interfaces.size(); i++) {
558 string fqInstanceName = interfaces[i];
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700559 string::size_type n = fqInstanceName.find('/');
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100560 if (n == std::string::npos || interfaces[i].size() == n+1)
561 continue;
562 hidl_string fqInterfaceName = fqInstanceName.substr(0, n);
563 hidl_string instanceName = fqInstanceName.substr(n+1, std::string::npos);
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100564 Return<sp<IBase>> interfaceRet = sm->get(fqInterfaceName, instanceName);
565 if (!interfaceRet.isOk()) {
Martijn Coenen1e4a7fb2017-02-17 14:57:38 +0100566 // ignore
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100567 continue;
568 }
Carmen Jackson73206122017-04-18 15:37:57 -0700569
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100570 sp<IBase> interface = interfaceRet;
Carmen Jackson73206122017-04-18 15:37:57 -0700571 if (interface == nullptr) {
572 // ignore
573 continue;
574 }
575
Martijn Coenenf6ac8482017-01-02 15:17:11 +0100576 auto notifyRet = interface->notifySyspropsChanged();
577 if (!notifyRet.isOk()) {
Martijn Coenen1e4a7fb2017-02-17 14:57:38 +0100578 // ignore
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800579 }
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100580 }
581 });
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800582 if (!listRet.isOk()) {
Martijn Coenen64d54eb2017-01-12 17:16:31 +0100583 // TODO(b/34242478) fix this when we determine the correct ACL
584 //fprintf(stderr, "failed to list services: %s\n", listRet.description().c_str());
Yifan Hong8cf4ed12016-11-30 17:28:58 -0800585 }
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100586}
587
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800588// Set the trace tags that userland tracing uses, and poke the running
589// processes to pick up the new value.
590static bool setTagsProperty(uint64_t tags)
591{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700592 std::string value = android::base::StringPrintf("%#" PRIx64, tags);
593 if (!android::base::SetProperty(k_traceTagsProperty, value)) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800594 fprintf(stderr, "error setting trace tags system property\n");
595 return false;
596 }
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700597 return true;
598}
599
sergeyv4144eff2016-04-28 11:40:04 -0700600static void clearAppProperties()
601{
sergeyv4144eff2016-04-28 11:40:04 -0700602 for (int i = 0; i < MAX_PACKAGES; i++) {
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700603 std::string key = android::base::StringPrintf(k_traceAppsPropertyTemplate, i);
604 if (!android::base::SetProperty(key, "")) {
605 fprintf(stderr, "failed to clear system property: %s\n", key.c_str());
sergeyv4144eff2016-04-28 11:40:04 -0700606 }
607 }
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700608 if (!android::base::SetProperty(k_traceAppsNumberProperty, "")) {
sergeyv4144eff2016-04-28 11:40:04 -0700609 fprintf(stderr, "failed to clear system property: %s",
610 k_traceAppsNumberProperty);
611 }
612}
613
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700614// Set the system property that indicates which apps should perform
615// application-level tracing.
Dan Austin09a79872016-05-31 13:27:03 -0700616static bool setAppCmdlineProperty(char* cmdline)
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700617{
sergeyv4144eff2016-04-28 11:40:04 -0700618 int i = 0;
Dan Austin09a79872016-05-31 13:27:03 -0700619 char* start = cmdline;
sergeyv4144eff2016-04-28 11:40:04 -0700620 while (start != NULL) {
621 if (i == MAX_PACKAGES) {
622 fprintf(stderr, "error: only 16 packages could be traced at once\n");
623 clearAppProperties();
624 return false;
625 }
626 char* end = strchr(start, ',');
627 if (end != NULL) {
628 *end = '\0';
629 end++;
630 }
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700631 std::string key = android::base::StringPrintf(k_traceAppsPropertyTemplate, i);
632 if (!android::base::SetProperty(key, start)) {
633 fprintf(stderr, "error setting trace app %d property to %s\n", i, key.c_str());
sergeyv4144eff2016-04-28 11:40:04 -0700634 clearAppProperties();
635 return false;
636 }
637 start = end;
638 i++;
639 }
640
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700641 std::string value = android::base::StringPrintf("%d", i);
642 if (!android::base::SetProperty(k_traceAppsNumberProperty, value)) {
643 fprintf(stderr, "error setting trace app number property to %s\n", value.c_str());
sergeyv4144eff2016-04-28 11:40:04 -0700644 clearAppProperties();
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700645 return false;
646 }
647 return true;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800648}
649
650// Disable all /sys/ enable files.
651static bool disableKernelTraceEvents() {
652 bool ok = true;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700653 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800654 const TracingCategory &c = k_categories[i];
655 for (int j = 0; j < MAX_SYS_FILES; j++) {
656 const char* path = c.sysfiles[j].path;
657 if (path != NULL && fileIsWritable(path)) {
658 ok &= setKernelOptionEnable(path, false);
659 }
660 }
661 }
662 return ok;
663}
664
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700665// Verify that the comma separated list of functions are being traced by the
666// kernel.
667static bool verifyKernelTraceFuncs(const char* funcs)
668{
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100669 std::string buf;
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800670 if (!android::base::ReadFileToString(g_traceFolder + k_ftraceFilterPath, &buf)) {
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100671 fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath,
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700672 strerror(errno), errno);
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100673 return false;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700674 }
675
Stephane Gasparinid8419c22016-03-02 13:45:15 +0100676 String8 funcList = String8::format("\n%s",buf.c_str());
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700677
678 // Make sure that every function listed in funcs is in the list we just
Thomas Buhota2c22872016-01-27 09:44:31 +0100679 // read from the kernel, except for wildcard inputs.
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700680 bool ok = true;
681 char* myFuncs = strdup(funcs);
682 char* func = strtok(myFuncs, ",");
683 while (func) {
Thomas Buhota2c22872016-01-27 09:44:31 +0100684 if (!strchr(func, '*')) {
685 String8 fancyFunc = String8::format("\n%s\n", func);
686 bool found = funcList.find(fancyFunc.string(), 0) >= 0;
687 if (!found || func[0] == '\0') {
688 fprintf(stderr, "error: \"%s\" is not a valid kernel function "
689 "to trace.\n", func);
690 ok = false;
691 }
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700692 }
693 func = strtok(NULL, ",");
694 }
695 free(myFuncs);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700696 return ok;
697}
698
699// Set the comma separated list of functions that the kernel is to trace.
700static bool setKernelTraceFuncs(const char* funcs)
701{
702 bool ok = true;
703
704 if (funcs == NULL || funcs[0] == '\0') {
705 // Disable kernel function tracing.
Jamie Gennis6f6f3f72013-03-27 15:50:30 -0700706 if (fileIsWritable(k_currentTracerPath)) {
707 ok &= writeStr(k_currentTracerPath, "nop");
708 }
709 if (fileIsWritable(k_ftraceFilterPath)) {
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700710 ok &= truncateFile(k_ftraceFilterPath);
711 }
712 } else {
713 // Enable kernel function tracing.
714 ok &= writeStr(k_currentTracerPath, "function_graph");
715 ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true);
716 ok &= setKernelOptionEnable(k_funcgraphCpuPath, true);
717 ok &= setKernelOptionEnable(k_funcgraphProcPath, true);
718 ok &= setKernelOptionEnable(k_funcgraphFlatPath, true);
719
720 // Set the requested filter functions.
721 ok &= truncateFile(k_ftraceFilterPath);
722 char* myFuncs = strdup(funcs);
723 char* func = strtok(myFuncs, ",");
724 while (func) {
725 ok &= appendStr(k_ftraceFilterPath, func);
726 func = strtok(NULL, ",");
727 }
728 free(myFuncs);
729
730 // Verify that the set functions are being traced.
731 if (ok) {
732 ok &= verifyKernelTraceFuncs(funcs);
733 }
734 }
735
736 return ok;
737}
738
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900739static bool setCategoryEnable(const char* name, bool enable)
740{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700741 for (size_t i = 0; i < arraysize(k_categories); i++) {
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900742 const TracingCategory& c = k_categories[i];
743 if (strcmp(name, c.name) == 0) {
744 if (isCategorySupported(c)) {
745 g_categoryEnables[i] = enable;
746 return true;
747 } else {
748 if (isCategorySupportedForRoot(c)) {
749 fprintf(stderr, "error: category \"%s\" requires root "
750 "privileges.\n", name);
751 } else {
752 fprintf(stderr, "error: category \"%s\" is not supported "
753 "on this device.\n", name);
754 }
755 return false;
756 }
757 }
758 }
759 fprintf(stderr, "error: unknown tracing category \"%s\"\n", name);
760 return false;
761}
762
763static bool setCategoriesEnableFromFile(const char* categories_file)
764{
765 if (!categories_file) {
766 return true;
767 }
768 Tokenizer* tokenizer = NULL;
769 if (Tokenizer::open(String8(categories_file), &tokenizer) != NO_ERROR) {
770 return false;
771 }
772 bool ok = true;
773 while (!tokenizer->isEol()) {
774 String8 token = tokenizer->nextToken(" ");
775 if (token.isEmpty()) {
776 tokenizer->skipDelimiters(" ");
777 continue;
778 }
779 ok &= setCategoryEnable(token.string(), true);
780 }
781 delete tokenizer;
782 return ok;
783}
784
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700785// Set all the kernel tracing settings to the desired state for this trace
786// capture.
787static bool setUpTrace()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800788{
789 bool ok = true;
790
791 // Set up the tracing options.
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +0900792 ok &= setCategoriesEnableFromFile(g_categoriesFile);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800793 ok &= setTraceOverwriteEnable(g_traceOverwrite);
794 ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
John Reckba54d5b2017-06-23 09:44:08 -0700795 // TODO: Re-enable after stabilization
796 //ok &= setCmdlineSize();
Carmen Jacksonea826792017-05-05 11:42:32 -0700797 ok &= setClock();
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700798 ok &= setPrintTgidEnableIfPresent(true);
799 ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800800
801 // Set up the tags property.
802 uint64_t tags = 0;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700803 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800804 if (g_categoryEnables[i]) {
805 const TracingCategory &c = k_categories[i];
806 tags |= c.tags;
807 }
808 }
809 ok &= setTagsProperty(tags);
sergeyvdb404152016-05-02 19:26:07 -0700810
811 bool coreServicesTagEnabled = false;
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700812 for (size_t i = 0; i < arraysize(k_categories); i++) {
sergeyvdb404152016-05-02 19:26:07 -0700813 if (strcmp(k_categories[i].name, k_coreServiceCategory) == 0) {
814 coreServicesTagEnabled = g_categoryEnables[i];
815 }
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700816
817 // Set whether to poke PDX services in this session.
818 if (strcmp(k_categories[i].name, k_pdxServiceCategory) == 0) {
819 g_tracePdx = g_categoryEnables[i];
820 }
sergeyvdb404152016-05-02 19:26:07 -0700821 }
822
823 std::string packageList(g_debugAppCmdLine);
824 if (coreServicesTagEnabled) {
sergeyvdb404152016-05-02 19:26:07 -0700825 if (!packageList.empty()) {
826 packageList += ",";
827 }
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700828 packageList += android::base::GetProperty(k_coreServicesProp, "");
sergeyvdb404152016-05-02 19:26:07 -0700829 }
Dan Austin09a79872016-05-31 13:27:03 -0700830 ok &= setAppCmdlineProperty(&packageList[0]);
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700831 ok &= pokeBinderServices();
Martijn Coenenee9b97e2016-11-16 16:00:26 +0100832 pokeHalServices();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800833
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700834 if (g_tracePdx) {
835 ok &= ServiceUtility::PokeServices();
836 }
837
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800838 // Disable all the sysfs enables. This is done as a separate loop from
839 // the enables to allow the same enable to exist in multiple categories.
840 ok &= disableKernelTraceEvents();
841
842 // Enable all the sysfs enables that are in an enabled category.
Elliott Hughes5fd6ff62017-03-28 14:55:31 -0700843 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800844 if (g_categoryEnables[i]) {
845 const TracingCategory &c = k_categories[i];
846 for (int j = 0; j < MAX_SYS_FILES; j++) {
847 const char* path = c.sysfiles[j].path;
848 bool required = c.sysfiles[j].required == REQ;
849 if (path != NULL) {
850 if (fileIsWritable(path)) {
851 ok &= setKernelOptionEnable(path, true);
852 } else if (required) {
853 fprintf(stderr, "error writing file %s\n", path);
854 ok = false;
855 }
856 }
857 }
858 }
859 }
860
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800861 return ok;
862}
863
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700864// Reset all the kernel tracing settings to their default state.
865static void cleanUpTrace()
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800866{
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800867 // Disable all tracing that we're able to.
868 disableKernelTraceEvents();
869
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700870 // Reset the system properties.
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800871 setTagsProperty(0);
sergeyv4144eff2016-04-28 11:40:04 -0700872 clearAppProperties();
Jamie Gennisf7f29c82013-03-27 15:50:58 -0700873 pokeBinderServices();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800874
Corey Tabakaa6c0a722017-05-31 16:37:40 -0700875 if (g_tracePdx) {
876 ServiceUtility::PokeServices();
877 }
878
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800879 // Set the options back to their defaults.
880 setTraceOverwriteEnable(true);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700881 setTraceBufferSizeKB(1);
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700882 setPrintTgidEnableIfPresent(false);
883 setKernelTraceFuncs(NULL);
884}
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800885
Jamie Gennise9b8cfb2013-03-12 16:00:10 -0700886
887// Enable tracing in the kernel.
888static bool startTrace()
889{
890 return setTracingEnabled(true);
891}
892
893// Disable tracing in the kernel.
894static void stopTrace()
895{
896 setTracingEnabled(false);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800897}
898
Martijn Coenend9535872015-11-26 10:00:55 +0100899// Read data from the tracing pipe and forward to stdout
900static void streamTrace()
901{
902 char trace_data[4096];
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800903 int traceFD = open((g_traceFolder + k_traceStreamPath).c_str(), O_RDWR);
Martijn Coenend9535872015-11-26 10:00:55 +0100904 if (traceFD == -1) {
905 fprintf(stderr, "error opening %s: %s (%d)\n", k_traceStreamPath,
906 strerror(errno), errno);
907 return;
908 }
909 while (!g_traceAborted) {
910 ssize_t bytes_read = read(traceFD, trace_data, 4096);
911 if (bytes_read > 0) {
912 write(STDOUT_FILENO, trace_data, bytes_read);
913 fflush(stdout);
914 } else {
915 if (!g_traceAborted) {
916 fprintf(stderr, "read returned %zd bytes err %d (%s)\n",
917 bytes_read, errno, strerror(errno));
918 }
919 break;
920 }
921 }
922}
923
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800924// Read the current kernel trace and write it to stdout.
John Reck40b26b42016-03-30 09:44:36 -0700925static void dumpTrace(int outFd)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800926{
John Reck6c8ac922016-03-28 11:25:30 -0700927 ALOGI("Dumping trace");
Paul Lawrence2cd93cc2017-01-17 09:50:18 -0800928 int traceFD = open((g_traceFolder + k_tracePath).c_str(), O_RDWR);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800929 if (traceFD == -1) {
930 fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath,
931 strerror(errno), errno);
932 return;
933 }
934
935 if (g_compress) {
936 z_stream zs;
Elliott Hughes3da5d232015-01-25 08:35:20 -0800937 memset(&zs, 0, sizeof(zs));
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700938
939 int result = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800940 if (result != Z_OK) {
941 fprintf(stderr, "error initializing zlib: %d\n", result);
942 close(traceFD);
943 return;
944 }
945
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700946 constexpr size_t bufSize = 64*1024;
947 std::unique_ptr<uint8_t> in(new uint8_t[bufSize]);
948 std::unique_ptr<uint8_t> out(new uint8_t[bufSize]);
949 if (!in || !out) {
950 fprintf(stderr, "couldn't allocate buffers\n");
951 close(traceFD);
952 return;
953 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800954
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700955 int flush = Z_NO_FLUSH;
956
957 zs.next_out = reinterpret_cast<Bytef*>(out.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800958 zs.avail_out = bufSize;
959
960 do {
961
962 if (zs.avail_in == 0) {
963 // More input is needed.
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700964 result = read(traceFD, in.get(), bufSize);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800965 if (result < 0) {
966 fprintf(stderr, "error reading trace: %s (%d)\n",
967 strerror(errno), errno);
968 result = Z_STREAM_END;
969 break;
970 } else if (result == 0) {
971 flush = Z_FINISH;
972 } else {
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700973 zs.next_in = reinterpret_cast<Bytef*>(in.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800974 zs.avail_in = result;
975 }
976 }
977
978 if (zs.avail_out == 0) {
979 // Need to write the output.
Elliott Hughesb59e2962016-07-22 09:00:59 -0700980 result = write(outFd, out.get(), bufSize);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800981 if ((size_t)result < bufSize) {
982 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
983 strerror(errno), errno);
984 result = Z_STREAM_END; // skip deflate error message
985 zs.avail_out = bufSize; // skip the final write
986 break;
987 }
Elliott Hughesa252f4d2016-07-21 17:12:15 -0700988 zs.next_out = reinterpret_cast<Bytef*>(out.get());
Jamie Gennis6eea6fb2012-12-07 14:03:07 -0800989 zs.avail_out = bufSize;
990 }
991
992 } while ((result = deflate(&zs, flush)) == Z_OK);
993
994 if (result != Z_STREAM_END) {
995 fprintf(stderr, "error deflating trace: %s\n", zs.msg);
996 }
997
998 if (zs.avail_out < bufSize) {
999 size_t bytes = bufSize - zs.avail_out;
Elliott Hughesb59e2962016-07-22 09:00:59 -07001000 result = write(outFd, out.get(), bytes);
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001001 if ((size_t)result < bytes) {
1002 fprintf(stderr, "error writing deflated trace: %s (%d)\n",
1003 strerror(errno), errno);
1004 }
1005 }
1006
1007 result = deflateEnd(&zs);
1008 if (result != Z_OK) {
1009 fprintf(stderr, "error cleaning up zlib: %d\n", result);
1010 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001011 } else {
Josh Gaod3d36e72017-04-11 15:21:13 -07001012 char buf[4096];
1013 ssize_t rc;
1014 while ((rc = TEMP_FAILURE_RETRY(read(traceFD, buf, sizeof(buf)))) > 0) {
1015 if (!android::base::WriteFully(outFd, buf, rc)) {
1016 fprintf(stderr, "error writing trace: %s\n", strerror(errno));
1017 break;
1018 }
1019 }
1020 if (rc == -1) {
1021 fprintf(stderr, "error dumping trace: %s\n", strerror(errno));
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001022 }
1023 }
1024
1025 close(traceFD);
1026}
1027
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -07001028static void handleSignal(int /*signo*/)
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001029{
1030 if (!g_nohup) {
1031 g_traceAborted = true;
1032 }
1033}
1034
1035static void registerSigHandler()
1036{
1037 struct sigaction sa;
1038 sigemptyset(&sa.sa_mask);
1039 sa.sa_flags = 0;
1040 sa.sa_handler = handleSignal;
1041 sigaction(SIGHUP, &sa, NULL);
1042 sigaction(SIGINT, &sa, NULL);
1043 sigaction(SIGQUIT, &sa, NULL);
1044 sigaction(SIGTERM, &sa, NULL);
1045}
1046
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001047static void listSupportedCategories()
1048{
Elliott Hughes5fd6ff62017-03-28 14:55:31 -07001049 for (size_t i = 0; i < arraysize(k_categories); i++) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001050 const TracingCategory& c = k_categories[i];
1051 if (isCategorySupported(c)) {
1052 printf(" %10s - %s\n", c.name, c.longname);
1053 }
1054 }
1055}
1056
1057// Print the command usage help to stderr.
1058static void showHelp(const char *cmd)
1059{
1060 fprintf(stderr, "usage: %s [options] [categories...]\n", cmd);
1061 fprintf(stderr, "options include:\n"
Jamie Gennisf7f29c82013-03-27 15:50:58 -07001062 " -a appname enable app-level tracing for a comma "
1063 "separated list of cmdlines\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001064 " -b N use a trace buffer size of N KB\n"
1065 " -c trace into a circular buffer\n"
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +09001066 " -f filename use the categories written in a file as space-separated\n"
1067 " values in a line\n"
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001068 " -k fname,... trace the listed kernel functions\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001069 " -n ignore signals\n"
1070 " -s N sleep for N seconds before tracing [default 0]\n"
Fabien Sanglardb5c95472016-06-08 11:40:12 -07001071 " -t N trace for N seconds [default 5]\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001072 " -z compress the trace dump\n"
Fabien Sanglardb5c95472016-06-08 11:40:12 -07001073 " --async_start start circular trace and return immediately\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001074 " --async_dump dump the current contents of circular trace buffer\n"
1075 " --async_stop stop tracing and dump the current contents of circular\n"
1076 " trace buffer\n"
Martijn Coenend9535872015-11-26 10:00:55 +01001077 " --stream stream trace to stdout as it enters the trace buffer\n"
1078 " Note: this can take significant CPU time, and is best\n"
1079 " used for measuring things that are not affected by\n"
1080 " CPU performance, like pagecache usage.\n"
Jamie Gennis92573f12012-12-07 16:29:03 -08001081 " --list_categories\n"
1082 " list the available tracing categories\n"
John Reck40b26b42016-03-30 09:44:36 -07001083 " -o filename write the trace to the specified file instead\n"
1084 " of stdout.\n"
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001085 );
1086}
1087
Paul Lawrence2cd93cc2017-01-17 09:50:18 -08001088bool findTraceFiles()
1089{
1090 static const std::string debugfs_path = "/sys/kernel/debug/tracing/";
1091 static const std::string tracefs_path = "/sys/kernel/tracing/";
1092 static const std::string trace_file = "trace_marker";
1093
1094 bool tracefs = access((tracefs_path + trace_file).c_str(), F_OK) != -1;
1095 bool debugfs = access((debugfs_path + trace_file).c_str(), F_OK) != -1;
1096
1097 if (!tracefs && !debugfs) {
1098 fprintf(stderr, "Error: Did not find trace folder\n");
1099 return false;
1100 }
1101
1102 if (tracefs) {
1103 g_traceFolder = tracefs_path;
1104 } else {
1105 g_traceFolder = debugfs_path;
1106 }
1107
1108 return true;
1109}
1110
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001111int main(int argc, char **argv)
1112{
1113 bool async = false;
1114 bool traceStart = true;
1115 bool traceStop = true;
1116 bool traceDump = true;
Martijn Coenend9535872015-11-26 10:00:55 +01001117 bool traceStream = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001118
1119 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
1120 showHelp(argv[0]);
1121 exit(0);
1122 }
1123
Paul Lawrence2cd93cc2017-01-17 09:50:18 -08001124 if (!findTraceFiles()) {
1125 fprintf(stderr, "No trace folder found\n");
1126 exit(-1);
1127 }
1128
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001129 for (;;) {
1130 int ret;
1131 int option_index = 0;
1132 static struct option long_options[] = {
1133 {"async_start", no_argument, 0, 0 },
1134 {"async_stop", no_argument, 0, 0 },
1135 {"async_dump", no_argument, 0, 0 },
1136 {"list_categories", no_argument, 0, 0 },
Martijn Coenend9535872015-11-26 10:00:55 +01001137 {"stream", no_argument, 0, 0 },
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001138 { 0, 0, 0, 0 }
1139 };
1140
John Reck40b26b42016-03-30 09:44:36 -07001141 ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:zo:",
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001142 long_options, &option_index);
1143
1144 if (ret < 0) {
1145 for (int i = optind; i < argc; i++) {
1146 if (!setCategoryEnable(argv[i], true)) {
1147 fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]);
1148 exit(1);
1149 }
1150 }
1151 break;
1152 }
1153
1154 switch(ret) {
Jamie Gennisf7f29c82013-03-27 15:50:58 -07001155 case 'a':
1156 g_debugAppCmdLine = optarg;
1157 break;
1158
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001159 case 'b':
1160 g_traceBufferSizeKB = atoi(optarg);
1161 break;
1162
1163 case 'c':
1164 g_traceOverwrite = true;
1165 break;
1166
Yasuhiro Matsuda46c51fb2015-06-29 19:20:39 +09001167 case 'f':
1168 g_categoriesFile = optarg;
1169 break;
1170
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001171 case 'k':
1172 g_kernelTraceFuncs = optarg;
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001173 break;
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001174
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001175 case 'n':
1176 g_nohup = true;
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001177 break;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001178
1179 case 's':
1180 g_initialSleepSecs = atoi(optarg);
1181 break;
1182
1183 case 't':
1184 g_traceDurationSeconds = atoi(optarg);
1185 break;
1186
1187 case 'z':
1188 g_compress = true;
1189 break;
1190
John Reck40b26b42016-03-30 09:44:36 -07001191 case 'o':
1192 g_outputFile = optarg;
1193 break;
1194
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001195 case 0:
1196 if (!strcmp(long_options[option_index].name, "async_start")) {
1197 async = true;
1198 traceStop = false;
1199 traceDump = false;
1200 g_traceOverwrite = true;
1201 } else if (!strcmp(long_options[option_index].name, "async_stop")) {
1202 async = true;
John Reck4ba2b632015-05-15 10:00:34 -07001203 traceStart = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001204 } else if (!strcmp(long_options[option_index].name, "async_dump")) {
1205 async = true;
1206 traceStart = false;
1207 traceStop = false;
Martijn Coenend9535872015-11-26 10:00:55 +01001208 } else if (!strcmp(long_options[option_index].name, "stream")) {
1209 traceStream = true;
1210 traceDump = false;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001211 } else if (!strcmp(long_options[option_index].name, "list_categories")) {
1212 listSupportedCategories();
1213 exit(0);
1214 }
Jamie Gennis6f6f3f72013-03-27 15:50:30 -07001215 break;
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001216
1217 default:
1218 fprintf(stderr, "\n");
1219 showHelp(argv[0]);
1220 exit(-1);
1221 break;
1222 }
1223 }
1224
1225 registerSigHandler();
1226
1227 if (g_initialSleepSecs > 0) {
1228 sleep(g_initialSleepSecs);
1229 }
1230
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001231 bool ok = true;
1232 ok &= setUpTrace();
1233 ok &= startTrace();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001234
1235 if (ok && traceStart) {
Martijn Coenend9535872015-11-26 10:00:55 +01001236 if (!traceStream) {
Carmen Jacksonac53e732017-05-02 16:55:33 -07001237 printf("capturing trace...");
Martijn Coenend9535872015-11-26 10:00:55 +01001238 fflush(stdout);
1239 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001240
1241 // We clear the trace after starting it because tracing gets enabled for
1242 // each CPU individually in the kernel. Having the beginning of the trace
1243 // contain entries from only one CPU can cause "begin" entries without a
1244 // matching "end" entry to show up if a task gets migrated from one CPU to
1245 // another.
1246 ok = clearTrace();
1247
Martijn Coenen0bcd97a2015-07-15 14:25:23 +02001248 writeClockSyncMarker();
Martijn Coenend9535872015-11-26 10:00:55 +01001249 if (ok && !async && !traceStream) {
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001250 // Sleep to allow the trace to be captured.
1251 struct timespec timeLeft;
1252 timeLeft.tv_sec = g_traceDurationSeconds;
1253 timeLeft.tv_nsec = 0;
1254 do {
1255 if (g_traceAborted) {
1256 break;
1257 }
1258 } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR);
1259 }
Martijn Coenend9535872015-11-26 10:00:55 +01001260
1261 if (traceStream) {
1262 streamTrace();
1263 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001264 }
1265
1266 // Stop the trace and restore the default settings.
1267 if (traceStop)
1268 stopTrace();
1269
1270 if (ok && traceDump) {
1271 if (!g_traceAborted) {
John Reck40b26b42016-03-30 09:44:36 -07001272 printf(" done\n");
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001273 fflush(stdout);
John Reck40b26b42016-03-30 09:44:36 -07001274 int outFd = STDOUT_FILENO;
1275 if (g_outputFile) {
Martijn Coenenc5791982017-02-22 09:25:31 +01001276 outFd = open(g_outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
John Reck40b26b42016-03-30 09:44:36 -07001277 }
1278 if (outFd == -1) {
1279 printf("Failed to open '%s', err=%d", g_outputFile, errno);
1280 } else {
1281 dprintf(outFd, "TRACE:\n");
1282 dumpTrace(outFd);
1283 if (g_outputFile) {
1284 close(outFd);
1285 }
1286 }
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001287 } else {
1288 printf("\ntrace aborted.\n");
1289 fflush(stdout);
1290 }
1291 clearTrace();
1292 } else if (!ok) {
1293 fprintf(stderr, "unable to start tracing\n");
1294 }
1295
1296 // Reset the trace buffer size to 1.
1297 if (traceStop)
Jamie Gennise9b8cfb2013-03-12 16:00:10 -07001298 cleanUpTrace();
Jamie Gennis6eea6fb2012-12-07 14:03:07 -08001299
1300 return g_traceAborted ? 1 : 0;
1301}