memprof_descriptions.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===-- memprof_descriptions.cpp -------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of MemProfiler, a memory profiler.
  10. //
  11. // MemProf functions for getting information about an address and/or printing
  12. // it.
  13. //===----------------------------------------------------------------------===//
  14. #include "memprof_descriptions.h"
  15. #include "memprof_mapping.h"
  16. #include "memprof_stack.h"
  17. #include "sanitizer_common/sanitizer_stackdepot.h"
  18. namespace __memprof {
  19. MemprofThreadIdAndName::MemprofThreadIdAndName(MemprofThreadContext *t) {
  20. Init(t->tid, t->name);
  21. }
  22. MemprofThreadIdAndName::MemprofThreadIdAndName(u32 tid) {
  23. if (tid == kInvalidTid) {
  24. Init(tid, "");
  25. } else {
  26. memprofThreadRegistry().CheckLocked();
  27. MemprofThreadContext *t = GetThreadContextByTidLocked(tid);
  28. Init(tid, t->name);
  29. }
  30. }
  31. void MemprofThreadIdAndName::Init(u32 tid, const char *tname) {
  32. int len = internal_snprintf(name, sizeof(name), "T%d", tid);
  33. CHECK(((unsigned int)len) < sizeof(name));
  34. if (tname[0] != '\0')
  35. internal_snprintf(&name[len], sizeof(name) - len, " (%s)", tname);
  36. }
  37. void DescribeThread(MemprofThreadContext *context) {
  38. CHECK(context);
  39. memprofThreadRegistry().CheckLocked();
  40. // No need to announce the main thread.
  41. if (context->tid == kMainTid || context->announced) {
  42. return;
  43. }
  44. context->announced = true;
  45. InternalScopedString str;
  46. str.append("Thread %s", MemprofThreadIdAndName(context).c_str());
  47. if (context->parent_tid == kInvalidTid) {
  48. str.append(" created by unknown thread\n");
  49. Printf("%s", str.data());
  50. return;
  51. }
  52. str.append(" created by %s here:\n",
  53. MemprofThreadIdAndName(context->parent_tid).c_str());
  54. Printf("%s", str.data());
  55. StackDepotGet(context->stack_id).Print();
  56. // Recursively described parent thread if needed.
  57. if (flags()->print_full_thread_history) {
  58. MemprofThreadContext *parent_context =
  59. GetThreadContextByTidLocked(context->parent_tid);
  60. DescribeThread(parent_context);
  61. }
  62. }
  63. } // namespace __memprof