-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[NFC][sanitizer] Add Debug utility to print thread history #111948
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
Merged
vitalybuka
merged 7 commits into
main
from
users/vitalybuka/spr/nfcsanitizer-add-debug-utility-to-print-thread-history
Oct 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5256967
[𝘀𝗽𝗿] changes to main this commit is based on
vitalybuka 14d9012
[𝘀𝗽𝗿] initial version
vitalybuka 7405d32
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka e18b2ff
rebase
vitalybuka 12f4a2f
prev is context
vitalybuka a67b684
[𝘀𝗽𝗿] changes introduced through rebase
vitalybuka 1189173
test
vitalybuka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
compiler-rt/lib/sanitizer_common/sanitizer_thread_history.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| //===-- sanitizer_thread_history.cpp --------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "sanitizer_thread_history.h" | ||
|
|
||
| #include "sanitizer_stackdepot.h" | ||
| namespace __sanitizer { | ||
|
|
||
| void PrintThreadHistory(ThreadRegistry ®istry, InternalScopedString &out) { | ||
| ThreadRegistryLock l(®istry); | ||
| // Stack traces are largest part of printout and they offten the same for | ||
| // multiple threads, so we will deduplicate them. | ||
| InternalMmapVector<const ThreadContextBase *> stacks; | ||
|
|
||
| registry.RunCallbackForEachThreadLocked( | ||
| [](ThreadContextBase *context, void *arg) { | ||
| static_cast<decltype(&stacks)>(arg)->push_back(context); | ||
| }, | ||
| &stacks); | ||
|
|
||
| Sort(stacks.data(), stacks.size(), | ||
| [](const ThreadContextBase *a, const ThreadContextBase *b) { | ||
| if (a->stack_id < b->stack_id) | ||
| return true; | ||
| if (a->stack_id > b->stack_id) | ||
| return false; | ||
| return a->tid < b->tid; | ||
| }); | ||
|
|
||
| auto describe_thread = [&](const ThreadContextBase *context) { | ||
| if (!context) { | ||
| out.Append("T-1"); | ||
| return; | ||
| } | ||
| out.AppendF("T%llu/%llu", context->unique_id, context->os_id); | ||
| if (internal_strlen(context->name)) | ||
| out.AppendF(" (%s)", context->name); | ||
| }; | ||
|
|
||
| auto get_parent = | ||
| [&](const ThreadContextBase *context) -> const ThreadContextBase * { | ||
| if (!context) | ||
| return nullptr; | ||
| ThreadContextBase *parent = registry.GetThreadLocked(context->parent_tid); | ||
| if (!parent) | ||
| return nullptr; | ||
| if (parent->unique_id >= context->unique_id) | ||
| return nullptr; | ||
| return parent; | ||
| }; | ||
|
|
||
| u32 stack_id = 0; | ||
| for (const ThreadContextBase *context : stacks) { | ||
| if (stack_id != context->stack_id) { | ||
| StackDepotGet(stack_id).PrintTo(&out); | ||
| stack_id = context->stack_id; | ||
| } | ||
| out.Append("Thread "); | ||
| describe_thread(context); | ||
| out.Append(" was created by "); | ||
| describe_thread(get_parent(context)); | ||
| out.Append("\n"); | ||
| } | ||
| if (!stacks.empty()) | ||
| StackDepotGet(stack_id).PrintTo(&out); | ||
vitalybuka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } // namespace __sanitizer | ||
24 changes: 24 additions & 0 deletions
24
compiler-rt/lib/sanitizer_common/sanitizer_thread_history.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| //===-- sanitizer_thread_history.h ------------------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Utility to print thread histroy from ThreadRegistry. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef SANITIZER_THREAD_HISTORY_H | ||
| #define SANITIZER_THREAD_HISTORY_H | ||
|
|
||
| #include "sanitizer_thread_registry.h" | ||
|
|
||
| namespace __sanitizer { | ||
|
|
||
| void PrintThreadHistory(ThreadRegistry& registry, InternalScopedString& out); | ||
|
|
||
| } // namespace __sanitizer | ||
|
|
||
| #endif // SANITIZER_THREAD_HISTORY_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.