diff --git a/common/dfxutil/dfx_util.cpp b/common/dfxutil/dfx_util.cpp index a90f4303cb393346e5246bff536dc2db3fcc21d9..9429fd431a92ef43de6e3f061379247b43ec182d 100644 --- a/common/dfxutil/dfx_util.cpp +++ b/common/dfxutil/dfx_util.cpp @@ -229,7 +229,11 @@ bool ReadFdToString(int fd, std::string& content) void CloseFd(int &fd) { if (fd > 0) { +#if is_ohos fdsan_close_with_tag(fd, fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, LOG_DOMAIN)); +#else + close(fd); +#endif fd = -1; } } diff --git a/common/dfxutil/string_view_util.h b/common/dfxutil/string_view_util.h index b44a59ea775b368b93391ce19b440f617c309477..7dbd3568df9035edea91e920bf12fa5c58fe116f 100644 --- a/common/dfxutil/string_view_util.h +++ b/common/dfxutil/string_view_util.h @@ -24,6 +24,24 @@ namespace OHOS { namespace HiviewDFX { +#ifdef is_mac +class SpinLock { +public: + void lock() + { + while (locked_.test_and_set(std::memory_order_acquire)) { + ; + } + } + void unlock() + { + locked_.clear(std::memory_order_release); + } + +private: + std::atomic_flag locked_ = ATOMIC_FLAG_INIT; +}; +#endif class StringViewHold { public: static StringViewHold &Get() @@ -34,50 +52,77 @@ public: const char* Hold(STRING_VIEW view) { +#ifndef is_mac pthread_spin_lock(&spin_lock_); +#else + std::lock_guard lockGurand(spinlock_); +#endif if (view.size() == 0) { +#ifndef is_mac pthread_spin_unlock(&spin_lock_); +#endif return ""; } char *p = new (std::nothrow) char[view.size() + 1]; if (p == nullptr) { +#ifndef is_mac pthread_spin_unlock(&spin_lock_); +#endif return ""; } if (memset_s(p, view.size() + 1, '\0', view.size() + 1) != 0) { +#ifndef is_mac pthread_spin_unlock(&spin_lock_); +#endif return ""; } std::copy(view.data(), view.data() + view.size(), p); views_.emplace_back(p); +#ifndef is_mac pthread_spin_unlock(&spin_lock_); +#endif return p; } // only use in UT void Clean() { +#ifndef is_mac pthread_spin_lock(&spin_lock_); +#else + std::lock_guard lockGurand(spinlock_); +#endif for (auto &p : views_) { delete[] p; } views_.clear(); +#ifndef is_mac pthread_spin_unlock(&spin_lock_); +#endif } + private: +#ifndef is_mac StringViewHold() { pthread_spin_init(&spin_lock_, PTHREAD_PROCESS_PRIVATE); } +#endif ~StringViewHold() { Clean(); +#ifndef is_mac pthread_spin_destroy(&spin_lock_); +#endif } std::vector views_; +#ifndef is_mac pthread_spinlock_t spin_lock_; +#else + SpinLock spinlock_; +#endif }; } // namespace HiviewDFX } // namespace OHOS diff --git a/interfaces/common/byte_order.h b/interfaces/common/byte_order.h index 3c40993ec56288deec6e40420a97d182587e9b62..a55d9db076a6fe1476a52a102fb968adb08073d7 100644 --- a/interfaces/common/byte_order.h +++ b/interfaces/common/byte_order.h @@ -16,7 +16,7 @@ #ifndef BYTE_ORDER_H #define BYTE_ORDER_H -#if is_mingw +#if is_mingw || is_mac #define UNWIND_LITTLE_ENDIAN 1234 #define UNWIND_BIG_ENDIAN 4321 #define UNWIND_BYTE_ORDER -1 // Unknown diff --git a/interfaces/innerkits/unwinder/include/dfx_elf_define.h b/interfaces/innerkits/unwinder/include/dfx_elf_define.h index 6bc9394912c193417cbfe588551b07c255fce62a..a71d76b5641ec347f014736173137cf1115c446b 100644 --- a/interfaces/innerkits/unwinder/include/dfx_elf_define.h +++ b/interfaces/innerkits/unwinder/include/dfx_elf_define.h @@ -17,7 +17,7 @@ #include #include -#if !is_mingw +#if !is_mingw && !is_mac #include #include #endif diff --git a/interfaces/innerkits/unwinder/include/dfx_elf_parser.h b/interfaces/innerkits/unwinder/include/dfx_elf_parser.h index b4c84437735176d28f7756930a8027152fc08155..86a4bdd197918e6246edf683eec2d213b1414803 100644 --- a/interfaces/innerkits/unwinder/include/dfx_elf_parser.h +++ b/interfaces/innerkits/unwinder/include/dfx_elf_parser.h @@ -16,7 +16,7 @@ #define DFX_ELF_PARSER_H #include -#if is_mingw +#if is_mingw || is_mac #include "dfx_nonlinux_define.h" #else #include diff --git a/interfaces/innerkits/unwinder/src/elf/dfx_elf.cpp b/interfaces/innerkits/unwinder/src/elf/dfx_elf.cpp index 9398e59acea6722bb1bfebcd0f312ee826a6f5a1..d071f2b934610fb15a921972a9eb97f3c646506f 100644 --- a/interfaces/innerkits/unwinder/src/elf/dfx_elf.cpp +++ b/interfaces/innerkits/unwinder/src/elf/dfx_elf.cpp @@ -20,7 +20,7 @@ #include #include #include -#if is_mingw +#if is_mingw || is_mac #include "dfx_nonlinux_define.h" #else #include diff --git a/interfaces/innerkits/unwinder/src/elf/elf_factory.cpp b/interfaces/innerkits/unwinder/src/elf/elf_factory.cpp index cbe6706288992a4ad849fd8cae7f04dbaa6c9513..52ced53159cfe2f1bac18b8f741829acbe6cb208 100644 --- a/interfaces/innerkits/unwinder/src/elf/elf_factory.cpp +++ b/interfaces/innerkits/unwinder/src/elf/elf_factory.cpp @@ -151,8 +151,10 @@ std::shared_ptr RegularElfFactory::Create() DFXLOGE("Failed to open file: %{public}s, errno(%{public}d)", filePath_.c_str(), errno); return regularElf; } +#if is_ohos uint64_t ownerTag = fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, LOG_DOMAIN); fdsan_exchange_owner_tag(fd, 0, ownerTag); +#endif do { auto size = static_cast(GetFileSize(fd)); auto mMap = std::make_shared(); @@ -162,7 +164,11 @@ std::shared_ptr RegularElfFactory::Create() } regularElf->SetMmap(mMap); } while (false); +#if is_ohos fdsan_close_with_tag(fd, ownerTag); +#else + close(fd); +#endif return regularElf; } @@ -215,8 +221,10 @@ std::shared_ptr CompressHapElfFactory::Create() DFXLOGE("Failed to open hap file, errno(%{public}d)", errno); return nullptr; } +#if is_ohos uint64_t ownerTag = fdsan_create_owner_tag(FDSAN_OWNER_TYPE_FILE, LOG_DOMAIN); fdsan_exchange_owner_tag(fd, 0, ownerTag); +#endif std::shared_ptr compressHapElf = nullptr; do { size_t elfSize = 0; @@ -237,7 +245,11 @@ std::shared_ptr CompressHapElfFactory::Create() break; } } while (false); +#if is_ohos fdsan_close_with_tag(fd, ownerTag); +#else + close(fd); +#endif return compressHapElf; }