Skip to content

Commit 47fa405

Browse files
committed
fix for macOS Monterey - use DYLD_INTERPOSE macro
1 parent 8a99b07 commit 47fa405

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Supported Platforms
3030
Stackusage is primarily developed and tested on Linux, but basic
3131
functionality should work in macOS / OS X as well. Current version has been
3232
tested on:
33-
- macOS Big Sur 11.0
33+
- macOS Monterey 12.1
3434
- Ubuntu 20.04 LTS
3535

3636
Limitation: On macOS / OS X this tool relies on code injection using

make.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fi
9595

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

101101
# doc

src/stackusage

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

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

4444
showversion()
4545
{
46-
echo "stackusage v1.15"
46+
echo "stackusage v1.16"
4747
echo ""
48-
echo "Copyright (C) 2015-2021 Kristofer Berggren"
48+
echo "Copyright (C) 2015-2022 Kristofer Berggren"
4949
echo ""
5050
echo "stackusage is distributed under the BSD 3-Clause license."
5151
echo ""
@@ -182,15 +182,13 @@ while [ "${LIBPATHS[CNT]}" != "" ]; do
182182
SU_SIGNO="${SIGNO}" \
183183
LD_PRELOAD="${LIBPATH}" \
184184
DYLD_INSERT_LIBRARIES="${LIBPATH}" \
185-
DYLD_FORCE_FLAT_NAMESPACE=1 \
186185
"${@:1}"
187186
else
188187
LLDBCMDPATH="${TMP}/lldb.cmd"
189188
echo "env SU_FILE=\"${TMPLOG}${OUTFILE}\"" > "${LLDBCMDPATH}"
190189
echo "env SU_SIGNO=\"${SIGNO}\"" >> "${LLDBCMDPATH}"
191190
echo "env LD_PRELOAD=\"${LIBPATH}\"" >> "${LLDBCMDPATH}"
192191
echo "env DYLD_INSERT_LIBRARIES=\"${LIBPATH}\"" >> "${LLDBCMDPATH}"
193-
echo "env DYLD_FORCE_FLAT_NAMESPACE=1" >> "${LLDBCMDPATH}"
194192
echo "run ${@:2}" >> "${LLDBCMDPATH}"
195193
lldb "${1}" -s "${LLDBCMDPATH}"
196194
fi

src/stackusage.1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
2-
.TH STACKUSAGE "1" "December 2021" "stackusage v1.15" "User Commands"
2+
.TH STACKUSAGE "1" "January 2022" "stackusage v1.16" "User Commands"
33
.SH NAME
44
stackusage \- measure stack usage in applications
55
.SH SYNOPSIS
@@ -57,6 +57,6 @@ Written by Kristofer Berggren
5757
.SH "REPORTING BUGS"
5858
Report bugs at https://github.com/d99kris/stackusage
5959
.SH COPYRIGHT
60-
Copyright \(co 2015\-2021 Kristofer Berggren
60+
Copyright \(co 2015\-2022 Kristofer Berggren
6161
.PP
6262
stackusage is distributed under the BSD 3\-Clause license.

src/sumain.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* sumain.c
33
*
4-
* Copyright (C) 2015-2018 Kristofer Berggren
4+
* Copyright (C) 2015-2022 Kristofer Berggren
55
* All rights reserved.
66
*
77
* stackusage is distributed under the BSD 3-Clause license, see LICENSE for details.
@@ -50,6 +50,13 @@
5050
#define SU_LOG_WARN SU_LOG("%s (pid %d): %s:%d warning\n", \
5151
su_name, getpid(), __FUNCTION__, __LINE__)
5252

53+
#if defined(__APPLE__)
54+
#define DYLD_INTERPOSE(_newfun, _orgfun) \
55+
__attribute__((used)) static struct{ const void *newfun; const void *orgfun; } _interpose_##_orgfun \
56+
__attribute__ ((section ("__DATA,__interpose"))) = { (const void *)(unsigned long)&_newfun, \
57+
(const void *)(unsigned long)&_orgfun }
58+
#endif
59+
5360

5461
/* ----------- Types --------------------------------------------- */
5562
typedef struct
@@ -168,20 +175,32 @@ void signal_handler(int num)
168175
}
169176

170177

178+
#if defined(__APPLE__)
179+
int pthread_create_wrap(pthread_t *thread, const pthread_attr_t *attr,
180+
void *(*start_routine) (void *), void *arg);
181+
182+
int pthread_create_wrap(pthread_t *thread, const pthread_attr_t *attr,
183+
void *(*start_routine) (void *), void *arg)
184+
#else
171185
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
172186
void *(*start_routine) (void *), void *arg)
187+
#endif
173188
{
174189
int rv = -1;
175190
su_threadstart_t *tstart = NULL;
176191

177192
if(real_pthread_create == NULL)
178193
{
194+
#if defined(__APPLE__)
195+
real_pthread_create = pthread_create;
196+
#else
179197
/* Get function ptr to real pthread_create */
180198
real_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
181199
if(real_pthread_create == NULL)
182200
{
183201
SU_LOG_ERR;
184202
}
203+
#endif
185204

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

226245
return rv;
227246
}
247+
#if defined(__APPLE__)
248+
DYLD_INTERPOSE(pthread_create_wrap, pthread_create);
249+
#endif
228250

229251

230252
/* ----------- Local Functions ----------------------------------- */

0 commit comments

Comments
 (0)