From 5345006580293971dfc946a14d5bc56800dc4554 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Thu, 7 Mar 2024 07:23:21 +0900 Subject: [PATCH] Add help text and -q options --- tools/callgrind.c | 33 +++++++++++++++++++++++++++------ tools/watchdog.c | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/tools/callgrind.c b/tools/callgrind.c index 35ae142..f52b2d9 100644 --- a/tools/callgrind.c +++ b/tools/callgrind.c @@ -26,10 +26,24 @@ extern KrkValue krk_operator_add (KrkValue a, KrkValue b); extern KrkValue krk_operator_sub (KrkValue a, KrkValue b); static int usage(char * argv[]) { - fprintf(stderr, "usage: %s [FILE] [args...]\n", argv[0]); + fprintf(stderr, "usage: %s [-f LOG_FILE] [-q] FILE [args...]\n", argv[0]); return 1; } +static int help(char * argv[]) { + usage(argv); + fprintf(stderr, + "Generate callgrind-format trace files.\n" + "\n" + "Options:\n" + " -f LOG_FILE Output callgrind-format data to LOG_FILE.\n" + " -q Do not print execution summary.\n" + "\n" + " --help Show this help text.\n" + "\n"); + return 0; +} + static KrkValue lineCache; /* {sourceCO: {line: count}} */ static KrkValue callCache; /* {sourceCO: {(codeobject,targetLine,sourceLine): [calls,instructions,time]}} */ static KrkValue timeCache; /* {sourceCO: time} */ @@ -37,6 +51,7 @@ static KrkValue timeCache; /* {sourceCO: time} */ static size_t lastFrameCount = 0; /* Previously seen frame count, to track function entry/exit */ static size_t instrCounter = 0; /* Total counter of executed instructions */ static size_t functionsEntered = 0; /* Number of function entries seen */ +static int quiet = 0; /** * @brief Calculate time difference as string. @@ -203,11 +218,14 @@ int main(int argc, char *argv[]) { snprintf(outfile,1024,"callgrind.out.%d",getpid()); int opt; - while ((opt = getopt(argc, argv, "+:f:-:")) != -1) { + while ((opt = getopt(argc, argv, "+:f:q-:")) != -1) { switch (opt) { case 'f': snprintf(outfile,1024,"%s", optarg); break; + case 'q': + quiet = 1; + break; case '?': if (optopt != '-') { fprintf(stderr, "%s: unrocognized option '%c'\n", argv[0], optopt); @@ -217,6 +235,7 @@ int main(int argc, char *argv[]) { /* fall through */ case '-': if (!strcmp(optarg,"help")) { + return help(argv); } else { fprintf(stderr, "%s: unrecognized option: '--%s'\n", argv[0], optarg); return 1; @@ -245,14 +264,16 @@ int main(int argc, char *argv[]) { if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) { krk_currentThread.flags &= ~(KRK_THREAD_HAS_EXCEPTION); - fprintf(stderr, "== Executed ended by exception ==\n"); + if (!quiet) fprintf(stderr, "== Executed ended by exception ==\n"); } else { - fprintf(stderr, "== Execution completed ==\n"); + if (!quiet) fprintf(stderr, "== Execution completed ==\n"); } krk_callgrind_debuggerHook(NULL); - fprintf(stderr, "%10zu total instruction%s\n", instrCounter, (instrCounter != 1) ? "s" : ""); - fprintf(stderr, "%10zu function%s calls\n", functionsEntered, (functionsEntered != 1) ? "s" : ""); + if (!quiet) { + fprintf(stderr, "%10zu total instruction%s\n", instrCounter, (instrCounter != 1) ? "s" : ""); + fprintf(stderr, "%10zu function%s calls\n", functionsEntered, (functionsEntered != 1) ? "s" : ""); + } FILE * f = fopen(outfile,"w"); if (!f) { diff --git a/tools/watchdog.c b/tools/watchdog.c index a29c15c..b07e3dd 100644 --- a/tools/watchdog.c +++ b/tools/watchdog.c @@ -9,13 +9,35 @@ #include "common.h" +#define DEFAULT_LIMIT 500000 + static int usage(char * argv[]) { - fprintf(stderr, "usage: %s [-s count] [FILE] [args...]\n", argv[0]); + fprintf(stderr, "usage: %s [-s COUNT] [-q] FILE [args...]\n", argv[0]); return 1; } +static int help(char * argv[]) { + usage(argv); + fprintf(stderr, + "Run scripts with an instruction counter and halt when a " + "limit is exceeded. The default limit is %d. A total count of " + "executed instructions is printed after completion.\n" + "\n" + "Options:\n" + " -s COUNT Set watchdog timeout to COUNT instructions.\n" + " Specify -1 to set disable limit.\n" + " -q Do not print total instruction count.\n" + "\n" + " --help Show this help text.\n" + "\n", + DEFAULT_LIMIT); + return 0; +} + + static size_t instrCounter = 0; /* Total counter of executed instructions */ -static size_t stopAt = 500000; +static size_t stopAt = DEFAULT_LIMIT; +static int quiet = 0; int krk_callgrind_debuggerHook(KrkCallFrame * frame) { instrCounter++; @@ -29,11 +51,14 @@ int krk_callgrind_debuggerHook(KrkCallFrame * frame) { int main(int argc, char *argv[]) { int opt; - while ((opt = getopt(argc, argv, "+:s:-:")) != -1) { + while ((opt = getopt(argc, argv, "+:s:q-:")) != -1) { switch (opt) { case 's': stopAt = strtoul(optarg,NULL,10); break; + case 'q': + quiet = 1; + break; case '?': if (optopt != '-') { fprintf(stderr, "%s: unrocognized option '%c'\n", argv[0], optopt); @@ -43,6 +68,7 @@ int main(int argc, char *argv[]) { /* fall through */ case '-': if (!strcmp(optarg,"help")) { + return help(argv); } else { fprintf(stderr, "%s: unrecognized option: '--%s'\n", argv[0], optarg); return 1; @@ -62,7 +88,9 @@ int main(int argc, char *argv[]) { krk_startModule("__main__"); krk_runfile(argv[optind],argv[optind]); - fprintf(stderr, "%zu total instructions\n", instrCounter); + if (!quiet) { + fprintf(stderr, "%zu total instructions\n", instrCounter); + } krk_freeVM(); return 0;