Skip to content

Commit

Permalink
Merge pull request #196 from jnpkrn/demystify-qblog.h
Browse files Browse the repository at this point in the history
Low: explain mysterious lines in a public header (qblog.h)
  • Loading branch information
chrissie-c committed Apr 1, 2016
2 parents 6677fe6 + f5ed676 commit 67af307
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
6 changes: 4 additions & 2 deletions configure.ac
Expand Up @@ -549,10 +549,12 @@ AC_SUBST(HAVE_SYSLOG_TESTS)

# --- callsite sections ---
if test "x${GCC}" = xyes; then
AC_MSG_CHECKING([whether GCC supports __attribute__((section())])
AC_MSG_CHECKING([whether GCC supports __attribute__((section()) + ld supports orphan sections])
if test "x${ac_cv_link_attribute_section}" = x ; then
AC_TRY_LINK([],
AC_TRY_LINK([#include <assert.h>
extern void * __start___verbose, * __stop___verbose;],
[static int my_var __attribute__((section("__verbose"))) = 5;
if (__start___verbose == __stop___verbose) assert(0);
if (my_var == 5) return 0;
else return -1;
],
Expand Down
9 changes: 9 additions & 0 deletions include/qb/qbdefs.h
Expand Up @@ -56,6 +56,15 @@ extern "C" {
#define qb_bit_is_set(barray, bit) (barray & qb_bit_value(bit))
#define qb_bit_is_clear(barray, bit) (!(barray & qb_bit_value(bit)))

/*
* wrappers over preprocessor operators
*/

#define QB_PP_JOIN_(a, b) a##b
#define QB_PP_JOIN(a, b) QB_PP_JOIN_(a, b)
#define QB_PP_STRINGIFY_(arg) #arg
#define QB_PP_STRINGIFY(arg) QB_PP_STRINGIFY_(arg)


/*
* handy time based converters.
Expand Down
30 changes: 21 additions & 9 deletions include/qb/qblog.h
Expand Up @@ -253,14 +253,26 @@ struct qb_log_callsite {

typedef void (*qb_log_filter_fn)(struct qb_log_callsite * cs);

/* will be assigned by ld linker magic */
/* will be assigned by linker magic (assuming linker supports that):
* https://sourceware.org/binutils/docs/ld/Orphan-Sections.html
*/
#ifdef QB_HAVE_ATTRIBUTE_SECTION
extern struct qb_log_callsite __start___verbose[];
extern struct qb_log_callsite __stop___verbose[];

#define QB_LOG_INIT_DATA(name) \
void name(void); \
void name(void) { if (__start___verbose != __stop___verbose) {assert(1);} } \
#define QB_ATTR_SECTION __verbose /* conforms to C ident. */
#define QB_ATTR_SECTION_STR QB_PP_STRINGIFY(QB_ATTR_SECTION)
#define QB_ATTR_SECTION_START QB_PP_JOIN(__start_, QB_ATTR_SECTION)
#define QB_ATTR_SECTION_STOP QB_PP_JOIN(__stop_, QB_ATTR_SECTION)
#define QB_ATTR_SECTION_START_STR QB_PP_STRINGIFY(QB_ATTR_SECTION_START)
#define QB_ATTR_SECTION_STOP_STR QB_PP_STRINGIFY(QB_ATTR_SECTION_STOP)

extern struct qb_log_callsite QB_ATTR_SECTION_START[];
extern struct qb_log_callsite QB_ATTR_SECTION_STOP[];

/* mere linker sanity check, possible future extension for internal purposes */
#define QB_LOG_INIT_DATA(name) \
void name(void); \
void name(void) \
{ if (QB_ATTR_SECTION_START == QB_ATTR_SECTION_STOP) assert(0); } \
void __attribute__ ((constructor)) name(void);
#else
#define QB_LOG_INIT_DATA(name)
Expand Down Expand Up @@ -345,7 +357,7 @@ void qb_log_from_external_source_va(const char *function,
#ifdef QB_HAVE_ATTRIBUTE_SECTION
#define qb_logt(priority, tags, fmt, args...) do { \
static struct qb_log_callsite descriptor \
__attribute__((section("__verbose"), aligned(8))) = \
__attribute__((section(QB_ATTR_SECTION_STR), aligned(8))) = \
{ __func__, __FILE__, fmt, priority, __LINE__, 0, tags }; \
qb_log_real_(&descriptor, ##args); \
} while(0)
Expand Down Expand Up @@ -506,8 +518,8 @@ void qb_log_fini(void);
* you will need to do the following to get the filters to work
* in that module:
* @code
* _start = dlsym (dl_handle, "__start___verbose");
* _stop = dlsym (dl_handle, "__stop___verbose");
* _start = dlsym (dl_handle, QB_ATTR_SECTION_START_STR);
* _stop = dlsym (dl_handle, QB_ATTR_SECTION_STOP_STR);
* qb_log_callsites_register(_start, _stop);
* @endcode
*/
Expand Down
6 changes: 3 additions & 3 deletions lib/log.c
Expand Up @@ -800,13 +800,13 @@ _log_so_walk_dlnames(void)
goto done;
}

start = dlsym(handle, "__start___verbose");
start = dlsym(handle, QB_ATTR_SECTION_START_STR);
error = dlerror();
if (error) {
goto done;
}

stop = dlsym(handle, "__stop___verbose");
stop = dlsym(handle, QB_ATTR_SECTION_STOP_STR);
error = dlerror();
if (error) {
goto done;
Expand Down Expand Up @@ -868,7 +868,7 @@ qb_log_init(const char *name, int32_t facility, uint8_t priority)

qb_log_dcs_init();
#ifdef QB_HAVE_ATTRIBUTE_SECTION
qb_log_callsites_register(__start___verbose, __stop___verbose);
qb_log_callsites_register(QB_ATTR_SECTION_START, QB_ATTR_SECTION_STOP);
dl_iterate_phdr(_log_so_walk_callback, NULL);
_log_so_walk_dlnames();
#endif /* QB_HAVE_ATTRIBUTE_SECTION */
Expand Down

0 comments on commit 67af307

Please sign in to comment.