Skip to content

Commit c74a0f2

Browse files
committed
Add internal instrumentation for tracking stack sizes
1 parent 2d9382d commit c74a0f2

File tree

5 files changed

+133
-38
lines changed

5 files changed

+133
-38
lines changed

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.3.0 Build 8C04:1728
2+
cppfront compiler v0.3.0 Build 8C09:1607
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8C04:1728"
1+
"8C09:1607"

source/common.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,96 @@ static cmdline_processor::register_flag cmd_gen_version(
871871
[]{ cmdline.gen_version(); }
872872
);
873873

874+
static auto flag_internal_debug = false;
875+
static cmdline_processor::register_flag cmd_internal_debug(
876+
0,
877+
"_debug",
878+
"Generate internal debug instrumentation",
879+
[]{ flag_internal_debug = true; }
880+
);
881+
882+
883+
//-----------------------------------------------------------------------
884+
//
885+
// Internal instrumentation
886+
//
887+
//-----------------------------------------------------------------------
888+
//
889+
890+
class stackinstr
891+
{
892+
struct entry
893+
{
894+
ptrdiff_t delta;
895+
ptrdiff_t cumulative;
896+
std::string_view func_name;
897+
std::string_view file;
898+
int line;
899+
char* ptr;
900+
901+
entry(
902+
std::string_view n,
903+
std::string_view f,
904+
int l,
905+
char* p
906+
)
907+
: delta { entries.empty() ? 0 : std::abs(entries.back().ptr - p) }
908+
, cumulative{ entries.empty() ? 0 : entries.back().cumulative + delta }
909+
, func_name { n }
910+
, file { f }
911+
, line { l }
912+
, ptr { p }
913+
{ }
914+
};
915+
static std::vector<entry> entries;
916+
static std::vector<entry> deepest;
917+
static std::vector<entry> largest;
918+
919+
static auto print(auto&& ee, std::string_view label) {
920+
std::cout << "\n=== Stack debug information: " << label << " stack ===\n";
921+
for (auto& e: ee)
922+
if (e.ptr) {
923+
std::cout
924+
<< " " << std::setw(6)
925+
<< ((std::abs(e.delta) < 1000000)? std::to_string(e.delta) : "-----") << " "
926+
<< std::setw(8)
927+
<< ((std::abs(e.delta) < 1000000)? std::to_string(e.cumulative) : "-------") << " "
928+
<< e.func_name << " (" << e.file << ":" << e.line << ")\n";
929+
}
930+
}
931+
932+
public:
933+
struct guard {
934+
guard( std::string_view name, std::string_view file, int line, char* p ) {
935+
if (flag_internal_debug) {
936+
entries.emplace_back(name, file, line ,p);
937+
if (ssize(deepest) < ssize(entries)) {
938+
deepest = entries;
939+
}
940+
if (largest.empty() || largest.back().cumulative < entries.back().cumulative) {
941+
largest = entries;
942+
}
943+
}
944+
}
945+
~guard() {
946+
if (flag_internal_debug) {
947+
entries.pop_back();
948+
}
949+
}
950+
};
951+
952+
static auto print_entries() { print( entries, "Current" ); }
953+
static auto print_deepest() { print( deepest, "Deepest" ); }
954+
static auto print_largest() { print( largest, "Largest" ); }
955+
};
956+
957+
std::vector<stackinstr::entry> stackinstr::entries;
958+
std::vector<stackinstr::entry> stackinstr::deepest;
959+
std::vector<stackinstr::entry> stackinstr::largest;
960+
961+
#define STACKINSTR stackinstr::guard _s_guard{ __func__, __FILE__, __LINE__, reinterpret_cast<char*>(&_s_guard) };
962+
963+
874964
}
875965

876966
#endif

source/cppfront.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
#include "to_cpp1.h"
1919

20-
static auto enable_debug_output_files = false;
20+
static auto flag_debug_output = false;
2121
static cpp2::cmdline_processor::register_flag cmd_debug(
2222
9,
2323
"debug",
24-
"Emit compiler debug output files",
25-
[]{ enable_debug_output_files = true; }
24+
"Emit compiler debug output",
25+
[]{ flag_debug_output = true; }
2626
);
2727

2828
auto main(
@@ -101,10 +101,15 @@ auto main(
101101
}
102102

103103
// And, if requested, the debug information
104-
if (enable_debug_output_files) {
104+
if (flag_debug_output) {
105105
c.debug_print();
106106
}
107107
}
108108

109+
if (flag_internal_debug) {
110+
stackinstr::print_deepest();
111+
stackinstr::print_largest();
112+
}
113+
109114
return exit_status;
110115
}

0 commit comments

Comments
 (0)