From 2a4a5e72f161699be75b247e6fcb3a9ac6d8a852 Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Mon, 20 Mar 2017 10:54:52 -0700 Subject: [PATCH] Add end() method to bionic's ScopedTrace class Bug: http://b/27195126 Test: make Change-Id: I8243629200606ca87b11cbd479ca093add42eb56 --- libc/bionic/bionic_systrace.cpp | 31 ++++++++++++++++++++++++------- libc/private/bionic_systrace.h | 5 +++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/libc/bionic/bionic_systrace.cpp b/libc/bionic/bionic_systrace.cpp index b522b109b..8e8ff768c 100644 --- a/libc/bionic/bionic_systrace.cpp +++ b/libc/bionic/bionic_systrace.cpp @@ -48,10 +48,11 @@ static bool should_trace() { // case an audit will be logged, and during boot before the property server has // been started, in which case we store the global property_area serial to prevent // the costly find operation until we see a changed property_area. - if (!g_pinfo && g_property_area_serial != __system_property_area_serial()) { + if (g_pinfo == nullptr && g_property_area_serial != __system_property_area_serial()) { g_property_area_serial = __system_property_area_serial(); g_pinfo = __system_property_find(SYSTRACE_PROPERTY_NAME); } + if (g_pinfo) { // Find out which tags have been enabled on the command line and set // the value of tags accordingly. If the value of the property changes, @@ -61,10 +62,11 @@ static bool should_trace() { // not to move. uint32_t cur_serial = __system_property_serial(g_pinfo); if (cur_serial != g_property_serial) { - g_property_serial = cur_serial; - char value[PROP_VALUE_MAX]; - __system_property_read(g_pinfo, 0, value); - g_tags = strtoull(value, nullptr, 0); + __system_property_read_callback(g_pinfo, + [] (void*, const char*, const char* value, uint32_t serial) { + g_property_serial = serial; + g_tags = strtoull(value, nullptr, 0); + }, nullptr); } result = ((g_tags & ATRACE_TAG_BIONIC) != 0); } @@ -81,7 +83,7 @@ static int get_trace_marker_fd() { return g_trace_marker_fd; } -ScopedTrace::ScopedTrace(const char* message) { +void bionic_trace_begin(const char* message) { if (!should_trace()) { return; } @@ -102,7 +104,7 @@ ScopedTrace::ScopedTrace(const char* message) { TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len)); } -ScopedTrace::~ScopedTrace() { +void bionic_trace_end() { if (!should_trace()) { return; } @@ -114,3 +116,18 @@ ScopedTrace::~ScopedTrace() { TEMP_FAILURE_RETRY(write(trace_marker_fd, "E", 1)); } + +ScopedTrace::ScopedTrace(const char* message) : called_end_(false) { + bionic_trace_begin(message); +} + +ScopedTrace::~ScopedTrace() { + End(); +} + +void ScopedTrace::End() { + if (!called_end_) { + bionic_trace_end(); + called_end_ = true; + } +} diff --git a/libc/private/bionic_systrace.h b/libc/private/bionic_systrace.h index 0b4560f92..304fb8061 100644 --- a/libc/private/bionic_systrace.h +++ b/libc/private/bionic_systrace.h @@ -28,8 +28,13 @@ class __LIBC_HIDDEN__ ScopedTrace { explicit ScopedTrace(const char* message); ~ScopedTrace(); + void End(); private: + bool called_end_; DISALLOW_COPY_AND_ASSIGN(ScopedTrace); }; +void bionic_trace_begin(const char* message); +void bionic_trace_end(); + #endif