diff --git a/cobc/codegen.c b/cobc/codegen.c index 7e6e26867..2dd0c7520 100644 --- a/cobc/codegen.c +++ b/cobc/codegen.c @@ -4374,24 +4374,24 @@ output_funcall (cb_tree x) output ("cob_prof_enter_procedure (prof_info, %d);", proc_idx); output_newline (); output_prefix (); - output ("cob_prof_fallthrough_label = 0"); + output ("fallthrough_label = 0"); break; case COB_PROF_USE_PARAGRAPH_ENTRY: { int paragraph_idx = CB_INTEGER(p->argv[1])->val; int entry_idx = CB_INTEGER(p->argv[2])->val; - output ("if (!cob_prof_fallthrough_label)"); + output ("if (!fallthrough_label)"); output_block_open (); output_line ("cob_prof_use_paragraph_entry (prof_info, %d, %d);", paragraph_idx, entry_idx); output_block_close (); output_line ("else"); output_block_open (); - output_line ("cob_prof_fallthrough_label = 0;"); + output_line ("fallthrough_label = 0;"); output_block_close (); break; } case COB_PROF_STAYIN_PARAGRAPH: - output ("cob_prof_fallthrough_label = 1"); + output ("fallthrough_label = 1"); break; } return; @@ -13782,7 +13782,7 @@ output_cob_prof_data ( struct cb_program * program ) } output_local ("};\n"); - output_local ("static int cob_prof_fallthrough_label = 0;\n"); + output_local ("static int fallthrough_label = 0;\n"); output_local ("static struct cob_prof_module *prof_info;\n"); output_local ("\n/* End of cob_prof data */\n"); diff --git a/libcob/ChangeLog b/libcob/ChangeLog index 14bed86a2..d5333b6e7 100644 --- a/libcob/ChangeLog +++ b/libcob/ChangeLog @@ -11,6 +11,8 @@ $f (executable filename), $d (date in yyyymmdd) and $t (time in hhmmss) * common.c (cob_set_main_argv0): extracted from cob_init + * fileio.c (cob_path_to_absolute): extracted from insert + and cob_set_main_argv0 2024-01-25 David Declerck diff --git a/libcob/call.c b/libcob/call.c index 9df2cfd4d..bae84f135 100644 --- a/libcob/call.c +++ b/libcob/call.c @@ -661,25 +661,9 @@ insert (const char *name, void *func, lt_dlhandle handle, p->func = func; p->handle = handle; p->module = module; - if (path) { -#if defined(HAVE_CANONICALIZE_FILE_NAME) - /* Malloced path or NULL */ - p->path = canonicalize_file_name (path); -#elif defined(HAVE_REALPATH) - char *s; - s = cob_malloc ((size_t)COB_NORMAL_BUFF); - if (realpath (path, s) != NULL) { - p->path = cob_strdup (s); - } - cob_free (s); -#elif defined (_WIN32) - /* Malloced path or NULL */ - p->path = _fullpath (NULL, path, 1); -#endif - if (!p->path) { - p->path = cob_strdup (path); - } + if (path) { + p->path = cob_path_to_absolute (path); } p->no_phys_cancel = nocanc; val = hash ((const unsigned char *)name); diff --git a/libcob/coblocal.h b/libcob/coblocal.h index 51fe7565b..46dfcc576 100644 --- a/libcob/coblocal.h +++ b/libcob/coblocal.h @@ -583,6 +583,9 @@ COB_HIDDEN int cob_cmps (const unsigned char *, const unsigned char *, COB_HIDDEN FILE * cob_open_logfile (const char *filename); +/* Whether we are in testsuite mode */ +COB_HIDDEN int is_test; + #undef COB_HIDDEN #endif /* COB_LOCAL_H */ diff --git a/libcob/common.c b/libcob/common.c index aa2c2968e..6b47f3f12 100644 --- a/libcob/common.c +++ b/libcob/common.c @@ -333,6 +333,8 @@ static const unsigned char *sort_collate = NULL; static const char *cob_source_file = NULL; static unsigned int cob_source_line = 0; +int is_test = 0; + #ifdef HAVE_DESIGNATED_INITS const char *cob_statement_name[STMT_MAX_ENTRY] = { [STMT_UNKNOWN] = "UNKNOWN" @@ -7864,7 +7866,11 @@ cob_expand_env_string (const char *strval) const char *s = NULL; switch ( strval[k+1] ){ case '$': /* Replace $$ with process-id */ - j += sprintf (&env[j], "%d", cob_sys_getpid()); + if (is_test) { + j += sprintf (&env[j], "%d", 123456); + } else { + j += sprintf (&env[j], "%d", cob_sys_getpid()); + } k++; break; case 'f': /* $f is the executable filename */ @@ -10204,22 +10210,7 @@ void cob_set_main_argv0 (const int argc, char **argv) #endif if (argc && argv && argv[0]) { -#if defined (HAVE_CANONICALIZE_FILE_NAME) - /* Returns malloced path or NULL */ - cobglobptr->cob_main_argv0 = canonicalize_file_name (argv[0]); -#elif defined (HAVE_REALPATH) - s = cob_malloc ((size_t)COB_LARGE_BUFF); - if (realpath (argv[0], s) != NULL) { - cobglobptr->cob_main_argv0 = cob_strdup (s); - } - cob_free (s); -#elif defined (_WIN32) - /* Returns malloced path or NULL */ - cobglobptr->cob_main_argv0 = _fullpath (NULL, argv[0], 1); -#endif - if (!cobglobptr->cob_main_argv0) { - cobglobptr->cob_main_argv0 = cob_strdup (argv[0]); - } + cobglobptr->cob_main_argv0 = cob_path_to_absolute (argv[0]); } else { cobglobptr->cob_main_argv0 = cob_strdup (_("unknown")); } @@ -10289,6 +10280,8 @@ cob_init (const int argc, char **argv) cob_initialized = 1; + is_test = !!getenv ("COB_IS_RUNNING_IN_TESTMODE"); + #ifdef HAVE_SETLOCALE /* Prime the locale from user settings */ s = setlocale (LC_ALL, ""); diff --git a/libcob/common.h b/libcob/common.h index 32e09c194..b49d78f89 100644 --- a/libcob/common.h +++ b/libcob/common.h @@ -2680,6 +2680,8 @@ COB_EXPIMP void cob_file_sort_giving_extfh (cob_file *, const size_t, ...); COB_EXPIMP void cob_file_release (cob_file *); COB_EXPIMP void cob_file_return (cob_file *); +COB_EXPIMP char * cob_path_to_absolute (const char *path); + /***************************/ /* Functions in reportio.c */ /***************************/ diff --git a/libcob/fileio.c b/libcob/fileio.c index 0670d792a..c39b3403a 100644 --- a/libcob/fileio.c +++ b/libcob/fileio.c @@ -10754,3 +10754,31 @@ EXTFH3 (unsigned char *opcode, FCD3 *fcd) } return sts; } + + +char * +cob_path_to_absolute (const char *path) +{ + char *abs_path = NULL; + if (path) { +#if defined(HAVE_CANONICALIZE_FILE_NAME) + /* Returns malloced path or NULL */ + abs_path = canonicalize_file_name (path); +#elif defined(HAVE_REALPATH) + char *s; + + s = cob_malloc ((size_t)COB_NORMAL_BUFF); + if (realpath (path, s) != NULL) { + abs_path = cob_strdup (s); + } + cob_free (s); +#elif defined (_WIN32) + /* Returns malloced path or NULL */ + abs_path = _fullpath (NULL, path, 1); +#endif + if (!abs_path) { + abs_path = cob_strdup (path); + } + } + return abs_path; +} diff --git a/libcob/profiling.c b/libcob/profiling.c index c428cc277..7f6285301 100644 --- a/libcob/profiling.c +++ b/libcob/profiling.c @@ -63,8 +63,6 @@ static int current_idx = -1; /* Whether profiling is active or not. */ static int is_active = 0; -/* Whether we are in testsuite mode */ -static int is_test = 0; /* Which clock to use for clock_gettime (if available) */ #ifdef HAVE_CLOCK_GETTIME @@ -155,9 +153,6 @@ prof_init_static () if (!init_done && cobsetptr) { prof_setup_clock (); is_active = cobsetptr->cob_prof_enable; - if (is_active) { - is_test = !!getenv ("COB_IS_RUNNING_IN_TESTMODE"); - } init_done = 1; } } @@ -460,7 +455,7 @@ cob_prof_print_line ( case 'i': if (info){ if (is_test){ - fprintf (file, "%d", 42); + fprintf (file, "%d", 123456); } else { fprintf (file, "%d", cob_sys_getpid()); } diff --git a/tests/testsuite.src/run_misc.at b/tests/testsuite.src/run_misc.at index 1e0534d57..34686150f 100644 --- a/tests/testsuite.src/run_misc.at +++ b/tests/testsuite.src/run_misc.at @@ -14675,26 +14675,30 @@ prog.cob:11: warning: GO TO SECTION '2ND' AT_CHECK([COB_PROF_ENABLE=0 COB_PROF_FILE='prof-$b.csv' $COBCRUN_DIRECT ./prog], [0], [], []) -AT_CHECK([COB_PROF_ENABLE=1 COB_PROF_FILE='prof-$b.csv' COB_PROF_FORMAT=%i,%m,%s,%p,%e,%f,%l,%w,%k,%t,%h,%n,%x $COBCRUN_DIRECT ./prog], [0], [], -[File prof-prog.csv generated +# Specific test for $f, which is an absolute file name +AT_CHECK([COB_PROF_ENABLE=1 COB_PROF_FILE='$f-prof.csv' $COBCRUN_DIRECT ./prog], [0], [], [ignore]) +AT_CHECK([ls prog-prof.csv], [0], [ignore], []) + +AT_CHECK([COB_PROF_ENABLE=1 COB_PROF_FILE='prof-$$-$b.csv' COB_PROF_FORMAT=%i,%m,%s,%p,%e,%f,%l,%w,%k,%t,%h,%n,%x $COBCRUN_DIRECT ./prog], [0], [], +[File prof-123456-prog.csv generated ]) # note: The time here is actually the number of times the procedure has # been run, to avoid any indeterminism in the running time of the # procedure. -AT_CHECK([cat prof-prog.csv], [0], +AT_CHECK([cat prof-123456-prog.csv], [0], [pid,program-id,section,paragraph,entry,file,line,location,kind,time-ns,time,ncalls,%x -42,prog,,,,prog.cob,4,prog.cob:4,PROGRAM,13000000,0.013 s,1,%x -42,prog,1ST,,,prog.cob,5,prog.cob:5,SECTION,12000000,0.012 s,1,%x -42,prog,1ST,PARA-0001,,prog.cob,6,prog.cob:6,PARAGRAPH,11000000,0.011 s,1,%x -42,prog,1ST,PARA-0002,,prog.cob,8,prog.cob:8,PARAGRAPH,0,0.000 s,0,%x -42,prog,1ST,PARA-0003,,prog.cob,10,prog.cob:10,PARAGRAPH,1000000,0.001 s,1,%x -42,prog,1ST,PARA-0004,,prog.cob,12,prog.cob:12,PARAGRAPH,0,0.000 s,0,%x -42,prog,2ND,,,prog.cob,14,prog.cob:14,SECTION,6000000,0.006 s,1,%x -42,prog,2ND,PARA-0005,,prog.cob,15,prog.cob:15,PARAGRAPH,3000000,0.003 s,1,%x -42,prog,2ND,PARA-0006,,prog.cob,17,prog.cob:17,PARAGRAPH,2000000,0.002 s,2,%x -42,prog,2ND,PARA-0007,,prog.cob,19,prog.cob:19,PARAGRAPH,1000000,0.001 s,1,%x +123456,prog,,,,prog.cob,4,prog.cob:4,PROGRAM,13000000,0.013 s,1,%x +123456,prog,1ST,,,prog.cob,5,prog.cob:5,SECTION,12000000,0.012 s,1,%x +123456,prog,1ST,PARA-0001,,prog.cob,6,prog.cob:6,PARAGRAPH,11000000,0.011 s,1,%x +123456,prog,1ST,PARA-0002,,prog.cob,8,prog.cob:8,PARAGRAPH,0,0.000 s,0,%x +123456,prog,1ST,PARA-0003,,prog.cob,10,prog.cob:10,PARAGRAPH,1000000,0.001 s,1,%x +123456,prog,1ST,PARA-0004,,prog.cob,12,prog.cob:12,PARAGRAPH,0,0.000 s,0,%x +123456,prog,2ND,,,prog.cob,14,prog.cob:14,SECTION,6000000,0.006 s,1,%x +123456,prog,2ND,PARA-0005,,prog.cob,15,prog.cob:15,PARAGRAPH,3000000,0.003 s,1,%x +123456,prog,2ND,PARA-0006,,prog.cob,17,prog.cob:17,PARAGRAPH,2000000,0.002 s,2,%x +123456,prog,2ND,PARA-0007,,prog.cob,19,prog.cob:19,PARAGRAPH,1000000,0.001 s,1,%x ]) AT_CLEANUP