aboutsummaryrefslogtreecommitdiffstats
path: root/debug.c
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2023-10-11 21:26:35 +0900
committerKoichi Sasada <ko1@atdot.net>2023-10-12 03:14:39 +0900
commit802ca3a1023e8018599d36fda250cbe64e77013c (patch)
tree3c7a008c04edbe651154c3bfc90bad45093f3b0d /debug.c
parentf413e50ec22046dcdc8655061f9a7a5e5c1b9508 (diff)
downloadruby-802ca3a1023e8018599d36fda250cbe64e77013c.tar.gz
`RUBY_DEBUG_LOG` supports `%p` for pid
Diffstat (limited to 'debug.c')
-rw-r--r--debug.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/debug.c b/debug.c
index b5cba590ba..b65f368fbf 100644
--- a/debug.c
+++ b/debug.c
@@ -285,6 +285,12 @@ static const char *dlf_type_names[] = {
"func",
};
+#ifdef MAX_PATH
+#define DEBUG_LOG_MAX_PATH (MAX_PATH-1)
+#else
+#define DEBUG_LOG_MAX_PATH 255
+#endif
+
static struct {
char *mem;
unsigned int cnt;
@@ -292,6 +298,7 @@ static struct {
unsigned int filters_num;
bool show_pid;
rb_nativethread_lock_t lock;
+ char output_file[DEBUG_LOG_MAX_PATH+1];
FILE *output;
} debug_log;
@@ -393,7 +400,39 @@ setup_debug_log(void)
}
else {
ruby_debug_log_mode |= ruby_debug_log_file;
- if ((debug_log.output = fopen(log_config, "w")) == NULL) {
+
+ // pid extension with %p
+ unsigned long len = strlen(log_config);
+
+ for (unsigned long i=0, j=0; i<len; i++) {
+ const char c = log_config[i];
+
+ if (c == '%') {
+ i++;
+ switch (log_config[i]) {
+ case '%':
+ debug_log.output_file[j++] = '%';
+ break;
+ case 'p':
+ snprintf(debug_log.output_file + j, DEBUG_LOG_MAX_PATH - j, "%d", getpid());
+ j = strlen(debug_log.output_file);
+ break;
+ default:
+ fprintf(stderr, "can not parse RUBY_DEBUG_LOG filename: %s\n", log_config);
+ exit(1);
+ }
+ }
+ else {
+ debug_log.output_file[j++] = c;
+ }
+
+ if (j >= DEBUG_LOG_MAX_PATH) {
+ fprintf(stderr, "RUBY_DEBUG_LOG=%s is too long\n", log_config);
+ exit(1);
+ }
+ }
+
+ if ((debug_log.output = fopen(debug_log.output_file, "w")) == NULL) {
fprintf(stderr, "can not open %s for RUBY_DEBUG_LOG\n", log_config);
exit(1);
}