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

Fix stacktraces on MacOS #59690

Merged
merged 8 commits into from Feb 9, 2024
Merged

Fix stacktraces on MacOS #59690

merged 8 commits into from Feb 9, 2024

Conversation

nikitamikhaylov
Copy link
Member

@nikitamikhaylov nikitamikhaylov commented Feb 7, 2024

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Fix stack unwinding on MacOS. This closes #53653

@robot-clickhouse robot-clickhouse added pr-improvement Pull request with some product improvements submodule changed At least one submodule changed in this PR. labels Feb 7, 2024
@robot-clickhouse
Copy link
Member

robot-clickhouse commented Feb 7, 2024

This is an automated comment for commit 2a79f56 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success
Check nameDescriptionStatus
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR⏳ pending
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure

@nikitamikhaylov
Copy link
Member Author

nikitamikhaylov commented Feb 7, 2024

Previously ClickHouse binary natively compiled on MacOS was writing lot of errors like

libunwind: malformed __unwind_info at 0x112A73C54 bad second level page

whereas cross-compiled binary just had empty traces.
I looked into the LLDB source code and found out that it uses backtrace function from MacOS SDK (somewhat like libc or system library). ClickHouse already depends on it, so this change won't make anything any worse

❯ otool -L ./clickhouse
./clickhouse:
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 2202.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)

With this patch I was able to get stacktraces like this:

❯ ./clickhouse local --stacktrace -q "SELECT throwIf(1)"
Code: 395. DB::Exception: Value passed to 'throwIf' function is non-zero: while executing 'FUNCTION throwIf(1 :: 0) -> throwIf(1) UInt8 : 1'. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO), Stack trace (when copying this message, always include the lines below):

0. std::exception::capture() @ 0x00000001041226a4 in clickhouse
1. std::exception::exception[abi:v15000]() @ 0x0000000104122670 in clickhouse
2. Poco::Exception::Exception(String const&, int) @ 0x000000011cd0d1a4 in clickhouse
3. DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000010c530cbc in clickhouse
4. DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000010c530e20 in clickhouse
5. DB::Exception::Exception(String&&, int, bool) @ 0x000000010410dab4 in clickhouse
6. DB::Exception::Exception<String>(int, FormatStringHelperImpl<std::type_identity<String>::type>, String&&) @ 0x000000010410d98c in clickhouse
7. COW<DB::IColumn>::immutable_ptr<DB::IColumn> DB::(anonymous namespace)::FunctionThrowIf::execute<char8_t>(DB::IColumn const*, std::optional<String> const&, std::optional<int> const&) const @ 0x000000010ad57388 in clickhouse
8. DB::(anonymous namespace)::FunctionThrowIf::executeImpl(std::vector<DB::ColumnWithTypeAndName, std::allocator<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x000000010ad56218 in clickhouse
9. DB::FunctionToExecutableFunctionAdaptor::executeImpl(std::vector<DB::ColumnWithTypeAndName, std::allocator<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x0000000104110bcc in clickhouse
10. DB::IExecutableFunction::executeWithoutLowCardinalityColumns(std::vector<DB::ColumnWithTypeAndName, std::allocator<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x000000011354bbd4 in clickhouse
11. DB::IExecutableFunction::executeWithoutSparseColumns(std::vector<DB::ColumnWithTypeAndName, std::allocator<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x000000011354cb28 in clickhouse
12. DB::IExecutableFunction::execute(std::vector<DB::ColumnWithTypeAndName, std::allocator<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x000000011354db08 in clickhouse
13. DB::executeAction(DB::ExpressionActions::Action const&, DB::(anonymous namespace)::ExecutionContext&, bool) @ 0x00000001165f4110 in clickhouse
14. DB::ExpressionActions::execute(DB::Block&, unsigned long&, bool) const @ 0x00000001165f3370 in clickhouse
15. DB::ExpressionTransform::transform(DB::Chunk&) @ 0x000000011a0f7508 in clickhouse
16. DB::ISimpleTransform::transform(DB::Chunk&, DB::Chunk&) @ 0x000000011005aa2c in clickhouse
17. DB::ISimpleTransform::work() @ 0x0000000119a13684 in clickhouse
18. DB::executeJob(DB::ExecutingGraph::Node*, DB::ReadProgressCallback*) @ 0x0000000119a54d8c in clickhouse
19. DB::ExecutionThreadContext::executeTask() @ 0x0000000119a54abc in clickhouse
20. DB::PipelineExecutor::executeStepImpl(unsigned long, std::atomic<bool>*) @ 0x0000000119a5e550 in clickhouse
21. DB::PipelineExecutor::executeSingleThread(unsigned long) @ 0x0000000119a5e9cc in clickhouse
22. DB::PipelineExecutor::executeImpl(unsigned long, bool) @ 0x0000000119a5d628 in clickhouse
23. DB::PipelineExecutor::execute(unsigned long, bool) @ 0x0000000119a5cdd0 in clickhouse
24. DB::threadFunction(DB::PullingAsyncPipelineExecutor::Data&, std::shared_ptr<DB::ThreadGroup>, unsigned long, bool) @ 0x0000000119a76548 in clickhouse
25. DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0::operator()() const @ 0x0000000119a763dc in clickhouse
26. decltype(std::declval<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&>()()) std::__invoke[abi:v15000]<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&) @ 0x0000000119a76338 in clickhouse
27. decltype(auto) std::__apply_tuple_impl[abi:v15000]<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&, std::tuple<>&>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&, std::tuple<>&, std::__tuple_indices<>) @ 0x0000000119a762fc in clickhouse
28. decltype(auto) std::apply[abi:v15000]<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&, std::tuple<>&>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&, std::tuple<>&) @ 0x0000000119a76200 in clickhouse
29. ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&&)::'lambda'()::operator()() @ 0x0000000119a7610c in clickhouse
30. decltype(std::declval<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0>()()) std::__invoke[abi:v15000]<ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&&)::'lambda'()&>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&&) @ 0x0000000119a76058 in clickhouse
31. void std::__invoke_void_return_wrapper<void, true>::__call<ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&&)::'lambda'()&>(ThreadFromGlobalPoolImpl<true>::ThreadFromGlobalPoolImpl<DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0>(DB::PullingAsyncPipelineExecutor::pull(DB::Chunk&, unsigned long long)::$_0&&)::'lambda'()&) @ 0x0000000119a76018 in clickhouse

@@ -1,6 +1,10 @@
#include <libunwind.h>

#if defined(__APPLE__)
int backtrace(void ** buffer, int size);
#else
int backtrace(void ** buffer, int size)
{
return unw_backtrace(buffer, size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly it doesn't work on Apple Macintosh OS X?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why unw_backtrace doesn't work? I have no idea. I tried to use libunwind just from llvm's master - the result is the same. The only thing which bothers me - why LLDB can unwind stack while ClickHouse cannot. This was the motivation for this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is prohibitive on Linux with glibc.
glibc uses an idiotic implementation: - it locates a .so at runtime and does dlopen.

It also does not work on Musl.

But I thought that it could be acceptable on Mac.

@nikitamikhaylov nikitamikhaylov marked this pull request as ready for review February 8, 2024 22:31
@alesapin alesapin self-assigned this Feb 9, 2024
@nikitamikhaylov nikitamikhaylov merged commit 50828da into master Feb 9, 2024
265 of 269 checks passed
@nikitamikhaylov nikitamikhaylov deleted the fix-stacktraces-on-mac branch February 9, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-improvement Pull request with some product improvements submodule changed At least one submodule changed in this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No stack trace on error on macOS w/Apple Silicon
4 participants