Skip to content

Commit

Permalink
[xtrace] Convert All Source Files To CPP
Browse files Browse the repository at this point in the history
* Changed all sources files from `c` to `cpp`.
* Updated header files to include `extern "C"`.
* Added a few more C-styled casts.
* Fail build if hook struct is not implemented for arch.
* Misc formatting.
  • Loading branch information
CuriousTommy committed Aug 29, 2023
1 parent a12ec5d commit 38f24ca
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 77 deletions.
12 changes: 6 additions & 6 deletions src/xtrace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ include_directories(
)

set(xtrace_sources
xtracelib.c
xtracelib.cpp
trampoline.S
mach_trace.cpp
bsd_trace.cpp
mig_trace.c
tls.c
malloc.c
lock.c
posix_spawn_args.c
mig_trace.cpp
tls.cpp
malloc.cpp
lock.cpp
posix_spawn_args.cpp
)

if (TARGET_x86_64)
Expand Down
9 changes: 5 additions & 4 deletions src/xtrace/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#define XTRACE_INLINE static __attribute__((always_inline))

#ifdef __cplusplus
#define XTRACE_DECLARATIONS_BEGIN extern "C" {
#define XTRACE_DECLARATIONS_C_BEGIN extern "C" {
#define XTRACE_CPP __cplusplus
#else
#define XTRACE_DECLARATIONS_BEGIN
#define XTRACE_DECLARATIONS_C_BEGIN
#endif

#ifdef __cplusplus
#define XTRACE_DECLARATIONS_END }
#define XTRACE_DECLARATIONS_C_END }
#else
#define XTRACE_DECLARATIONS_END
#define XTRACE_DECLARATIONS_C_END
#endif

#endif // _XTRACE_BASE_H_
14 changes: 8 additions & 6 deletions src/xtrace/bsd_trace.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifdef __cplusplus
extern "C" {
#endif
#ifndef XTRACE_BSD_TRACE
#define XTRACE_BSD_TRACE

#include "base.h"

XTRACE_DECLARATIONS_C_BEGIN

extern void xtrace_print_string_literal(const char* str);

#ifdef __cplusplus
}
#endif
XTRACE_DECLARATIONS_C_END

#endif // XTRACE_BSD_TRACE
11 changes: 11 additions & 0 deletions src/xtrace/include/xtrace/xtrace-mig-types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#ifndef XTRACE_XTRACE_MIG_TYPES
#define XTRACE_XTRACE_MIG_TYPES

#include <mach/message.h>

#include "../base.h"

XTRACE_DECLARATIONS_C_BEGIN

