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 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <getopt.h> |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 21 | #include <signal.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Elliott Hughes | 3da5d23 | 2015-01-25 08:35:20 -0800 | [diff] [blame] | 26 | #include <string.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 27 | #include <sys/sendfile.h> |
| 28 | #include <time.h> |
| 29 | #include <zlib.h> |
| 30 | |
| 31 | #include <binder/IBinder.h> |
| 32 | #include <binder/IServiceManager.h> |
| 33 | #include <binder/Parcel.h> |
| 34 | |
| 35 | #include <cutils/properties.h> |
| 36 | |
| 37 | #include <utils/String8.h> |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 38 | #include <utils/Timers.h> |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 39 | #include <utils/Trace.h> |
| 40 | |
| 41 | using namespace android; |
| 42 | |
| 43 | #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 44 | |
Mohamad Ayyash | 26dbcbe | 2014-04-08 15:24:11 -0700 | [diff] [blame] | 45 | enum { MAX_SYS_FILES = 10 }; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 46 | |
| 47 | const char* k_traceTagsProperty = "debug.atrace.tags.enableflags"; |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 48 | const char* k_traceAppCmdlineProperty = "debug.atrace.app_cmdlines"; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 49 | |
| 50 | typedef enum { OPT, REQ } requiredness ; |
| 51 | |
| 52 | struct TracingCategory { |
| 53 | // The name identifying the category. |
| 54 | const char* name; |
| 55 | |
| 56 | // A longer description of the category. |
| 57 | const char* longname; |
| 58 | |
| 59 | // The userland tracing tags that the category enables. |
| 60 | uint64_t tags; |
| 61 | |
| 62 | // The fname==NULL terminated list of /sys/ files that the category |
| 63 | // enables. |
| 64 | struct { |
| 65 | // Whether the file must be writable in order to enable the tracing |
| 66 | // category. |
| 67 | requiredness required; |
| 68 | |
| 69 | // The path to the enable file. |
| 70 | const char* path; |
| 71 | } sysfiles[MAX_SYS_FILES]; |
| 72 | }; |
| 73 | |
| 74 | /* Tracing categories */ |
| 75 | static const TracingCategory k_categories[] = { |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 76 | { "gfx", "Graphics", ATRACE_TAG_GRAPHICS, { } }, |
| 77 | { "input", "Input", ATRACE_TAG_INPUT, { } }, |
| 78 | { "view", "View System", ATRACE_TAG_VIEW, { } }, |
| 79 | { "webview", "WebView", ATRACE_TAG_WEBVIEW, { } }, |
| 80 | { "wm", "Window Manager", ATRACE_TAG_WINDOW_MANAGER, { } }, |
| 81 | { "am", "Activity Manager", ATRACE_TAG_ACTIVITY_MANAGER, { } }, |
Patrick Auchter | 70ec294 | 2014-09-30 15:38:30 -0500 | [diff] [blame] | 82 | { "sm", "Sync Manager", ATRACE_TAG_SYNC_MANAGER, { } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 83 | { "audio", "Audio", ATRACE_TAG_AUDIO, { } }, |
| 84 | { "video", "Video", ATRACE_TAG_VIDEO, { } }, |
| 85 | { "camera", "Camera", ATRACE_TAG_CAMERA, { } }, |
| 86 | { "hal", "Hardware Modules", ATRACE_TAG_HAL, { } }, |
Jeff Brown | 3200b0b | 2014-08-14 19:24:47 -0700 | [diff] [blame] | 87 | { "app", "Application", ATRACE_TAG_APP, { } }, |
Dianne Hackborn | 9380d78 | 2013-04-12 14:52:35 -0700 | [diff] [blame] | 88 | { "res", "Resource Loading", ATRACE_TAG_RESOURCES, { } }, |
Jamie Gennis | eff2e8d | 2013-05-07 15:20:39 -0700 | [diff] [blame] | 89 | { "dalvik", "Dalvik VM", ATRACE_TAG_DALVIK, { } }, |
Tim Murray | f0f2841 | 2013-05-23 14:39:42 -0700 | [diff] [blame] | 90 | { "rs", "RenderScript", ATRACE_TAG_RS, { } }, |
Brigid Smith | 750aa97 | 2014-05-28 14:23:24 -0700 | [diff] [blame] | 91 | { "bionic", "Bionic C Library", ATRACE_TAG_BIONIC, { } }, |
Jeff Brown | 3200b0b | 2014-08-14 19:24:47 -0700 | [diff] [blame] | 92 | { "power", "Power Management", ATRACE_TAG_POWER, { } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 93 | { "sched", "CPU Scheduling", 0, { |
| 94 | { REQ, "/sys/kernel/debug/tracing/events/sched/sched_switch/enable" }, |
| 95 | { REQ, "/sys/kernel/debug/tracing/events/sched/sched_wakeup/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 96 | } }, |
Dan Willemsen | f440d39 | 2014-04-11 15:44:09 -0700 | [diff] [blame] | 97 | { "irq", "IRQ Events", 0, { |
| 98 | { REQ, "/sys/kernel/debug/tracing/events/irq/enable" }, |
| 99 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 100 | { "freq", "CPU Frequency", 0, { |
| 101 | { REQ, "/sys/kernel/debug/tracing/events/power/cpu_frequency/enable" }, |
| 102 | { OPT, "/sys/kernel/debug/tracing/events/power/clock_set_rate/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 103 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 104 | { "membus", "Memory Bus Utilization", 0, { |
| 105 | { REQ, "/sys/kernel/debug/tracing/events/memory_bus/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 106 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 107 | { "idle", "CPU Idle", 0, { |
| 108 | { REQ, "/sys/kernel/debug/tracing/events/power/cpu_idle/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 109 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 110 | { "disk", "Disk I/O", 0, { |
Greg Hackmann | e80d32c | 2014-11-20 12:59:44 -0800 | [diff] [blame] | 111 | { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_sync_file_enter/enable" }, |
| 112 | { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_sync_file_exit/enable" }, |
| 113 | { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_write_begin/enable" }, |
| 114 | { OPT, "/sys/kernel/debug/tracing/events/f2fs/f2fs_write_end/enable" }, |
| 115 | { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_da_write_begin/enable" }, |
| 116 | { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_da_write_end/enable" }, |
| 117 | { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_sync_file_enter/enable" }, |
| 118 | { OPT, "/sys/kernel/debug/tracing/events/ext4/ext4_sync_file_exit/enable" }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 119 | { REQ, "/sys/kernel/debug/tracing/events/block/block_rq_issue/enable" }, |
| 120 | { REQ, "/sys/kernel/debug/tracing/events/block/block_rq_complete/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 121 | } }, |
Ken Sumrall | d3fa561 | 2013-07-03 12:32:03 -0700 | [diff] [blame] | 122 | { "mmc", "eMMC commands", 0, { |
| 123 | { REQ, "/sys/kernel/debug/tracing/events/mmc/enable" }, |
| 124 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 125 | { "load", "CPU Load", 0, { |
| 126 | { REQ, "/sys/kernel/debug/tracing/events/cpufreq_interactive/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 127 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 128 | { "sync", "Synchronization", 0, { |
| 129 | { REQ, "/sys/kernel/debug/tracing/events/sync/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 130 | } }, |
Jamie Gennis | b2a89e3 | 2013-03-11 19:37:53 -0700 | [diff] [blame] | 131 | { "workq", "Kernel Workqueues", 0, { |
| 132 | { REQ, "/sys/kernel/debug/tracing/events/workqueue/enable" }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 133 | } }, |
Colin Cross | 580407f | 2014-08-18 15:22:13 -0700 | [diff] [blame] | 134 | { "memreclaim", "Kernel Memory Reclaim", 0, { |
| 135 | { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_begin/enable" }, |
| 136 | { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_direct_reclaim_end/enable" }, |
| 137 | { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_wake/enable" }, |
| 138 | { REQ, "/sys/kernel/debug/tracing/events/vmscan/mm_vmscan_kswapd_sleep/enable" }, |
| 139 | } }, |
Aaron Schulman | c2c6ecd | 2015-02-25 08:37:09 -0800 | [diff] [blame] | 140 | { "regulators", "Voltage and Current Regulators", 0, { |
| 141 | { REQ, "/sys/kernel/debug/tracing/events/regulator/enable" }, |
| 142 | } }, |
Scott Bauer | ae47336 | 2015-06-08 16:32:36 -0700 | [diff] [blame^] | 143 | { "binder_driver", "Binder Kernel driver", 0, { |
| 144 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_transaction/enable" }, |
| 145 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_transaction_received/enable" }, |
| 146 | } }, |
| 147 | { "binder_lock", "Binder global lock trace", 0, { |
| 148 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_lock/enable" }, |
| 149 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_locked/enable" }, |
| 150 | { REQ, "/sys/kernel/debug/tracing/events/binder/binder_unlock/enable" }, |
| 151 | } }, |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /* Command line options */ |
| 155 | static int g_traceDurationSeconds = 5; |
| 156 | static bool g_traceOverwrite = false; |
| 157 | static int g_traceBufferSizeKB = 2048; |
| 158 | static bool g_compress = false; |
| 159 | static bool g_nohup = false; |
| 160 | static int g_initialSleepSecs = 0; |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 161 | static const char* g_kernelTraceFuncs = NULL; |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 162 | static const char* g_debugAppCmdLine = ""; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 163 | |
| 164 | /* Global state */ |
| 165 | static bool g_traceAborted = false; |
| 166 | static bool g_categoryEnables[NELEM(k_categories)] = {}; |
| 167 | |
| 168 | /* Sys file paths */ |
| 169 | static const char* k_traceClockPath = |
| 170 | "/sys/kernel/debug/tracing/trace_clock"; |
| 171 | |
| 172 | static const char* k_traceBufferSizePath = |
| 173 | "/sys/kernel/debug/tracing/buffer_size_kb"; |
| 174 | |
| 175 | static const char* k_tracingOverwriteEnablePath = |
| 176 | "/sys/kernel/debug/tracing/options/overwrite"; |
| 177 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 178 | static const char* k_currentTracerPath = |
| 179 | "/sys/kernel/debug/tracing/current_tracer"; |
| 180 | |
| 181 | static const char* k_printTgidPath = |
| 182 | "/sys/kernel/debug/tracing/options/print-tgid"; |
| 183 | |
| 184 | static const char* k_funcgraphAbsTimePath = |
| 185 | "/sys/kernel/debug/tracing/options/funcgraph-abstime"; |
| 186 | |
| 187 | static const char* k_funcgraphCpuPath = |
| 188 | "/sys/kernel/debug/tracing/options/funcgraph-cpu"; |
| 189 | |
| 190 | static const char* k_funcgraphProcPath = |
| 191 | "/sys/kernel/debug/tracing/options/funcgraph-proc"; |
| 192 | |
| 193 | static const char* k_funcgraphFlatPath = |
| 194 | "/sys/kernel/debug/tracing/options/funcgraph-flat"; |
| 195 | |
| 196 | static const char* k_funcgraphDurationPath = |
| 197 | "/sys/kernel/debug/tracing/options/funcgraph-duration"; |
| 198 | |
| 199 | static const char* k_ftraceFilterPath = |
| 200 | "/sys/kernel/debug/tracing/set_ftrace_filter"; |
| 201 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 202 | static const char* k_tracingOnPath = |
| 203 | "/sys/kernel/debug/tracing/tracing_on"; |
| 204 | |
| 205 | static const char* k_tracePath = |
| 206 | "/sys/kernel/debug/tracing/trace"; |
| 207 | |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 208 | static const char* k_traceMarkerPath = |
| 209 | "/sys/kernel/debug/tracing/trace_marker"; |
| 210 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 211 | // Check whether a file exists. |
| 212 | static bool fileExists(const char* filename) { |
| 213 | return access(filename, F_OK) != -1; |
| 214 | } |
| 215 | |
| 216 | // Check whether a file is writable. |
| 217 | static bool fileIsWritable(const char* filename) { |
| 218 | return access(filename, W_OK) != -1; |
| 219 | } |
| 220 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 221 | // Truncate a file. |
| 222 | static bool truncateFile(const char* path) |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 223 | { |
Jamie Gennis | 43122e7 | 2013-03-21 14:06:31 -0700 | [diff] [blame] | 224 | // This uses creat rather than truncate because some of the debug kernel |
| 225 | // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by |
| 226 | // calls to truncate, but they are cleared by calls to creat. |
| 227 | int traceFD = creat(path, 0); |
| 228 | if (traceFD == -1) { |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 229 | fprintf(stderr, "error truncating %s: %s (%d)\n", path, |
Jamie Gennis | 43122e7 | 2013-03-21 14:06:31 -0700 | [diff] [blame] | 230 | strerror(errno), errno); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | |
Jamie Gennis | 43122e7 | 2013-03-21 14:06:31 -0700 | [diff] [blame] | 234 | close(traceFD); |
| 235 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 236 | return true; |
| 237 | } |
| 238 | |
| 239 | static bool _writeStr(const char* filename, const char* str, int flags) |
| 240 | { |
| 241 | int fd = open(filename, flags); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 242 | if (fd == -1) { |
| 243 | fprintf(stderr, "error opening %s: %s (%d)\n", filename, |
| 244 | strerror(errno), errno); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | bool ok = true; |
| 249 | ssize_t len = strlen(str); |
| 250 | if (write(fd, str, len) != len) { |
| 251 | fprintf(stderr, "error writing to %s: %s (%d)\n", filename, |
| 252 | strerror(errno), errno); |
| 253 | ok = false; |
| 254 | } |
| 255 | |
| 256 | close(fd); |
| 257 | |
| 258 | return ok; |
| 259 | } |
| 260 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 261 | // Write a string to a file, returning true if the write was successful. |
| 262 | static bool writeStr(const char* filename, const char* str) |
| 263 | { |
| 264 | return _writeStr(filename, str, O_WRONLY); |
| 265 | } |
| 266 | |
| 267 | // Append a string to a file, returning true if the write was successful. |
| 268 | static bool appendStr(const char* filename, const char* str) |
| 269 | { |
| 270 | return _writeStr(filename, str, O_APPEND|O_WRONLY); |
| 271 | } |
| 272 | |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 273 | static void writeClockSyncMarker() |
| 274 | { |
| 275 | char buffer[128]; |
| 276 | float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f; |
| 277 | snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds); |
| 278 | writeStr(k_traceMarkerPath, buffer); |
| 279 | } |
| 280 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 281 | // Enable or disable a kernel option by writing a "1" or a "0" into a /sys |
| 282 | // file. |
| 283 | static bool setKernelOptionEnable(const char* filename, bool enable) |
| 284 | { |
| 285 | return writeStr(filename, enable ? "1" : "0"); |
| 286 | } |
| 287 | |
| 288 | // Check whether the category is supported on the device with the current |
| 289 | // rootness. A category is supported only if all its required /sys/ files are |
| 290 | // writable and if enabling the category will enable one or more tracing tags |
| 291 | // or /sys/ files. |
| 292 | static bool isCategorySupported(const TracingCategory& category) |
| 293 | { |
| 294 | bool ok = category.tags != 0; |
| 295 | for (int i = 0; i < MAX_SYS_FILES; i++) { |
| 296 | const char* path = category.sysfiles[i].path; |
| 297 | bool req = category.sysfiles[i].required == REQ; |
| 298 | if (path != NULL) { |
| 299 | if (req) { |
| 300 | if (!fileIsWritable(path)) { |
| 301 | return false; |
| 302 | } else { |
| 303 | ok = true; |
| 304 | } |
| 305 | } else { |
| 306 | ok |= fileIsWritable(path); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | return ok; |
| 311 | } |
| 312 | |
| 313 | // Check whether the category would be supported on the device if the user |
| 314 | // were root. This function assumes that root is able to write to any file |
| 315 | // that exists. It performs the same logic as isCategorySupported, but it |
| 316 | // uses file existance rather than writability in the /sys/ file checks. |
| 317 | static bool isCategorySupportedForRoot(const TracingCategory& category) |
| 318 | { |
| 319 | bool ok = category.tags != 0; |
| 320 | for (int i = 0; i < MAX_SYS_FILES; i++) { |
| 321 | const char* path = category.sysfiles[i].path; |
| 322 | bool req = category.sysfiles[i].required == REQ; |
| 323 | if (path != NULL) { |
| 324 | if (req) { |
| 325 | if (!fileExists(path)) { |
| 326 | return false; |
| 327 | } else { |
| 328 | ok = true; |
| 329 | } |
| 330 | } else { |
| 331 | ok |= fileExists(path); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | return ok; |
| 336 | } |
| 337 | |
| 338 | // Enable or disable overwriting of the kernel trace buffers. Disabling this |
| 339 | // will cause tracing to stop once the trace buffers have filled up. |
| 340 | static bool setTraceOverwriteEnable(bool enable) |
| 341 | { |
| 342 | return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable); |
| 343 | } |
| 344 | |
| 345 | // Enable or disable kernel tracing. |
| 346 | static bool setTracingEnabled(bool enable) |
| 347 | { |
| 348 | return setKernelOptionEnable(k_tracingOnPath, enable); |
| 349 | } |
| 350 | |
| 351 | // Clear the contents of the kernel trace. |
| 352 | static bool clearTrace() |
| 353 | { |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 354 | return truncateFile(k_tracePath); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // Set the size of the kernel's trace buffer in kilobytes. |
| 358 | static bool setTraceBufferSizeKB(int size) |
| 359 | { |
| 360 | char str[32] = "1"; |
| 361 | int len; |
| 362 | if (size < 1) { |
| 363 | size = 1; |
| 364 | } |
| 365 | snprintf(str, 32, "%d", size); |
| 366 | return writeStr(k_traceBufferSizePath, str); |
| 367 | } |
| 368 | |
Colin Cross | b1ce49b | 2014-08-20 14:28:47 -0700 | [diff] [blame] | 369 | // Read the trace_clock sysfs file and return true if it matches the requested |
| 370 | // value. The trace_clock file format is: |
| 371 | // local [global] counter uptime perf |
| 372 | static bool isTraceClock(const char *mode) |
| 373 | { |
| 374 | int fd = open(k_traceClockPath, O_RDONLY); |
| 375 | if (fd == -1) { |
| 376 | fprintf(stderr, "error opening %s: %s (%d)\n", k_traceClockPath, |
| 377 | strerror(errno), errno); |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | char buf[4097]; |
| 382 | ssize_t n = read(fd, buf, 4096); |
| 383 | close(fd); |
| 384 | if (n == -1) { |
| 385 | fprintf(stderr, "error reading %s: %s (%d)\n", k_traceClockPath, |
| 386 | strerror(errno), errno); |
| 387 | return false; |
| 388 | } |
| 389 | buf[n] = '\0'; |
| 390 | |
| 391 | char *start = strchr(buf, '['); |
| 392 | if (start == NULL) { |
| 393 | return false; |
| 394 | } |
| 395 | start++; |
| 396 | |
| 397 | char *end = strchr(start, ']'); |
| 398 | if (end == NULL) { |
| 399 | return false; |
| 400 | } |
| 401 | *end = '\0'; |
| 402 | |
| 403 | return strcmp(mode, start) == 0; |
| 404 | } |
| 405 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 406 | // Enable or disable the kernel's use of the global clock. Disabling the global |
| 407 | // clock will result in the kernel using a per-CPU local clock. |
Colin Cross | b1ce49b | 2014-08-20 14:28:47 -0700 | [diff] [blame] | 408 | // Any write to the trace_clock sysfs file will reset the buffer, so only |
| 409 | // update it if the requested value is not the current value. |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 410 | static bool setGlobalClockEnable(bool enable) |
| 411 | { |
Colin Cross | b1ce49b | 2014-08-20 14:28:47 -0700 | [diff] [blame] | 412 | const char *clock = enable ? "global" : "local"; |
| 413 | |
| 414 | if (isTraceClock(clock)) { |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | return writeStr(k_traceClockPath, clock); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 421 | static bool setPrintTgidEnableIfPresent(bool enable) |
| 422 | { |
| 423 | if (fileExists(k_printTgidPath)) { |
| 424 | return setKernelOptionEnable(k_printTgidPath, enable); |
| 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 429 | // Poke all the binder-enabled processes in the system to get them to re-read |
| 430 | // their system properties. |
| 431 | static bool pokeBinderServices() |
| 432 | { |
| 433 | sp<IServiceManager> sm = defaultServiceManager(); |
| 434 | Vector<String16> services = sm->listServices(); |
| 435 | for (size_t i = 0; i < services.size(); i++) { |
| 436 | sp<IBinder> obj = sm->checkService(services[i]); |
| 437 | if (obj != NULL) { |
| 438 | Parcel data; |
| 439 | if (obj->transact(IBinder::SYSPROPS_TRANSACTION, data, |
| 440 | NULL, 0) != OK) { |
| 441 | if (false) { |
| 442 | // XXX: For some reason this fails on tablets trying to |
| 443 | // poke the "phone" service. It's not clear whether some |
| 444 | // are expected to fail. |
| 445 | String8 svc(services[i]); |
| 446 | fprintf(stderr, "error poking binder service %s\n", |
| 447 | svc.string()); |
| 448 | return false; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | // Set the trace tags that userland tracing uses, and poke the running |
| 457 | // processes to pick up the new value. |
| 458 | static bool setTagsProperty(uint64_t tags) |
| 459 | { |
| 460 | char buf[64]; |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 461 | snprintf(buf, 64, "%#" PRIx64, tags); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 462 | if (property_set(k_traceTagsProperty, buf) < 0) { |
| 463 | fprintf(stderr, "error setting trace tags system property\n"); |
| 464 | return false; |
| 465 | } |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 466 | return true; |
| 467 | } |
| 468 | |
| 469 | // Set the system property that indicates which apps should perform |
| 470 | // application-level tracing. |
| 471 | static bool setAppCmdlineProperty(const char* cmdline) |
| 472 | { |
| 473 | if (property_set(k_traceAppCmdlineProperty, cmdline) < 0) { |
| 474 | fprintf(stderr, "error setting trace app system property\n"); |
| 475 | return false; |
| 476 | } |
| 477 | return true; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | // Disable all /sys/ enable files. |
| 481 | static bool disableKernelTraceEvents() { |
| 482 | bool ok = true; |
| 483 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 484 | const TracingCategory &c = k_categories[i]; |
| 485 | for (int j = 0; j < MAX_SYS_FILES; j++) { |
| 486 | const char* path = c.sysfiles[j].path; |
| 487 | if (path != NULL && fileIsWritable(path)) { |
| 488 | ok &= setKernelOptionEnable(path, false); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | return ok; |
| 493 | } |
| 494 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 495 | // Verify that the comma separated list of functions are being traced by the |
| 496 | // kernel. |
| 497 | static bool verifyKernelTraceFuncs(const char* funcs) |
| 498 | { |
| 499 | int fd = open(k_ftraceFilterPath, O_RDONLY); |
| 500 | if (fd == -1) { |
| 501 | fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath, |
| 502 | strerror(errno), errno); |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | char buf[4097]; |
| 507 | ssize_t n = read(fd, buf, 4096); |
| 508 | close(fd); |
| 509 | if (n == -1) { |
| 510 | fprintf(stderr, "error reading %s: %s (%d)\n", k_ftraceFilterPath, |
| 511 | strerror(errno), errno); |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | buf[n] = '\0'; |
| 516 | String8 funcList = String8::format("\n%s", buf); |
| 517 | |
| 518 | // Make sure that every function listed in funcs is in the list we just |
| 519 | // read from the kernel. |
| 520 | bool ok = true; |
| 521 | char* myFuncs = strdup(funcs); |
| 522 | char* func = strtok(myFuncs, ","); |
| 523 | while (func) { |
| 524 | String8 fancyFunc = String8::format("\n%s\n", func); |
| 525 | bool found = funcList.find(fancyFunc.string(), 0) >= 0; |
| 526 | if (!found || func[0] == '\0') { |
| 527 | fprintf(stderr, "error: \"%s\" is not a valid kernel function " |
| 528 | "to trace.\n", func); |
| 529 | ok = false; |
| 530 | } |
| 531 | func = strtok(NULL, ","); |
| 532 | } |
| 533 | free(myFuncs); |
| 534 | |
| 535 | return ok; |
| 536 | } |
| 537 | |
| 538 | // Set the comma separated list of functions that the kernel is to trace. |
| 539 | static bool setKernelTraceFuncs(const char* funcs) |
| 540 | { |
| 541 | bool ok = true; |
| 542 | |
| 543 | if (funcs == NULL || funcs[0] == '\0') { |
| 544 | // Disable kernel function tracing. |
Jamie Gennis | 6f6f3f7 | 2013-03-27 15:50:30 -0700 | [diff] [blame] | 545 | if (fileIsWritable(k_currentTracerPath)) { |
| 546 | ok &= writeStr(k_currentTracerPath, "nop"); |
| 547 | } |
| 548 | if (fileIsWritable(k_ftraceFilterPath)) { |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 549 | ok &= truncateFile(k_ftraceFilterPath); |
| 550 | } |
| 551 | } else { |
| 552 | // Enable kernel function tracing. |
| 553 | ok &= writeStr(k_currentTracerPath, "function_graph"); |
| 554 | ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true); |
| 555 | ok &= setKernelOptionEnable(k_funcgraphCpuPath, true); |
| 556 | ok &= setKernelOptionEnable(k_funcgraphProcPath, true); |
| 557 | ok &= setKernelOptionEnable(k_funcgraphFlatPath, true); |
| 558 | |
| 559 | // Set the requested filter functions. |
| 560 | ok &= truncateFile(k_ftraceFilterPath); |
| 561 | char* myFuncs = strdup(funcs); |
| 562 | char* func = strtok(myFuncs, ","); |
| 563 | while (func) { |
| 564 | ok &= appendStr(k_ftraceFilterPath, func); |
| 565 | func = strtok(NULL, ","); |
| 566 | } |
| 567 | free(myFuncs); |
| 568 | |
| 569 | // Verify that the set functions are being traced. |
| 570 | if (ok) { |
| 571 | ok &= verifyKernelTraceFuncs(funcs); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | return ok; |
| 576 | } |
| 577 | |
| 578 | // Set all the kernel tracing settings to the desired state for this trace |
| 579 | // capture. |
| 580 | static bool setUpTrace() |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 581 | { |
| 582 | bool ok = true; |
| 583 | |
| 584 | // Set up the tracing options. |
| 585 | ok &= setTraceOverwriteEnable(g_traceOverwrite); |
| 586 | ok &= setTraceBufferSizeKB(g_traceBufferSizeKB); |
| 587 | ok &= setGlobalClockEnable(true); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 588 | ok &= setPrintTgidEnableIfPresent(true); |
| 589 | ok &= setKernelTraceFuncs(g_kernelTraceFuncs); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 590 | |
| 591 | // Set up the tags property. |
| 592 | uint64_t tags = 0; |
| 593 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 594 | if (g_categoryEnables[i]) { |
| 595 | const TracingCategory &c = k_categories[i]; |
| 596 | tags |= c.tags; |
| 597 | } |
| 598 | } |
| 599 | ok &= setTagsProperty(tags); |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 600 | ok &= setAppCmdlineProperty(g_debugAppCmdLine); |
| 601 | ok &= pokeBinderServices(); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 602 | |
| 603 | // Disable all the sysfs enables. This is done as a separate loop from |
| 604 | // the enables to allow the same enable to exist in multiple categories. |
| 605 | ok &= disableKernelTraceEvents(); |
| 606 | |
| 607 | // Enable all the sysfs enables that are in an enabled category. |
| 608 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 609 | if (g_categoryEnables[i]) { |
| 610 | const TracingCategory &c = k_categories[i]; |
| 611 | for (int j = 0; j < MAX_SYS_FILES; j++) { |
| 612 | const char* path = c.sysfiles[j].path; |
| 613 | bool required = c.sysfiles[j].required == REQ; |
| 614 | if (path != NULL) { |
| 615 | if (fileIsWritable(path)) { |
| 616 | ok &= setKernelOptionEnable(path, true); |
| 617 | } else if (required) { |
| 618 | fprintf(stderr, "error writing file %s\n", path); |
| 619 | ok = false; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 626 | return ok; |
| 627 | } |
| 628 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 629 | // Reset all the kernel tracing settings to their default state. |
| 630 | static void cleanUpTrace() |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 631 | { |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 632 | // Disable all tracing that we're able to. |
| 633 | disableKernelTraceEvents(); |
| 634 | |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 635 | // Reset the system properties. |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 636 | setTagsProperty(0); |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 637 | setAppCmdlineProperty(""); |
| 638 | pokeBinderServices(); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 639 | |
| 640 | // Set the options back to their defaults. |
| 641 | setTraceOverwriteEnable(true); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 642 | setTraceBufferSizeKB(1); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 643 | setGlobalClockEnable(false); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 644 | setPrintTgidEnableIfPresent(false); |
| 645 | setKernelTraceFuncs(NULL); |
| 646 | } |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 647 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 648 | |
| 649 | // Enable tracing in the kernel. |
| 650 | static bool startTrace() |
| 651 | { |
| 652 | return setTracingEnabled(true); |
| 653 | } |
| 654 | |
| 655 | // Disable tracing in the kernel. |
| 656 | static void stopTrace() |
| 657 | { |
John Reck | 469a194 | 2015-03-26 15:31:35 -0700 | [diff] [blame] | 658 | writeClockSyncMarker(); |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 659 | setTracingEnabled(false); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // Read the current kernel trace and write it to stdout. |
| 663 | static void dumpTrace() |
| 664 | { |
| 665 | int traceFD = open(k_tracePath, O_RDWR); |
| 666 | if (traceFD == -1) { |
| 667 | fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath, |
| 668 | strerror(errno), errno); |
| 669 | return; |
| 670 | } |
| 671 | |
| 672 | if (g_compress) { |
| 673 | z_stream zs; |
| 674 | uint8_t *in, *out; |
| 675 | int result, flush; |
| 676 | |
Elliott Hughes | 3da5d23 | 2015-01-25 08:35:20 -0800 | [diff] [blame] | 677 | memset(&zs, 0, sizeof(zs)); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 678 | result = deflateInit(&zs, Z_DEFAULT_COMPRESSION); |
| 679 | if (result != Z_OK) { |
| 680 | fprintf(stderr, "error initializing zlib: %d\n", result); |
| 681 | close(traceFD); |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | const size_t bufSize = 64*1024; |
| 686 | in = (uint8_t*)malloc(bufSize); |
| 687 | out = (uint8_t*)malloc(bufSize); |
| 688 | flush = Z_NO_FLUSH; |
| 689 | |
| 690 | zs.next_out = out; |
| 691 | zs.avail_out = bufSize; |
| 692 | |
| 693 | do { |
| 694 | |
| 695 | if (zs.avail_in == 0) { |
| 696 | // More input is needed. |
| 697 | result = read(traceFD, in, bufSize); |
| 698 | if (result < 0) { |
| 699 | fprintf(stderr, "error reading trace: %s (%d)\n", |
| 700 | strerror(errno), errno); |
| 701 | result = Z_STREAM_END; |
| 702 | break; |
| 703 | } else if (result == 0) { |
| 704 | flush = Z_FINISH; |
| 705 | } else { |
| 706 | zs.next_in = in; |
| 707 | zs.avail_in = result; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | if (zs.avail_out == 0) { |
| 712 | // Need to write the output. |
| 713 | result = write(STDOUT_FILENO, out, bufSize); |
| 714 | if ((size_t)result < bufSize) { |
| 715 | fprintf(stderr, "error writing deflated trace: %s (%d)\n", |
| 716 | strerror(errno), errno); |
| 717 | result = Z_STREAM_END; // skip deflate error message |
| 718 | zs.avail_out = bufSize; // skip the final write |
| 719 | break; |
| 720 | } |
| 721 | zs.next_out = out; |
| 722 | zs.avail_out = bufSize; |
| 723 | } |
| 724 | |
| 725 | } while ((result = deflate(&zs, flush)) == Z_OK); |
| 726 | |
| 727 | if (result != Z_STREAM_END) { |
| 728 | fprintf(stderr, "error deflating trace: %s\n", zs.msg); |
| 729 | } |
| 730 | |
| 731 | if (zs.avail_out < bufSize) { |
| 732 | size_t bytes = bufSize - zs.avail_out; |
| 733 | result = write(STDOUT_FILENO, out, bytes); |
| 734 | if ((size_t)result < bytes) { |
| 735 | fprintf(stderr, "error writing deflated trace: %s (%d)\n", |
| 736 | strerror(errno), errno); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | result = deflateEnd(&zs); |
| 741 | if (result != Z_OK) { |
| 742 | fprintf(stderr, "error cleaning up zlib: %d\n", result); |
| 743 | } |
| 744 | |
| 745 | free(in); |
| 746 | free(out); |
| 747 | } else { |
| 748 | ssize_t sent = 0; |
| 749 | while ((sent = sendfile(STDOUT_FILENO, traceFD, NULL, 64*1024*1024)) > 0); |
| 750 | if (sent == -1) { |
| 751 | fprintf(stderr, "error dumping trace: %s (%d)\n", strerror(errno), |
| 752 | errno); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | close(traceFD); |
| 757 | } |
| 758 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 759 | static void handleSignal(int /*signo*/) |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 760 | { |
| 761 | if (!g_nohup) { |
| 762 | g_traceAborted = true; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static void registerSigHandler() |
| 767 | { |
| 768 | struct sigaction sa; |
| 769 | sigemptyset(&sa.sa_mask); |
| 770 | sa.sa_flags = 0; |
| 771 | sa.sa_handler = handleSignal; |
| 772 | sigaction(SIGHUP, &sa, NULL); |
| 773 | sigaction(SIGINT, &sa, NULL); |
| 774 | sigaction(SIGQUIT, &sa, NULL); |
| 775 | sigaction(SIGTERM, &sa, NULL); |
| 776 | } |
| 777 | |
| 778 | static bool setCategoryEnable(const char* name, bool enable) |
| 779 | { |
| 780 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 781 | const TracingCategory& c = k_categories[i]; |
| 782 | if (strcmp(name, c.name) == 0) { |
| 783 | if (isCategorySupported(c)) { |
| 784 | g_categoryEnables[i] = enable; |
| 785 | return true; |
| 786 | } else { |
| 787 | if (isCategorySupportedForRoot(c)) { |
| 788 | fprintf(stderr, "error: category \"%s\" requires root " |
| 789 | "privileges.\n", name); |
| 790 | } else { |
| 791 | fprintf(stderr, "error: category \"%s\" is not supported " |
| 792 | "on this device.\n", name); |
| 793 | } |
| 794 | return false; |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | fprintf(stderr, "error: unknown tracing category \"%s\"\n", name); |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | static void listSupportedCategories() |
| 803 | { |
| 804 | for (int i = 0; i < NELEM(k_categories); i++) { |
| 805 | const TracingCategory& c = k_categories[i]; |
| 806 | if (isCategorySupported(c)) { |
| 807 | printf(" %10s - %s\n", c.name, c.longname); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | // Print the command usage help to stderr. |
| 813 | static void showHelp(const char *cmd) |
| 814 | { |
| 815 | fprintf(stderr, "usage: %s [options] [categories...]\n", cmd); |
| 816 | fprintf(stderr, "options include:\n" |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 817 | " -a appname enable app-level tracing for a comma " |
| 818 | "separated list of cmdlines\n" |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 819 | " -b N use a trace buffer size of N KB\n" |
| 820 | " -c trace into a circular buffer\n" |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 821 | " -k fname,... trace the listed kernel functions\n" |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 822 | " -n ignore signals\n" |
| 823 | " -s N sleep for N seconds before tracing [default 0]\n" |
| 824 | " -t N trace for N seconds [defualt 5]\n" |
| 825 | " -z compress the trace dump\n" |
| 826 | " --async_start start circular trace and return immediatly\n" |
| 827 | " --async_dump dump the current contents of circular trace buffer\n" |
| 828 | " --async_stop stop tracing and dump the current contents of circular\n" |
| 829 | " trace buffer\n" |
Jamie Gennis | 92573f1 | 2012-12-07 16:29:03 -0800 | [diff] [blame] | 830 | " --list_categories\n" |
| 831 | " list the available tracing categories\n" |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 832 | ); |
| 833 | } |
| 834 | |
| 835 | int main(int argc, char **argv) |
| 836 | { |
| 837 | bool async = false; |
| 838 | bool traceStart = true; |
| 839 | bool traceStop = true; |
| 840 | bool traceDump = true; |
| 841 | |
| 842 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
| 843 | showHelp(argv[0]); |
| 844 | exit(0); |
| 845 | } |
| 846 | |
| 847 | for (;;) { |
| 848 | int ret; |
| 849 | int option_index = 0; |
| 850 | static struct option long_options[] = { |
| 851 | {"async_start", no_argument, 0, 0 }, |
| 852 | {"async_stop", no_argument, 0, 0 }, |
| 853 | {"async_dump", no_argument, 0, 0 }, |
| 854 | {"list_categories", no_argument, 0, 0 }, |
| 855 | { 0, 0, 0, 0 } |
| 856 | }; |
| 857 | |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 858 | ret = getopt_long(argc, argv, "a:b:ck:ns:t:z", |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 859 | long_options, &option_index); |
| 860 | |
| 861 | if (ret < 0) { |
| 862 | for (int i = optind; i < argc; i++) { |
| 863 | if (!setCategoryEnable(argv[i], true)) { |
| 864 | fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]); |
| 865 | exit(1); |
| 866 | } |
| 867 | } |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | switch(ret) { |
Jamie Gennis | f7f29c8 | 2013-03-27 15:50:58 -0700 | [diff] [blame] | 872 | case 'a': |
| 873 | g_debugAppCmdLine = optarg; |
| 874 | break; |
| 875 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 876 | case 'b': |
| 877 | g_traceBufferSizeKB = atoi(optarg); |
| 878 | break; |
| 879 | |
| 880 | case 'c': |
| 881 | g_traceOverwrite = true; |
| 882 | break; |
| 883 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 884 | case 'k': |
| 885 | g_kernelTraceFuncs = optarg; |
Jamie Gennis | 6f6f3f7 | 2013-03-27 15:50:30 -0700 | [diff] [blame] | 886 | break; |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 887 | |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 888 | case 'n': |
| 889 | g_nohup = true; |
Jamie Gennis | 6f6f3f7 | 2013-03-27 15:50:30 -0700 | [diff] [blame] | 890 | break; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 891 | |
| 892 | case 's': |
| 893 | g_initialSleepSecs = atoi(optarg); |
| 894 | break; |
| 895 | |
| 896 | case 't': |
| 897 | g_traceDurationSeconds = atoi(optarg); |
| 898 | break; |
| 899 | |
| 900 | case 'z': |
| 901 | g_compress = true; |
| 902 | break; |
| 903 | |
| 904 | case 0: |
| 905 | if (!strcmp(long_options[option_index].name, "async_start")) { |
| 906 | async = true; |
| 907 | traceStop = false; |
| 908 | traceDump = false; |
| 909 | g_traceOverwrite = true; |
| 910 | } else if (!strcmp(long_options[option_index].name, "async_stop")) { |
| 911 | async = true; |
| 912 | traceStop = false; |
| 913 | } else if (!strcmp(long_options[option_index].name, "async_dump")) { |
| 914 | async = true; |
| 915 | traceStart = false; |
| 916 | traceStop = false; |
| 917 | } else if (!strcmp(long_options[option_index].name, "list_categories")) { |
| 918 | listSupportedCategories(); |
| 919 | exit(0); |
| 920 | } |
Jamie Gennis | 6f6f3f7 | 2013-03-27 15:50:30 -0700 | [diff] [blame] | 921 | break; |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 922 | |
| 923 | default: |
| 924 | fprintf(stderr, "\n"); |
| 925 | showHelp(argv[0]); |
| 926 | exit(-1); |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | registerSigHandler(); |
| 932 | |
| 933 | if (g_initialSleepSecs > 0) { |
| 934 | sleep(g_initialSleepSecs); |
| 935 | } |
| 936 | |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 937 | bool ok = true; |
| 938 | ok &= setUpTrace(); |
| 939 | ok &= startTrace(); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 940 | |
| 941 | if (ok && traceStart) { |
| 942 | printf("capturing trace..."); |
| 943 | fflush(stdout); |
| 944 | |
| 945 | // We clear the trace after starting it because tracing gets enabled for |
| 946 | // each CPU individually in the kernel. Having the beginning of the trace |
| 947 | // contain entries from only one CPU can cause "begin" entries without a |
| 948 | // matching "end" entry to show up if a task gets migrated from one CPU to |
| 949 | // another. |
| 950 | ok = clearTrace(); |
| 951 | |
| 952 | if (ok && !async) { |
| 953 | // Sleep to allow the trace to be captured. |
| 954 | struct timespec timeLeft; |
| 955 | timeLeft.tv_sec = g_traceDurationSeconds; |
| 956 | timeLeft.tv_nsec = 0; |
| 957 | do { |
| 958 | if (g_traceAborted) { |
| 959 | break; |
| 960 | } |
| 961 | } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // Stop the trace and restore the default settings. |
| 966 | if (traceStop) |
| 967 | stopTrace(); |
| 968 | |
| 969 | if (ok && traceDump) { |
| 970 | if (!g_traceAborted) { |
| 971 | printf(" done\nTRACE:\n"); |
| 972 | fflush(stdout); |
| 973 | dumpTrace(); |
| 974 | } else { |
| 975 | printf("\ntrace aborted.\n"); |
| 976 | fflush(stdout); |
| 977 | } |
| 978 | clearTrace(); |
| 979 | } else if (!ok) { |
| 980 | fprintf(stderr, "unable to start tracing\n"); |
| 981 | } |
| 982 | |
| 983 | // Reset the trace buffer size to 1. |
| 984 | if (traceStop) |
Jamie Gennis | e9b8cfb | 2013-03-12 16:00:10 -0700 | [diff] [blame] | 985 | cleanUpTrace(); |
Jamie Gennis | 6eea6fb | 2012-12-07 14:03:07 -0800 | [diff] [blame] | 986 | |
| 987 | return g_traceAborted ? 1 : 0; |
| 988 | } |