Skip to content

Commit

Permalink
fix for macOS Monterey - use DYLD_INTERPOSE macro
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Jan 16, 2022
1 parent 8a99b07 commit 47fa405
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Supported Platforms
Stackusage is primarily developed and tested on Linux, but basic
functionality should work in macOS / OS X as well. Current version has been
tested on:
- macOS Big Sur 11.0
- macOS Monterey 12.1
- Ubuntu 20.04 LTS

Limitation: On macOS / OS X this tool relies on code injection using
Expand Down
2 changes: 1 addition & 1 deletion make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fi

# tests
if [[ "${TESTS}" == "1" ]]; then
cd build && ctest -C unit --output-on-failure && ctest -C perf --verbose && cd .. || exiterr "tests failed, exiting."
cd build && ctest --output-on-failure && cd .. || exiterr "tests failed, exiting."
fi

# doc
Expand Down
8 changes: 3 additions & 5 deletions src/stackusage
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Copyright (C) 2015-2021 Kristofer Berggren
# Copyright (C) 2015-2022 Kristofer Berggren
# All rights reserved.
#
# stackusage is distributed under the BSD 3-Clause license, see LICENSE for details.
Expand Down Expand Up @@ -43,9 +43,9 @@ showusage()

showversion()
{
echo "stackusage v1.15"
echo "stackusage v1.16"
echo ""
echo "Copyright (C) 2015-2021 Kristofer Berggren"
echo "Copyright (C) 2015-2022 Kristofer Berggren"
echo ""
echo "stackusage is distributed under the BSD 3-Clause license."
echo ""
Expand Down Expand Up @@ -182,15 +182,13 @@ while [ "${LIBPATHS[CNT]}" != "" ]; do
SU_SIGNO="${SIGNO}" \
LD_PRELOAD="${LIBPATH}" \
DYLD_INSERT_LIBRARIES="${LIBPATH}" \
DYLD_FORCE_FLAT_NAMESPACE=1 \
"${@:1}"
else
LLDBCMDPATH="${TMP}/lldb.cmd"
echo "env SU_FILE=\"${TMPLOG}${OUTFILE}\"" > "${LLDBCMDPATH}"
echo "env SU_SIGNO=\"${SIGNO}\"" >> "${LLDBCMDPATH}"
echo "env LD_PRELOAD=\"${LIBPATH}\"" >> "${LLDBCMDPATH}"
echo "env DYLD_INSERT_LIBRARIES=\"${LIBPATH}\"" >> "${LLDBCMDPATH}"
echo "env DYLD_FORCE_FLAT_NAMESPACE=1" >> "${LLDBCMDPATH}"
echo "run ${@:2}" >> "${LLDBCMDPATH}"
lldb "${1}" -s "${LLDBCMDPATH}"
fi
Expand Down
4 changes: 2 additions & 2 deletions src/stackusage.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
.TH STACKUSAGE "1" "December 2021" "stackusage v1.15" "User Commands"
.TH STACKUSAGE "1" "January 2022" "stackusage v1.16" "User Commands"
.SH NAME
stackusage \- measure stack usage in applications
.SH SYNOPSIS
Expand Down Expand Up @@ -57,6 +57,6 @@ Written by Kristofer Berggren
.SH "REPORTING BUGS"
Report bugs at https://github.com/d99kris/stackusage
.SH COPYRIGHT
Copyright \(co 2015\-2021 Kristofer Berggren
Copyright \(co 2015\-2022 Kristofer Berggren
.PP
stackusage is distributed under the BSD 3\-Clause license.
24 changes: 23 additions & 1 deletion src/sumain.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* sumain.c
*
* Copyright (C) 2015-2018 Kristofer Berggren
* Copyright (C) 2015-2022 Kristofer Berggren
* All rights reserved.
*
* stackusage is distributed under the BSD 3-Clause license, see LICENSE for details.
Expand Down Expand Up @@ -50,6 +50,13 @@
#define SU_LOG_WARN SU_LOG("%s (pid %d): %s:%d warning\n", \
su_name, getpid(), __FUNCTION__, __LINE__)

#if defined(__APPLE__)
#define DYLD_INTERPOSE(_newfun, _orgfun) \
__attribute__((used)) static struct{ const void *newfun; const void *orgfun; } _interpose_##_orgfun \
__attribute__ ((section ("__DATA,__interpose"))) = { (const void *)(unsigned long)&_newfun, \
(const void *)(unsigned long)&_orgfun }
#endif


/* ----------- Types --------------------------------------------- */
typedef struct
Expand Down Expand Up @@ -168,20 +175,32 @@ void signal_handler(int num)
}


#if defined(__APPLE__)
int pthread_create_wrap(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

int pthread_create_wrap(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
#else
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
#endif
{
int rv = -1;
su_threadstart_t *tstart = NULL;

if(real_pthread_create == NULL)
{
#if defined(__APPLE__)
real_pthread_create = pthread_create;
#else
/* Get function ptr to real pthread_create */
real_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
if(real_pthread_create == NULL)
{
SU_LOG_ERR;
}
#endif

/* Initialize thread key with callback at thread termination */
pthread_key_create(&threadkey, su_thread_fini);
Expand Down Expand Up @@ -225,6 +244,9 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

return rv;
}
#if defined(__APPLE__)
DYLD_INTERPOSE(pthread_create_wrap, pthread_create);
#endif


/* ----------- Local Functions ----------------------------------- */
Expand Down

0 comments on commit 47fa405

Please sign in to comment.