https://gcc.gnu.org/g:f0644c8aa6e19fcdf3da305c78a34b0d99819c54
commit r17-6325-gf0644c8aa6e19fcdf3da305c78a34b0d99819c54 Author: Robert Dubner <[email protected]> Date: Mon Nov 16 14:20:03 2026 -0500 cobol: Support for GDB-COBOL NEXT-over-PERFORM and logical frames. COBOL's out-of-line PERFORM behaves a lot like a CALL statement. They are not, however, implemented in GCOBOL as CALLs that create frames. They are, instead, implemented with branch instructions. In order for the matching GDB-COBOL debugger to be capable of NEXT-over-PERFORM, the compiler creates run-time tables of information about each PERFORM site in the source code, including exactly where the PERFORM is invoked, and where the return location is. While developing and testing NEXT-over-PERFORM capability, it became clear that not having the logical equivalent of equivalent of a backtrace through PERFORM invocations was frustrating. These changes create additional tables of information that allow GDB-COBOL to display PERFORM invocations in a backtrace as if they were subroutine CALLS. Those tables provide information that allow GDB "jump" and "forced return" commands to unwind the logical frames. A separate set of changes here provide run-time information so that GDB-COBOL can implement a "info cobol-files" command that displays the status of COBOL file connectors. gcc/cobol/ChangeLog: * cobol1.cc (cobol_langhook_init): Keep STOP RUN processing out of the "cold partition" until PR/127011 is resolved. * genapi.cc (parser_statement_begin): Establish detailed information for creating debugging information. (debug_perform_section): Return section name when not the default section. (save_debug_perform_site): Keep a list of PERFORM sites. (finish_debug_perform_sites): Finish creating the list of PERFORM sites. (create_debug_perform_sites): Creates detailed list of debugging information for PERFORM sites. (create_debug_file_catalogue): Creates catalog information for debug display of cobol file data. (save_debug_file_connector): Likewise. (internal_perform_through): Establish PERFORM return location so that GDB can target it with "jump" and "finish". (enter_program_common): Call creators of PERFORM and FILE debugging data. (parser_end_program): Finalize PERFORM site data. (parser_division): Create debug information only when not -g0. (parser_file_add): Save debugging information. * gengen.h (struct gg_function_t): libgcobol/ChangeLog: * Makefile.am: Add some tests to the build. * Makefile.in: Regenerated. * libgcobol.cc (struct perform_invocation_cleanup): struct for run-time debugging information. (static_assert): Check sizes and signatures. (class perform_stack_update): Support for GDB logical PERFORM backtrace. (__gg__pseudo_return_push): Likewise. (__gg__pseudo_return_pop): Likewise. (__gg__debug_pseudo_return_bookmark): Likewise. (__gg__pseudo_return_flush): Likewise. (__gg__debug_perform_unwind): Likewise. (__gg__allocate): module_name_stack moved elsewhere. * libgcobol.map: New functions added. * perform-stack.h: New file. gcc/testsuite/ChangeLog: * cobol.dg/debug-file-catalogue-off.cob: New test. * cobol.dg/debug-file-catalogue.cob: New test. * cobol.dg/debug-perform-sites-off.cob: New test. * cobol.dg/debug-perform-sites.cob: New test. Diff: --- gcc/cobol/cobol1.cc | 7 +- gcc/cobol/genapi.cc | 253 ++++++++++++++++++++- gcc/cobol/gengen.h | 12 + .../cobol.dg/debug-file-catalogue-off.cob | 23 ++ gcc/testsuite/cobol.dg/debug-file-catalogue.cob | 23 ++ gcc/testsuite/cobol.dg/debug-perform-sites-off.cob | 18 ++ gcc/testsuite/cobol.dg/debug-perform-sites.cob | 19 ++ libgcobol/Makefile.am | 12 + libgcobol/Makefile.in | 115 +++++++--- libgcobol/libgcobol.cc | 178 ++++++++++++++- libgcobol/libgcobol.map | 3 + libgcobol/perform-stack.h | 57 +++++ 12 files changed, 685 insertions(+), 35 deletions(-) diff --git a/gcc/cobol/cobol1.cc b/gcc/cobol/cobol1.cc index bc49f6059d42..3c34d1f8d796 100644 --- a/gcc/cobol/cobol1.cc +++ b/gcc/cobol/cobol1.cc @@ -246,8 +246,11 @@ cobol_langhook_init (void) ftype = build_function_type_list (void_type_node, integer_type_node, NULL_TREE); - gfc_define_builtin ("__builtin_exit", ftype, BUILT_IN_EXIT, - "exit", ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST); + /* Restore the cold attribute for STOP RUN once PR rtl-optimization/127011 + is fixed. For now, keep the PERFORM continuation that falls through + to this call out of the cold partition. */ + gfc_define_builtin ("__builtin_exit", ftype, BUILT_IN_EXIT, "exit", + ATTR_TMPURE_NORETURN_NOTHROW_LEAF_COLD_LIST & ~ECF_COLD); ftype = build_function_type_list (integer_type_node, const_char_pointer_type_node, diff --git a/gcc/cobol/genapi.cc b/gcc/cobol/genapi.cc index 9012f8e66519..9ac2a1b01d65 100644 --- a/gcc/cobol/genapi.cc +++ b/gcc/cobol/genapi.cc @@ -32,6 +32,7 @@ #include "coretypes.h" #include "tree.h" +#include "function.h" #include "options.h" #include "stor-layout.h" #include "ggc.h" @@ -1239,6 +1240,12 @@ parser_statement_begin( const cbl_name_t statement_name, gcc_assert( gg_trans_unit.function_stack.size() ); + // Implicit performs (for example SORT procedures) retain their initiating + // statement location as well as explicit PERFORM statements. + current_function->debug_statement_line = cobol_location().first_line; + current_function->debug_statement_column = cobol_location().first_column; + current_function->debug_statement_file = current_filename.back(); + // If a PERFORM is armed, that's the line that the PERFORM is on. The // cobol_location().first_line here is the major statement following the // the PERFORM statement. (We don't use .loc information in GDB because of @@ -2077,6 +2084,205 @@ paragraph_label(struct cbl_proc_t *procedure) gg_append_statement(procedure->no_alter_label); } +/* A paragraph's section disambiguates names reused by another section. + The compiler's unnamed section has no user-facing qualifier. */ +static const char * +debug_perform_section(const cbl_label_t *label) + { + if( label->type == LblParagraph && label->parent ) + { + const cbl_label_t *section = cbl_label_of(symbol_at(label->parent)); + if( section->name[0] != '_' ) + { + return section->name; + } + } + return ""; + } + +/* Retain an immutable, function-local list of PERFORM sites. The key is + the pair already stored by the runtime, end_signature and dispatch_index. + Version 2 retains continuation entry addresses. Optimized code uses + nonlocal landing points to reload live state for a debugger-forced return. */ +static void +save_debug_perform_site(cbl_label_t *start, cbl_proc_t *end, size_t index, + tree continuation) + { + if( debug_info_level == DINFO_LEVEL_NONE ) + { + return; + } + + bool generated = start->name[0] == '_'; + int line = generated ? cobol_location().first_line + : current_function->debug_statement_line; + int column = generated ? cobol_location().first_column + : current_function->debug_statement_column; + const char *file = generated ? current_filename.back().c_str() + : current_function->debug_statement_file.c_str(); + tree head = current_function->debug_perform_sites; + tree record_type = TREE_TYPE(TREE_TYPE(head)); + static size_t serial = 0; + char name[80]; + snprintf(name, sizeof(name), "_gdb_perform_site_" HOST_SIZE_T_PRINT_DEC, + (fmt_size_t)++serial); + tree site = gg_define_variable(record_type, name, vs_static); + TREE_READONLY(site) = 1; + TREE_USED(site) = 1; + DECL_PRESERVE_P(site) = 1; + gg_structure_type_constructor(site, + build_int_cst_type(UINT, 2), + end->exit.addr, + build_int_cst_type(SIZE_T, index), + gg_get_address_of(continuation), + gg_cast(CONST_CHAR_P, gg_string_literal(current_function->our_unmangled_name)), + gg_cast(CONST_CHAR_P, gg_string_literal(file)), + gg_cast(CONST_CHAR_P, gg_string_literal(start->name)), + gg_cast(CONST_CHAR_P, gg_string_literal(end->label->name)), + gg_cast(CONST_CHAR_P, gg_string_literal(debug_perform_section(start))), + gg_cast(CONST_CHAR_P, gg_string_literal(debug_perform_section(end->label))), + build_int_cst_type(UINT, line), + build_int_cst_type(UINT, column), + build_int_cst_type(UINT, generated), + DECL_INITIAL(head), + NULL_TREE); + DECL_INITIAL(head) = gg_get_address_of(site); + current_function->debug_perform_records.push_back({site, start, end->label}); + } + +/* Forward-referenced paragraphs acquire their section at definition time. + Resolve qualifiers after every paragraph in this program has been parsed. */ +static void +finish_debug_perform_sites() + { + for( const auto &record : current_function->debug_perform_records ) + { + tree start_field = gg_find_field_in_struct(record.site, "start_section"); + tree end_field = gg_find_field_in_struct(record.site, "through_section"); + tree initializer = DECL_INITIAL(record.site); + for( unsigned int i = 0; i < CONSTRUCTOR_NELTS(initializer); i += 1 ) + { + constructor_elt *element = CONSTRUCTOR_ELT(initializer, i); + if( element->index == start_field ) + { + element->value = gg_cast(CONST_CHAR_P, + gg_string_literal(debug_perform_section(record.start))); + } + else if( element->index == end_field ) + { + element->value = gg_cast(CONST_CHAR_P, + gg_string_literal(debug_perform_section(record.end))); + } + } + } + } + +/* Each native invocation has this list in its lexical scope. All records + and the head are initialized by the compiler, with no runtime stores. */ +static void +create_debug_perform_sites() + { + if( debug_info_level == DINFO_LEVEL_NONE ) + { + return; + } + tree record_type = make_node(RECORD_TYPE); + tree type_decl = build_decl(UNKNOWN_LOCATION, TYPE_DECL, + get_identifier("__gg__debug_perform_site_v2"), + record_type); + TYPE_NAME(record_type) = type_decl; + TYPE_STUB_DECL(record_type) = type_decl; + DECL_ARTIFICIAL(type_decl) = 1; + tree pointer_type = build_pointer_type(record_type); + tree types[] = {UINT, VOID_P, SIZE_T, VOID_P, CONST_CHAR_P, CONST_CHAR_P, + CONST_CHAR_P, CONST_CHAR_P, CONST_CHAR_P, CONST_CHAR_P, + UINT, UINT, UINT, pointer_type}; + const char *names[] = {"version", "end_signature", "dispatch_index", + "continuation", "program", "file", "start", "through", + "start_section", "through_section", "line", "column", + "generated", "next"}; + tree *link = &TYPE_FIELDS(record_type); + for( size_t i = 0; i < ARRAY_SIZE(types); i += 1 ) + { + tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL, + get_identifier(names[i]), types[i]); + DECL_CONTEXT(field) = record_type; + *link = field; + link = &DECL_CHAIN(field); + } + layout_type(record_type); + tree head = gg_define_variable(pointer_type, "_gdb_perform_sites", vs_static, + gg_cast(pointer_type, null_pointer_node)); + TREE_READONLY(head) = 1; + TREE_USED(head) = 1; + DECL_PRESERVE_P(head) = 1; + current_function->debug_perform_sites = head; + } + +/* File catalogue ABI v1. Typed constant records preserve program ownership + of local, GLOBAL and EXTERNAL connectors without runtime publication code. + Flags 1, 2 and 4 denote GLOBAL, EXTERNAL and SD respectively. */ +static void +create_debug_file_catalogue() + { + if( debug_info_level == DINFO_LEVEL_NONE ) + { + return; + } + tree record_type = make_node(RECORD_TYPE); + tree type_decl = build_decl(UNKNOWN_LOCATION, TYPE_DECL, + get_identifier("__gg__debug_file_v1"), record_type); + TYPE_NAME(record_type) = type_decl; + TYPE_STUB_DECL(record_type) = type_decl; + DECL_ARTIFICIAL(type_decl) = 1; + tree pointer_type = build_pointer_type(record_type); + tree types[] = {UINT, UINT, CONST_CHAR_P, cblc_file_p_type_node, pointer_type}; + const char *names[] = {"version", "flags", "name", "connector", "next"}; + tree *link = &TYPE_FIELDS(record_type); + for( size_t i = 0; i < ARRAY_SIZE(types); i += 1 ) + { + tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL, + get_identifier(names[i]), types[i]); + DECL_CONTEXT(field) = record_type; + *link = field; + link = &DECL_CHAIN(field); + } + layout_type(record_type); + tree head = gg_define_variable(pointer_type, "_gdb_file_connectors", vs_static, + gg_cast(pointer_type, null_pointer_node)); + TREE_READONLY(head) = 1; + TREE_USED(head) = 1; + DECL_PRESERVE_P(head) = 1; + current_function->debug_file_catalogue = head; + } + +static void +save_debug_file_connector(const cbl_file_t *file, tree connector) + { + if( debug_info_level == DINFO_LEVEL_NONE ) + { + return; + } + tree head = current_function->debug_file_catalogue; + tree record_type = TREE_TYPE(TREE_TYPE(head)); + static size_t serial = 0; + char name[80]; + snprintf(name, sizeof(name), "_gdb_file_connector_" HOST_SIZE_T_PRINT_DEC, + (fmt_size_t)++serial); + tree record = gg_define_variable(record_type, name, vs_static); + TREE_READONLY(record) = 1; + TREE_USED(record) = 1; + DECL_PRESERVE_P(record) = 1; + unsigned int flags = (file->attr & global_e ? 1 : 0) + | (file->attr & external_e ? 2 : 0) + | (file->entry_type == sd_e ? 4 : 0); + gg_structure_type_constructor(record, + build_int_cst_type(UINT, 1), build_int_cst_type(UINT, flags), + gg_cast(CONST_CHAR_P, gg_string_literal(file->name)), + gg_get_address_of(connector), DECL_INITIAL(head), NULL_TREE); + DECL_INITIAL(head) = gg_get_address_of(record); + } + static void pseudo_return_push(cbl_proc_t *procedure, size_t index) { @@ -2674,12 +2880,24 @@ internal_perform_through( cbl_label_t *proc_1, void_type_node); DECL_CONTEXT(return_address_decl) = current_function->function_decl; TREE_USED(return_address_decl) = 1; + if( debug_info_level != DINFO_LEVEL_NONE && optimize ) + { + /* A debugger can transfer control here from any active paragraph. + Nonlocal-label semantics make the compiler spill/reload live state + across abnormal control transfers and retain a real landing point. + GDB restores this invocation's native registers before entering it. */ + DECL_NONLOCAL(return_address_decl) = 1; + DECL_STRUCT_FUNCTION(current_function->function_decl)->has_nonlocal_label = 1; + } free(psz); tree return_label_expr = build1(LABEL_EXPR, void_type_node, return_address_decl); + save_debug_perform_site(proc_1, proc2, dispatch_index, + return_address_decl); + // Put the dispatch_index for this PERFORM onto the stack pseudo_return_push(proc2, dispatch_index); @@ -3027,6 +3245,9 @@ enter_program_common(const char *funcname, const char *funcname_) funcname_, NULL_TREE); + create_debug_perform_sites(); + create_debug_file_catalogue(); + current_function->first_time_through = gg_define_variable(INT, "_first_time_through", @@ -3084,6 +3305,17 @@ enter_program_common(const char *funcname, const char *funcname_) ach, vs_static, integer_zero_node); + if( debug_info_level != DINFO_LEVEL_NONE ) + { + /* A constant debug pointer adds no instructions to program entry. The + debugger supplies it to runtime cleanup only when forcing a return. */ + tree marker = gg_define_variable(build_pointer_type(INT), + "_gdb_perform_return_counter", vs_static, + gg_get_address_of(current_function->called_by_main_counter)); + TREE_READONLY(marker) = 1; + TREE_USED(marker) = 1; + DECL_PRESERVE_P(marker) = 1; + } counter += 1; // Initialize the TRACE logic, which has to be done before the first TRACE1 @@ -3557,6 +3789,8 @@ parser_end_program(const char *prog_name ) // executed, and this generated code will never be executed parser_exit( cbl_refer_t() ); + finish_debug_perform_sites(); + // Tell the GCC compiler to do the GIMPLIFY thing. gg_finalize_function(); } @@ -7222,9 +7456,21 @@ parser_division(cbl_division_t division, // length. We establish those lengths based on the types of the target // for each USING. - gg_call(VOID, - "__gg__pseudo_return_bookmark", - NULL_TREE); + if( debug_info_level != DINFO_LEVEL_NONE ) + { + // One-based active bookmark identity, retained separately in every frame. + tree marker_type = build_qualified_type(SIZE_T, TYPE_QUAL_VOLATILE); + tree marker = gg_define_variable(marker_type, "_gdb_perform_invocation", + vs_stack); + TREE_THIS_VOLATILE(marker) = 1; + TREE_SIDE_EFFECTS(marker) = 1; + gg_assign(marker, gg_cast(marker_type, + gg_call_expr(SIZE_T, "__gg__debug_pseudo_return_bookmark", NULL_TREE))); + } + else + { + gg_call(VOID, "__gg__pseudo_return_bookmark", NULL_TREE); + } // The MODULE-NAME function requires a stack of program names. We push the // name on here. The first character is a 'T' or an 'N', where 'N' means @@ -9438,6 +9684,7 @@ parser_file_add(struct cbl_file_t *file) build_int_cst_type(INT, (int)file->codeset.alphabet), NULL_TREE); file->var_decl_node = new_var_decl; + save_debug_file_connector(file, new_var_decl); } void diff --git a/gcc/cobol/gengen.h b/gcc/cobol/gengen.h index ad5400c44d9c..6fcb54842cdf 100644 --- a/gcc/cobol/gengen.h +++ b/gcc/cobol/gengen.h @@ -189,6 +189,18 @@ struct gg_function_t tree first_time_through; tree saved_currency_state; // Static pointer to this program's immutable table. tree saved_currency_view; // Debugger-readable view for this program. + tree debug_perform_sites; // Immutable PERFORM-site list for this program. + tree debug_file_catalogue; // Immutable file connector list for this program. + int debug_statement_line = 0; + int debug_statement_column = 0; + std::string debug_statement_file; + struct debug_perform_site_t + { + tree site; + struct cbl_label_t *start; + struct cbl_label_t *end; + }; + std::vector<debug_perform_site_t> debug_perform_records; struct debug_field_binding_t { diff --git a/gcc/testsuite/cobol.dg/debug-file-catalogue-off.cob b/gcc/testsuite/cobol.dg/debug-file-catalogue-off.cob new file mode 100644 index 000000000000..ffd31dd9efbf --- /dev/null +++ b/gcc/testsuite/cobol.dg/debug-file-catalogue-off.cob @@ -0,0 +1,23 @@ +*> { dg-do compile } +*> { dg-options "-g0 -O2 -ffree-form" } +*> { dg-final { scan-assembler-not "_gdb_file_connectors" } } +*> { dg-final { scan-assembler-not "_gdb_file_connector_" } } +*> { dg-final { scan-assembler-not "__gg__debug_file_v1" } } +identification division. +program-id. debug-files. +environment division. +input-output section. +file-control. + select data-file assign to "debug-files.dat". +data division. +file section. +fd data-file external. +01 data-record pic x(8). +procedure division. + goback. +end program debug-files. +identification division. +program-id. empty-program. +procedure division. + goback. +end program empty-program. diff --git a/gcc/testsuite/cobol.dg/debug-file-catalogue.cob b/gcc/testsuite/cobol.dg/debug-file-catalogue.cob new file mode 100644 index 000000000000..8621d5787048 --- /dev/null +++ b/gcc/testsuite/cobol.dg/debug-file-catalogue.cob @@ -0,0 +1,23 @@ +*> { dg-do compile } +*> { dg-options "-g -O2 -ffree-form" } +*> { dg-final { scan-assembler "_gdb_file_connectors" } } +*> { dg-final { scan-assembler "_gdb_file_connector_" } } +*> { dg-final { scan-assembler "__gg__debug_file_v1" } } +identification division. +program-id. debug-files. +environment division. +input-output section. +file-control. + select data-file assign to "debug-files.dat". +data division. +file section. +fd data-file external. +01 data-record pic x(8). +procedure division. + goback. +end program debug-files. +identification division. +program-id. empty-program. +procedure division. + goback. +end program empty-program. diff --git a/gcc/testsuite/cobol.dg/debug-perform-sites-off.cob b/gcc/testsuite/cobol.dg/debug-perform-sites-off.cob new file mode 100644 index 000000000000..831d4c5b4195 --- /dev/null +++ b/gcc/testsuite/cobol.dg/debug-perform-sites-off.cob @@ -0,0 +1,18 @@ +*> { dg-do compile } +*> { dg-options "-g0 -O2 -ffree-form" } +*> { dg-final { scan-assembler-not "_gdb_perform_" } } +*> { dg-final { scan-assembler-not "__gg__debug_pseudo_return_bookmark" } } +*> { dg-final { scan-assembler "__gg__pseudo_return_bookmark" } } +identification division. +program-id. debug-perform. +data division. +working-storage section. +01 total binary-long value 0. +procedure division. + perform worker 2 times + goback. +worker-section section. +worker. + add 1 to total + exit paragraph. +end program debug-perform. diff --git a/gcc/testsuite/cobol.dg/debug-perform-sites.cob b/gcc/testsuite/cobol.dg/debug-perform-sites.cob new file mode 100644 index 000000000000..8ea657c9cebe --- /dev/null +++ b/gcc/testsuite/cobol.dg/debug-perform-sites.cob @@ -0,0 +1,19 @@ +*> { dg-do compile } +*> { dg-options "-g -O2 -ffree-form" } +*> { dg-final { scan-assembler "_gdb_perform_sites" } } +*> { dg-final { scan-assembler "_gdb_perform_invocation" } } +*> { dg-final { scan-assembler "__gg__debug_pseudo_return_bookmark" } } +*> { dg-final { scan-assembler "_gdb_perform_return_counter" } } +identification division. +program-id. debug-perform. +data division. +working-storage section. +01 total binary-long value 0. +procedure division. + perform worker 2 times + goback. +worker-section section. +worker. + add 1 to total + exit paragraph. +end program debug-perform. diff --git a/libgcobol/Makefile.am b/libgcobol/Makefile.am index 402608fd68ed..67358a12adda 100644 --- a/libgcobol/Makefile.am +++ b/libgcobol/Makefile.am @@ -82,6 +82,18 @@ libgcobol_la_SOURCES = \ valconv.cc \ xmlparse.cc +# Native runtime ABI regression, including vector relocation and rollback. +check_PROGRAMS = perform-stack-test +perform_stack_test_SOURCES = testsuite/perform-stack.cc perform-stack.h +perform_stack_test_LDADD = libgcobol.la + +check-local: perform-stack-test$(EXEEXT) + @if test "$(build)" = "$(target)"; then \ + $(LIBTOOL) --mode=execute ./perform-stack-test$(EXEEXT); \ + else \ + echo "UNSUPPORTED: PERFORM-stack runtime test requires native execution"; \ + fi + libgcobol_compat_gnu_la_SOURCES = \ compat/gnu/lib/cobrt-file-status.cbl \ compat/gnu/lib/stored-char-length.cbl \ diff --git a/libgcobol/Makefile.in b/libgcobol/Makefile.in index 9ad6aba9f810..a1fccc70876a 100644 --- a/libgcobol/Makefile.in +++ b/libgcobol/Makefile.in @@ -112,6 +112,7 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ target_triplet = @target@ +@BUILD_LIBGCOBOL_TRUE@check_PROGRAMS = perform-stack-test$(EXEEXT) # Handle embedded rpaths for Darwin. @BUILD_LIBGCOBOL_TRUE@@ENABLE_DARWIN_AT_RPATH_TRUE@am__append_1 = -Wc,-nodefaultrpaths \ @@ -231,6 +232,14 @@ libgcobol_posix_la_LIBADD = libgcobol_posix_la_OBJECTS = $(am_libgcobol_posix_la_OBJECTS) @BUILD_LIBGCOBOL_TRUE@am_libgcobol_posix_la_rpath = -rpath \ @BUILD_LIBGCOBOL_TRUE@ $(toolexeclibdir) +@BUILD_LIBGCOBOL_TRUE@am_perform_stack_test_OBJECTS = \ +@BUILD_LIBGCOBOL_TRUE@ testsuite/perform-stack.$(OBJEXT) +perform_stack_test_OBJECTS = $(am_perform_stack_test_OBJECTS) +@BUILD_LIBGCOBOL_TRUE@perform_stack_test_DEPENDENCIES = libgcobol.la +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) am__v_P_0 = false @@ -249,10 +258,6 @@ am__depfiles_maybe = depfiles am__mv = mv -f CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -AM_V_lt = $(am__v_lt_@AM_V@) -am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) -am__v_lt_0 = --silent -am__v_lt_1 = LTCXXCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) \ $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ @@ -269,8 +274,26 @@ AM_V_CXXLD = $(am__v_CXXLD_@AM_V@) am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@) am__v_CXXLD_0 = @echo " CXXLD " $@; am__v_CXXLD_1 = +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = SOURCES = $(libgcobol_la_SOURCES) $(libgcobol_compat_gnu_la_SOURCES) \ - $(libgcobol_posix_la_SOURCES) + $(libgcobol_posix_la_SOURCES) $(perform_stack_test_SOURCES) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -504,6 +527,8 @@ gcc_version := $(shell @get_gcc_base_ver@ $(top_srcdir)/../gcc/BASE-VER) @BUILD_LIBGCOBOL_TRUE@ valconv.cc \ @BUILD_LIBGCOBOL_TRUE@ xmlparse.cc +@BUILD_LIBGCOBOL_TRUE@perform_stack_test_SOURCES = testsuite/perform-stack.cc perform-stack.h +@BUILD_LIBGCOBOL_TRUE@perform_stack_test_LDADD = libgcobol.la @BUILD_LIBGCOBOL_TRUE@libgcobol_compat_gnu_la_SOURCES = \ @BUILD_LIBGCOBOL_TRUE@ compat/gnu/lib/cobrt-file-status.cbl \ @BUILD_LIBGCOBOL_TRUE@ compat/gnu/lib/stored-char-length.cbl \ @@ -833,6 +858,27 @@ posix/udf/posix-clock-gettime.lo: posix/udf/$(am__dirstamp) \ libgcobol_posix.la: $(libgcobol_posix_la_OBJECTS) $(libgcobol_posix_la_DEPENDENCIES) $(EXTRA_libgcobol_posix_la_DEPENDENCIES) $(AM_V_GEN)$(libgcobol_posix_la_LINK) $(am_libgcobol_posix_la_rpath) $(libgcobol_posix_la_OBJECTS) $(libgcobol_posix_la_LIBADD) $(LIBS) +clean-checkPROGRAMS: + @list='$(check_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list +testsuite/$(am__dirstamp): + @$(MKDIR_P) testsuite + @: > testsuite/$(am__dirstamp) +testsuite/$(DEPDIR)/$(am__dirstamp): + @$(MKDIR_P) testsuite/$(DEPDIR) + @: > testsuite/$(DEPDIR)/$(am__dirstamp) +testsuite/perform-stack.$(OBJEXT): testsuite/$(am__dirstamp) \ + testsuite/$(DEPDIR)/$(am__dirstamp) + +perform-stack-test$(EXEEXT): $(perform_stack_test_OBJECTS) $(perform_stack_test_DEPENDENCIES) $(EXTRA_perform_stack_test_DEPENDENCIES) + @rm -f perform-stack-test$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(perform_stack_test_OBJECTS) $(perform_stack_test_LDADD) $(LIBS) + mostlyclean-compile: -rm -f *.$(OBJEXT) -rm -f compat/gnu/lib/*.$(OBJEXT) @@ -841,6 +887,7 @@ mostlyclean-compile: -rm -f posix/shim/*.lo -rm -f posix/udf/*.$(OBJEXT) -rm -f posix/udf/*.lo + -rm -f testsuite/*.$(OBJEXT) distclean-compile: -rm -f *.tab.c @@ -865,6 +912,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/lseek.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/open.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/stat.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@testsuite/$(DEPDIR)/perform-stack.Po@am__quote@ .cc.o: @am__fastdepCXX_TRUE@ $(AM_V_CXX)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ @@ -1005,7 +1053,10 @@ cscopelist-am: $(am__tagged_files) distclean-tags: -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags -rm -f cscope.out cscope.in.out cscope.po.out cscope.files +@BUILD_LIBGCOBOL_FALSE@check-local: check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) $(check_PROGRAMS) + $(MAKE) $(AM_MAKEFLAGS) check-local check: check-am all-am: Makefile $(LTLIBRARIES) $(DATA) $(HEADERS) config.h installdirs: @@ -1045,18 +1096,20 @@ distclean-generic: -rm -f posix/shim/$(am__dirstamp) -rm -f posix/udf/$(DEPDIR)/$(am__dirstamp) -rm -f posix/udf/$(am__dirstamp) + -rm -f testsuite/$(DEPDIR)/$(am__dirstamp) + -rm -f testsuite/$(am__dirstamp) maintainer-clean-generic: @echo "This command is intended for maintainers to use" @echo "it deletes files that may require special tools to rebuild." clean: clean-am -clean-am: clean-generic clean-libtool clean-toolexeclibLTLIBRARIES \ - mostlyclean-am +clean-am: clean-checkPROGRAMS clean-generic clean-libtool \ + clean-toolexeclibLTLIBRARIES mostlyclean-am distclean: distclean-am -rm -f $(am__CONFIG_DISTCLEAN_FILES) - -rm -rf ./$(DEPDIR) compat/gnu/lib/$(DEPDIR) posix/shim/$(DEPDIR) + -rm -rf ./$(DEPDIR) compat/gnu/lib/$(DEPDIR) posix/shim/$(DEPDIR) testsuite/$(DEPDIR) -rm -f Makefile distclean-am: clean-am distclean-compile distclean-generic \ distclean-hdr distclean-libtool distclean-tags @@ -1105,7 +1158,7 @@ installcheck-am: maintainer-clean: maintainer-clean-am -rm -f $(am__CONFIG_DISTCLEAN_FILES) -rm -rf $(top_srcdir)/autom4te.cache - -rm -rf ./$(DEPDIR) compat/gnu/lib/$(DEPDIR) posix/shim/$(DEPDIR) + -rm -rf ./$(DEPDIR) compat/gnu/lib/$(DEPDIR) posix/shim/$(DEPDIR) testsuite/$(DEPDIR) -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic @@ -1125,24 +1178,25 @@ ps-am: uninstall-am: uninstall-nobase_libsubincludeHEADERS \ uninstall-toolexeclibDATA uninstall-toolexeclibLTLIBRARIES -.MAKE: all install-am install-strip - -.PHONY: CTAGS GTAGS TAGS all all-am am--refresh check check-am clean \ - clean-cscope clean-generic clean-libtool \ - clean-toolexeclibLTLIBRARIES cscope cscopelist-am ctags \ - ctags-am distclean distclean-compile distclean-generic \ - distclean-hdr distclean-libtool distclean-tags dvi dvi-am html \ - html-am info info-am install install-am install-data \ - install-data-am install-dvi install-dvi-am install-exec \ - install-exec-am install-html install-html-am install-info \ - install-info-am install-man \ - install-nobase_libsubincludeHEADERS install-pdf install-pdf-am \ - install-ps install-ps-am install-strip install-toolexeclibDATA \ - install-toolexeclibLTLIBRARIES installcheck installcheck-am \ - installdirs maintainer-clean maintainer-clean-generic \ - mostlyclean mostlyclean-compile mostlyclean-generic \ - mostlyclean-libtool pdf pdf-am ps ps-am tags tags-am uninstall \ - uninstall-am uninstall-nobase_libsubincludeHEADERS \ +.MAKE: all check-am install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am am--refresh check check-am \ + check-local clean clean-checkPROGRAMS clean-cscope \ + clean-generic clean-libtool clean-toolexeclibLTLIBRARIES \ + cscope cscopelist-am ctags ctags-am distclean \ + distclean-compile distclean-generic distclean-hdr \ + distclean-libtool distclean-tags dvi dvi-am html html-am info \ + info-am install install-am install-data install-data-am \ + install-dvi install-dvi-am install-exec install-exec-am \ + install-html install-html-am install-info install-info-am \ + install-man install-nobase_libsubincludeHEADERS install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + install-toolexeclibDATA install-toolexeclibLTLIBRARIES \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags tags-am uninstall uninstall-am \ + uninstall-nobase_libsubincludeHEADERS \ uninstall-toolexeclibDATA uninstall-toolexeclibLTLIBRARIES .PRECIOUS: Makefile @@ -1156,6 +1210,13 @@ uninstall-am: uninstall-nobase_libsubincludeHEADERS \ @BUILD_LIBGCOBOL_TRUE@ $(SHELL) $(srcdir)/mk-rpath-spec.sh "$(toolexeclibdir)" < $< > [email protected] @BUILD_LIBGCOBOL_TRUE@ mv [email protected] $@ +@BUILD_LIBGCOBOL_TRUE@check-local: perform-stack-test$(EXEEXT) +@BUILD_LIBGCOBOL_TRUE@ @if test "$(build)" = "$(target)"; then \ +@BUILD_LIBGCOBOL_TRUE@ $(LIBTOOL) --mode=execute ./perform-stack-test$(EXEEXT); \ +@BUILD_LIBGCOBOL_TRUE@ else \ +@BUILD_LIBGCOBOL_TRUE@ echo "UNSUPPORTED: PERFORM-stack runtime test requires native execution"; \ +@BUILD_LIBGCOBOL_TRUE@ fi + @BUILD_LIBGCOBOL_TRUE@$(LC_COPYDIR_BUILD)/%.cpy: ${srcdir}/compat/gnu/cpy/%.cpy @BUILD_LIBGCOBOL_TRUE@ mkdir -p `dirname $@` @BUILD_LIBGCOBOL_TRUE@ cp $< $@ diff --git a/libgcobol/libgcobol.cc b/libgcobol/libgcobol.cc index 8ee2dc6850c0..c22bdeab07dc 100644 --- a/libgcobol/libgcobol.cc +++ b/libgcobol/libgcobol.cc @@ -59,6 +59,7 @@ #include "config.h" #include "libgcobol-fp.h" +#include "perform-stack.h" #include "ec.h" #include "common-defs.h" @@ -10435,14 +10436,79 @@ __gg__match_exception( cblc_field_t *index ) static std::vector<void *>proc_signatures; static std::vector<size_t>return_addresses; static std::vector<size_t>bookmarks; +static std::vector<std::string>module_name_stack; + +/* Saved once per invocation. Normal entry and exit remain constant-time + apart from vector growth. Debugger unwinding consumes only discarded + invocations and the local descriptors owned by them. */ +struct perform_invocation_cleanup + { + size_t program_states; + size_t module_names; + size_t unique_id; + }; +static std::vector<perform_invocation_cleanup> perform_cleanups; + + +static_assert(sizeof(uintptr_t) == sizeof(size_t)); +static_assert(offsetof(gg_debug_perform_stack_v1, signatures) == 16); +static_assert(sizeof(gg_debug_perform_stack_v1) == 16 + 6 * sizeof(uintptr_t)); + +static struct gg_debug_perform_stack_v1 perform_stack_view = + { 1, sizeof(gg_debug_perform_stack_v1), 0, sizeof(uintptr_t), + 0, 0, 0, 0, 0, 0 }; + +extern "C" { +const struct gg_debug_perform_stack_v1 *const __gg__debug_perform_stack + = &perform_stack_view; +} + +/* Publication has fixed cost independently of stack depth. Keep the + in-progress marker visible before buffer mutation, and publish the + completed view with release ordering. The guard also publishes a + coherent view after an allocation exception. */ +class perform_stack_update + { + uint32_t generation; +public: + perform_stack_update() + : generation(__atomic_load_n(&perform_stack_view.generation, + __ATOMIC_RELAXED)) + { + __atomic_store_n(&perform_stack_view.generation, generation + 1, + __ATOMIC_RELAXED); + __atomic_thread_fence(__ATOMIC_RELEASE); + } + + ~perform_stack_update() + { + auto &view = perform_stack_view; + view.signatures = reinterpret_cast<uintptr_t>(proc_signatures.data()); + view.signature_count = proc_signatures.size(); + view.indexes = reinterpret_cast<uintptr_t>(return_addresses.data()); + view.index_count = return_addresses.size(); + view.bookmarks = reinterpret_cast<uintptr_t>(bookmarks.data()); + view.bookmark_count = bookmarks.size(); + __atomic_store_n(&view.generation, generation + 2, __ATOMIC_RELEASE); + } + }; extern "C" void __gg__pseudo_return_push( void *proc_signature, size_t index) { + perform_stack_update update; proc_signatures.push_back(proc_signature); - return_addresses.push_back(index); + try + { + return_addresses.push_back(index); + } + catch(...) + { + proc_signatures.pop_back(); + throw; + } __gg__exit_address = proc_signature; } @@ -10450,6 +10516,7 @@ extern "C" size_t __gg__pseudo_return_pop() { + perform_stack_update update; size_t retval = return_addresses.back(); return_addresses.pop_back(); @@ -10472,7 +10539,30 @@ extern "C" void __gg__pseudo_return_bookmark() { - bookmarks.push_back(proc_signatures.size()); + perform_stack_update update; + perform_cleanups.push_back({program_states.size(), module_name_stack.size(), + __gg__unique_prog_id}); + try + { + bookmarks.push_back(proc_signatures.size()); + } + catch(...) + { + perform_cleanups.pop_back(); + throw; + } + } + +/* Debug builds retain this one-based bookmark identity in the native frame. + It identifies an active invocation, including one with no PERFORM entries. + Identities can be reused after an invocation returns. Existing callers of + the void bookmark entry point retain their ABI and behavior. */ +extern "C" +size_t +__gg__debug_pseudo_return_bookmark() + { + __gg__pseudo_return_bookmark(); + return bookmarks.size(); } extern "C" @@ -10483,9 +10573,11 @@ __gg__pseudo_return_flush() { __gg__abort("__gg__pseudo_return_flush(): bookmarks.size() is zero"); } + perform_stack_update update; proc_signatures.resize(bookmarks.back()); return_addresses.resize(bookmarks.back()); bookmarks.pop_back(); + perform_cleanups.pop_back(); if( proc_signatures.size() ) { @@ -11030,6 +11122,86 @@ __gg__pop_local_variables() } } +/* Remove activations bypassed by a debugger's forced return. NATIVE + discards INVOCATION itself, otherwise DEPTH is the retained PERFORM depth + within it. Return 1 for invalid/transient state and 2 for older compiler + instrumentation. Validate the entire request before any mutation. + No allocation, inferior callback, or exception is needed after validation. */ +extern "C" +int +__gg__debug_perform_unwind(size_t invocation, size_t depth, int native, ...) + { + if( invocation == 0 || invocation > bookmarks.size() + || perform_cleanups.size() != bookmarks.size() + || proc_signatures.size() != return_addresses.size() + || (perform_stack_view.generation & 1) ) + { + return 1; + } + const size_t base = bookmarks[invocation - 1]; + const size_t end = invocation == bookmarks.size() + ? proc_signatures.size() : bookmarks[invocation]; + if( base > end || end > proc_signatures.size() || depth > end - base + || (native && depth != 0) ) + { + return 1; + } + const auto saved = perform_cleanups[invocation - 1]; + const size_t kept = native ? invocation - 1 : invocation; + if( saved.unique_id == 0 || saved.program_states == 0 + || saved.program_states > program_states.size() + || saved.module_names + (native ? 0 : 1) > module_name_stack.size() + || perform_cleanups.back().unique_id != __gg__unique_prog_id ) + { + return 1; + } + va_list counters; + va_start(counters, native); + int status = 0; + for( size_t i = perform_cleanups.size(); i > kept; i -= 1 ) + { + const auto &entry = perform_cleanups[i - 1]; + if( !va_arg(counters, int *) ) + { + status = 2; + } + if( entry.unique_id != saved.unique_id + i - invocation ) + { + status = 1; + } + } + va_end(counters); + if( status ) + { + return status; + } + perform_stack_update update; + va_start(counters, native); + for( size_t i = perform_cleanups.size(); i > kept; i -= 1 ) + { + int *called_by_main = va_arg(counters, int *); + __gg__pop_local_variables(); + __gg__unique_prog_id -= 1; + if( *called_by_main > 0 ) + { + *called_by_main -= 1; + } + } + va_end(counters); + while( program_states.size() > saved.program_states ) + { + __gg__pop_program_state(); + } + module_name_stack.resize(saved.module_names + (native ? 0 : 1)); + proc_signatures.resize(base + depth); + return_addresses.resize(base + depth); + bookmarks.resize(kept); + perform_cleanups.resize(kept); + __gg__exit_address = kept && proc_signatures.size() > bookmarks.back() + ? proc_signatures.back() : nullptr; + return 0; + } + extern "C" void __gg__copy_as_big_endian( unsigned char *dest, @@ -12060,7 +12232,7 @@ __gg__allocate( cblc_field_t *first, } } -static std::vector<std::string>module_name_stack; + extern "C" void diff --git a/libgcobol/libgcobol.map b/libgcobol/libgcobol.map index 8d6f9e1e6bed..599dfdb9007f 100644 --- a/libgcobol/libgcobol.map +++ b/libgcobol/libgcobol.map @@ -65,6 +65,9 @@ GCOBOL_17.1 __gg__deallocate; __gg__decimal_point_is_comma; __gg__debug_currency_signs; + __gg__debug_perform_stack; + __gg__debug_pseudo_return_bookmark; + __gg__debug_perform_unwind; __gg__default_compute_error; __gg__dialects; __gg__display; diff --git a/libgcobol/perform-stack.h b/libgcobol/perform-stack.h new file mode 100644 index 000000000000..dddc0b9f9a0e --- /dev/null +++ b/libgcobol/perform-stack.h @@ -0,0 +1,57 @@ +/* Copyright (C) 2026 Free Software Foundation, Inc. + This file is part of GCC, distributed under the GNU General Public License, + version 3 or (at your option) any later version. See COPYING3. */ + +#ifndef LIBGCOBOL_PERFORM_STACK_H +#define LIBGCOBOL_PERFORM_STACK_H + +#include <stddef.h> +#include <stdint.h> + +/* Debugger ABI v1. The 16-byte header is followed by six native unsigned + address/count words. Addresses are integer representations of existing + buffers. WORD_SIZE is sizeof (uintptr_t), equal to sizeof (size_t). + BYTE_SIZE is 16 + 6 * WORD_SIZE, giving 64 bytes on a 64-bit target. + + The process-global stacks retain their existing serialized-writer model. + An odd generation marks an update in progress. A reader accepts data + only when generation is even and unchanged across its complete read. + Buffers can move or shrink during any update. A stopped odd generation + requires a diagnostic or native-frame fallback, without retrying in a + loop. The descriptor and all entries use the target's native byte order. + The exported constant pointer addresses this descriptor for its lifetime. + Reading needs neither runtime DWARF nor an inferior function call. */ +struct gg_debug_perform_stack_v1 + { + uint32_t version; + uint32_t byte_size; + uint32_t generation; + uint32_t word_size; + uintptr_t signatures; + size_t signature_count; + uintptr_t indexes; + size_t index_count; + uintptr_t bookmarks; + size_t bookmark_count; + }; + +#ifdef __cplusplus +extern "C" { +#endif +extern const struct gg_debug_perform_stack_v1 *const __gg__debug_perform_stack; +/* Forced-return ABI. INVOCATION is one-based. For NATIVE != 0 discard + that native invocation and every deeper one, with DEPTH == 0. Otherwise + retain that invocation and its outermost DEPTH PERFORMs. Restore local + descriptors, module names, program/currency state and main-entry counters. + Success is 0. Invalid/transient requests return 1, older instrumentation + returns 2. Rejected requests leave all runtime state unchanged. Unlike + stack inspection, this operation requires an inferior function call. */ +/* Supply one int* main-entry counter for each discarded native invocation, + in innermost-to-outermost order, as variadic arguments. */ +extern int __gg__debug_perform_unwind(size_t invocation, size_t depth, int native, ...); + +#ifdef __cplusplus +} +#endif + +#endif
