Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "atrace" |
| 18 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <getopt.h> |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 23 | #include <signal.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <stdbool.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Elliott Hughes | 3da5d23 | 2015-01-25 08:35:20 -0800 | [diff] [blame] | 28 | #include <string.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 29 | #include <sys/sendfile.h> |
| 30 | #include <time.h> |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 31 | #include <unistd.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 32 | #include <zlib.h> |
| 33 | |
Elliott Hughes | a252f4d | 2016-07-21 17:12:15 -0700 | [diff] [blame] | 34 | #include <memory> |
| 35 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 36 | #include <binder/IBinder.h> |
| 37 | #include <binder/IServiceManager.h> |
| 38 | #include <binder/Parcel.h> |
| 39 | |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 40 | #include <android/hidl/manager/1.0/IServiceManager.h> |
| 41 | #include <hidl/ServiceManagement.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 42 | #include <cutils/properties.h> |
| 43 | |
| 44 | #include <utils/String8.h> |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 45 | #include <utils/Timers.h> |
Yasuhiro Matsuda | 46c51fb | 2015-06-29 19:20:39 +0900 | [diff] [blame] | 46 | #include <utils/Tokenizer.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 47 | #include <utils/Trace.h> |
Stephane Gasparini | d8419c2 | 2016-03-02 13:45:15 +0100 | [diff] [blame] | 48 | #include <android-base/file.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 49 | |
| 50 | using namespace android; |
| 51 | |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 52 | using std::string; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 53 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 54 | |
sergeyv | 4144eff | 2016-04-28 11:40:04 -0700 | [diff] [blame] | 55 | #define MAX_SYS_FILES 10 |
| 56 | #define MAX_PACKAGES 16 |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 57 | |
| 58 | const char* k_traceTagsProperty = "debug.atrace.tags.enableflags"; |
sergeyv | 4144eff | 2016-04-28 11:40:04 -0700 | [diff] [blame] | 59 | |
| 60 | const char* k_traceAppsNumberProperty = "debug.atrace.app_number"; |
| 61 | const char* k_traceAppsPropertyTemplate = "debug.atrace.app_%d"; |
sergeyv | db40415 | 2016-05-02 19:26:07 -0700 | [diff] [blame] | 62 | const char* k_coreServiceCategory = "core_services"; |
| 63 | const char* k_coreServicesProp = "ro.atrace.core.services"; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 64 | |
| 65 | typedef enum { OPT, REQ } requiredness ; |
| 66 | |
| 67 | struct TracingCategory { |
| 68 | // The name identifying the category. |
| 69 | const char* name; |
| 70 | |
| 71 | // A longer description of the category. |
| 72 | const char* longname; |
| 73 | |
| 74 | // The userland tracing tags that the category enables. |
| 75 | uint64_t tags; |
| 76 | |
| 77 | // The fname==NULL terminated list of /sys/ files that the category |
| 78 | // enables. |
| 79 | struct { |
| 80 | // Whether the file must be writable in order to enable the tracing |
| 81 | // category. |
| 82 | requiredness required; |
| 83 | |
| 84 | // The path to the enable file. |
| 85 | const char* path; |
| 86 | } sysfiles[MAX_SYS_FILES]; |
| 87 | }; |
| 88 | |
| 89 | /* Tracing categories */ |
| 90 | static const TracingCategory k_categories[] = { |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 91 | { "gfx", "Graphics", ATRACE_TAG_GRAPHICS, { } }, |
| 92 | { "input", "Input", ATRACE_TAG_INPUT, { } }, |
| 93 | { "view", "View System", ATRACE_TAG_VIEW, { } }, |
| 94 | { "webview", "WebView", ATRACE_TAG_WEBVIEW, { } }, |
| 95 | { "wm", "Window Manager", ATRACE_TAG_WINDOW_MANAGER, { } }, |
| 96 | { "am", "Activity Manager", ATRACE_TAG_ACTIVITY_MANAGER, { } }, |
Patrick Auchter | 70ec294 | 2014-09-30 15:38:30 -0500 | [diff] [blame] | 97 | { "sm", "Sync Manager", ATRACE_TAG_SYNC_MANAGER, { } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 98 | { "audio", "Audio", ATRACE_TAG_AUDIO, { } }, |
| 99 | { "video", "Video", ATRACE_TAG_VIDEO, { } }, |
| 100 | { "camera", "Camera", ATRACE_TAG_CAMERA, { } }, |
| 101 | { "hal", "Hardware Modules", ATRACE_TAG_HAL, { } }, |
Jeff Brown | 3200b0b | 2014-08-14 19:24:47 -0700 | [diff] [blame] | 102 | { "app", "Application", ATRACE_TAG_APP, { } }, |
Dianne Hackborn | 9380d78 | 2013-04-12 14:52:35 -0700 | [diff] [blame] | 103 | { "res", "Resource Loading", ATRACE_TAG_RESOURCES, { } }, |
Jamie Gennis | eff2e8d | 2013-05-07 15:20:39 -0700 | [diff] [blame] | 104 | { "dalvik", "Dalvik VM", ATRACE_TAG_DALVIK, { } }, |
Tim Murray | f0f2841 | 2013-05-23 14:39:42 -0700 | [diff] [blame] | 105 | { "rs", "RenderScript", ATRACE_TAG_RS, { } }, |
Brigid Smith | 750aa97 | 2014-05-28 14:23:24 -0700 | [diff] [blame] | 106 | { "bionic", "Bionic C Library", ATRACE_TAG_BIONIC, { } }, |
Jeff Brown | 3200b0b | 2014-08-14 19:24:47 -0700 | [diff] [blame] | 107 | { "power", "Power Management", ATRACE_TAG_POWER, { } }, |
Todd Kennedy | 01e111b | 2015-07-31 14:36:20 -0700 | [diff] [blame] | 108 | { "pm", "Package Manager", ATRACE_TAG_PACKAGE_MANAGER, { } }, |
Yasuhiro Matsuda | 7cc4977 | 2015-07-01 01:46:25 +0900 | [diff] [blame] | 109 | { "ss", "System Server", ATRACE_TAG_SYSTEM_SERVER, { } }, |
Greg Hackmann | bbd7d99 | 2014-12-01 14:43:34 -0800 | [diff] [blame] | 110 | { "database", "Database", ATRACE_TAG_DATABASE, { } }, |
Felipe Leme | 0f97c1d | 2016-09-07 11:33:26 -0700 | [diff] [blame] | 111 | { "network", "Network", ATRACE_TAG_NETWORK, { } }, |
Josh Gao | 468b4cb | 2016-11-29 10:55:21 -0800 | [diff] [blame] | 112 | { "adb", "ADB", ATRACE_TAG_ADB, { } }, |
sergeyv | db40415 | 2016-05-02 19:26:07 -0700 | [diff] [blame] | 113 | { k_coreServiceCategory, "Core services", 0, { } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 114 | { "sched", "CPU Scheduling", 0, { |
| 115 | { REQ, "/sys/kernel/debug/tracing/events/sched/sched_switch/enable" }, |
| 116 | { REQ, "/sys/kernel/debug/tracing/events/sched/sched_wakeup/enable" }, |
Riley Andrews | 5672bb7 | 2015-11-19 13:31:17 -0800 | [diff] [blame] | 117 | { OPT, "/sys/kernel/debug/tracing/events/sched/sched_blocked_reason/enable" }, |
Ruchi Kandoi | cfe500d | 2015-11-23 13:47:20 -0800 | [diff] [blame] | 118 | { OPT, "/sys/kernel/debug/tracing/events/sched/sched_cpu_hotplug/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 119 | } }, |
Dan Willemsen | f440d39 | 2014-04-11 15:44:09 -0700 | [diff] [blame] | 120 | { "irq", "IRQ Events", 0, { |
| 121 | { REQ, "/sys/kernel/debug/tracing/events/irq/enable" }, |
Riley Andrews | 412e4f6 | 2015-11-02 21:01:34 -0800 | [diff] [blame] | 122 | { OPT, "/sys/kernel/debug/tracing/events/ipi/enable" }, |
Dan Willemsen | f440d39 | 2014-04-11 15:44:09 -0700 | [diff] [blame] | 123 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 124 | { "freq", "CPU Frequency", 0, { |
| 125 | { REQ, "/sys/kernel/debug/tracing/events/power/cpu_frequency/enable" }, |
| 126 | { OPT, "/sys/kernel/debug/tracing/events/power/clock_set_rate/enable" }, |
Ruchi Kandoi | ffcc711 | 2015-11-19 18:32:00 -0800 | [diff] [blame] | 127 | { OPT, "/sys/kernel/debug/tracing/events/power/cpu_frequency_limits/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 128 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 129 | { "membus", "Memory Bus Utilization", 0, { |
| 130 | { REQ, "/sys/kernel/debug/tracing/events/memory_bus/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 131 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 132 | { "idle", "CPU Idle", 0, { |
| 133 | { REQ, "/sys/kernel/debug/tracing/events/power/cpu_idle/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 134 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 135 | { "disk", "Disk I/O", 0, { |
Greg Hackmann | e80d32c | 2014-11-20 12:59:44 -0800 | [diff] [blame] | 136 | { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_sync_file_enter/enable" }, |
| 137 | { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_sync_file_exit/enable" }, |
| 138 | { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_write_begin/enable" }, |
| 139 | { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_write_end/enable" }, |
| 140 | { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_da_write_begin/enable" }, |
| 141 | { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_da_write_end/enable" }, |
| 142 | { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_sync_file_enter/enable" }, |
| 143 | { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_sync_file_exit/enable" }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 144 | { REQ, "/sys/kernel/debug/tracing/events/block/block_rq_issue/enable" }, |
| 145 | { REQ, "/sys/kernel/debug/tracing/events/block/block_rq_complete/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 146 | } }, |
Ken Sumrall | d3fa561 | 2013-07-03 12:32:03 -0700 | [diff] [blame] | 147 | { "mmc", "eMMC commands", 0, { |
| 148 | { REQ, "/sys/kernel/debug/tracing/events/mmc/enable" }, |
| 149 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 150 | { "load", "CPU Load", 0, { |
| 151 | { REQ, "/sys/kernel/debug/tracing/events/cpufreq_interactive/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 152 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 153 | { "sync", "Synchronization", 0, { |
| 154 | { REQ, "/sys/kernel/debug/tracing/events/sync/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 155 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 156 | { "workq", "Kernel Workqueues", 0, { |
| 157 | { REQ, "/sys/kernel/debug/tracing/events/workqueue/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 158 | } }, |
Colin Cross | 580407f | 2014-08-18 15:22:13 -0700 | [diff] [blame] | 159 | { "memreclaim", "Kernel Memory Reclaim", 0, { |
| 160 | { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_begin/enable" }, |
| 161 | { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_end/enable" }, |
| 162 | { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_wake/enable" }, |
| 163 | { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_sleep/enable" }, |
| 164 | } }, |
Aaron Schulman | c2c6ecd | 2015-02-25 08:37:09 -0800 | [diff] [blame] | 165 | { "regulators", "Voltage and Current Regulators", 0, { |
| 166 | { REQ, "/sys/kernel/debug/tracing/events/regulator/enable" }, |
| 167 | } }, |
Scott Bauer | ae47336 | 2015-06-08 16:32:36 -0700 | [diff] [blame] | 168 | { "binder_driver", "Binder Kernel driver", 0, { |
| 169 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_transaction/enable" }, |
| 170 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_transaction_received/enable" }, |
| 171 | } }, |
| 172 | { "binder_lock", "Binder global lock trace", 0, { |
| 173 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_lock/enable" }, |
| 174 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_locked/enable" }, |
| 175 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_unlock/enable" }, |
| 176 | } }, |
Martijn Coenen | 7048161 | 2015-10-23 13:57:05 +0200 | [diff] [blame] | 177 | { "pagecache", "Page cache", 0, { |
| 178 | { REQ, "/sys/kernel/debug/tracing/events/filemap/enable" }, |
| 179 | } }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /* Command line options */ |
| 183 | static int g_traceDurationSeconds = 5; |
| 184 | static bool g_traceOverwrite = false; |
| 185 | static int g_traceBufferSizeKB = 2048; |
| 186 | static bool g_compress = false; |
| 187 | static bool g_nohup = false; |
| 188 | static int g_initialSleepSecs = 0; |
Yasuhiro Matsuda | 46c51fb | 2015-06-29 19:20:39 +0900 | [diff] [blame] | 189 | static const char* g_categoriesFile = NULL; |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 190 | static const char* g_kernelTraceFuncs = NULL; |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 191 | static const char* g_debugAppCmdLine = ""; |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 192 | static const char* g_outputFile = nullptr; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 193 | |
| 194 | /* Global state */ |
| 195 | static bool g_traceAborted = false; |
| 196 | static bool g_categoryEnables[NELEM(k_categories)] = {}; |
| 197 | |
| 198 | /* Sys file paths */ |
| 199 | static const char* k_traceClockPath = |
| 200 | "/sys/kernel/debug/tracing/trace_clock"; |
| 201 | |
| 202 | static const char* k_traceBufferSizePath = |
| 203 | "/sys/kernel/debug/tracing/buffer_size_kb"; |
| 204 | |
| 205 | static const char* k_tracingOverwriteEnablePath = |
| 206 | "/sys/kernel/debug/tracing/options/overwrite"; |
| 207 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 208 | static const char* k_currentTracerPath = |
| 209 | "/sys/kernel/debug/tracing/current_tracer"; |
| 210 | |
| 211 | static const char* k_printTgidPath = |
| 212 | "/sys/kernel/debug/tracing/options/print-tgid"; |
| 213 | |
| 214 | static const char* k_funcgraphAbsTimePath = |
| 215 | "/sys/kernel/debug/tracing/options/funcgraph-abstime"; |
| 216 | |
| 217 | static const char* k_funcgraphCpuPath = |
| 218 | "/sys/kernel/debug/tracing/options/funcgraph-cpu"; |
| 219 | |
| 220 | static const char* k_funcgraphProcPath = |
| 221 | "/sys/kernel/debug/tracing/options/funcgraph-proc"; |
| 222 | |
| 223 | static const char* k_funcgraphFlatPath = |
| 224 | "/sys/kernel/debug/tracing/options/funcgraph-flat"; |
| 225 | |
| 226 | static const char* k_funcgraphDurationPath = |
| 227 | "/sys/kernel/debug/tracing/options/funcgraph-duration"; |
| 228 | |
| 229 | static const char* k_ftraceFilterPath = |
| 230 | "/sys/kernel/debug/tracing/set_ftrace_filter"; |
| 231 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 232 | static const char* k_tracingOnPath = |
| 233 | "/sys/kernel/debug/tracing/tracing_on"; |
| 234 | |
| 235 | static const char* k_tracePath = |
| 236 | "/sys/kernel/debug/tracing/trace"; |
| 237 | |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 238 | static const char* k_traceStreamPath = |
| 239 | "/sys/kernel/debug/tracing/trace_pipe"; |
| 240 | |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 241 | static const char* k_traceMarkerPath = |
| 242 | "/sys/kernel/debug/tracing/trace_marker"; |
| 243 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 244 | // Check whether a file exists. |
| 245 | static bool fileExists(const char* filename) { |
| 246 | return access(filename, F_OK) != -1; |
| 247 | } |
| 248 | |
| 249 | // Check whether a file is writable. |
| 250 | static bool fileIsWritable(const char* filename) { |
| 251 | return access(filename, W_OK) != -1; |
| 252 | } |
| 253 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 254 | // Truncate a file. |
| 255 | static bool truncateFile(const char* path) |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 256 | { |
Jamie Gennis | 43122e7 | 2013-03-21 14:06:31 -0700 | [diff] [blame] | 257 | // This uses creat rather than truncate because some of the debug kernel |
| 258 | // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by |
| 259 | // calls to truncate, but they are cleared by calls to creat. |
| 260 | int traceFD = creat(path, 0); |
| 261 | if (traceFD == -1) { |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 262 | fprintf(stderr, "error truncating %s: %s (%d)\n", path, |
Jamie Gennis | 43122e7 | 2013-03-21 14:06:31 -0700 | [diff] [blame] | 263 | strerror(errno), errno); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | |
Jamie Gennis | 43122e7 | 2013-03-21 14:06:31 -0700 | [diff] [blame] | 267 | close(traceFD); |
| 268 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 269 | return true; |
| 270 | } |
| 271 | |
| 272 | static bool _writeStr(const char* filename, const char* str, int flags) |
| 273 | { |
| 274 | int fd = open(filename, flags); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 275 | if (fd == -1) { |
| 276 | fprintf(stderr, "error opening %s: %s (%d)\n", filename, |
| 277 | strerror(errno), errno); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | bool ok = true; |
| 282 | ssize_t len = strlen(str); |
| 283 | if (write(fd, str, len) != len) { |
| 284 | fprintf(stderr, "error writing to %s: %s (%d)\n", filename, |
| 285 | strerror(errno), errno); |
| 286 | ok = false; |
| 287 | } |
| 288 | |
| 289 | close(fd); |
| 290 | |
| 291 | return ok; |
| 292 | } |
| 293 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 294 | // Write a string to a file, returning true if the write was successful. |
| 295 | static bool writeStr(const char* filename, const char* str) |
| 296 | { |
| 297 | return _writeStr(filename, str, O_WRONLY); |
| 298 | } |
| 299 | |
| 300 | // Append a string to a file, returning true if the write was successful. |
| 301 | static bool appendStr(const char* filename, const char* str) |
| 302 | { |
| 303 | return _writeStr(filename, str, O_APPEND|O_WRONLY); |
| 304 | } |
| 305 | |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 306 | static void writeClockSyncMarker() |
| 307 | { |
| 308 | char buffer[128]; |
Martijn Coenen | 0bcd97a | 2015-07-15 14:25:23 +0200 | [diff] [blame] | 309 | int len = 0; |
| 310 | int fd = open(k_traceMarkerPath, O_WRONLY); |
| 311 | if (fd == -1) { |
| 312 | fprintf(stderr, "error opening %s: %s (%d)\n", k_traceMarkerPath, |
| 313 | strerror(errno), errno); |
| 314 | return; |
| 315 | } |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 316 | float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f; |
Martijn Coenen | 0bcd97a | 2015-07-15 14:25:23 +0200 | [diff] [blame] | 317 | |
| 318 | len = snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds); |
| 319 | if (write(fd, buffer, len) != len) { |
| 320 | fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno); |
| 321 | } |
| 322 | |
| 323 | int64_t realtime_in_ms = systemTime(CLOCK_REALTIME) / 1000000; |
| 324 | len = snprintf(buffer, 128, "trace_event_clock_sync: realtime_ts=%" PRId64 "\n", realtime_in_ms); |
| 325 | if (write(fd, buffer, len) != len) { |
| 326 | fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno); |
| 327 | } |
| 328 | |
| 329 | close(fd); |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 332 | // Enable or disable a kernel option by writing a "1" or a "0" into a /sys |
| 333 | // file. |
| 334 | static bool setKernelOptionEnable(const char* filename, bool enable) |
| 335 | { |
| 336 | return writeStr(filename, enable ? "1" : "0"); |
| 337 | } |
| 338 | |
| 339 | // Check whether the category is supported on the device with the current |
| 340 | // rootness. A category is supported only if all its required /sys/ files are |
| 341 | // writable and if enabling the category will enable one or more tracing tags |
| 342 | // or /sys/ files. |
| 343 | static bool isCategorySupported(const TracingCategory& category) |
| 344 | { |
sergeyv | db40415 | 2016-05-02 19:26:07 -0700 | [diff] [blame] | 345 | if (strcmp(category.name, k_coreServiceCategory) == 0) { |
| 346 | char value[PROPERTY_VALUE_MAX]; |
| 347 | property_get(k_coreServicesProp, value, ""); |
| 348 | return strlen(value) != 0; |
| 349 | } |
| 350 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 351 | bool ok = category.tags != 0; |
| 352 | for (int i = 0; i < MAX_SYS_FILES; i++) { |
| 353 | const char* path = category.sysfiles[i].path; |
| 354 | bool req = category.sysfiles[i].required == REQ; |
| 355 | if (path != NULL) { |
| 356 | if (req) { |
| 357 | if (!fileIsWritable(path)) { |
| 358 | return false; |
| 359 | } else { |
| 360 | ok = true; |
| 361 | } |
| 362 | } else { |
| 363 | ok |= fileIsWritable(path); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | return ok; |
| 368 | } |
| 369 | |
| 370 | // Check whether the category would be supported on the device if the user |
| 371 | // were root. This function assumes that root is able to write to any file |
| 372 | // that exists. It performs the same logic as isCategorySupported, but it |
| 373 | // uses file existance rather than writability in the /sys/ file checks. |
| 374 | static bool isCategorySupportedForRoot(const TracingCategory& category) |
| 375 | { |
| 376 | bool ok = category.tags != 0; |
| 377 | for (int i = 0; i < MAX_SYS_FILES; i++) { |
| 378 | const char* path = category.sysfiles[i].path; |
| 379 | bool req = category.sysfiles[i].required == REQ; |
| 380 | if (path != NULL) { |
| 381 | if (req) { |
| 382 | if (!fileExists(path)) { |
| 383 | return false; |
| 384 | } else { |
| 385 | ok = true; |
| 386 | } |
| 387 | } else { |
| 388 | ok |= fileExists(path); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | return ok; |
| 393 | } |
| 394 | |
| 395 | // Enable or disable overwriting of the kernel trace buffers. Disabling this |
| 396 | // will cause tracing to stop once the trace buffers have filled up. |
| 397 | static bool setTraceOverwriteEnable(bool enable) |
| 398 | { |
| 399 | return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable); |
| 400 | } |
| 401 | |
| 402 | // Enable or disable kernel tracing. |
| 403 | static bool setTracingEnabled(bool enable) |
| 404 | { |
| 405 | return setKernelOptionEnable(k_tracingOnPath, enable); |
| 406 | } |
| 407 | |
| 408 | // Clear the contents of the kernel trace. |
| 409 | static bool clearTrace() |
| 410 | { |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 411 | return truncateFile(k_tracePath); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // Set the size of the kernel's trace buffer in kilobytes. |
| 415 | static bool setTraceBufferSizeKB(int size) |
| 416 | { |
| 417 | char str[32] = "1"; |
| 418 | int len; |
| 419 | if (size < 1) { |
| 420 | size = 1; |
| 421 | } |
| 422 | snprintf(str, 32, "%d", size); |
| 423 | return writeStr(k_traceBufferSizePath, str); |
| 424 | } |
| 425 | |
Colin Cross | b1ce49b | 2014-08-20 14:28:47 -0700 | [diff] [blame] | 426 | // Read the trace_clock sysfs file and return true if it matches the requested |
| 427 | // value. The trace_clock file format is: |
| 428 | // local [global] counter uptime perf |
| 429 | static bool isTraceClock(const char *mode) |
| 430 | { |
| 431 | int fd = open(k_traceClockPath, O_RDONLY); |
| 432 | if (fd == -1) { |
| 433 | fprintf(stderr, "error opening %s: %s (%d)\n", k_traceClockPath, |
| 434 | strerror(errno), errno); |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | char buf[4097]; |
| 439 | ssize_t n = read(fd, buf, 4096); |
| 440 | close(fd); |
| 441 | if (n == -1) { |
| 442 | fprintf(stderr, "error reading %s: %s (%d)\n", k_traceClockPath, |
| 443 | strerror(errno), errno); |
| 444 | return false; |
| 445 | } |
| 446 | buf[n] = '\0'; |
| 447 | |
| 448 | char *start = strchr(buf, '['); |
| 449 | if (start == NULL) { |
| 450 | return false; |
| 451 | } |
| 452 | start++; |
| 453 | |
| 454 | char *end = strchr(start, ']'); |
| 455 | if (end == NULL) { |
| 456 | return false; |
| 457 | } |
| 458 | *end = '\0'; |
| 459 | |
| 460 | return strcmp(mode, start) == 0; |
| 461 | } |
| 462 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 463 | // Enable or disable the kernel's use of the global clock. Disabling the global |
| 464 | // clock will result in the kernel using a per-CPU local clock. |
Colin Cross | b1ce49b | 2014-08-20 14:28:47 -0700 | [diff] [blame] | 465 | // Any write to the trace_clock sysfs file will reset the buffer, so only |
| 466 | // update it if the requested value is not the current value. |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 467 | static bool setGlobalClockEnable(bool enable) |
| 468 | { |
Colin Cross | b1ce49b | 2014-08-20 14:28:47 -0700 | [diff] [blame] | 469 | const char *clock = enable ? "global" : "local"; |
| 470 | |
| 471 | if (isTraceClock(clock)) { |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | return writeStr(k_traceClockPath, clock); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 478 | static bool setPrintTgidEnableIfPresent(bool enable) |
| 479 | { |
| 480 | if (fileExists(k_printTgidPath)) { |
| 481 | return setKernelOptionEnable(k_printTgidPath, enable); |
| 482 | } |
| 483 | return true; |
| 484 | } |
| 485 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 486 | // Poke all the binder-enabled processes in the system to get them to re-read |
| 487 | // their system properties. |
| 488 | static bool pokeBinderServices() |
| 489 | { |
| 490 | sp<IServiceManager> sm = defaultServiceManager(); |
| 491 | Vector<String16> services = sm->listServices(); |
| 492 | for (size_t i = 0; i < services.size(); i++) { |
| 493 | sp<IBinder> obj = sm->checkService(services[i]); |
| 494 | if (obj != NULL) { |
| 495 | Parcel data; |
| 496 | if (obj->transact(IBinder::SYSPROPS_TRANSACTION, data, |
| 497 | NULL, 0) != OK) { |
| 498 | if (false) { |
| 499 | // XXX: For some reason this fails on tablets trying to |
| 500 | // poke the "phone" service. It's not clear whether some |
| 501 | // are expected to fail. |
| 502 | String8 svc(services[i]); |
| 503 | fprintf(stderr, "error poking binder service %s\n", |
| 504 | svc.string()); |
| 505 | return false; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | return true; |
| 511 | } |
| 512 | |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 513 | // Poke all the HAL processes in the system to get them to re-read |
| 514 | // their system properties. |
| 515 | static void pokeHalServices() |
| 516 | { |
| 517 | using ::android::hidl::manager::V1_0::IServiceManager; |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 518 | using ::android::hardware::hidl_string; |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 519 | |
| 520 | sp<IServiceManager> sm = ::android::hardware::defaultServiceManager(); |
Steven Moreland | a42866b | 2016-12-12 15:18:06 -0800 | [diff] [blame] | 521 | |
| 522 | if (sm == nullptr) { |
| 523 | fprintf(stderr, "failed to get IServiceManager to poke hal services\n"); |
| 524 | return; |
| 525 | } |
| 526 | |
Yifan Hong | 8cf4ed1 | 2016-11-30 17:28:58 -0800 | [diff] [blame] | 527 | auto listRet = sm->list([&](const auto &interfaces) { |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 528 | for (size_t i = 0; i < interfaces.size(); i++) { |
| 529 | string fqInstanceName = interfaces[i]; |
| 530 | string::size_type n = fqInstanceName.find("/"); |
| 531 | if (n == std::string::npos || interfaces[i].size() == n+1) |
| 532 | continue; |
| 533 | hidl_string fqInterfaceName = fqInstanceName.substr(0, n); |
| 534 | hidl_string instanceName = fqInstanceName.substr(n+1, std::string::npos); |
Yifan Hong | 8cf4ed1 | 2016-11-30 17:28:58 -0800 | [diff] [blame] | 535 | auto getRet = sm->get(fqInterfaceName, instanceName, [&](const auto &interface) { |
Yifan Hong | 61954a4 | 2016-12-05 12:58:05 -0800 | [diff] [blame] | 536 | auto notifyRet = interface->notifySyspropsChanged(); |
| 537 | if (!notifyRet.isOk()) { |
| 538 | fprintf(stderr, "failed to notifySyspropsChanged on service %s: %s\n", |
| 539 | fqInstanceName.c_str(), |
| 540 | notifyRet.getStatus().toString8().string()); |
| 541 | } |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 542 | }); |
Yifan Hong | 8cf4ed1 | 2016-11-30 17:28:58 -0800 | [diff] [blame] | 543 | if (!getRet.isOk()) { |
| 544 | fprintf(stderr, "failed to get service %s: %s\n", |
| 545 | fqInstanceName.c_str(), |
| 546 | getRet.getStatus().toString8().string()); |
| 547 | } |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 548 | } |
| 549 | }); |
Yifan Hong | 8cf4ed1 | 2016-11-30 17:28:58 -0800 | [diff] [blame] | 550 | if (!listRet.isOk()) { |
| 551 | fprintf(stderr, "failed to list services: %s\n", listRet.getStatus().toString8().string()); |
| 552 | } |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 553 | } |
| 554 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 555 | // Set the trace tags that userland tracing uses, and poke the running |
| 556 | // processes to pick up the new value. |
| 557 | static bool setTagsProperty(uint64_t tags) |
| 558 | { |
sergeyv | 4144eff | 2016-04-28 11:40:04 -0700 | [diff] [blame] | 559 | char buf[PROPERTY_VALUE_MAX]; |
| 560 | snprintf(buf, sizeof(buf), "%#" PRIx64, tags); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 561 | if (property_set(k_traceTagsProperty, buf) < 0) { |
| 562 | fprintf(stderr, "error setting trace tags system property\n"); |
| 563 | return false; |
| 564 | } |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | |
sergeyv | 4144eff | 2016-04-28 11:40:04 -0700 | [diff] [blame] | 568 | static void clearAppProperties() |
| 569 | { |
| 570 | char buf[PROPERTY_KEY_MAX]; |
| 571 | for (int i = 0; i < MAX_PACKAGES; i++) { |
| 572 | snprintf(buf, sizeof(buf), k_traceAppsPropertyTemplate, i); |
| 573 | if (property_set(buf, "") < 0) { |
| 574 | fprintf(stderr, "failed to clear system property: %s\n", buf); |
| 575 | } |
| 576 | } |
| 577 | if (property_set(k_traceAppsNumberProperty, "") < 0) { |
| 578 | fprintf(stderr, "failed to clear system property: %s", |
| 579 | k_traceAppsNumberProperty); |
| 580 | } |
| 581 | } |
| 582 | |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 583 | // Set the system property that indicates which apps should perform |
| 584 | // application-level tracing. |
Dan Austin | 497951c | 2016-05-31 13:27:03 -0700 | [diff] [blame] | 585 | static bool setAppCmdlineProperty(char* cmdline) |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 586 | { |
sergeyv | 4144eff | 2016-04-28 11:40:04 -0700 | [diff] [blame] | 587 | char buf[PROPERTY_KEY_MAX]; |
| 588 | int i = 0; |
Dan Austin | 497951c | 2016-05-31 13:27:03 -0700 | [diff] [blame] | 589 | char* start = cmdline; |
sergeyv | 4144eff | 2016-04-28 11:40:04 -0700 | [diff] [blame] | 590 | while (start != NULL) { |
| 591 | if (i == MAX_PACKAGES) { |
| 592 | fprintf(stderr, "error: only 16 packages could be traced at once\n"); |
| 593 | clearAppProperties(); |
| 594 | return false; |
| 595 | } |
| 596 | char* end = strchr(start, ','); |
| 597 | if (end != NULL) { |
| 598 | *end = '\0'; |
| 599 | end++; |
| 600 | } |
| 601 | snprintf(buf, sizeof(buf), k_traceAppsPropertyTemplate, i); |
| 602 | if (property_set(buf, start) < 0) { |
| 603 | fprintf(stderr, "error setting trace app %d property to %s\n", i, buf); |
| 604 | clearAppProperties(); |
| 605 | return false; |
| 606 | } |
| 607 | start = end; |
| 608 | i++; |
| 609 | } |
| 610 | |
| 611 | snprintf(buf, sizeof(buf), "%d", i); |
| 612 | if (property_set(k_traceAppsNumberProperty, buf) < 0) { |
| 613 | fprintf(stderr, "error setting trace app number property to %s\n", buf); |
| 614 | clearAppProperties(); |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | return true; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // Disable all /sys/ enable files. |
| 621 | static bool disableKernelTraceEvents() { |
| 622 | bool ok = true; |
| 623 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 624 | const TracingCategory &c = k_categories[i]; |
| 625 | for (int j = 0; j < MAX_SYS_FILES; j++) { |
| 626 | const char* path = c.sysfiles[j].path; |
| 627 | if (path != NULL && fileIsWritable(path)) { |
| 628 | ok &= setKernelOptionEnable(path, false); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | return ok; |
| 633 | } |
| 634 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 635 | // Verify that the comma separated list of functions are being traced by the |
| 636 | // kernel. |
| 637 | static bool verifyKernelTraceFuncs(const char* funcs) |
| 638 | { |
Stephane Gasparini | d8419c2 | 2016-03-02 13:45:15 +0100 | [diff] [blame] | 639 | std::string buf; |
| 640 | if (!android::base::ReadFileToString(k_ftraceFilterPath, &buf)) { |
| 641 | fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath, |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 642 | strerror(errno), errno); |
Stephane Gasparini | d8419c2 | 2016-03-02 13:45:15 +0100 | [diff] [blame] | 643 | return false; |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Stephane Gasparini | d8419c2 | 2016-03-02 13:45:15 +0100 | [diff] [blame] | 646 | String8 funcList = String8::format("\n%s",buf.c_str()); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 647 | |
| 648 | // Make sure that every function listed in funcs is in the list we just |
Thomas Buhot | a2c2287 | 2016-01-27 09:44:31 +0100 | [diff] [blame] | 649 | // read from the kernel, except for wildcard inputs. |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 650 | bool ok = true; |
| 651 | char* myFuncs = strdup(funcs); |
| 652 | char* func = strtok(myFuncs, ","); |
| 653 | while (func) { |
Thomas Buhot | a2c2287 | 2016-01-27 09:44:31 +0100 | [diff] [blame] | 654 | if (!strchr(func, '*')) { |
| 655 | String8 fancyFunc = String8::format("\n%s\n", func); |
| 656 | bool found = funcList.find(fancyFunc.string(), 0) >= 0; |
| 657 | if (!found || func[0] == '\0') { |
| 658 | fprintf(stderr, "error: \"%s\" is not a valid kernel function " |
| 659 | "to trace.\n", func); |
| 660 | ok = false; |
| 661 | } |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 662 | } |
| 663 | func = strtok(NULL, ","); |
| 664 | } |
| 665 | free(myFuncs); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 666 | return ok; |
| 667 | } |
| 668 | |
| 669 | // Set the comma separated list of functions that the kernel is to trace. |
| 670 | static bool setKernelTraceFuncs(const char* funcs) |
| 671 | { |
| 672 | bool ok = true; |
| 673 | |
| 674 | if (funcs == NULL || funcs[0] == '\0') { |
| 675 | // Disable kernel function tracing. |
Jamie Gennis | 6f6f3f7 | 2013-03-27 15:50:30 -0700 | [diff] [blame] | 676 | if (fileIsWritable(k_currentTracerPath)) { |
| 677 | ok &= writeStr(k_currentTracerPath, "nop"); |
| 678 | } |
| 679 | if (fileIsWritable(k_ftraceFilterPath)) { |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 680 | ok &= truncateFile(k_ftraceFilterPath); |
| 681 | } |
| 682 | } else { |
| 683 | // Enable kernel function tracing. |
| 684 | ok &= writeStr(k_currentTracerPath, "function_graph"); |
| 685 | ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true); |
| 686 | ok &= setKernelOptionEnable(k_funcgraphCpuPath, true); |
| 687 | ok &= setKernelOptionEnable(k_funcgraphProcPath, true); |
| 688 | ok &= setKernelOptionEnable(k_funcgraphFlatPath, true); |
| 689 | |
| 690 | // Set the requested filter functions. |
| 691 | ok &= truncateFile(k_ftraceFilterPath); |
| 692 | char* myFuncs = strdup(funcs); |
| 693 | char* func = strtok(myFuncs, ","); |
| 694 | while (func) { |
| 695 | ok &= appendStr(k_ftraceFilterPath, func); |
| 696 | func = strtok(NULL, ","); |
| 697 | } |
| 698 | free(myFuncs); |
| 699 | |
| 700 | // Verify that the set functions are being traced. |
| 701 | if (ok) { |
| 702 | ok &= verifyKernelTraceFuncs(funcs); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return ok; |
| 707 | } |
| 708 | |
Yasuhiro Matsuda | 46c51fb | 2015-06-29 19:20:39 +0900 | [diff] [blame] | 709 | static bool setCategoryEnable(const char* name, bool enable) |
| 710 | { |
| 711 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 712 | const TracingCategory& c = k_categories[i]; |
| 713 | if (strcmp(name, c.name) == 0) { |
| 714 | if (isCategorySupported(c)) { |
| 715 | g_categoryEnables[i] = enable; |
| 716 | return true; |
| 717 | } else { |
| 718 | if (isCategorySupportedForRoot(c)) { |
| 719 | fprintf(stderr, "error: category \"%s\" requires root " |
| 720 | "privileges.\n", name); |
| 721 | } else { |
| 722 | fprintf(stderr, "error: category \"%s\" is not supported " |
| 723 | "on this device.\n", name); |
| 724 | } |
| 725 | return false; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | fprintf(stderr, "error: unknown tracing category \"%s\"\n", name); |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | static bool setCategoriesEnableFromFile(const char* categories_file) |
| 734 | { |
| 735 | if (!categories_file) { |
| 736 | return true; |
| 737 | } |
| 738 | Tokenizer* tokenizer = NULL; |
| 739 | if (Tokenizer::open(String8(categories_file), &tokenizer) != NO_ERROR) { |
| 740 | return false; |
| 741 | } |
| 742 | bool ok = true; |
| 743 | while (!tokenizer->isEol()) { |
| 744 | String8 token = tokenizer->nextToken(" "); |
| 745 | if (token.isEmpty()) { |
| 746 | tokenizer->skipDelimiters(" "); |
| 747 | continue; |
| 748 | } |
| 749 | ok &= setCategoryEnable(token.string(), true); |
| 750 | } |
| 751 | delete tokenizer; |
| 752 | return ok; |
| 753 | } |
| 754 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 755 | // Set all the kernel tracing settings to the desired state for this trace |
| 756 | // capture. |
| 757 | static bool setUpTrace() |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 758 | { |
| 759 | bool ok = true; |
| 760 | |
| 761 | // Set up the tracing options. |
Yasuhiro Matsuda | 46c51fb | 2015-06-29 19:20:39 +0900 | [diff] [blame] | 762 | ok &= setCategoriesEnableFromFile(g_categoriesFile); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 763 | ok &= setTraceOverwriteEnable(g_traceOverwrite); |
| 764 | ok &= setTraceBufferSizeKB(g_traceBufferSizeKB); |
| 765 | ok &= setGlobalClockEnable(true); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 766 | ok &= setPrintTgidEnableIfPresent(true); |
| 767 | ok &= setKernelTraceFuncs(g_kernelTraceFuncs); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 768 | |
| 769 | // Set up the tags property. |
| 770 | uint64_t tags = 0; |
| 771 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 772 | if (g_categoryEnables[i]) { |
| 773 | const TracingCategory &c = k_categories[i]; |
| 774 | tags |= c.tags; |
| 775 | } |
| 776 | } |
| 777 | ok &= setTagsProperty(tags); |
sergeyv | db40415 | 2016-05-02 19:26:07 -0700 | [diff] [blame] | 778 | |
| 779 | bool coreServicesTagEnabled = false; |
| 780 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 781 | if (strcmp(k_categories[i].name, k_coreServiceCategory) == 0) { |
| 782 | coreServicesTagEnabled = g_categoryEnables[i]; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | std::string packageList(g_debugAppCmdLine); |
| 787 | if (coreServicesTagEnabled) { |
| 788 | char value[PROPERTY_VALUE_MAX]; |
| 789 | property_get(k_coreServicesProp, value, ""); |
| 790 | if (!packageList.empty()) { |
| 791 | packageList += ","; |
| 792 | } |
| 793 | packageList += value; |
| 794 | } |
Dan Austin | 497951c | 2016-05-31 13:27:03 -0700 | [diff] [blame] | 795 | ok &= setAppCmdlineProperty(&packageList[0]); |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 796 | ok &= pokeBinderServices(); |
Martijn Coenen | ee9b97e | 2016-11-16 16:00:26 +0100 | [diff] [blame] | 797 | pokeHalServices(); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 798 | |
| 799 | // Disable all the sysfs enables. This is done as a separate loop from |
| 800 | // the enables to allow the same enable to exist in multiple categories. |
| 801 | ok &= disableKernelTraceEvents(); |
| 802 | |
| 803 | // Enable all the sysfs enables that are in an enabled category. |
| 804 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 805 | if (g_categoryEnables[i]) { |
| 806 | const TracingCategory &c = k_categories[i]; |
| 807 | for (int j = 0; j < MAX_SYS_FILES; j++) { |
| 808 | const char* path = c.sysfiles[j].path; |
| 809 | bool required = c.sysfiles[j].required == REQ; |
| 810 | if (path != NULL) { |
| 811 | if (fileIsWritable(path)) { |
| 812 | ok &= setKernelOptionEnable(path, true); |
| 813 | } else if (required) { |
| 814 | fprintf(stderr, "error writing file %s\n", path); |
| 815 | ok = false; |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 822 | return ok; |
| 823 | } |
| 824 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 825 | // Reset all the kernel tracing settings to their default state. |
| 826 | static void cleanUpTrace() |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 827 | { |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 828 | // Disable all tracing that we're able to. |
| 829 | disableKernelTraceEvents(); |
| 830 | |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 831 | // Reset the system properties. |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 832 | setTagsProperty(0); |
sergeyv | 4144eff | 2016-04-28 11:40:04 -0700 | [diff] [blame] | 833 | clearAppProperties(); |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 834 | pokeBinderServices(); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 835 | |
| 836 | // Set the options back to their defaults. |
| 837 | setTraceOverwriteEnable(true); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 838 | setTraceBufferSizeKB(1); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 839 | setGlobalClockEnable(false); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 840 | setPrintTgidEnableIfPresent(false); |
| 841 | setKernelTraceFuncs(NULL); |
| 842 | } |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 843 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 844 | |
| 845 | // Enable tracing in the kernel. |
| 846 | static bool startTrace() |
| 847 | { |
| 848 | return setTracingEnabled(true); |
| 849 | } |
| 850 | |
| 851 | // Disable tracing in the kernel. |
| 852 | static void stopTrace() |
| 853 | { |
| 854 | setTracingEnabled(false); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 857 | // Read data from the tracing pipe and forward to stdout |
| 858 | static void streamTrace() |
| 859 | { |
| 860 | char trace_data[4096]; |
| 861 | int traceFD = open(k_traceStreamPath, O_RDWR); |
| 862 | if (traceFD == -1) { |
| 863 | fprintf(stderr, "error opening %s: %s (%d)\n", k_traceStreamPath, |
| 864 | strerror(errno), errno); |
| 865 | return; |
| 866 | } |
| 867 | while (!g_traceAborted) { |
| 868 | ssize_t bytes_read = read(traceFD, trace_data, 4096); |
| 869 | if (bytes_read > 0) { |
| 870 | write(STDOUT_FILENO, trace_data, bytes_read); |
| 871 | fflush(stdout); |
| 872 | } else { |
| 873 | if (!g_traceAborted) { |
| 874 | fprintf(stderr, "read returned %zd bytes err %d (%s)\n", |
| 875 | bytes_read, errno, strerror(errno)); |
| 876 | } |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 882 | // Read the current kernel trace and write it to stdout. |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 883 | static void dumpTrace(int outFd) |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 884 | { |
John Reck | 6c8ac92 | 2016-03-28 11:25:30 -0700 | [diff] [blame] | 885 | ALOGI("Dumping trace"); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 886 | int traceFD = open(k_tracePath, O_RDWR); |
| 887 | if (traceFD == -1) { |
| 888 | fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath, |
| 889 | strerror(errno), errno); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | if (g_compress) { |
| 894 | z_stream zs; |
Elliott Hughes | 3da5d23 | 2015-01-25 08:35:20 -0800 | [diff] [blame] | 895 | memset(&zs, 0, sizeof(zs)); |
Elliott Hughes | a252f4d | 2016-07-21 17:12:15 -0700 | [diff] [blame] | 896 | |
| 897 | int result = deflateInit(&zs, Z_DEFAULT_COMPRESSION); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 898 | if (result != Z_OK) { |
| 899 | fprintf(stderr, "error initializing zlib: %d\n", result); |
| 900 | close(traceFD); |
| 901 | return; |
| 902 | } |
| 903 | |
Elliott Hughes | a252f4d | 2016-07-21 17:12:15 -0700 | [diff] [blame] | 904 | constexpr size_t bufSize = 64*1024; |
| 905 | std::unique_ptr<uint8_t> in(new uint8_t[bufSize]); |
| 906 | std::unique_ptr<uint8_t> out(new uint8_t[bufSize]); |
| 907 | if (!in || !out) { |
| 908 | fprintf(stderr, "couldn't allocate buffers\n"); |
| 909 | close(traceFD); |
| 910 | return; |
| 911 | } |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 912 | |
Elliott Hughes | a252f4d | 2016-07-21 17:12:15 -0700 | [diff] [blame] | 913 | int flush = Z_NO_FLUSH; |
| 914 | |
| 915 | zs.next_out = reinterpret_cast<Bytef*>(out.get()); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 916 | zs.avail_out = bufSize; |
| 917 | |
| 918 | do { |
| 919 | |
| 920 | if (zs.avail_in == 0) { |
| 921 | // More input is needed. |
Elliott Hughes | a252f4d | 2016-07-21 17:12:15 -0700 | [diff] [blame] | 922 | result = read(traceFD, in.get(), bufSize); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 923 | if (result < 0) { |
| 924 | fprintf(stderr, "error reading trace: %s (%d)\n", |
| 925 | strerror(errno), errno); |
| 926 | result = Z_STREAM_END; |
| 927 | break; |
| 928 | } else if (result == 0) { |
| 929 | flush = Z_FINISH; |
| 930 | } else { |
Elliott Hughes | a252f4d | 2016-07-21 17:12:15 -0700 | [diff] [blame] | 931 | zs.next_in = reinterpret_cast<Bytef*>(in.get()); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 932 | zs.avail_in = result; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | if (zs.avail_out == 0) { |
| 937 | // Need to write the output. |
Elliott Hughes | b59e296 | 2016-07-22 09:00:59 -0700 | [diff] [blame] | 938 | result = write(outFd, out.get(), bufSize); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 939 | if ((size_t)result < bufSize) { |
| 940 | fprintf(stderr, "error writing deflated trace: %s (%d)\n", |
| 941 | strerror(errno), errno); |
| 942 | result = Z_STREAM_END; // skip deflate error message |
| 943 | zs.avail_out = bufSize; // skip the final write |
| 944 | break; |
| 945 | } |
Elliott Hughes | a252f4d | 2016-07-21 17:12:15 -0700 | [diff] [blame] | 946 | zs.next_out = reinterpret_cast<Bytef*>(out.get()); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 947 | zs.avail_out = bufSize; |
| 948 | } |
| 949 | |
| 950 | } while ((result = deflate(&zs, flush)) == Z_OK); |
| 951 | |
| 952 | if (result != Z_STREAM_END) { |
| 953 | fprintf(stderr, "error deflating trace: %s\n", zs.msg); |
| 954 | } |
| 955 | |
| 956 | if (zs.avail_out < bufSize) { |
| 957 | size_t bytes = bufSize - zs.avail_out; |
Elliott Hughes | b59e296 | 2016-07-22 09:00:59 -0700 | [diff] [blame] | 958 | result = write(outFd, out.get(), bytes); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 959 | if ((size_t)result < bytes) { |
| 960 | fprintf(stderr, "error writing deflated trace: %s (%d)\n", |
| 961 | strerror(errno), errno); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | result = deflateEnd(&zs); |
| 966 | if (result != Z_OK) { |
| 967 | fprintf(stderr, "error cleaning up zlib: %d\n", result); |
| 968 | } |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 969 | } else { |
| 970 | ssize_t sent = 0; |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 971 | while ((sent = sendfile(outFd, traceFD, NULL, 64*1024*1024)) > 0); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 972 | if (sent == -1) { |
| 973 | fprintf(stderr, "error dumping trace: %s (%d)\n", strerror(errno), |
| 974 | errno); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | close(traceFD); |
| 979 | } |
| 980 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 981 | static void handleSignal(int /*signo*/) |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 982 | { |
| 983 | if (!g_nohup) { |
| 984 | g_traceAborted = true; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | static void registerSigHandler() |
| 989 | { |
| 990 | struct sigaction sa; |
| 991 | sigemptyset(&sa.sa_mask); |
| 992 | sa.sa_flags = 0; |
| 993 | sa.sa_handler = handleSignal; |
| 994 | sigaction(SIGHUP, &sa, NULL); |
| 995 | sigaction(SIGINT, &sa, NULL); |
| 996 | sigaction(SIGQUIT, &sa, NULL); |
| 997 | sigaction(SIGTERM, &sa, NULL); |
| 998 | } |
| 999 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1000 | static void listSupportedCategories() |
| 1001 | { |
| 1002 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 1003 | const TracingCategory& c = k_categories[i]; |
| 1004 | if (isCategorySupported(c)) { |
| 1005 | printf(" %10s - %s\n", c.name, c.longname); |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | // Print the command usage help to stderr. |
| 1011 | static void showHelp(const char *cmd) |
| 1012 | { |
| 1013 | fprintf(stderr, "usage: %s [options] [categories...]\n", cmd); |
| 1014 | fprintf(stderr, "options include:\n" |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 1015 | " -a appname enable app-level tracing for a comma " |
| 1016 | "separated list of cmdlines\n" |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1017 | " -b N use a trace buffer size of N KB\n" |
| 1018 | " -c trace into a circular buffer\n" |
Yasuhiro Matsuda | 46c51fb | 2015-06-29 19:20:39 +0900 | [diff] [blame] | 1019 | " -f filename use the categories written in a file as space-separated\n" |
| 1020 | " values in a line\n" |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 1021 | " -k fname,... trace the listed kernel functions\n" |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1022 | " -n ignore signals\n" |
| 1023 | " -s N sleep for N seconds before tracing [default 0]\n" |
Elliott Hughes | f884a06 | 2016-07-22 09:38:44 -0700 | [diff] [blame] | 1024 | " -t N trace for N seconds [default 5]\n" |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1025 | " -z compress the trace dump\n" |
| 1026 | " --async_start start circular trace and return immediatly\n" |
| 1027 | " --async_dump dump the current contents of circular trace buffer\n" |
| 1028 | " --async_stop stop tracing and dump the current contents of circular\n" |
| 1029 | " trace buffer\n" |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 1030 | " --stream stream trace to stdout as it enters the trace buffer\n" |
| 1031 | " Note: this can take significant CPU time, and is best\n" |
| 1032 | " used for measuring things that are not affected by\n" |
| 1033 | " CPU performance, like pagecache usage.\n" |
Jamie Gennis | 92573f1 | 2012-12-07 16:29:03 -0800 | [diff] [blame] | 1034 | " --list_categories\n" |
| 1035 | " list the available tracing categories\n" |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 1036 | " -o filename write the trace to the specified file instead\n" |
| 1037 | " of stdout.\n" |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1038 | ); |
| 1039 | } |
| 1040 | |
| 1041 | int main(int argc, char **argv) |
| 1042 | { |
| 1043 | bool async = false; |
| 1044 | bool traceStart = true; |
| 1045 | bool traceStop = true; |
| 1046 | bool traceDump = true; |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 1047 | bool traceStream = false; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1048 | |
| 1049 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
| 1050 | showHelp(argv[0]); |
| 1051 | exit(0); |
| 1052 | } |
| 1053 | |
| 1054 | for (;;) { |
| 1055 | int ret; |
| 1056 | int option_index = 0; |
| 1057 | static struct option long_options[] = { |
| 1058 | {"async_start", no_argument, 0, 0 }, |
| 1059 | {"async_stop", no_argument, 0, 0 }, |
| 1060 | {"async_dump", no_argument, 0, 0 }, |
| 1061 | {"list_categories", no_argument, 0, 0 }, |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 1062 | {"stream", no_argument, 0, 0 }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1063 | { 0, 0, 0, 0 } |
| 1064 | }; |
| 1065 | |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 1066 | ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:zo:", |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1067 | long_options, &option_index); |
| 1068 | |
| 1069 | if (ret < 0) { |
| 1070 | for (int i = optind; i < argc; i++) { |
| 1071 | if (!setCategoryEnable(argv[i], true)) { |
| 1072 | fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]); |
| 1073 | exit(1); |
| 1074 | } |
| 1075 | } |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | switch(ret) { |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 1080 | case 'a': |
| 1081 | g_debugAppCmdLine = optarg; |
| 1082 | break; |
| 1083 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1084 | case 'b': |
| 1085 | g_traceBufferSizeKB = atoi(optarg); |
| 1086 | break; |
| 1087 | |
| 1088 | case 'c': |
| 1089 | g_traceOverwrite = true; |
| 1090 | break; |
| 1091 | |
Yasuhiro Matsuda | 46c51fb | 2015-06-29 19:20:39 +0900 | [diff] [blame] | 1092 | case 'f': |
| 1093 | g_categoriesFile = optarg; |
| 1094 | break; |
| 1095 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 1096 | case 'k': |
| 1097 | g_kernelTraceFuncs = optarg; |
Jamie Gennis | 6f6f3f7 | 2013-03-27 15:50:30 -0700 | [diff] [blame] | 1098 | break; |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 1099 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1100 | case 'n': |
| 1101 | g_nohup = true; |
Jamie Gennis | 6f6f3f7 | 2013-03-27 15:50:30 -0700 | [diff] [blame] | 1102 | break; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1103 | |
| 1104 | case 's': |
| 1105 | g_initialSleepSecs = atoi(optarg); |
| 1106 | break; |
| 1107 | |
| 1108 | case 't': |
| 1109 | g_traceDurationSeconds = atoi(optarg); |
| 1110 | break; |
| 1111 | |
| 1112 | case 'z': |
| 1113 | g_compress = true; |
| 1114 | break; |
| 1115 | |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 1116 | case 'o': |
| 1117 | g_outputFile = optarg; |
| 1118 | break; |
| 1119 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1120 | case 0: |
| 1121 | if (!strcmp(long_options[option_index].name, "async_start")) { |
| 1122 | async = true; |
| 1123 | traceStop = false; |
| 1124 | traceDump = false; |
| 1125 | g_traceOverwrite = true; |
| 1126 | } else if (!strcmp(long_options[option_index].name, "async_stop")) { |
| 1127 | async = true; |
John Reck | 4ba2b63 | 2015-05-15 10:00:34 -0700 | [diff] [blame] | 1128 | traceStart = false; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1129 | } else if (!strcmp(long_options[option_index].name, "async_dump")) { |
| 1130 | async = true; |
| 1131 | traceStart = false; |
| 1132 | traceStop = false; |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 1133 | } else if (!strcmp(long_options[option_index].name, "stream")) { |
| 1134 | traceStream = true; |
| 1135 | traceDump = false; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1136 | } else if (!strcmp(long_options[option_index].name, "list_categories")) { |
| 1137 | listSupportedCategories(); |
| 1138 | exit(0); |
| 1139 | } |
Jamie Gennis | 6f6f3f7 | 2013-03-27 15:50:30 -0700 | [diff] [blame] | 1140 | break; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1141 | |
| 1142 | default: |
| 1143 | fprintf(stderr, "\n"); |
| 1144 | showHelp(argv[0]); |
| 1145 | exit(-1); |
| 1146 | break; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | registerSigHandler(); |
| 1151 | |
| 1152 | if (g_initialSleepSecs > 0) { |
| 1153 | sleep(g_initialSleepSecs); |
| 1154 | } |
| 1155 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 1156 | bool ok = true; |
| 1157 | ok &= setUpTrace(); |
| 1158 | ok &= startTrace(); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1159 | |
| 1160 | if (ok && traceStart) { |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 1161 | if (!traceStream) { |
| 1162 | printf("capturing trace..."); |
| 1163 | fflush(stdout); |
| 1164 | } |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1165 | |
| 1166 | // We clear the trace after starting it because tracing gets enabled for |
| 1167 | // each CPU individually in the kernel. Having the beginning of the trace |
| 1168 | // contain entries from only one CPU can cause "begin" entries without a |
| 1169 | // matching "end" entry to show up if a task gets migrated from one CPU to |
| 1170 | // another. |
| 1171 | ok = clearTrace(); |
| 1172 | |
Martijn Coenen | 0bcd97a | 2015-07-15 14:25:23 +0200 | [diff] [blame] | 1173 | writeClockSyncMarker(); |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 1174 | if (ok && !async && !traceStream) { |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1175 | // Sleep to allow the trace to be captured. |
| 1176 | struct timespec timeLeft; |
| 1177 | timeLeft.tv_sec = g_traceDurationSeconds; |
| 1178 | timeLeft.tv_nsec = 0; |
| 1179 | do { |
| 1180 | if (g_traceAborted) { |
| 1181 | break; |
| 1182 | } |
| 1183 | } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR); |
| 1184 | } |
Martijn Coenen | d953587 | 2015-11-26 10:00:55 +0100 | [diff] [blame] | 1185 | |
| 1186 | if (traceStream) { |
| 1187 | streamTrace(); |
| 1188 | } |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | // Stop the trace and restore the default settings. |
| 1192 | if (traceStop) |
| 1193 | stopTrace(); |
| 1194 | |
| 1195 | if (ok && traceDump) { |
| 1196 | if (!g_traceAborted) { |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 1197 | printf(" done\n"); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1198 | fflush(stdout); |
John Reck | 40b26b4 | 2016-03-30 09:44:36 -0700 | [diff] [blame] | 1199 | int outFd = STDOUT_FILENO; |
| 1200 | if (g_outputFile) { |
| 1201 | outFd = open(g_outputFile, O_WRONLY | O_CREAT); |
| 1202 | } |
| 1203 | if (outFd == -1) { |
| 1204 | printf("Failed to open '%s', err=%d", g_outputFile, errno); |
| 1205 | } else { |
| 1206 | dprintf(outFd, "TRACE:\n"); |
| 1207 | dumpTrace(outFd); |
| 1208 | if (g_outputFile) { |
| 1209 | close(outFd); |
| 1210 | } |
| 1211 | } |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1212 | } else { |
| 1213 | printf("\ntrace aborted.\n"); |
| 1214 | fflush(stdout); |
| 1215 | } |
| 1216 | clearTrace(); |
| 1217 | } else if (!ok) { |
| 1218 | fprintf(stderr, "unable to start tracing\n"); |
| 1219 | } |
| 1220 | |
| 1221 | // Reset the trace buffer size to 1. |
| 1222 | if (traceStop) |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 1223 | cleanUpTrace(); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 1224 | |
| 1225 | return g_traceAborted ? 1 : 0; |
| 1226 | } |