Skip to content

Commit

Permalink
folly: Update tracing headers
Browse files Browse the repository at this point in the history
Sync to upstream folly.

Also turn all users of the headers to be C++ programs instead of C b/c
folly now #includes <cstddef>.
  • Loading branch information
danobi committed Oct 21, 2020
1 parent 28a5cef commit 8287f17
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 45 deletions.
2 changes: 1 addition & 1 deletion tests/cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tests/cc/usdt_test_lib.c → tests/cc/usdt_test_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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:https://www.apache.org/licenses/LICENSE-2.0
* http:https://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,
Expand All @@ -16,14 +16,13 @@

#pragma once

// clang-format off
#include <cstddef>

// 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
Expand All @@ -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
Expand All @@ -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)), \
Expand All @@ -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)
Expand All @@ -94,34 +94,56 @@
#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) \
FOLLY_SDT_ASM_3( .4byte 992f-991f, 994f-993f, FOLLY_SDT_NOTE_TYPE) \
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) \
FOLLY_SDT_ASM_1(994: .balign 4) \
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__))
37 changes: 27 additions & 10 deletions tests/python/include/folly/tracing/StaticTracepoint.h
Original file line number Diff line number Diff line change
@@ -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:https://www.apache.org/licenses/LICENSE-2.0
* http:https://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,
Expand All @@ -16,14 +16,31 @@

#pragma once

#if defined(__ELF__) && \
(defined(__powerpc64__) || defined(__powerpc__) || defined(__aarch64__) || \
defined(__x86_64__) || defined(__i386__))
#include <folly/tracing/StaticTracepoint-ELF.h>
#if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) && \
!FOLLY_DISABLE_SDT

#include <folly/tracing/StaticTracepoint-ELFx86.h>

#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
2 changes: 1 addition & 1 deletion tests/python/test_usdt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/python/test_usdt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions tests/python/test_usdt3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8287f17

Please sign in to comment.