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