Tao Guo | d58f58b | 2021-07-23 14:09:27 +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 | |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <getopt.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <signal.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | #include <zlib.h> |
| 31 | |
| 32 | #include <fstream> |
| 33 | #include <memory> |
| 34 | #include <sys/time.h> |
| 35 | #include <sys/stat.h> |
| 36 | |
| 37 | |
| 38 | using std::string; |
| 39 | |
| 40 | #define MAX_SYS_FILES 10 |
| 41 | |
| 42 | typedef int64_t nsecs_t; // nano-seconds |
| 43 | typedef enum { OPT, REQ } requiredness ; |
| 44 | |
| 45 | template <typename T, size_t N> |
| 46 | char(&ArraySizeHelper(T(&array)[N]))[N]; // NOLINT(readability/casting) |
| 47 | |
| 48 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
| 49 | |
| 50 | bool ReadFileToString(const std::string& path, std::string* content, |
| 51 | bool follow_symlinks = false); |
| 52 | |
| 53 | struct TracingCategory { |
| 54 | // The name identifying the category. |
| 55 | const char* name; |
| 56 | |
| 57 | // A longer description of the category. |
| 58 | const char* longname; |
| 59 | |
| 60 | // The userland tracing tags that the category enables. |
| 61 | uint64_t tags; |
| 62 | |
| 63 | // The fname==NULL terminated list of /sys/ files that the category |
| 64 | // enables. |
| 65 | struct { |
| 66 | // Whether the file must be writable in order to enable the tracing |
| 67 | // category. |
| 68 | requiredness required; |
| 69 | |
| 70 | // The path to the enable file. |
| 71 | const char* path; |
| 72 | } sysfiles[MAX_SYS_FILES]; |
| 73 | }; |
| 74 | |
| 75 | /* Tracing categories */ |
| 76 | static const TracingCategory k_categories[] = { |
| 77 | { "sched", "CPU Scheduling", 0, { |
| 78 | { REQ, "events/sched/sched_switch/enable" }, |
| 79 | { REQ, "events/sched/sched_wakeup/enable" }, |
| 80 | { OPT, "events/sched/sched_waking/enable" }, |
| 81 | { OPT, "events/sched/sched_blocked_reason/enable" }, |
| 82 | { OPT, "events/sched/sched_cpu_hotplug/enable" }, |
| 83 | { OPT, "events/sched/sched_pi_setprio/enable" }, |
| 84 | { OPT, "events/cgroup/enable" }, |
| 85 | } }, |
| 86 | { "irq", "IRQ Events", 0, { |
| 87 | { REQ, "events/irq/enable" }, |
| 88 | { OPT, "events/ipi/enable" }, |
| 89 | } }, |
| 90 | { "irqoff", "IRQ-disabled code section tracing", 0, { |
| 91 | { REQ, "events/preemptirq/irq_enable/enable" }, |
| 92 | { REQ, "events/preemptirq/irq_disable/enable" }, |
| 93 | } }, |
| 94 | { "preemptoff", "Preempt-disabled code section tracing", 0, { |
| 95 | { REQ, "events/preemptirq/preempt_enable/enable" }, |
| 96 | { REQ, "events/preemptirq/preempt_disable/enable" }, |
| 97 | } }, |
| 98 | { "i2c", "I2C Events", 0, { |
| 99 | { REQ, "events/i2c/enable" }, |
| 100 | { REQ, "events/i2c/i2c_read/enable" }, |
| 101 | { REQ, "events/i2c/i2c_write/enable" }, |
| 102 | { REQ, "events/i2c/i2c_result/enable" }, |
| 103 | { REQ, "events/i2c/i2c_reply/enable" }, |
| 104 | { OPT, "events/i2c/smbus_read/enable" }, |
| 105 | { OPT, "events/i2c/smbus_write/enable" }, |
| 106 | { OPT, "events/i2c/smbus_result/enable" }, |
| 107 | { OPT, "events/i2c/smbus_reply/enable" }, |
| 108 | } }, |
| 109 | { "freq", "CPU Frequency", 0, { |
| 110 | { REQ, "events/power/cpu_frequency/enable" }, |
| 111 | { OPT, "events/power/clock_set_rate/enable" }, |
| 112 | { OPT, "events/power/clock_disable/enable" }, |
| 113 | { OPT, "events/power/clock_enable/enable" }, |
| 114 | { OPT, "events/clk/clk_set_rate/enable" }, |
| 115 | { OPT, "events/clk/clk_disable/enable" }, |
| 116 | { OPT, "events/clk/clk_enable/enable" }, |
| 117 | { OPT, "events/power/cpu_frequency_limits/enable" }, |
| 118 | } }, |
| 119 | { "membus", "Memory Bus Utilization", 0, { |
| 120 | { REQ, "events/memory_bus/enable" }, |
| 121 | } }, |
| 122 | { "idle", "CPU Idle", 0, { |
| 123 | { REQ, "events/power/cpu_idle/enable" }, |
| 124 | } }, |
| 125 | { "disk", "Disk I/O", 0, { |
| 126 | { OPT, "events/f2fs/f2fs_sync_file_enter/enable" }, |
| 127 | { OPT, "events/f2fs/f2fs_sync_file_exit/enable" }, |
| 128 | { OPT, "events/f2fs/f2fs_write_begin/enable" }, |
| 129 | { OPT, "events/f2fs/f2fs_write_end/enable" }, |
| 130 | { OPT, "events/ext4/ext4_da_write_begin/enable" }, |
| 131 | { OPT, "events/ext4/ext4_da_write_end/enable" }, |
| 132 | { OPT, "events/ext4/ext4_sync_file_enter/enable" }, |
| 133 | { OPT, "events/ext4/ext4_sync_file_exit/enable" }, |
| 134 | { REQ, "events/block/block_rq_issue/enable" }, |
| 135 | { REQ, "events/block/block_rq_complete/enable" }, |
| 136 | } }, |
| 137 | { "mmc", "eMMC commands", 0, { |
| 138 | { REQ, "events/mmc/enable" }, |
| 139 | } }, |
| 140 | { "load", "CPU Load", 0, { |
| 141 | { REQ, "events/cpufreq_interactive/enable" }, |
| 142 | } }, |
| 143 | { "sync", "Synchronization", 0, { |
| 144 | // before linux kernel 4.9 |
| 145 | { OPT, "events/sync/enable" }, |
| 146 | // starting in linux kernel 4.9 |
| 147 | { OPT, "events/fence/enable" }, |
| 148 | } }, |
| 149 | { "workq", "Kernel Workqueues", 0, { |
| 150 | { REQ, "events/workqueue/enable" }, |
| 151 | } }, |
| 152 | { "memreclaim", "Kernel Memory Reclaim", 0, { |
| 153 | { REQ, "events/vmscan/mm_vmscan_direct_reclaim_begin/enable" }, |
| 154 | { REQ, "events/vmscan/mm_vmscan_direct_reclaim_end/enable" }, |
| 155 | { REQ, "events/vmscan/mm_vmscan_kswapd_wake/enable" }, |
| 156 | { REQ, "events/vmscan/mm_vmscan_kswapd_sleep/enable" }, |
| 157 | { OPT, "events/lowmemorykiller/enable" }, |
| 158 | } }, |
| 159 | { "regulators", "Voltage and Current Regulators", 0, { |
| 160 | { REQ, "events/regulator/enable" }, |
| 161 | } }, |
| 162 | { "binder_driver", "Binder Kernel driver", 0, { |
| 163 | { REQ, "events/binder/binder_transaction/enable" }, |
| 164 | { REQ, "events/binder/binder_transaction_received/enable" }, |
| 165 | { OPT, "events/binder/binder_set_priority/enable" }, |
| 166 | } }, |
| 167 | { "binder_lock", "Binder global lock trace", 0, { |
| 168 | { OPT, "events/binder/binder_lock/enable" }, |
| 169 | { OPT, "events/binder/binder_locked/enable" }, |
| 170 | { OPT, "events/binder/binder_unlock/enable" }, |
| 171 | } }, |
| 172 | { "pagecache", "Page cache", 0, { |
| 173 | { REQ, "events/filemap/enable" }, |
| 174 | } }, |
| 175 | { "meson_atrace", "Meson Kernel Atrace", 0, { |
| 176 | {OPT, "events/meson_atrace/enable"}, |
| 177 | } }, |
| 178 | }; |
| 179 | |
| 180 | /* Command line options */ |
| 181 | static int g_traceDurationSeconds = 5; |
| 182 | static bool g_traceOverwrite = false; |
| 183 | static int g_traceBufferSizeKB = 2048; |
| 184 | static bool g_compress = false; |
| 185 | static bool g_nohup = false; |
| 186 | static int g_initialSleepSecs = 0; |
| 187 | static const char* g_kernelTraceFuncs = NULL; |
| 188 | static const char* g_outputFile = nullptr; |
| 189 | |
| 190 | /* Global state */ |
| 191 | static bool g_traceAborted = false; |
| 192 | static bool g_categoryEnables[arraysize(k_categories)] = {}; |
| 193 | static std::string g_traceFolder; |
| 194 | |
| 195 | /* Sys file paths */ |
| 196 | static const char* k_traceClockPath = |
| 197 | "trace_clock"; |
| 198 | |
| 199 | static const char* k_traceBufferSizePath = |
| 200 | "buffer_size_kb"; |
| 201 | |
| 202 | #if 0 |
| 203 | // TODO: Re-enable after stabilization |
| 204 | static const char* k_traceCmdlineSizePath = |
| 205 | "saved_cmdlines_size"; |
| 206 | #endif |
| 207 | |
| 208 | static const char* k_tracingOverwriteEnablePath = |
| 209 | "options/overwrite"; |
| 210 | |
| 211 | static const char* k_currentTracerPath = |
| 212 | "current_tracer"; |
| 213 | |
| 214 | static const char* k_printTgidPath = |
| 215 | "options/print-tgid"; |
| 216 | |
| 217 | static const char* k_funcgraphAbsTimePath = |
| 218 | "options/funcgraph-abstime"; |
| 219 | |
| 220 | static const char* k_funcgraphCpuPath = |
| 221 | "options/funcgraph-cpu"; |
| 222 | |
| 223 | static const char* k_funcgraphProcPath = |
| 224 | "options/funcgraph-proc"; |
| 225 | |
| 226 | static const char* k_funcgraphFlatPath = |
| 227 | "options/funcgraph-flat"; |
| 228 | |
| 229 | static const char* k_ftraceFilterPath = |
| 230 | "set_ftrace_filter"; |
| 231 | |
| 232 | static const char* k_tracingOnPath = |
| 233 | "tracing_on"; |
| 234 | |
| 235 | static const char* k_tracePath = |
| 236 | "trace"; |
| 237 | |
| 238 | static const char* k_traceStreamPath = |
| 239 | "trace_pipe"; |
| 240 | |
| 241 | static const char* k_traceMarkerPath = |
| 242 | "trace_marker"; |
| 243 | |
| 244 | nsecs_t systemTime(int /*clock*/) |
| 245 | { |
| 246 | // Clock support varies widely across hosts. Mac OS doesn't support |
| 247 | // posix clocks, older glibcs don't support CLOCK_BOOTTIME and Windows |
| 248 | // is windows. |
| 249 | struct timeval t; |
| 250 | t.tv_sec = t.tv_usec = 0; |
| 251 | gettimeofday(&t, NULL); |
| 252 | return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; |
| 253 | } |
| 254 | |
| 255 | bool ReadFdToString(int fd, std::string* content) { |
| 256 | content->clear(); |
| 257 | |
| 258 | // Although original we had small files in mind, this code gets used for |
| 259 | // very large files too, where the std::string growth heuristics might not |
| 260 | // be suitable. https://code.google.com/p/android/issues/detail?id=258500. |
| 261 | struct stat sb; |
| 262 | if (fstat(fd, &sb) != -1 && sb.st_size > 0) { |
| 263 | content->reserve(sb.st_size); |
| 264 | } |
| 265 | |
| 266 | char buf[BUFSIZ]; |
| 267 | ssize_t n; |
| 268 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { |
| 269 | content->append(buf, n); |
| 270 | } |
| 271 | return (n == 0) ? true : false; |
| 272 | } |
| 273 | |
| 274 | bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) { |
| 275 | content->clear(); |
| 276 | |
| 277 | int flags = O_RDONLY | O_CLOEXEC | (follow_symlinks ? 0 : O_NOFOLLOW); |
| 278 | int fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags))); |
| 279 | if (fd == -1) { |
| 280 | return false; |
| 281 | } |
kirk.wang | fcd26c7 | 2024-08-01 16:40:24 +0800 | [diff] [blame] | 282 | bool result = ReadFdToString(fd, content); |
| 283 | close(fd); |
| 284 | return result; |
Tao Guo | d58f58b | 2021-07-23 14:09:27 +0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | bool WriteFully(int fd, const void* data, size_t byte_count) { |
| 288 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 289 | size_t remaining = byte_count; |
| 290 | while (remaining > 0) { |
| 291 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, remaining)); |
| 292 | if (n == -1) return false; |
| 293 | p += n; |
| 294 | remaining -= n; |
| 295 | } |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | // Check whether a file exists. |
| 301 | static bool fileExists(const char* filename) { |
| 302 | return access((g_traceFolder + filename).c_str(), F_OK) != -1; |
| 303 | } |
| 304 | |
| 305 | // Check whether a file is writable. |
| 306 | static bool fileIsWritable(const char* filename) { |
| 307 | return access((g_traceFolder + filename).c_str(), W_OK) != -1; |
| 308 | } |
| 309 | |
| 310 | // Truncate a file. |
| 311 | static bool truncateFile(const char* path) |
| 312 | { |
| 313 | // This uses creat rather than truncate because some of the debug kernel |
| 314 | // device nodes (e.g. k_ftraceFilterPath) currently aren't changed by |
| 315 | // calls to truncate, but they are cleared by calls to creat. |
| 316 | int traceFD = creat((g_traceFolder + path).c_str(), 0); |
| 317 | if (traceFD == -1) { |
| 318 | fprintf(stderr, "error truncating %s: %s (%d)\n", (g_traceFolder + path).c_str(), |
| 319 | strerror(errno), errno); |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | close(traceFD); |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | static bool _writeStr(const char* filename, const char* str, int flags) |
| 329 | { |
| 330 | std::string fullFilename = g_traceFolder + filename; |
| 331 | int fd = open(fullFilename.c_str(), flags); |
| 332 | if (fd == -1) { |
| 333 | fprintf(stderr, "error opening %s: %s (%d)\n", fullFilename.c_str(), |
| 334 | strerror(errno), errno); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | bool ok = true; |
| 339 | ssize_t len = strlen(str); |
| 340 | if (write(fd, str, len) != len) { |
| 341 | fprintf(stderr, "error writing to %s: %s (%d)\n", fullFilename.c_str(), |
| 342 | strerror(errno), errno); |
| 343 | ok = false; |
| 344 | } |
| 345 | |
| 346 | close(fd); |
| 347 | |
| 348 | return ok; |
| 349 | } |
| 350 | |
| 351 | // Write a string to a file, returning true if the write was successful. |
| 352 | static bool writeStr(const char* filename, const char* str) |
| 353 | { |
| 354 | return _writeStr(filename, str, O_WRONLY); |
| 355 | } |
| 356 | |
| 357 | // Append a string to a file, returning true if the write was successful. |
| 358 | static bool appendStr(const char* filename, const char* str) |
| 359 | { |
| 360 | return _writeStr(filename, str, O_APPEND|O_WRONLY); |
| 361 | } |
| 362 | |
| 363 | static void writeClockSyncMarker() |
| 364 | { |
| 365 | char buffer[128]; |
| 366 | int len = 0; |
| 367 | int fd = open((g_traceFolder + k_traceMarkerPath).c_str(), O_WRONLY); |
| 368 | if (fd == -1) { |
| 369 | fprintf(stderr, "error opening %s: %s (%d)\n", k_traceMarkerPath, |
| 370 | strerror(errno), errno); |
| 371 | return; |
| 372 | } |
| 373 | float now_in_seconds = systemTime(CLOCK_MONOTONIC) / 1000000000.0f; |
| 374 | |
| 375 | len = snprintf(buffer, 128, "trace_event_clock_sync: parent_ts=%f\n", now_in_seconds); |
| 376 | if (write(fd, buffer, len) != len) { |
| 377 | fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno); |
| 378 | } |
| 379 | |
| 380 | int64_t realtime_in_ms = systemTime(CLOCK_REALTIME) / 1000000; |
| 381 | len = snprintf(buffer, 128, "trace_event_clock_sync: realtime_ts=%" PRId64 "\n", realtime_in_ms); |
| 382 | if (write(fd, buffer, len) != len) { |
| 383 | fprintf(stderr, "error writing clock sync marker %s (%d)\n", strerror(errno), errno); |
| 384 | } |
| 385 | |
| 386 | close(fd); |
| 387 | } |
| 388 | |
| 389 | // Enable or disable a kernel option by writing a "1" or a "0" into a /sys |
| 390 | // file. |
| 391 | static bool setKernelOptionEnable(const char* filename, bool enable) |
| 392 | { |
| 393 | return writeStr(filename, enable ? "1" : "0"); |
| 394 | } |
| 395 | |
| 396 | // Check whether the category is supported on the device with the current |
| 397 | // rootness. A category is supported only if all its required /sys/ files are |
| 398 | // writable and if enabling the category will enable one or more tracing tags |
| 399 | // or /sys/ files. |
| 400 | static bool isCategorySupported(const TracingCategory& category) |
| 401 | { |
| 402 | bool ok = category.tags != 0; |
| 403 | for (int i = 0; i < MAX_SYS_FILES; i++) { |
| 404 | const char* path = category.sysfiles[i].path; |
| 405 | bool req = category.sysfiles[i].required == REQ; |
| 406 | if (path != NULL) { |
| 407 | if (req) { |
| 408 | if (!fileIsWritable(path)) { |
| 409 | return false; |
| 410 | } else { |
| 411 | ok = true; |
| 412 | } |
| 413 | } else { |
| 414 | ok = true; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | return ok; |
| 419 | } |
| 420 | |
| 421 | // Check whether the category would be supported on the device if the user |
| 422 | // were root. This function assumes that root is able to write to any file |
| 423 | // that exists. It performs the same logic as isCategorySupported, but it |
| 424 | // uses file existence rather than writability in the /sys/ file checks. |
| 425 | static bool isCategorySupportedForRoot(const TracingCategory& category) |
| 426 | { |
| 427 | bool ok = category.tags != 0; |
| 428 | for (int i = 0; i < MAX_SYS_FILES; i++) { |
| 429 | const char* path = category.sysfiles[i].path; |
| 430 | bool req = category.sysfiles[i].required == REQ; |
| 431 | if (path != NULL) { |
| 432 | if (req) { |
| 433 | if (!fileExists(path)) { |
| 434 | return false; |
| 435 | } else { |
| 436 | ok = true; |
| 437 | } |
| 438 | } else { |
| 439 | ok |= fileExists(path); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | return ok; |
| 444 | } |
| 445 | |
| 446 | // Enable or disable overwriting of the kernel trace buffers. Disabling this |
| 447 | // will cause tracing to stop once the trace buffers have filled up. |
| 448 | static bool setTraceOverwriteEnable(bool enable) |
| 449 | { |
| 450 | return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable); |
| 451 | } |
| 452 | |
| 453 | // Set the user initiated trace property |
| 454 | //static bool setUserInitiatedTraceProperty(bool enable) |
| 455 | //{ |
| 456 | // if (!android::base::SetProperty(k_userInitiatedTraceProperty, enable ? "1" : "")) { |
| 457 | // fprintf(stderr, "error setting user initiated strace system property\n"); |
| 458 | // return false; |
| 459 | // } |
| 460 | // return true; |
| 461 | //} |
| 462 | |
| 463 | // Enable or disable kernel tracing. |
| 464 | static bool setTracingEnabled(bool enable) |
| 465 | { |
| 466 | return setKernelOptionEnable(k_tracingOnPath, enable); |
| 467 | } |
| 468 | |
| 469 | // Clear the contents of the kernel trace. |
| 470 | static bool clearTrace() |
| 471 | { |
| 472 | return truncateFile(k_tracePath); |
| 473 | } |
| 474 | |
| 475 | // Set the size of the kernel's trace buffer in kilobytes. |
| 476 | static bool setTraceBufferSizeKB(int size) |
| 477 | { |
| 478 | char str[32] = "1"; |
| 479 | if (size < 1) { |
| 480 | size = 1; |
| 481 | } |
| 482 | snprintf(str, 32, "%d", size); |
| 483 | return writeStr(k_traceBufferSizePath, str); |
| 484 | } |
| 485 | |
| 486 | #if 0 |
| 487 | // TODO: Re-enable after stabilization |
| 488 | // Set the default size of cmdline hashtable |
| 489 | static bool setCmdlineSize() |
| 490 | { |
| 491 | if (fileExists(k_traceCmdlineSizePath)) { |
| 492 | return writeStr(k_traceCmdlineSizePath, "8192"); |
| 493 | } |
| 494 | return true; |
| 495 | } |
| 496 | #endif |
| 497 | |
| 498 | // Set the clock to the best available option while tracing. Use 'boot' if it's |
| 499 | // available; otherwise, use 'mono'. If neither are available use 'global'. |
| 500 | // Any write to the trace_clock sysfs file will reset the buffer, so only |
| 501 | // update it if the requested value is not the current value. |
| 502 | static bool setClock() |
| 503 | { |
| 504 | std::ifstream clockFile((g_traceFolder + k_traceClockPath).c_str()); |
| 505 | std::string clockStr((std::istreambuf_iterator<char>(clockFile)), |
| 506 | std::istreambuf_iterator<char>()); |
| 507 | |
| 508 | std::string newClock; |
| 509 | if (clockStr.find("boot") != std::string::npos) { |
| 510 | newClock = "boot"; |
| 511 | } else if (clockStr.find("mono") != std::string::npos) { |
| 512 | newClock = "mono"; |
| 513 | } else { |
| 514 | newClock = "global"; |
| 515 | } |
| 516 | |
| 517 | size_t begin = clockStr.find('[') + 1; |
| 518 | size_t end = clockStr.find(']'); |
| 519 | if (newClock.compare(0, std::string::npos, clockStr, begin, end-begin) == 0) { |
| 520 | return true; |
| 521 | } |
| 522 | return writeStr(k_traceClockPath, newClock.c_str()); |
| 523 | } |
| 524 | |
| 525 | static bool setPrintTgidEnableIfPresent(bool enable) |
| 526 | { |
| 527 | if (fileExists(k_printTgidPath)) { |
| 528 | return setKernelOptionEnable(k_printTgidPath, enable); |
| 529 | } |
| 530 | return true; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | // Set the trace tags that userland tracing uses, and poke the running |
| 535 | // processes to pick up the new value. |
| 536 | //static bool setTagsProperty(uint64_t tags) |
| 537 | //{ |
| 538 | // std::string value = android::base::StringPrintf("%#" PRIx64, tags); |
| 539 | // if (!android::base::SetProperty(k_traceTagsProperty, value)) { |
| 540 | // fprintf(stderr, "error setting trace tags system property\n"); |
| 541 | // return false; |
| 542 | // } |
| 543 | // return true; |
| 544 | //} |
| 545 | |
| 546 | // Disable all /sys/ enable files. |
| 547 | static bool disableKernelTraceEvents() { |
| 548 | bool ok = true; |
| 549 | for (size_t i = 0; i < arraysize(k_categories); i++) { |
| 550 | const TracingCategory &c = k_categories[i]; |
| 551 | for (int j = 0; j < MAX_SYS_FILES; j++) { |
| 552 | const char* path = c.sysfiles[j].path; |
| 553 | if (path != NULL && fileIsWritable(path)) { |
| 554 | ok &= setKernelOptionEnable(path, false); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | return ok; |
| 559 | } |
| 560 | |
| 561 | // Verify that the comma separated list of functions are being traced by the |
| 562 | // kernel. |
| 563 | //static bool verifyKernelTraceFuncs(const char* funcs) |
| 564 | //{ |
| 565 | // std::string buf; |
| 566 | // if (!ReadFileToString(g_traceFolder + k_ftraceFilterPath, &buf)) { |
| 567 | // fprintf(stderr, "error opening %s: %s (%d)\n", k_ftraceFilterPath, |
| 568 | // strerror(errno), errno); |
| 569 | // return false; |
| 570 | // } |
| 571 | // |
| 572 | // String8 funcList = String8::format("\n%s",buf.c_str()); |
| 573 | // |
| 574 | // // Make sure that every function listed in funcs is in the list we just |
| 575 | // // read from the kernel, except for wildcard inputs. |
| 576 | // bool ok = true; |
| 577 | // char* myFuncs = strdup(funcs); |
| 578 | // char* func = strtok(myFuncs, ","); |
| 579 | // while (func) { |
| 580 | // if (!strchr(func, '*')) { |
| 581 | // String8 fancyFunc = String8::format("\n%s\n", func); |
| 582 | // bool found = funcList.find(fancyFunc.string(), 0) >= 0; |
| 583 | // if (!found || func[0] == '\0') { |
| 584 | // fprintf(stderr, "error: \"%s\" is not a valid kernel function " |
| 585 | // "to trace.\n", func); |
| 586 | // ok = false; |
| 587 | // } |
| 588 | // } |
| 589 | // func = strtok(NULL, ","); |
| 590 | // } |
| 591 | // free(myFuncs); |
| 592 | // return ok; |
| 593 | //} |
| 594 | |
| 595 | // Set the comma separated list of functions that the kernel is to trace. |
| 596 | static bool setKernelTraceFuncs(const char* funcs) |
| 597 | { |
| 598 | bool ok = true; |
| 599 | |
| 600 | if (funcs == NULL || funcs[0] == '\0') { |
| 601 | // Disable kernel function tracing. |
| 602 | if (fileIsWritable(k_currentTracerPath)) { |
| 603 | ok &= writeStr(k_currentTracerPath, "nop"); |
| 604 | } |
| 605 | if (fileIsWritable(k_ftraceFilterPath)) { |
| 606 | ok &= truncateFile(k_ftraceFilterPath); |
| 607 | } |
| 608 | } else { |
| 609 | // Enable kernel function tracing. |
| 610 | ok &= writeStr(k_currentTracerPath, "function_graph"); |
| 611 | ok &= setKernelOptionEnable(k_funcgraphAbsTimePath, true); |
| 612 | ok &= setKernelOptionEnable(k_funcgraphCpuPath, true); |
| 613 | ok &= setKernelOptionEnable(k_funcgraphProcPath, true); |
| 614 | ok &= setKernelOptionEnable(k_funcgraphFlatPath, true); |
| 615 | |
| 616 | // Set the requested filter functions. |
| 617 | ok &= truncateFile(k_ftraceFilterPath); |
| 618 | char* myFuncs = strdup(funcs); |
| 619 | char* func = strtok(myFuncs, ","); |
| 620 | while (func) { |
| 621 | ok &= appendStr(k_ftraceFilterPath, func); |
| 622 | func = strtok(NULL, ","); |
| 623 | } |
| 624 | free(myFuncs); |
| 625 | |
| 626 | // Verify that the set functions are being traced. |
| 627 | if (ok) { |
| 628 | // ok &= verifyKernelTraceFuncs(funcs); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | return ok; |
| 633 | } |
| 634 | |
| 635 | static bool setCategoryEnable(const char* name, bool enable) |
| 636 | { |
| 637 | for (size_t i = 0; i < arraysize(k_categories); i++) { |
| 638 | const TracingCategory& c = k_categories[i]; |
| 639 | if (strcmp(name, c.name) == 0) { |
| 640 | if (isCategorySupported(c)) { |
| 641 | g_categoryEnables[i] = enable; |
| 642 | return true; |
| 643 | } else { |
| 644 | if (isCategorySupportedForRoot(c)) { |
| 645 | fprintf(stderr, "error: category \"%s\" requires root " |
| 646 | "privileges.\n", name); |
| 647 | } else { |
| 648 | fprintf(stderr, "error: category \"%s\" is not supported " |
| 649 | "on this device.\n", name); |
| 650 | } |
| 651 | return false; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | fprintf(stderr, "error: unknown tracing category \"%s\"\n", name); |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | static bool setUpUserspaceTracing() |
| 660 | { |
| 661 | bool ok = true; |
| 662 | |
| 663 | // Set up the tags property. |
| 664 | uint64_t tags = 0; |
| 665 | for (size_t i = 0; i < arraysize(k_categories); i++) { |
| 666 | if (g_categoryEnables[i]) { |
| 667 | const TracingCategory &c = k_categories[i]; |
| 668 | tags |= c.tags; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return ok; |
| 673 | } |
| 674 | |
| 675 | static void cleanUpUserspaceTracing() |
| 676 | { |
| 677 | // setTagsProperty(0); |
| 678 | |
| 679 | } |
| 680 | |
| 681 | |
| 682 | // Set all the kernel tracing settings to the desired state for this trace |
| 683 | // capture. |
| 684 | static bool setUpKernelTracing() |
| 685 | { |
| 686 | bool ok = true; |
| 687 | |
| 688 | // ok &= setUserInitiatedTraceProperty(true); |
| 689 | |
| 690 | // Set up the tracing options. |
| 691 | ok &= setTraceOverwriteEnable(g_traceOverwrite); |
| 692 | ok &= setTraceBufferSizeKB(g_traceBufferSizeKB); |
| 693 | // TODO: Re-enable after stabilization |
| 694 | //ok &= setCmdlineSize(); |
| 695 | ok &= setClock(); |
| 696 | ok &= setPrintTgidEnableIfPresent(true); |
| 697 | ok &= setKernelTraceFuncs(g_kernelTraceFuncs); |
| 698 | |
| 699 | // Disable all the sysfs enables. This is done as a separate loop from |
| 700 | // the enables to allow the same enable to exist in multiple categories. |
| 701 | ok &= disableKernelTraceEvents(); |
| 702 | |
| 703 | // Enable all the sysfs enables that are in an enabled category. |
| 704 | for (size_t i = 0; i < arraysize(k_categories); i++) { |
| 705 | if (g_categoryEnables[i]) { |
| 706 | const TracingCategory &c = k_categories[i]; |
| 707 | for (int j = 0; j < MAX_SYS_FILES; j++) { |
| 708 | const char* path = c.sysfiles[j].path; |
| 709 | bool required = c.sysfiles[j].required == REQ; |
| 710 | if (path != NULL) { |
| 711 | if (fileIsWritable(path)) { |
| 712 | ok &= setKernelOptionEnable(path, true); |
| 713 | } else if (required) { |
| 714 | fprintf(stderr, "error writing file %s\n", path); |
| 715 | ok = false; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | return ok; |
| 723 | } |
| 724 | |
| 725 | // Reset all the kernel tracing settings to their default state. |
| 726 | static void cleanUpKernelTracing() |
| 727 | { |
| 728 | // Disable all tracing that we're able to. |
| 729 | disableKernelTraceEvents(); |
| 730 | |
| 731 | // Set the options back to their defaults. |
| 732 | setTraceOverwriteEnable(true); |
| 733 | setTraceBufferSizeKB(1); |
| 734 | setPrintTgidEnableIfPresent(false); |
| 735 | setKernelTraceFuncs(NULL); |
| 736 | // setUserInitiatedTraceProperty(false); |
| 737 | } |
| 738 | |
| 739 | // Enable tracing in the kernel. |
| 740 | static bool startTrace() |
| 741 | { |
| 742 | return setTracingEnabled(true); |
| 743 | } |
| 744 | |
| 745 | // Disable tracing in the kernel. |
| 746 | static void stopTrace() |
| 747 | { |
| 748 | setTracingEnabled(false); |
| 749 | } |
| 750 | |
| 751 | // Read data from the tracing pipe and forward to stdout |
| 752 | static void streamTrace() |
| 753 | { |
| 754 | char trace_data[4096]; |
| 755 | int traceFD = open((g_traceFolder + k_traceStreamPath).c_str(), O_RDWR); |
| 756 | if (traceFD == -1) { |
| 757 | fprintf(stderr, "error opening %s: %s (%d)\n", k_traceStreamPath, |
| 758 | strerror(errno), errno); |
| 759 | return; |
| 760 | } |
| 761 | while (!g_traceAborted) { |
| 762 | ssize_t bytes_read = read(traceFD, trace_data, 4096); |
| 763 | if (bytes_read > 0) { |
| 764 | write(STDOUT_FILENO, trace_data, bytes_read); |
| 765 | fflush(stdout); |
| 766 | } else { |
| 767 | if (!g_traceAborted) { |
| 768 | fprintf(stderr, "read returned %zd bytes err %d (%s)\n", |
| 769 | bytes_read, errno, strerror(errno)); |
| 770 | } |
| 771 | break; |
| 772 | } |
| 773 | } |
kirk.wang | fcd26c7 | 2024-08-01 16:40:24 +0800 | [diff] [blame] | 774 | close(traceFD); |
Tao Guo | d58f58b | 2021-07-23 14:09:27 +0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | // Read the current kernel trace and write it to stdout. |
| 778 | static void dumpTrace(int outFd) |
| 779 | { |
| 780 | int traceFD = open((g_traceFolder + k_tracePath).c_str(), O_RDWR); |
| 781 | if (traceFD == -1) { |
| 782 | fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath, |
| 783 | strerror(errno), errno); |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | if (g_compress) { |
| 788 | z_stream zs; |
| 789 | memset(&zs, 0, sizeof(zs)); |
| 790 | |
| 791 | int result = deflateInit(&zs, Z_DEFAULT_COMPRESSION); |
| 792 | if (result != Z_OK) { |
| 793 | fprintf(stderr, "error initializing zlib: %d\n", result); |
| 794 | close(traceFD); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | constexpr size_t bufSize = 64*1024; |
| 799 | std::unique_ptr<uint8_t> in(new uint8_t[bufSize]); |
| 800 | std::unique_ptr<uint8_t> out(new uint8_t[bufSize]); |
| 801 | if (!in || !out) { |
| 802 | fprintf(stderr, "couldn't allocate buffers\n"); |
| 803 | close(traceFD); |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | int flush = Z_NO_FLUSH; |
| 808 | |
| 809 | zs.next_out = reinterpret_cast<Bytef*>(out.get()); |
| 810 | zs.avail_out = bufSize; |
| 811 | |
| 812 | do { |
| 813 | |
| 814 | if (zs.avail_in == 0) { |
| 815 | // More input is needed. |
| 816 | result = read(traceFD, in.get(), bufSize); |
| 817 | if (result < 0) { |
| 818 | fprintf(stderr, "error reading trace: %s (%d)\n", |
| 819 | strerror(errno), errno); |
| 820 | result = Z_STREAM_END; |
| 821 | break; |
| 822 | } else if (result == 0) { |
| 823 | flush = Z_FINISH; |
| 824 | } else { |
| 825 | zs.next_in = reinterpret_cast<Bytef*>(in.get()); |
| 826 | zs.avail_in = result; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | if (zs.avail_out == 0) { |
| 831 | // Need to write the output. |
| 832 | result = write(outFd, out.get(), bufSize); |
| 833 | if ((size_t)result < bufSize) { |
| 834 | fprintf(stderr, "error writing deflated trace: %s (%d)\n", |
| 835 | strerror(errno), errno); |
| 836 | result = Z_STREAM_END; // skip deflate error message |
| 837 | zs.avail_out = bufSize; // skip the final write |
| 838 | break; |
| 839 | } |
| 840 | zs.next_out = reinterpret_cast<Bytef*>(out.get()); |
| 841 | zs.avail_out = bufSize; |
| 842 | } |
| 843 | |
| 844 | } while ((result = deflate(&zs, flush)) == Z_OK); |
| 845 | |
| 846 | if (result != Z_STREAM_END) { |
| 847 | fprintf(stderr, "error deflating trace: %s\n", zs.msg); |
| 848 | } |
| 849 | |
| 850 | if (zs.avail_out < bufSize) { |
| 851 | size_t bytes = bufSize - zs.avail_out; |
| 852 | result = write(outFd, out.get(), bytes); |
| 853 | if ((size_t)result < bytes) { |
| 854 | fprintf(stderr, "error writing deflated trace: %s (%d)\n", |
| 855 | strerror(errno), errno); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | result = deflateEnd(&zs); |
| 860 | if (result != Z_OK) { |
| 861 | fprintf(stderr, "error cleaning up zlib: %d\n", result); |
| 862 | } |
| 863 | } else { |
| 864 | char buf[4096]; |
| 865 | ssize_t rc; |
| 866 | while ((rc = TEMP_FAILURE_RETRY(read(traceFD, buf, sizeof(buf)))) > 0) { |
| 867 | if (!WriteFully(outFd, buf, rc)) { |
| 868 | fprintf(stderr, "error writing trace: %s\n", strerror(errno)); |
| 869 | break; |
| 870 | } |
| 871 | } |
| 872 | if (rc == -1) { |
| 873 | fprintf(stderr, "error dumping trace: %s\n", strerror(errno)); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | close(traceFD); |
| 878 | } |
| 879 | |
| 880 | static void handleSignal(int /*signo*/) |
| 881 | { |
| 882 | if (!g_nohup) { |
| 883 | g_traceAborted = true; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | static void registerSigHandler() |
| 888 | { |
| 889 | struct sigaction sa; |
| 890 | sigemptyset(&sa.sa_mask); |
| 891 | sa.sa_flags = 0; |
| 892 | sa.sa_handler = handleSignal; |
| 893 | sigaction(SIGHUP, &sa, NULL); |
| 894 | sigaction(SIGINT, &sa, NULL); |
| 895 | sigaction(SIGQUIT, &sa, NULL); |
| 896 | sigaction(SIGTERM, &sa, NULL); |
| 897 | } |
| 898 | |
| 899 | static void listSupportedCategories() |
| 900 | { |
| 901 | for (size_t i = 0; i < arraysize(k_categories); i++) { |
| 902 | const TracingCategory& c = k_categories[i]; |
| 903 | if (isCategorySupported(c)) { |
| 904 | printf(" %10s - %s\n", c.name, c.longname); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | // Print the command usage help to stderr. |
| 910 | static void showHelp(const char *cmd) |
| 911 | { |
| 912 | fprintf(stderr, "usage: %s [options] [categories...]\n", cmd); |
| 913 | fprintf(stderr, "options include:\n" |
| 914 | " -b N use a trace buffer size of N KB\n" |
| 915 | " -c trace into a circular buffer\n" |
| 916 | " -k fname,... trace the listed kernel functions\n" |
| 917 | " -n ignore signals\n" |
| 918 | " -s N sleep for N seconds before tracing [default 0]\n" |
| 919 | " -t N trace for N seconds [default 5]\n" |
| 920 | " -z compress the trace dump\n" |
| 921 | " --async_start start circular trace and return immediately\n" |
| 922 | " --async_dump dump the current contents of circular trace buffer\n" |
| 923 | " --async_stop stop tracing and dump the current contents of circular\n" |
| 924 | " trace buffer\n" |
| 925 | " --stream stream trace to stdout as it enters the trace buffer\n" |
| 926 | " Note: this can take significant CPU time, and is best\n" |
| 927 | " used for measuring things that are not affected by\n" |
| 928 | " CPU performance, like pagecache usage.\n" |
| 929 | " --list_categories\n" |
| 930 | " list the available tracing categories\n" |
| 931 | " -o filename write the trace to the specified file instead\n" |
| 932 | " of stdout.\n" |
| 933 | " -p keep trace data.\n" |
| 934 | ); |
| 935 | } |
| 936 | |
| 937 | bool findTraceFiles() |
| 938 | { |
| 939 | static const std::string debugfs_path = "/sys/kernel/debug/tracing/"; |
| 940 | static const std::string tracefs_path = "/sys/kernel/tracing/"; |
| 941 | static const std::string trace_file = "trace_marker"; |
| 942 | |
| 943 | bool tracefs = access((tracefs_path + trace_file).c_str(), F_OK) != -1; |
| 944 | bool debugfs = access((debugfs_path + trace_file).c_str(), F_OK) != -1; |
| 945 | |
| 946 | if (!tracefs && !debugfs) { |
| 947 | fprintf(stderr, "Error: Did not find trace folder\n"); |
| 948 | return false; |
| 949 | } |
| 950 | |
| 951 | if (tracefs) { |
| 952 | g_traceFolder = tracefs_path; |
| 953 | } else { |
| 954 | g_traceFolder = debugfs_path; |
| 955 | } |
| 956 | |
| 957 | return true; |
| 958 | } |
| 959 | |
| 960 | int main(int argc, char **argv) |
| 961 | { |
| 962 | bool async = false; |
| 963 | bool traceStart = true; |
| 964 | bool traceStop = true; |
| 965 | bool traceDump = true; |
| 966 | bool traceStream = false; |
| 967 | bool onlyUserspace = false; |
| 968 | bool keepBuffer = false; |
| 969 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
| 970 | showHelp(argv[0]); |
| 971 | exit(0); |
| 972 | } |
| 973 | |
| 974 | if (!findTraceFiles()) { |
| 975 | fprintf(stderr, "No trace folder found\n"); |
| 976 | exit(-1); |
| 977 | } |
| 978 | |
| 979 | for (;;) { |
| 980 | int ret; |
| 981 | int option_index = 0; |
| 982 | static struct option long_options[] = { |
| 983 | {"async_start", no_argument, 0, 0 }, |
| 984 | {"async_stop", no_argument, 0, 0 }, |
| 985 | {"async_dump", no_argument, 0, 0 }, |
| 986 | {"only_userspace", no_argument, 0, 0 }, |
| 987 | {"list_categories", no_argument, 0, 0 }, |
| 988 | {"stream", no_argument, 0, 0 }, |
| 989 | { 0, 0, 0, 0 } |
| 990 | }; |
| 991 | |
| 992 | ret = getopt_long(argc, argv, "a:b:cf:k:ns:t:zo:p", |
| 993 | long_options, &option_index); |
| 994 | |
| 995 | if (ret < 0) { |
| 996 | for (int i = optind; i < argc; i++) { |
| 997 | if (!setCategoryEnable(argv[i], true)) { |
| 998 | fprintf(stderr, "error enabling tracing category \"%s\"\n", argv[i]); |
| 999 | exit(1); |
| 1000 | } |
| 1001 | } |
| 1002 | break; |
| 1003 | } |
| 1004 | |
| 1005 | switch(ret) { |
| 1006 | case 'b': |
| 1007 | g_traceBufferSizeKB = atoi(optarg); |
| 1008 | break; |
| 1009 | |
| 1010 | case 'c': |
| 1011 | g_traceOverwrite = true; |
| 1012 | break; |
| 1013 | |
| 1014 | case 'k': |
| 1015 | g_kernelTraceFuncs = optarg; |
| 1016 | break; |
| 1017 | |
| 1018 | case 'n': |
| 1019 | g_nohup = true; |
| 1020 | break; |
| 1021 | |
| 1022 | case 's': |
| 1023 | g_initialSleepSecs = atoi(optarg); |
| 1024 | break; |
| 1025 | |
| 1026 | case 't': |
| 1027 | g_traceDurationSeconds = atoi(optarg); |
| 1028 | break; |
| 1029 | |
| 1030 | case 'z': |
| 1031 | g_compress = true; |
| 1032 | break; |
| 1033 | |
| 1034 | case 'o': |
| 1035 | g_outputFile = optarg; |
| 1036 | break; |
| 1037 | |
| 1038 | case 'p': |
| 1039 | keepBuffer = true; |
| 1040 | break; |
| 1041 | |
| 1042 | case 0: |
| 1043 | if (!strcmp(long_options[option_index].name, "async_start")) { |
| 1044 | async = true; |
| 1045 | traceStop = false; |
| 1046 | traceDump = false; |
| 1047 | g_traceOverwrite = true; |
| 1048 | } else if (!strcmp(long_options[option_index].name, "async_stop")) { |
| 1049 | async = true; |
| 1050 | traceStart = false; |
| 1051 | } else if (!strcmp(long_options[option_index].name, "async_dump")) { |
| 1052 | async = true; |
| 1053 | traceStart = false; |
| 1054 | traceStop = false; |
| 1055 | } else if (!strcmp(long_options[option_index].name, "only_userspace")) { |
| 1056 | onlyUserspace = true; |
| 1057 | } else if (!strcmp(long_options[option_index].name, "stream")) { |
| 1058 | traceStream = true; |
| 1059 | traceDump = false; |
| 1060 | } else if (!strcmp(long_options[option_index].name, "list_categories")) { |
| 1061 | listSupportedCategories(); |
| 1062 | exit(0); |
| 1063 | } |
| 1064 | break; |
| 1065 | |
| 1066 | default: |
| 1067 | fprintf(stderr, "\n"); |
| 1068 | showHelp(argv[0]); |
| 1069 | exit(-1); |
| 1070 | break; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | if (onlyUserspace) { |
| 1075 | if (!async || !(traceStart || traceStop)) { |
| 1076 | fprintf(stderr, "--only_userspace can only be used with " |
| 1077 | "--async_start or --async_stop\n"); |
| 1078 | exit(1); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | registerSigHandler(); |
| 1083 | |
| 1084 | if (g_initialSleepSecs > 0) { |
| 1085 | sleep(g_initialSleepSecs); |
| 1086 | } |
| 1087 | |
| 1088 | bool ok = true; |
| 1089 | |
| 1090 | if (traceStart) { |
| 1091 | ok &= setUpUserspaceTracing(); |
| 1092 | } |
| 1093 | |
| 1094 | if (ok && traceStart && !onlyUserspace) { |
| 1095 | ok &= setUpKernelTracing(); |
| 1096 | ok &= startTrace(); |
| 1097 | } |
| 1098 | |
| 1099 | if (ok && traceStart) { |
| 1100 | |
| 1101 | if (!traceStream && !onlyUserspace) { |
| 1102 | printf("capturing trace...\n"); |
| 1103 | fflush(stdout); |
| 1104 | } |
| 1105 | |
| 1106 | // We clear the trace after starting it because tracing gets enabled for |
| 1107 | // each CPU individually in the kernel. Having the beginning of the trace |
| 1108 | // contain entries from only one CPU can cause "begin" entries without a |
| 1109 | // matching "end" entry to show up if a task gets migrated from one CPU to |
| 1110 | // another. |
| 1111 | if (!onlyUserspace) |
| 1112 | ok = clearTrace(); |
| 1113 | |
| 1114 | if (!onlyUserspace) |
| 1115 | writeClockSyncMarker(); |
| 1116 | |
| 1117 | if (ok && !async && !traceStream) { |
| 1118 | // Sleep to allow the trace to be captured. |
| 1119 | struct timespec timeLeft; |
| 1120 | timeLeft.tv_sec = g_traceDurationSeconds; |
| 1121 | timeLeft.tv_nsec = 0; |
| 1122 | do { |
| 1123 | if (g_traceAborted) { |
| 1124 | break; |
| 1125 | } |
| 1126 | } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR); |
| 1127 | } |
| 1128 | |
| 1129 | if (traceStream) { |
| 1130 | streamTrace(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | // Stop the trace and restore the default settings. |
| 1135 | if (traceStop && !onlyUserspace) |
| 1136 | stopTrace(); |
| 1137 | |
| 1138 | if (ok && traceDump && !onlyUserspace) { |
| 1139 | if (!g_traceAborted) { |
| 1140 | printf("saving...\n"); |
| 1141 | fflush(stdout); |
| 1142 | int outFd = STDOUT_FILENO; |
| 1143 | if (g_outputFile) { |
| 1144 | outFd = open(g_outputFile, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1145 | } |
| 1146 | if (outFd == -1) { |
| 1147 | printf("Failed to open '%s', err=%d", g_outputFile, errno); |
| 1148 | } else { |
| 1149 | dprintf(outFd, "TRACE:\n"); |
| 1150 | dumpTrace(outFd); |
| 1151 | if (g_outputFile) { |
| 1152 | close(outFd); |
| 1153 | } |
| 1154 | } |
| 1155 | } else { |
| 1156 | printf("\ntrace aborted.\n"); |
| 1157 | fflush(stdout); |
| 1158 | } |
| 1159 | if (!keepBuffer) { |
| 1160 | clearTrace(); |
| 1161 | } |
| 1162 | } else if (!ok) { |
| 1163 | fprintf(stderr, "unable to start tracing\n"); |
| 1164 | } |
| 1165 | |
| 1166 | // Reset the trace buffer size to 1. |
| 1167 | if (traceStop) { |
| 1168 | cleanUpUserspaceTracing(); |
| 1169 | if (!onlyUserspace) |
| 1170 | cleanUpKernelTracing(); |
| 1171 | } |
| 1172 | sync(); |
| 1173 | printf("done\n"); |
| 1174 | return g_traceAborted ? 1 : 0; |
| 1175 | } |