1 //===-- memprof_descriptions.cpp -------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of MemProfiler, a memory profiler.
11 // MemProf functions for getting information about an address and/or printing
13 //===----------------------------------------------------------------------===//
15 #include "memprof_descriptions.h"
16 #include "memprof_mapping.h"
17 #include "memprof_stack.h"
18 #include "sanitizer_common/sanitizer_stackdepot.h"
22 MemprofThreadIdAndName::MemprofThreadIdAndName(MemprofThreadContext
*t
) {
23 Init(t
->tid
, t
->name
);
26 MemprofThreadIdAndName::MemprofThreadIdAndName(u32 tid
) {
27 if (tid
== kInvalidTid
) {
30 memprofThreadRegistry().CheckLocked();
31 MemprofThreadContext
*t
= GetThreadContextByTidLocked(tid
);
36 void MemprofThreadIdAndName::Init(u32 tid
, const char *tname
) {
37 int len
= internal_snprintf(name
, sizeof(name
), "T%d", tid
);
38 CHECK(((unsigned int)len
) < sizeof(name
));
40 internal_snprintf(&name
[len
], sizeof(name
) - len
, " (%s)", tname
);
43 void DescribeThread(MemprofThreadContext
*context
) {
45 memprofThreadRegistry().CheckLocked();
46 // No need to announce the main thread.
47 if (context
->tid
== kMainTid
|| context
->announced
) {
50 context
->announced
= true;
51 InternalScopedString str
;
52 str
.append("Thread %s", MemprofThreadIdAndName(context
).c_str());
53 if (context
->parent_tid
== kInvalidTid
) {
54 str
.append(" created by unknown thread\n");
55 Printf("%s", str
.data());
58 str
.append(" created by %s here:\n",
59 MemprofThreadIdAndName(context
->parent_tid
).c_str());
60 Printf("%s", str
.data());
61 StackDepotGet(context
->stack_id
).Print();
62 // Recursively described parent thread if needed.
63 if (flags()->print_full_thread_history
) {
64 MemprofThreadContext
*parent_context
=
65 GetThreadContextByTidLocked(context
->parent_tid
);
66 DescribeThread(parent_context
);
70 } // namespace __memprof