[compiler-rt] r302210 - [XRay][compiler-rt] Add function id utilities for XRay

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Thu May 4 18:27:12 PDT 2017


Author: dberris
Date: Thu May  4 20:27:11 2017
New Revision: 302210

URL: http://llvm.org/viewvc/llvm-project?rev=302210&view=rev
Log:
[XRay][compiler-rt] Add function id utilities for XRay

Summary:
This change allows us to provide users and implementers of XRay handlers
a means of converting XRay function id's to addresses. This, in
combination with the facilities provided in D32695, allows users to find
out:

  - How many function id's there are defined in the current binary.
  - Get the address of the function associated with this function id.
  - Patch only specific functions according to their requirements.

While we don't directly provide symbolization support in XRay, having
the function's address lets users determine this information easily
either during runtime, or offline with tools like 'addr2line'.

Reviewers: dblaikie, echristo, pelikan

Subscribers: kpw, llvm-commits

Differential Revision: https://reviews.llvm.org/D32846

Added:
    compiler-rt/trunk/test/xray/TestCases/Linux/func-id-utils.cc
Modified:
    compiler-rt/trunk/include/xray/xray_interface.h
    compiler-rt/trunk/lib/xray/xray_interface.cc

Modified: compiler-rt/trunk/include/xray/xray_interface.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/include/xray/xray_interface.h?rev=302210&r1=302209&r2=302210&view=diff
==============================================================================
--- compiler-rt/trunk/include/xray/xray_interface.h (original)
+++ compiler-rt/trunk/include/xray/xray_interface.h Thu May  4 20:27:11 2017
@@ -15,6 +15,7 @@
 #define XRAY_XRAY_INTERFACE_H
 
 #include <cstdint>
+#include <stddef.h>
 
 extern "C" {
 
@@ -86,6 +87,14 @@ extern XRayPatchingStatus __xray_patch_f
 /// result values.
 extern XRayPatchingStatus __xray_unpatch_function(int32_t FuncId);
 
+/// This function returns the address of the function provided a valid function
+/// id. We return 0 if we encounter any error, even if 0 may be a valid function
+/// address.
+extern uintptr_t __xray_function_address(int32_t FuncId);
+
+/// This function returns the maximum valid function id. Returns 0 if we
+/// encounter errors (when there are no instrumented functions, etc.).
+extern size_t __xray_max_function_id();
 
 }
 

Modified: compiler-rt/trunk/lib/xray/xray_interface.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_interface.cc?rev=302210&r1=302209&r2=302210&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_interface.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_interface.cc Thu May  4 20:27:11 2017
@@ -255,7 +255,7 @@ XRayPatchingStatus patchFunction(int32_t
 
   // FuncId must be a positive number, less than the number of functions
   // instrumented.
-  if (FuncId <= 0 || static_cast<size_t>(FuncId) >= InstrMap.Functions) {
+  if (FuncId <= 0 || static_cast<size_t>(FuncId) > InstrMap.Functions) {
     Report("Invalid function id provided: %d\n", FuncId);
     return XRayPatchingStatus::FAILED;
   }
@@ -302,3 +302,15 @@ int __xray_set_handler_arg1(void (*Handl
   return 1;
 }
 int __xray_remove_handler_arg1() { return __xray_set_handler_arg1(nullptr); }
+
+uintptr_t __xray_function_address(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
+  __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
+  if (FuncId <= 0 || static_cast<size_t>(FuncId) > XRayInstrMap.Functions)
+    return 0;
+  return XRayInstrMap.SledsIndex[FuncId - 1].Begin->Address;
+}
+
+size_t __xray_max_function_id() XRAY_NEVER_INSTRUMENT {
+  __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
+  return XRayInstrMap.Functions;
+}

Added: compiler-rt/trunk/test/xray/TestCases/Linux/func-id-utils.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/xray/TestCases/Linux/func-id-utils.cc?rev=302210&view=auto
==============================================================================
--- compiler-rt/trunk/test/xray/TestCases/Linux/func-id-utils.cc (added)
+++ compiler-rt/trunk/test/xray/TestCases/Linux/func-id-utils.cc Thu May  4 20:27:11 2017
@@ -0,0 +1,46 @@
+// Check that we can turn a function id to a function address, and also get the
+// maximum function id for the current binary.
+//
+// RUN: %clangxx_xray -std=c++11 %s -o %t
+// RUN: XRAY_OPTIONS="patch_premain=false xray_naive_log=false" %run %t | FileCheck %s
+
+#include "xray/xray_interface.h"
+#include <algorithm>
+#include <cstdio>
+#include <set>
+#include <iterator>
+
+[[clang::xray_always_instrument]] void bar(){
+    // do nothing!
+}
+
+    [[clang::xray_always_instrument]] void foo() {
+  bar();
+}
+
+[[clang::xray_always_instrument]] int main(int argc, char *argv[]) {
+  printf("max function id: %zu\n", __xray_max_function_id());
+  // CHECK: max function id: [[MAX:.*]]
+
+  std::set<void *> must_be_instrumented;
+  must_be_instrumented.insert(reinterpret_cast<void*>(&foo));
+  must_be_instrumented.insert(reinterpret_cast<void*>(&bar));
+  printf("addresses:\n");
+  std::set<void *> all_instrumented;
+  for (auto i = __xray_max_function_id(); i != 0; --i) {
+    auto addr = __xray_function_address(i);
+    printf("#%lu -> @%04lx\n", i, addr);
+    all_instrumented.insert(reinterpret_cast<void *>(addr));
+  }
+
+  // CHECK-LABEL: addresses:
+  // CHECK: #[[MAX]] -> @[[ADDR:.*]]
+  // CHECK-NOT: #0 -> @{{.*}}
+  std::set<void *> common;
+
+  std::set_intersection(all_instrumented.begin(), all_instrumented.end(),
+                        must_be_instrumented.begin(),
+                        must_be_instrumented.end(),
+                        std::inserter(common, common.begin()));
+  return common == must_be_instrumented ? 0 : 1;
+}




More information about the llvm-commits mailing list