Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apple OSX Support #1861

Open
paranlee opened this issue Dec 17, 2023 · 1 comment
Open

Apple OSX Support #1861

paranlee opened this issue Dec 17, 2023 · 1 comment

Comments

@paranlee
Copy link
Contributor

paranlee commented Dec 17, 2023

If Apple were to making an next Vision Pro, they would have no choice but to use uftrace for Verifying the deterministic software behavior and performance measurement.

I found the Apple clang version 15.0.0 block the -pg options(no error message but when we do, there was no mcount or someting like that on objdump -dS assembly codes), so i tried to -finstrument-functions

$ uname -a

Darwin MacBookAir 23.2.0 Darwin Kernel Version 23.2.0:
Wed Nov 15 21:59:33 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T8112 arm64
$ clang -v
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: arm64-apple-darwin23.2.0

Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ ./HelloMac

[MyLib.c] initializer()

Enter:

0, __cyg_profile_func_enter: fn = 0x1020ebef8, caller = 0x181ecd0e0

Hello, World!

Enter:

1, __cyg_profile_func_enter: fn = 0x1020ebe80, caller = 0x1020ebf58

Enter:

2, __cyg_profile_func_enter: fn = 0x1020ebe28, caller = 0x1020ebeb8

Exit:

1, __cyg_profile_func_exit: fn = 0x1020ebe28, caller = 0x1020ebeb8

Enter:

2, __cyg_profile_func_enter: fn = 0x1020ebe28, caller = 0x1020ebec4

Exit:

1, __cyg_profile_func_exit: fn = 0x1020ebe28, caller = 0x1020ebec4

Exit:

0, __cyg_profile_func_exit: fn = 0x1020ebe80, caller = 0x1020ebf58

Exit:

-1, __cyg_profile_func_exit: fn = 0x1020ebef8, caller = 0x181ecd0e0

[MyLib.c] finalizer()

My PoC Codes are right here.

Apple hide the __cyg_profile_func_enter, __cyg_profile_func_exit in there libc.

So when we compiled -finstrument-functions option only, that makes ld linking error.

ld: undefined symbols: ___cyg_profile_func_enter

MyLib implements the __cyg_profile_func_enter, __cyg_profile_func_exit [1].

/* File: MyLib.h

 * Interface to MyLib.lib 1.0.

 *************************************/

void __cyg_profile_func_enter(void *, void *); // __attribute__((no_instrument_function));

void __cyg_profile_func_exit(void *, void *); // __attribute__((no_instrument_function));
/* File: MyLib.c

 * Compile with -fvisibility=hidden.

 **********************************/

#include "MyLib.h"
#include <stdio.h>
#include <string.h>

#define EXPORT __attribute__((visibility("default")))

#define sustainable(fn, caller) \
        do{ \
            printf("%d, %s: fn = %p, caller = %p\n", depth_, __FUNCTION__, fn, caller); \
        } while(0)

int depth_ = -1;

// Initializer.
__attribute__((constructor))
static void initializer(void)
{
    printf("[%s] initializer()\n", __FILE__);
}

// Finalizer.
__attribute__((destructor))
static void finalizer(void)
{
    printf("[%s] finalizer()\n", __FILE__);
}

EXPORT
void __cyg_profile_func_enter(void *fn, void *caller)
{
        printf("Enter:\n");
        depth_++;
        sustainable(fn, caller);
}

EXPORT
void __cyg_profile_func_exit(void *fn, void *caller)
{
        printf("Exit:\n");
        depth_--;
        sustainable(fn, caller);
}

We need to our source codes building with shared library MyLib.lib.

$ clang -shared MyLib.c -current_version 1.0 -fvisibility=hidden -o MyLib.lib

Test code main.c.

//  main.c
//  HelloMac
//
//  Created by Paran Lee on 12/17/23.
//

#include <stdio.h>

int func2(int arg1)
{
    return 2 + arg1;
}

int func1(int arg1)
{

    int val1 = func2(arg1);
    int val2 = func2(val1);

    return val1 + val2;
}

int main(int argc, const char * argv[])
{

    printf("Hello, World!\n");

    func1(1);

    return 0;
}

Then

$ clang -v -O0 -ggdb -finstrument-functions main.c -o HelloMac MyLib.lib

Apple clang version 15.0.0 (clang-1500.1.0.2.5)

Target: arm64-apple-darwin23.2.0

Thread model: posix

InstalledDir: /Library/Developer/CommandLineTools/usr/bin

 "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple arm64-apple-macosx14.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -fno-strict-return -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fobjc-msgsend-selector-stubs -target-sdk-version=14.2 -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +crc -target-feature +lse -target-feature +rdm -target-feature +crypto -target-feature +dotprod -target-feature +fp-armv8 -target-feature +neon -target-feature +fp16fml -target-feature +ras -target-feature +rcpc -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-feature +sm4 -target-feature +sha3 -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=gdb -target-linker-version 1022.1 -v -finstrument-functions -fcoverage-compilation-dir=/Users/yunseong/Desktop/cpp-project/HelloMac/HelloMac -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/15.0.0 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Library/Developer/CommandLineTools/usr/include -O0 -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant -fdebug-compilation-dir=/Users/yunseong/Desktop/cpp-project/HelloMac/HelloMac -ferror-limit 19 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fmax-type-align=16 -fcommon -fcolor-diagnostics -clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf -clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -mllvm -disable-aligned-alloc-awareness=1 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/j0/gv5zl6kd1t549fwnw88p9bl40000gn/T/main-1bc209.o -x c main.c

clang -cc1 version 15.0.0 (clang-1500.1.0.2.5) default target arm64-apple-darwin23.2.0

ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"

#include "..." search starts here:
#include <...> search starts here:

 /Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)

End of search list.

 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 14.0.0 14.2 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -O0 -o HelloMac -L/usr/local/lib /var/folders/j0/gv5zl6kd1t549fwnw88p9bl40000gn/T/main-1bc209.o MyLib.lib -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/lib/darwin/libclang_rt.osx.a

 "/Library/Developer/CommandLineTools/usr/bin/dsymutil" -o HelloMac.dSYM HelloMac

I could found the reference that Apple used -finstrument-functions more than 10 years ago old age [2].

Saturn profiler -> macOS 10.15 Catalina

Apple' new profiler Instruments will replace the Saturn profiler
Instruments -> macOS 10.10 Yosemite

When I investigated the compile time what Apple clang version 15.0.0 looks library, I need some trace tool like strace so I can using ktrace on my Apple M2 [3].

[1] https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html
[2] https://adrianboeing.blogspot.com/2010/02/profiling-on-mac-osx-with-saturn.html
[3] https://stackoverflow.com/questions/31045575/how-to-trace-system-calls-of-a-program-in-mac-os-x

@paranlee
Copy link
Contributor Author

Checklist from @honggyukim

To use uftrace on a Mac, we need to do the following.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant