[PATCH 1/9] Cleanup backtrace setup code

Alexander Monakov amonakov at ispras.ru
Sun May 19 07:39:47 PDT 2013


Obtain the list of functions to backtrace from the environment on Android as
well.
---
 common/trace_backtrace.cpp | 87 +++++++++++-----------------------------------
 common/trace_backtrace.hpp | 18 +---------
 2 files changed, 21 insertions(+), 84 deletions(-)

diff --git a/common/trace_backtrace.cpp b/common/trace_backtrace.cpp
index 730cfef..93991a9 100644
--- a/common/trace_backtrace.cpp
+++ b/common/trace_backtrace.cpp
@@ -65,8 +65,6 @@ struct pstring {
 };
 
 
-#define PREFIX_BUF_SIZE (PREFIX_MAX_FUNC_NAME * MAX_BT_FUNC)
-
 class StringPrefixes {
 private:
     std::set<pstring> pset;
@@ -82,15 +80,33 @@ private:
         }
     }
 public:
-    StringPrefixes(const char* source);
+    StringPrefixes();
 
     bool contain(const char* s) {
         return pset.find(pstring(s, strlen(s) + 1)) != pset.end();
     }
 };
 
+StringPrefixes::StringPrefixes() {
+    char *list = getenv("APITRACE_BACKTRACE");
+    if (!list)
+        return;
+    for (char *t = strdup(list); ; t = NULL) {
+        char *tok = strtok(t, " \t\r\n");
+        if (!tok)
+            break;
+        if (tok[0] == '#')
+            continue;
+        if (tok[strlen(tok) - 1] == '*')
+            addPrefix(tok, strlen(tok) - 1);
+        else
+            addPrefix(tok, strlen(tok) + 1);
+    }
+}
+
+
 bool backtrace_is_needed(const char* fname) {
-    static StringPrefixes backtraceFunctionNamePrefixes(APITRACE_FNAMES_SOURCE);
+    static StringPrefixes backtraceFunctionNamePrefixes;
     return backtraceFunctionNamePrefixes.contain(fname);
 }
 
@@ -104,37 +120,6 @@ bool backtrace_is_needed(const char* fname) {
 
 namespace trace {
 
-StringPrefixes::StringPrefixes(const char* source) {
-    char* buf = (char*)malloc(sizeof(char) * PREFIX_BUF_SIZE);
-    char* startbuf = buf;
-    int n = 0;
-    FILE* f = fopen(source, "r");
-    if (f == NULL) {
-        os::log("Cannot open " APITRACE_FNAMES_FILE);
-    }
-    while ((startbuf = fgets(startbuf, PREFIX_MAX_FUNC_NAME, f))) {
-        n = strlen(startbuf);
-        if (startbuf[n - 1] == '\n') {
-            n--;
-        }
-        if (n > 2 && startbuf[0] != '#') {
-            int psize;
-            if (startbuf[n - 1] != '*') {
-                startbuf[n] = '\0';
-                psize = n + 1;
-            }
-            else {
-                psize = n - 1;
-            }
-            addPrefix(startbuf, psize);
-            startbuf += n + 1;
-            n = 0;
-        }
-    }
-    fclose(f);
-}
-
-
 /* The following two declarations are copied from Android sources */
 
 enum DebugTargetKind {
@@ -297,38 +282,6 @@ std::vector<RawStackFrame> get_backtrace() {
 namespace trace {
 
 
-StringPrefixes::StringPrefixes(const char* source) {
-    char* buf = (char*)malloc(sizeof(char) * PREFIX_BUF_SIZE);
-    char* startbuf = buf;
-    int n = 0;
-    char* s = getenv(source);
-    char end = ';';
-    if (s == NULL) {
-        return;
-    }
-    *buf = ';';
-    strncpy(buf + 1, s, PREFIX_BUF_SIZE - 2);
-    while (end != '\0') {
-        startbuf++;
-        while (*(startbuf + n) != ';' && *(startbuf + n) != '\0') {
-            n++;
-        }
-        end = startbuf[n];
-        if (n > 2 && startbuf[0] != '#') {
-            int psize;
-            if (startbuf[n - 1] != '*') {
-                startbuf[n] = '\0';
-                psize = n + 1;
-            }
-            else {
-                psize = n - 1;
-            }
-            addPrefix(startbuf, psize);
-            startbuf += n;
-            n = 0;
-        }
-    }
-}
 
 
 #define BT_DEPTH 10
diff --git a/common/trace_backtrace.hpp b/common/trace_backtrace.hpp
index de6f415..a8210d5 100644
--- a/common/trace_backtrace.hpp
+++ b/common/trace_backtrace.hpp
@@ -13,23 +13,7 @@ namespace trace {
 std::vector<RawStackFrame> get_backtrace();
 bool backtrace_is_needed(const char* fname);
 
-#if defined(ANDROID)
-
-#define MAX_BT_FUNC 20
-#define PREFIX_MAX_FUNC_NAME 100
-#define APITRACE_FNAMES_FILE "/data/apitrace.fnames"
-#define APITRACE_FNAMES_SOURCE APITRACE_FNAMES_FILE
-
-#elif defined(__linux__)
-
-#define MAX_BT_FUNC 20
-#define PREFIX_MAX_FUNC_NAME 100
-#define APITRACE_FNAMES_ENV "APITRACE_BT_FUNCTIONS"
-#define APITRACE_FNAMES_SOURCE APITRACE_FNAMES_ENV
-
-#endif
-
-#else /* !__linux__ && !ANDROID */
+#else
 
 static inline std::vector<RawStackFrame> get_backtrace() {
     return std::vector<RawStackFrame>();
-- 
1.8.1.2



More information about the apitrace mailing list