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