16db8c59fSFangrui Song // Check that we can turn a function id to a function address, and also get the
26db8c59fSFangrui Song // maximum function id for the current binary.
36db8c59fSFangrui Song //
46db8c59fSFangrui Song // RUN: %clangxx_xray -std=c++11 %s -o %t
502cdbc34SFangrui Song // RUN: XRAY_OPTIONS="patch_premain=false" %run %t
6*7c7c8e0dSIan Levesque // RUN: %clangxx_xray -fno-xray-function-index -std=c++11 %s -o %t
7*7c7c8e0dSIan Levesque // RUN: XRAY_OPTIONS="patch_premain=false" %run %t
86db8c59fSFangrui Song 
96db8c59fSFangrui Song // UNSUPPORTED: target-is-mips64,target-is-mips64el
106db8c59fSFangrui Song 
116db8c59fSFangrui Song #include "xray/xray_interface.h"
126db8c59fSFangrui Song #include <algorithm>
136db8c59fSFangrui Song #include <cassert>
146db8c59fSFangrui Song #include <cstdio>
156db8c59fSFangrui Song #include <iterator>
166db8c59fSFangrui Song #include <set>
176db8c59fSFangrui Song 
bar()186db8c59fSFangrui Song [[clang::xray_always_instrument]] void bar(){}
196db8c59fSFangrui Song 
foo()206db8c59fSFangrui Song [[clang::xray_always_instrument]] void foo() {
216db8c59fSFangrui Song   bar();
226db8c59fSFangrui Song }
236db8c59fSFangrui Song 
main(int argc,char * argv[])246db8c59fSFangrui Song [[clang::xray_always_instrument]] int main(int argc, char *argv[]) {
256db8c59fSFangrui Song   assert(__xray_max_function_id() != 0 && "we need xray instrumentation!");
266db8c59fSFangrui Song   std::set<void *> must_be_instrumented = {reinterpret_cast<void *>(&foo),
276db8c59fSFangrui Song                                            reinterpret_cast<void *>(&bar),
286db8c59fSFangrui Song                                            reinterpret_cast<void *>(&main)};
296db8c59fSFangrui Song   std::set<void *> all_instrumented;
306db8c59fSFangrui Song   for (auto i = __xray_max_function_id(); i != 0; --i) {
316db8c59fSFangrui Song     auto addr = __xray_function_address(i);
326db8c59fSFangrui Song     all_instrumented.insert(reinterpret_cast<void *>(addr));
336db8c59fSFangrui Song   }
346db8c59fSFangrui Song   assert(all_instrumented.size() == __xray_max_function_id() &&
356db8c59fSFangrui Song          "each function id must be assigned to a unique function");
366db8c59fSFangrui Song 
376db8c59fSFangrui Song   std::set<void *> not_instrumented;
386db8c59fSFangrui Song   std::set_difference(
396db8c59fSFangrui Song       must_be_instrumented.begin(), must_be_instrumented.end(),
406db8c59fSFangrui Song       all_instrumented.begin(), all_instrumented.end(),
416db8c59fSFangrui Song       std::inserter(not_instrumented, not_instrumented.begin()));
426db8c59fSFangrui Song   assert(
436db8c59fSFangrui Song       not_instrumented.empty() &&
446db8c59fSFangrui Song       "we should see all explicitly instrumented functions with function ids");
456db8c59fSFangrui Song   return not_instrumented.empty() ? 0 : 1;
466db8c59fSFangrui Song }
47