struct xtrace_mig_callbacks {
void (*add_raw_arg)(const char *format, ...) __attribute__((format(printf, 1, 2)));
void (*add_num_arg)(unsigned long long n);
Expand Down Expand Up @@ -35,3 +42,7 @@ struct xtrace_mig_subsystem {
unsigned long routine_cnt;
const struct xtrace_mig_routine_desc *routines;
};

XTRACE_DECLARATIONS_C_END

#endif // XTRACE_XTRACE_MIG_TYPES
11 changes: 7 additions & 4 deletions src/xtrace/lock.c → src/xtrace/lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ uint32_t cmpxchg_wrapper_u32(_Atomic uint32_t* atom, uint32_t expected, uint32_t
return expected;
};

extern "C"
void xtrace_lock_lock(xtrace_lock_t* _lock) {
xtrace_lock_internal_t* lock = (xtrace_lock_internal_t*)_lock;

Expand All @@ -70,7 +71,7 @@ void xtrace_lock_lock(xtrace_lock_t* _lock) {
xtrace_lock_debug("going to wait");
// do the actual sleeping
// we expect `xtrace_lock_state_locked_contended` because we don't want to sleep if it's not contended
__linux_futex_reterr(&lock->state, FUTEX_WAIT, xtrace_lock_state_locked_contended, NULL, 0, 0);
__linux_futex_reterr((int*)&lock->state, FUTEX_WAIT, xtrace_lock_state_locked_contended, nullptr, 0, 0);
xtrace_lock_debug("awoken");
}

Expand All @@ -83,6 +84,7 @@ void xtrace_lock_lock(xtrace_lock_t* _lock) {
xtrace_lock_debug("lock acquired");
};

extern "C"
void xtrace_lock_unlock(xtrace_lock_t* _lock) {
xtrace_lock_internal_t* lock = (xtrace_lock_internal_t*)_lock;

Expand All @@ -95,7 +97,7 @@ void xtrace_lock_unlock(xtrace_lock_t* _lock) {
// if it was previously contended, then we need to wake someone up
if (prev == xtrace_lock_state_locked_contended) {
xtrace_lock_debug("waking someone up");
__linux_futex_reterr(&lock->state, FUTEX_WAKE, 1, NULL, 0, 0);
__linux_futex_reterr((int*)&lock->state, FUTEX_WAKE, 1, NULL, 0, 0);
}
};

Expand Down Expand Up @@ -123,6 +125,7 @@ enum xtrace_once_state {
xtrace_once_state_completed = 3,
};

extern "C"
void xtrace_once(xtrace_once_t* _once, xtrace_once_callback callback) {
xtrace_once_internal_t* once = (xtrace_once_internal_t*)_once;

Expand Down Expand Up @@ -152,7 +155,7 @@ void xtrace_once(xtrace_once_t* _once, xtrace_once_callback callback) {
case xtrace_once_state_triggered_contended: {
xtrace_once_debug("waking up all waiters...");
// otherwise, we have to wake someone up
__linux_futex_reterr(&once->state, FUTEX_WAKE, INT_MAX, NULL, 0, 0);
__linux_futex_reterr((int*)&once->state, FUTEX_WAKE, INT_MAX, NULL, 0, 0);
} break;
}

Expand Down Expand Up @@ -184,7 +187,7 @@ void xtrace_once(xtrace_once_t* _once, xtrace_once_callback callback) {
// somebody is already performing the callback and there are already waiters;
// let's wait
xtrace_once_debug("someone is already performing the callback with waiters; going to wait...");
__linux_futex_reterr(&once->state, FUTEX_WAIT, prev, NULL, 0, 0);
__linux_futex_reterr((int*)&once->state, FUTEX_WAIT, prev, NULL, 0, 0);

xtrace_once_debug("woken up");

Expand Down
4 changes: 2 additions & 2 deletions src/xtrace/lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// we also have to implement our own locks, because we can't rely on libplatform's or libpthread's locks
// so we implement our own on top of Linux futexes

XTRACE_DECLARATIONS_BEGIN;
XTRACE_DECLARATIONS_C_BEGIN

//
// lock
Expand Down Expand Up @@ -48,6 +48,6 @@ void xtrace_once_init(xtrace_once_t* once) {

void xtrace_once(xtrace_once_t* once, xtrace_once_callback callback);

XTRACE_DECLARATIONS_END;
XTRACE_DECLARATIONS_C_END

#endif // _XTRACE_LOCK_H_
14 changes: 8 additions & 6 deletions src/xtrace/mach_trace.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifdef __cplusplus
extern "C" {
#endif
#ifndef XTRACE_MACH_TRACE
#define XTRACE_MACH_TRACE

#include "base.h"

XTRACE_DECLARATIONS_C_BEGIN

const char* xtrace_msg_type_to_str(mach_msg_type_name_t type_name, int full);
void xtrace_print_kern_return(kern_return_t kr);

#ifdef __cplusplus
}
#endif
XTRACE_DECLARATIONS_C_END

#endif // XTRACE_MACH_TRACE
9 changes: 6 additions & 3 deletions src/xtrace/malloc.c → src/xtrace/malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static xtrace_memory_fragment_t allocate_fragment(size_t required_size) {
if (viable_fragment == NULL) {
// ...allocate some memory
size_t allocation_size = round_up(required_size, BLOCK_SIZE_MULTIPLE);
xtrace_memory_block_t block = _mmap_for_xtrace(NULL, allocation_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
xtrace_memory_block_t block = (xtrace_memory_block_t)_mmap_for_xtrace(NULL, allocation_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);

xtrace_malloc_debug("mmap for %llu bytes returned %p", allocation_size, block);

Expand Down Expand Up @@ -414,6 +414,7 @@ static bool expand_fragment(xtrace_memory_fragment_t fragment, size_t new_size)
// api functions
//

extern "C"
void* xtrace_malloc(size_t size) {
if (size == 0) {
return NULL;
Expand All @@ -425,16 +426,18 @@ void* xtrace_malloc(size_t size) {
return fragment->memory;
};

extern "C"
void xtrace_free(void* pointer) {
if (pointer == NULL) {
return;
}
xtrace_memory_fragment_t fragment = __container_of(pointer, struct xtrace_memory_fragment, memory);
xtrace_memory_fragment_t fragment = __container_of((const char (*)[])pointer, struct xtrace_memory_fragment, memory);
release_fragment(fragment);
};

extern "C"
void* xtrace_realloc(void* old_pointer, size_t new_size) {
xtrace_memory_fragment_t fragment = __container_of(old_pointer, struct xtrace_memory_fragment, memory);
xtrace_memory_fragment_t fragment = __container_of((const char (*)[])old_pointer, struct xtrace_memory_fragment, memory);
size_t old_size = xtrace_memory_fragment_size(fragment);

xtrace_lock_lock(&free_lock);
Expand Down
4 changes: 2 additions & 2 deletions src/xtrace/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include <stddef.h>
#include "base.h"

XTRACE_DECLARATIONS_BEGIN;
XTRACE_DECLARATIONS_C_BEGIN

void* xtrace_malloc(size_t size);
void xtrace_free(void* pointer);
void* xtrace_realloc(void* old_pointer, size_t new_size);

XTRACE_DECLARATIONS_END;
XTRACE_DECLARATIONS_C_END

#endif // _XTRACE_MALLOC_H_
4 changes: 3 additions & 1 deletion src/xtrace/mig_trace.c → src/xtrace/mig_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static struct xtrace_mig_subsystem** subsystems = NULL;

static mach_port_name_t host_port;

extern "C"
void xtrace_setup_mig_tracing(void)
{
// This runs before the syscall tracing is enabled, so we can
Expand All @@ -37,7 +38,7 @@ void xtrace_setup_mig_tracing(void)
}
// Count the number of files, and allocate this many subsystem pointers;
for (struct dirent* dirent; (dirent = readdir(xtrace_mig_dir)) != NULL; subsystems_cnt++);
subsystems = malloc(subsystems_cnt * sizeof(struct xtrace_mig_subsystem*));
subsystems = (xtrace_mig_subsystem**)malloc(subsystems_cnt * sizeof(struct xtrace_mig_subsystem*));

rewinddir(xtrace_mig_dir);
for (size_t i = 0; i < subsystems_cnt; i++)
Expand Down Expand Up @@ -322,6 +323,7 @@ static int find_subsystem(
return 0;
}

extern "C"
void xtrace_print_mig_message(const mach_msg_header_t* message, mach_port_name_t request_port)
{
if (message == NULL)
Expand Down
14 changes: 8 additions & 6 deletions src/xtrace/mig_trace.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#ifndef XTRACE_MIG_TRACE
#define XTRACE_MIG_TRACE

#include <mach/message.h>
#include "base.h"

#ifdef __cplusplus
extern "C" {
#endif
XTRACE_DECLARATIONS_C_BEGIN

void xtrace_setup_mig_tracing(void);
void xtrace_print_mig_message(const mach_msg_header_t* message, mach_port_name_t request_port);

#ifdef __cplusplus
}
#endif
XTRACE_DECLARATIONS_C_END

#endif // XTRACE_MIG_TRACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "xtracelib.h"

extern void print_open_flags(void* arg);
extern "C" void print_open_flags(void* arg);

static struct {
unsigned short flag;
Expand All @@ -35,6 +35,7 @@ static struct {
#undef POSIX_SPAWN_ATTR_ENTRY
};

extern "C"
void print_arg_posix_spawn_args(void* arg) {
const struct _posix_spawn_args_desc* args = (const struct _posix_spawn_args_desc*)(arg);
bool is_first = true;
Expand Down
10 changes: 6 additions & 4 deletions src/xtrace/tls.c → src/xtrace/tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ struct tls_table {

// TODO: also perform TLS cleanup for other threads when doing a fork

extern "C"
void xtrace_tls_thread_cleanup(void) {
tls_table_t table = _pthread_getspecific_direct(__PTK_XTRACE_TLS);
tls_table_t table = (tls_table_t)_pthread_getspecific_direct(__PTK_XTRACE_TLS);
if (!table) {
xtrace_tls_debug("no table to cleanup for this thread");
return;
Expand All @@ -53,17 +54,18 @@ void xtrace_tls_thread_cleanup(void) {
xtrace_free(table);
};

extern "C"
void* xtrace_tls(void* key, size_t size, bool* created, xtrace_tls_destructor_f destructor) {
xtrace_tls_debug("looking up tls variable for key %p", key);

tls_table_t table = _pthread_getspecific_direct(__PTK_XTRACE_TLS);
tls_table_t table = (tls_table_t)_pthread_getspecific_direct(__PTK_XTRACE_TLS);

xtrace_tls_debug("got %p as table pointer from pthread", table);

// if the table doesn't exist yet, create it
if (table == NULL) {
xtrace_tls_debug("table is NULL, creating now...");
table = xtrace_malloc(sizeof(struct tls_table));
table = (tls_table_t)xtrace_malloc(sizeof(struct tls_table));
if (table == NULL) {
xtrace_abort("xtrace: failed TLS table memory allocation");
}
Expand All @@ -90,7 +92,7 @@ void* xtrace_tls(void* key, size_t size, bool* created, xtrace_tls_destructor_f
}
table->table[index][0] = key;
table->table[index][1] = xtrace_malloc(size);
table->table[index][2] = destructor;
table->table[index][2] = (void*)destructor;
if (table->table[index][1] == NULL) {
xtrace_abort("xtrace: failed TLS variable memory allocation");
}
Expand Down
4 changes: 2 additions & 2 deletions src/xtrace/tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <mach/port.h>
#include "base.h"

XTRACE_DECLARATIONS_BEGIN;
XTRACE_DECLARATIONS_C_BEGIN

typedef void (*xtrace_tls_destructor_f)(void* value);

Expand Down Expand Up @@ -47,6 +47,6 @@ typedef void (*xtrace_tls_destructor_f)(void* value);
void* xtrace_tls(void* key, size_t size, bool* created, xtrace_tls_destructor_f destructor);
void xtrace_tls_thread_cleanup(void);

XTRACE_DECLARATIONS_END;
XTRACE_DECLARATIONS_C_END

#endif // _XTRACE_TLS_H_
Loading

0 comments on commit 38f24ca

Please sign in to comment.