1//===-- copyprof_reporting.cpp -------------------------------------------===//
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 implements the reporting mechanisms for CopyProf, generating
10/// reports with stack traces when unnecessary object copies are destroyed.
11///
12//===----------------------------------------------------------------------===//
13
14#include "copyprof_reporting.h"
15
16#include "sanitizer_common/sanitizer_common.h"
17#include "sanitizer_common/sanitizer_stacktrace.h"
18
19namespace __copyprof {
20namespace {
21
22// TODO: Make configurable via flags.
23constexpr int kMaxNumStackFrames = 30;
24
25} // namespace
26
27void LogCopyProfReport(uptr pc, uptr bp, uptr obj_size, bool did_allocate) {
28 // FIXME: replace hard coded object size with flag.
29 if (obj_size <= 16 || !did_allocate)
30 return;
31 UNINITIALIZED BufferedStackTrace stack_trace;
32 stack_trace.Unwind(pc, bp, /*context=*/nullptr,
33 request_fast: common_flags()->fast_unwind_on_fatal, max_depth: kMaxNumStackFrames);
34 InternalScopedString output;
35 output.AppendF(
36 format: "[copyprof] Destroyed unnecessary copy amounting to %zu bytes:\n",
37 (usize)obj_size);
38 stack_trace.PrintTo(output: &output);
39 Printf(format: "%s", output.data());
40}
41
42} // namespace __copyprof
43