From 8287f172ddfc488d76b9af8645638da605ac516f Mon Sep 17 00:00:00 2001 From: Daniel Xu Date: Mon, 19 Oct 2020 14:05:47 -0700 Subject: [PATCH] folly: Update tracing headers Sync to upstream folly. Also turn all users of the headers to be C++ programs instead of C b/c folly now #includes . --- tests/cc/CMakeLists.txt | 2 +- .../cc/{usdt_test_lib.c => usdt_test_lib.cc} | 4 + ...epoint-ELF.h => StaticTracepoint-ELFx86.h} | 74 ++++++++++++------- .../include/folly/tracing/StaticTracepoint.h | 37 +++++++--- tests/python/test_usdt.py | 2 +- tests/python/test_usdt2.py | 2 +- tests/python/test_usdt3.py | 12 +-- 7 files changed, 88 insertions(+), 45 deletions(-) rename tests/cc/{usdt_test_lib.c => usdt_test_lib.cc} (92%) rename tests/python/include/folly/tracing/{StaticTracepoint-ELF.h => StaticTracepoint-ELFx86.h} (65%) diff --git a/tests/cc/CMakeLists.txt b/tests/cc/CMakeLists.txt index 1f1334431ba1..528f1bda944a 100644 --- a/tests/cc/CMakeLists.txt +++ b/tests/cc/CMakeLists.txt @@ -38,7 +38,7 @@ set(TEST_LIBBCC_SOURCES add_executable(test_libbcc ${TEST_LIBBCC_SOURCES}) file(COPY dummy_proc_map.txt DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) -add_library(usdt_test_lib SHARED usdt_test_lib.c) +add_library(usdt_test_lib SHARED usdt_test_lib.cc) add_dependencies(test_libbcc bcc-shared) target_link_libraries(test_libbcc ${PROJECT_BINARY_DIR}/src/cc/libbcc.so dl usdt_test_lib) diff --git a/tests/cc/usdt_test_lib.c b/tests/cc/usdt_test_lib.cc similarity index 92% rename from tests/cc/usdt_test_lib.c rename to tests/cc/usdt_test_lib.cc index 799ad9b78231..38800c253be6 100644 --- a/tests/cc/usdt_test_lib.c +++ b/tests/cc/usdt_test_lib.cc @@ -3,8 +3,12 @@ #include "folly/tracing/StaticTracepoint.h" +extern "C" { + int lib_probed_function() { int an_int = 42 + getpid(); FOLLY_SDT(libbcc_test, sample_lib_probe_1, an_int); return an_int; } + +} diff --git a/tests/python/include/folly/tracing/StaticTracepoint-ELF.h b/tests/python/include/folly/tracing/StaticTracepoint-ELFx86.h similarity index 65% rename from tests/python/include/folly/tracing/StaticTracepoint-ELF.h rename to tests/python/include/folly/tracing/StaticTracepoint-ELFx86.h index 4deb18a7166f..033809cb3cb7 100644 --- a/tests/python/include/folly/tracing/StaticTracepoint-ELF.h +++ b/tests/python/include/folly/tracing/StaticTracepoint-ELFx86.h @@ -1,11 +1,11 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright (c) Facebook, Inc. and its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,14 +16,13 @@ #pragma once +// clang-format off +#include + // Default constraint for the probe arguments as operands. #ifndef FOLLY_SDT_ARG_CONSTRAINT -#if defined(__powerpc64__) || defined(__powerpc__) -#define FOLLY_SDT_ARG_CONSTRAINT "nZr" -#else #define FOLLY_SDT_ARG_CONSTRAINT "nor" #endif -#endif // Instruction to emit for the probe. #define FOLLY_SDT_NOP nop @@ -32,6 +31,9 @@ #define FOLLY_SDT_NOTE_NAME "stapsdt" #define FOLLY_SDT_NOTE_TYPE 3 +// Semaphore variables are put in this section +#define FOLLY_SDT_SEMAPHORE_SECTION ".probes" + // Size of address depending on platform. #ifdef __LP64__ #define FOLLY_SDT_ASM_ADDR .8byte @@ -48,11 +50,14 @@ #define FOLLY_SDT_ASM_STRING(x) FOLLY_SDT_ASM_1(.asciz FOLLY_SDT_S(x)) // Helper to determine the size of an argument. -#define FOLLY_SDT_ISARRAY(x) (__builtin_classify_type(x) == 14) -#define FOLLY_SDT_ARGSIZE(x) (FOLLY_SDT_ISARRAY(x) ? sizeof(void*) : sizeof(x)) +#define FOLLY_SDT_IS_ARRAY_POINTER(x) ((__builtin_classify_type(x) == 14) || \ + (__builtin_classify_type(x) == 5)) +#define FOLLY_SDT_ARGSIZE(x) (FOLLY_SDT_IS_ARRAY_POINTER(x) \ + ? sizeof(void*) \ + : sizeof(x)) // Format of each probe arguments as operand. -// Size of the argument tagged with FOLLY_SDT_Sn, with "n" constraint. +// Size of the arugment tagged with FOLLY_SDT_Sn, with "n" constraint. // Value of the argument tagged with FOLLY_SDT_An, with configured constraint. #define FOLLY_SDT_ARG(n, x) \ [FOLLY_SDT_S##n] "n" ((size_t)FOLLY_SDT_ARGSIZE(x)), \ @@ -75,16 +80,11 @@ FOLLY_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6), FOLLY_SDT_ARG(7, _7) #define FOLLY_SDT_OPERANDS_8(_1, _2, _3, _4, _5, _6, _7, _8) \ FOLLY_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7), FOLLY_SDT_ARG(8, _8) +#define FOLLY_SDT_OPERANDS_9(_1, _2, _3, _4, _5, _6, _7, _8, _9) \ + FOLLY_SDT_OPERANDS_8(_1, _2, _3, _4, _5, _6, _7, _8), FOLLY_SDT_ARG(9, _9) // Templates to reference the arguments from operands in note section. -#if defined(__powerpc64__ ) || defined(__powerpc__) -#define FOLLY_SDT_ARGTMPL(id) %I[id]%[id] -#elif defined(__i386__) -#define FOLLY_SDT_ARGTMPL(id) %w[id] -#else -#define FOLLY_SDT_ARGTMPL(id) %[id] -#endif -#define FOLLY_SDT_ARGFMT(no) %n[FOLLY_SDT_S##no]@FOLLY_SDT_ARGTMPL(FOLLY_SDT_A##no) +#define FOLLY_SDT_ARGFMT(no) %n[FOLLY_SDT_S##no]@%[FOLLY_SDT_A##no] #define FOLLY_SDT_ARG_TEMPLATE_0 /*No arguments*/ #define FOLLY_SDT_ARG_TEMPLATE_1 FOLLY_SDT_ARGFMT(1) #define FOLLY_SDT_ARG_TEMPLATE_2 FOLLY_SDT_ARG_TEMPLATE_1 FOLLY_SDT_ARGFMT(2) @@ -94,9 +94,30 @@ #define FOLLY_SDT_ARG_TEMPLATE_6 FOLLY_SDT_ARG_TEMPLATE_5 FOLLY_SDT_ARGFMT(6) #define FOLLY_SDT_ARG_TEMPLATE_7 FOLLY_SDT_ARG_TEMPLATE_6 FOLLY_SDT_ARGFMT(7) #define FOLLY_SDT_ARG_TEMPLATE_8 FOLLY_SDT_ARG_TEMPLATE_7 FOLLY_SDT_ARGFMT(8) +#define FOLLY_SDT_ARG_TEMPLATE_9 FOLLY_SDT_ARG_TEMPLATE_8 FOLLY_SDT_ARGFMT(9) + +// Semaphore define, declare and probe note format + +#define FOLLY_SDT_SEMAPHORE(provider, name) \ + folly_sdt_semaphore_##provider##_##name + +#define FOLLY_SDT_DEFINE_SEMAPHORE(provider, name) \ + extern "C" { \ + volatile unsigned short FOLLY_SDT_SEMAPHORE(provider, name) \ + __attribute__((section(FOLLY_SDT_SEMAPHORE_SECTION), used)) = 0; \ + } + +#define FOLLY_SDT_DECLARE_SEMAPHORE(provider, name) \ + extern "C" volatile unsigned short FOLLY_SDT_SEMAPHORE(provider, name) + +#define FOLLY_SDT_SEMAPHORE_NOTE_0(provider, name) \ + FOLLY_SDT_ASM_1( FOLLY_SDT_ASM_ADDR 0) /*No Semaphore*/ \ + +#define FOLLY_SDT_SEMAPHORE_NOTE_1(provider, name) \ + FOLLY_SDT_ASM_1(FOLLY_SDT_ASM_ADDR FOLLY_SDT_SEMAPHORE(provider, name)) // Structure of note section for the probe. -#define FOLLY_SDT_NOTE_CONTENT(provider, name, arg_template) \ +#define FOLLY_SDT_NOTE_CONTENT(provider, name, has_semaphore, arg_template) \ FOLLY_SDT_ASM_1(990: FOLLY_SDT_NOP) \ FOLLY_SDT_ASM_3( .pushsection .note.stapsdt,"","note") \ FOLLY_SDT_ASM_1( .balign 4) \ @@ -104,8 +125,8 @@ FOLLY_SDT_ASM_1(991: .asciz FOLLY_SDT_NOTE_NAME) \ FOLLY_SDT_ASM_1(992: .balign 4) \ FOLLY_SDT_ASM_1(993: FOLLY_SDT_ASM_ADDR 990b) \ - FOLLY_SDT_ASM_1( FOLLY_SDT_ASM_ADDR 0) /*Reserved for Semaphore address*/\ - FOLLY_SDT_ASM_1( FOLLY_SDT_ASM_ADDR 0) /*Reserved for Semaphore name*/ \ + FOLLY_SDT_ASM_1( FOLLY_SDT_ASM_ADDR 0) /*Reserved for Base Address*/ \ + FOLLY_SDT_SEMAPHORE_NOTE_##has_semaphore(provider, name) \ FOLLY_SDT_ASM_STRING(provider) \ FOLLY_SDT_ASM_STRING(name) \ FOLLY_SDT_ASM_STRING(arg_template) \ @@ -113,15 +134,16 @@ FOLLY_SDT_ASM_1( .popsection) // Main probe Macro. -#define FOLLY_SDT_PROBE(provider, name, n, arglist) \ +#define FOLLY_SDT_PROBE(provider, name, has_semaphore, n, arglist) \ __asm__ __volatile__ ( \ - FOLLY_SDT_NOTE_CONTENT(provider, name, FOLLY_SDT_ARG_TEMPLATE_##n) \ + FOLLY_SDT_NOTE_CONTENT( \ + provider, name, has_semaphore, FOLLY_SDT_ARG_TEMPLATE_##n) \ :: FOLLY_SDT_OPERANDS_##n arglist \ ) \ // Helper Macros to handle variadic arguments. -#define FOLLY_SDT_NARG_(_0, _1, _2, _3, _4, _5, _6, _7, _8, N, ...) N +#define FOLLY_SDT_NARG_(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N #define FOLLY_SDT_NARG(...) \ - FOLLY_SDT_NARG_(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0) -#define FOLLY_SDT_PROBE_N(provider, name, N, ...) \ - FOLLY_SDT_PROBE(provider, name, N, (__VA_ARGS__)) + FOLLY_SDT_NARG_(__VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) +#define FOLLY_SDT_PROBE_N(provider, name, has_semaphore, N, ...) \ + FOLLY_SDT_PROBE(provider, name, has_semaphore, N, (__VA_ARGS__)) diff --git a/tests/python/include/folly/tracing/StaticTracepoint.h b/tests/python/include/folly/tracing/StaticTracepoint.h index 37e271c9f341..858b7dbc0cee 100644 --- a/tests/python/include/folly/tracing/StaticTracepoint.h +++ b/tests/python/include/folly/tracing/StaticTracepoint.h @@ -1,11 +1,11 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright (c) Facebook, Inc. and its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -16,14 +16,31 @@ #pragma once -#if defined(__ELF__) && \ - (defined(__powerpc64__) || defined(__powerpc__) || defined(__aarch64__) || \ - defined(__x86_64__) || defined(__i386__)) -#include +#if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) && \ + !FOLLY_DISABLE_SDT + +#include + +#define FOLLY_SDT(provider, name, ...) \ + FOLLY_SDT_PROBE_N( \ + provider, name, 0, FOLLY_SDT_NARG(0, ##__VA_ARGS__), ##__VA_ARGS__) +// Use FOLLY_SDT_DEFINE_SEMAPHORE(provider, name) to define the semaphore +// as global variable before using the FOLLY_SDT_WITH_SEMAPHORE macro +#define FOLLY_SDT_WITH_SEMAPHORE(provider, name, ...) \ + FOLLY_SDT_PROBE_N( \ + provider, name, 1, FOLLY_SDT_NARG(0, ##__VA_ARGS__), ##__VA_ARGS__) +#define FOLLY_SDT_IS_ENABLED(provider, name) \ + (FOLLY_SDT_SEMAPHORE(provider, name) > 0) -#define FOLLY_SDT(provider, name, ...) \ - FOLLY_SDT_PROBE_N( \ - provider, name, FOLLY_SDT_NARG(0, ##__VA_ARGS__), ##__VA_ARGS__) #else -#define FOLLY_SDT(provider, name, ...) do {} while(0) + +#define FOLLY_SDT(provider, name, ...) \ + do { \ + } while (0) +#define FOLLY_SDT_WITH_SEMAPHORE(provider, name, ...) \ + do { \ + } while (0) +#define FOLLY_SDT_IS_ENABLED(provider, name) (false) +#define FOLLY_SDT_DEFINE_SEMAPHORE(provider, name) +#define FOLLY_SDT_DECLARE_SEMAPHORE(provider, name) #endif diff --git a/tests/python/test_usdt.py b/tests/python/test_usdt.py index 27a0e47835de..d84ccc99afe1 100755 --- a/tests/python/test_usdt.py +++ b/tests/python/test_usdt.py @@ -132,7 +132,7 @@ def setUp(self): self.ftemp = NamedTemporaryFile(delete=False) self.ftemp.close() comp = Popen(["gcc", "-I", "%s/include" % os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))), - "-x", "c", "-o", self.ftemp.name, "-"], + "-x", "c++", "-o", self.ftemp.name, "-"], stdin=PIPE) comp.stdin.write(app_text) comp.stdin.close() diff --git a/tests/python/test_usdt2.py b/tests/python/test_usdt2.py index c8c47fe6bbc6..c2b2daeb8f24 100755 --- a/tests/python/test_usdt2.py +++ b/tests/python/test_usdt2.py @@ -81,7 +81,7 @@ def setUp(self): self.ftemp = NamedTemporaryFile(delete=False) self.ftemp.close() comp = Popen(["gcc", "-I", "%s/include" % os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))), - "-x", "c", "-o", self.ftemp.name, "-"], + "-x", "c++", "-o", self.ftemp.name, "-"], stdin=PIPE) comp.stdin.write(app_text) comp.stdin.close() diff --git a/tests/python/test_usdt3.py b/tests/python/test_usdt3.py index 61c1b54ca2c0..5fe2ef4f18c3 100755 --- a/tests/python/test_usdt3.py +++ b/tests/python/test_usdt3.py @@ -82,20 +82,20 @@ def _create_file(name, text): self.tmp_dir = tempfile.mkdtemp() print("temp directory: " + self.tmp_dir) _create_file(self.tmp_dir + "/common.h", common_h) - _create_file(self.tmp_dir + "/a.c", a_c) - _create_file(self.tmp_dir + "/b.c", b_c) - _create_file(self.tmp_dir + "/m.c", m_c) + _create_file(self.tmp_dir + "/a.cpp", a_c) + _create_file(self.tmp_dir + "/b.cpp", b_c) + _create_file(self.tmp_dir + "/m.cpp", m_c) # Compilation # the usdt test:probe exists in liba.so, libb.so and a.out include_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + "/include" - a_src = self.tmp_dir + "/a.c" + a_src = self.tmp_dir + "/a.cpp" a_obj = self.tmp_dir + "/a.o" a_lib = self.tmp_dir + "/liba.so" - b_src = self.tmp_dir + "/b.c" + b_src = self.tmp_dir + "/b.cpp" b_obj = self.tmp_dir + "/b.o" b_lib = self.tmp_dir + "/libb.so" - m_src = self.tmp_dir + "/m.c" + m_src = self.tmp_dir + "/m.cpp" m_bin = self.tmp_dir + "/a.out" m_linker_opt = " -L" + self.tmp_dir + " -la -lb" self.assertEqual(os.system("gcc -I" + include_path + " -fpic -c -o " + a_obj + " " + a_src), 0)