Ruby: vm_dump.c Source File

Ruby 3.4.5p51 (2025-07-16 revision 20cda200d3ce092571d0b5d342dadca69636cb0f)

vm_dump.c
1/**********************************************************************
2
3 vm_dump.c -
4
5 $Author$
6
7 Copyright (C) 2004-2007 Koichi Sasada
8
9**********************************************************************/
10
11#include "ruby/internal/config.h"
12#include "ruby/fiber/scheduler.h"
13
14#ifdef HAVE_UCONTEXT_H
15# include <ucontext.h>
16#endif
17
18#ifdef __APPLE__
19# ifdef HAVE_LIBPROC_H
20# include <libproc.h>
21# endif
22# include <mach/vm_map.h>
23# include <mach/mach_init.h>
24# ifdef __LP64__
25# define vm_region_recurse vm_region_recurse_64
26# endif
27/* that is defined in sys/queue.h, and conflicts with
28 * ccan/list/list.h */
29# undef LIST_HEAD
30#endif
31
32#include "addr2line.h"
33#include "internal.h"
34#include "internal/gc.h"
35#include "internal/variable.h"
36#include "internal/vm.h"
37#include "iseq.h"
38#include "vm_core.h"
39#include "ractor_core.h"
40
41#define MAX_POSBUF 128
42
43#define VM_CFP_CNT(ec, cfp) \
44 ((rb_control_frame_t *)((ec)->vm_stack + (ec)->vm_stack_size) - \
45 (rb_control_frame_t *)(cfp))
46
47const char *rb_method_type_name(rb_method_type_t type);
48int ruby_on_ci;
49
50#define kprintf(...) if (fprintf(errout, __VA_ARGS__) < 0) goto error
51#define kputs(s) if (fputs(s, errout) < 0) goto error
52
53static bool
54control_frame_dump(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *errout)
55{
56 ptrdiff_t pc = -1;
57 ptrdiff_t ep = cfp->ep - ec->vm_stack;
58 char ep_in_heap = ' ';
59 char posbuf[MAX_POSBUF+1];
60 int line = 0;
61 const char *magic, *iseq_name = "-", *selfstr = "-", *biseq_name = "-";
62 VALUE tmp;
63 const rb_iseq_t *iseq = NULL;
64 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
65
66 if (ep < 0 || (size_t)ep > ec->vm_stack_size) {
67 ep = (ptrdiff_t)cfp->ep;
68 ep_in_heap = 'p';
69 }
70
71 switch (VM_FRAME_TYPE(cfp)) {
72 case VM_FRAME_MAGIC_TOP:
73 magic = "TOP";
74 break;
75 case VM_FRAME_MAGIC_METHOD:
76 magic = "METHOD";
77 break;
78 case VM_FRAME_MAGIC_CLASS:
79 magic = "CLASS";
80 break;
81 case VM_FRAME_MAGIC_BLOCK:
82 magic = "BLOCK";
83 break;
84 case VM_FRAME_MAGIC_CFUNC:
85 magic = "CFUNC";
86 break;
87 case VM_FRAME_MAGIC_IFUNC:
88 magic = "IFUNC";
89 break;
90 case VM_FRAME_MAGIC_EVAL:
91 magic = "EVAL";
92 break;
93 case VM_FRAME_MAGIC_RESCUE:
94 magic = "RESCUE";
95 break;
96 case VM_FRAME_MAGIC_DUMMY:
97 magic = "DUMMY";
98 break;
99 case 0:
100 magic = "------";
101 break;
102 default:
103 magic = "(none)";
104 break;
105 }
106
107 if (0) {
108 tmp = rb_inspect(cfp->self);
109 selfstr = StringValueCStr(tmp);
110 }
111 else {
112 selfstr = "";
113 }
114
115 if (cfp->iseq != 0) {
116#define RUBY_VM_IFUNC_P(ptr) IMEMO_TYPE_P(ptr, imemo_ifunc)
117 if (RUBY_VM_IFUNC_P(cfp->iseq)) {
118 iseq_name = "<ifunc>";
119 }
120 else if (SYMBOL_P((VALUE)cfp->iseq)) {
121 tmp = rb_sym2str((VALUE)cfp->iseq);
122 iseq_name = RSTRING_PTR(tmp);
123 snprintf(posbuf, MAX_POSBUF, ":%s", iseq_name);
124 line = -1;
125 }
126 else {
127 if (cfp->pc) {
128 iseq = cfp->iseq;
129 pc = cfp->pc - ISEQ_BODY(iseq)->iseq_encoded;
130 iseq_name = RSTRING_PTR(ISEQ_BODY(iseq)->location.label);
131 line = rb_vm_get_sourceline(cfp);
132 if (line) {
133 snprintf(posbuf, MAX_POSBUF, "%s:%d", RSTRING_PTR(rb_iseq_path(iseq)), line);
134 }
135 }
136 else {
137 iseq_name = "<dummy_frame>";
138 }
139 }
140 }
141 else if (me != NULL) {
142 iseq_name = rb_id2name(me->def->original_id);
143 snprintf(posbuf, MAX_POSBUF, ":%s", iseq_name);
144 line = -1;
145 }
146
147 kprintf("c:%04"PRIdPTRDIFF" ",
148 ((rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size) - cfp));
149 if (pc == -1) {
150 kprintf("p:---- ");
151 }
152 else {
153 kprintf("p:%04"PRIdPTRDIFF" ", pc);
154 }
155 kprintf("s:%04"PRIdPTRDIFF" ", cfp->sp - ec->vm_stack);
156 kprintf(ep_in_heap == ' ' ? "e:%06"PRIdPTRDIFF" " : "E:%06"PRIxPTRDIFF" ", ep % 10000);
157 kprintf("%-6s", magic);
158 if (line) {
159 kprintf(" %s", posbuf);
160 }
161 if (VM_FRAME_FINISHED_P(cfp)) {
162 kprintf(" [FINISH]");
163 }
164 if (0) {
165 kprintf(" \t");
166 kprintf("iseq: %-24s ", iseq_name);
167 kprintf("self: %-24s ", selfstr);
168 kprintf("%-1s ", biseq_name);
169 }
170 kprintf("\n");
171
172 // additional information for CI machines
173 if (ruby_on_ci) {
174 char buff[0x100];
175
176 if (me) {
177 if (IMEMO_TYPE_P(me, imemo_ment)) {
178 kprintf(" me:\n");
179 kprintf(" called_id: %s, type: %s\n", rb_id2name(me->called_id), rb_method_type_name(me->def->type));
180 kprintf(" owner class: %s\n", rb_raw_obj_info(buff, 0x100, me->owner));
181 if (me->owner != me->defined_class) {
182 kprintf(" defined_class: %s\n", rb_raw_obj_info(buff, 0x100, me->defined_class));
183 }
184 }
185 else {
186 kprintf(" me is corrupted (%s)\n", rb_raw_obj_info(buff, 0x100, (VALUE)me));
187 }
188 }
189
190 kprintf(" self: %s\n", rb_raw_obj_info(buff, 0x100, cfp->self));
191
192 if (iseq) {
193 if (ISEQ_BODY(iseq)->local_table_size > 0) {
194 kprintf(" lvars:\n");
195 for (unsigned int i=0; i<ISEQ_BODY(iseq)->local_table_size; i++) {
196 const VALUE *argv = cfp->ep - ISEQ_BODY(cfp->iseq)->local_table_size - VM_ENV_DATA_SIZE + 1;
197 kprintf(" %s: %s\n",
198 rb_id2name(ISEQ_BODY(iseq)->local_table[i]),
199 rb_raw_obj_info(buff, 0x100, argv[i]));
200 }
201 }
202 }
203 }
204 return true;
205 error:
206 return false;
207}
208
209bool
210rb_vmdebug_stack_dump_raw(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *errout)
211{
212#if 0
213 VALUE *sp = cfp->sp;
214 const VALUE *ep = cfp->ep;
215 VALUE *p, *st, *t;
216
217 kprintf("-- stack frame ------------\n");
218 for (p = st = ec->vm_stack; p < sp; p++) {
219 kprintf("%04ld (%p): %08"PRIxVALUE, (long)(p - st), p, *p);
220
221 t = (VALUE *)*p;
222 if (ec->vm_stack <= t && t < sp) {
223 kprintf(" (= %ld)", (long)((VALUE *)GC_GUARDED_PTR_REF((VALUE)t) - ec->vm_stack));
224 }
225
226 if (p == ep)
227 kprintf(" <- ep");
228
229 kprintf("\n");
230 }
231#endif
232
233 kprintf("-- Control frame information "
234 "-----------------------------------------------\n");
235 while ((void *)cfp < (void *)(ec->vm_stack + ec->vm_stack_size)) {
236 control_frame_dump(ec, cfp, errout);
237 cfp++;
238 }
239 kprintf("\n");
240 return true;
241
242 error:
243 return false;
244}
245
246bool
247rb_vmdebug_stack_dump_raw_current(void)
248{
249 const rb_execution_context_t *ec = GET_EC();
250 return rb_vmdebug_stack_dump_raw(ec, ec->cfp, stderr);
251}
252
253bool
254rb_vmdebug_env_dump_raw(const rb_env_t *env, const VALUE *ep, FILE *errout)
255{
256 unsigned int i;
257 kprintf("-- env --------------------\n");
258
259 while (env) {
260 kprintf("--\n");
261 for (i = 0; i < env->env_size; i++) {
262 kprintf("%04d: %08"PRIxVALUE" (%p)", i, env->env[i], (void *)&env->env[i]);
263 if (&env->env[i] == ep) kprintf(" <- ep");
264 kprintf("\n");
265 }
266
267 env = rb_vm_env_prev_env(env);
268 }
269 kprintf("---------------------------\n");
270 return true;
271
272 error:
273 return false;
274}
275
276bool
277rb_vmdebug_proc_dump_raw(rb_proc_t *proc, FILE *errout)
278{
279 const rb_env_t *env;
280 char *selfstr;
281 VALUE val = rb_inspect(vm_block_self(&proc->block));
282 selfstr = StringValueCStr(val);
283
284 kprintf("-- proc -------------------\n");
285 kprintf("self: %s\n", selfstr);
286 env = VM_ENV_ENVVAL_PTR(vm_block_ep(&proc->block));
287 rb_vmdebug_env_dump_raw(env, vm_block_ep(&proc->block), errout);
288 return true;
289
290 error:
291 return false;
292}
293
294bool
295rb_vmdebug_stack_dump_th(VALUE thval, FILE *errout)
296{
297 rb_thread_t *target_th = rb_thread_ptr(thval);
298 return rb_vmdebug_stack_dump_raw(target_th->ec, target_th->ec->cfp, errout);
299}
300
301#if VMDEBUG > 2
302
303/* copy from vm_insnhelper.c */
304static const VALUE *
305vm_base_ptr(const rb_control_frame_t *cfp)
306{
307 const rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
308 const VALUE *bp = prev_cfp->sp + ISEQ_BODY(cfp->iseq)->local_table_size + VM_ENV_DATA_SIZE;
309
310 if (ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_METHOD || VM_FRAME_BMETHOD_P(cfp)) {
311 bp += 1;
312 }
313 return bp;
314}
315
316static void
317vm_stack_dump_each(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *errout)
318{
319 int i, argc = 0, local_table_size = 0;
320 VALUE rstr;
321 VALUE *sp = cfp->sp;
322 const VALUE *ep = cfp->ep;
323
324 if (VM_FRAME_RUBYFRAME_P(cfp)) {
325 const rb_iseq_t *iseq = cfp->iseq;
326 argc = ISEQ_BODY(iseq)->param.lead_num;
327 local_table_size = ISEQ_BODY(iseq)->local_table_size;
328 }
329
330 /* stack trace header */
331
332 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_METHOD||
333 VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_TOP ||
334 VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_BLOCK ||
335 VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CLASS ||
336 VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC ||
337 VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_IFUNC ||
338 VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_EVAL ||
339 VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_RESCUE)
340 {
341 const VALUE *ptr = ep - local_table_size;
342
343 control_frame_dump(ec, cfp, errout);
344
345 for (i = 0; i < argc; i++) {
346 rstr = rb_inspect(*ptr);
347 kprintf(" arg %2d: %8s (%p)\n", i, StringValueCStr(rstr),
348 (void *)ptr++);
349 }
350 for (; i < local_table_size - 1; i++) {
351 rstr = rb_inspect(*ptr);
352 kprintf(" local %2d: %8s (%p)\n", i, StringValueCStr(rstr),
353 (void *)ptr++);
354 }
355
356 ptr = vm_base_ptr(cfp);
357 for (; ptr < sp; ptr++, i++) {
358 switch (TYPE(*ptr)) {
359 case T_UNDEF:
360 rstr = rb_str_new2("undef");
361 break;
362 case T_IMEMO:
363 rstr = rb_str_new2("imemo"); /* TODO: can put mode detail information */
364 break;
365 default:
366 rstr = rb_inspect(*ptr);
367 break;
368 }
369 kprintf(" stack %2d: %8s (%"PRIdPTRDIFF")\n", i, StringValueCStr(rstr),
370 (ptr - ec->vm_stack));
371 }
372 }
373 else if (VM_FRAME_FINISHED_P(cfp)) {
374 if (ec->vm_stack + ec->vm_stack_size > (VALUE *)(cfp + 1)) {
375 vm_stack_dump_each(ec, cfp + 1, errout);
376 }
377 else {
378 /* SDR(); */
379 }
380 }
381 else {
382 rb_bug("unsupported frame type: %08lx", VM_FRAME_TYPE(cfp));
383 }
384}
385#endif
386
387bool
388rb_vmdebug_debug_print_register(const rb_execution_context_t *ec, FILE *errout)
389{
390 rb_control_frame_t *cfp = ec->cfp;
391 ptrdiff_t pc = -1;
392 ptrdiff_t ep = cfp->ep - ec->vm_stack;
393 ptrdiff_t cfpi;
394
395 if (VM_FRAME_RUBYFRAME_P(cfp)) {
396 pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
397 }
398
399 if (ep < 0 || (size_t)ep > ec->vm_stack_size) {
400 ep = -1;
401 }
402
403 cfpi = ((rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size)) - cfp;
404 kprintf(" [PC] %04"PRIdPTRDIFF", [SP] %04"PRIdPTRDIFF", [EP] %04"PRIdPTRDIFF", [CFP] %04"PRIdPTRDIFF"\n",
405 pc, (cfp->sp - ec->vm_stack), ep, cfpi);
406 return true;
407
408 error:
409 return false;
410}
411
412bool
413rb_vmdebug_thread_dump_regs(VALUE thval, FILE *errout)
414{
415 return rb_vmdebug_debug_print_register(rb_thread_ptr(thval)->ec, errout);
416}
417
418bool
419rb_vmdebug_debug_print_pre(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE *_pc, FILE *errout)
420{
421 const rb_iseq_t *iseq = cfp->iseq;
422
423 if (iseq != 0) {
424 ptrdiff_t pc = _pc - ISEQ_BODY(iseq)->iseq_encoded;
425 int i;
426
427 for (i=0; i<(int)VM_CFP_CNT(ec, cfp); i++) {
428 kprintf(" ");
429 }
430 kprintf("| ");
431 if(0) kprintf("[%03ld] ", (long)(cfp->sp - ec->vm_stack));
432
433 /* printf("%3"PRIdPTRDIFF" ", VM_CFP_CNT(ec, cfp)); */
434 if (pc >= 0) {
435 const VALUE *iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
436
437 rb_iseq_disasm_insn(0, iseq_original, (size_t)pc, iseq, 0);
438 }
439 }
440
441#if VMDEBUG > 3
442 kprintf(" (1)");
443 rb_vmdebug_debug_print_register(errout, ec);
444#endif
445 return true;
446
447 error:
448 return false;
449}
450
451bool
452rb_vmdebug_debug_print_post(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *errout)
453{
454#if VMDEBUG > 9
455 if (!rb_vmdebug_stack_dump_raw(ec, cfp, errout)) goto errout;
456#endif
457
458#if VMDEBUG > 3
459 kprintf(" (2)");
460 rb_vmdebug_debug_print_register(errout, ec);
461#endif
462 /* stack_dump_raw(ec, cfp); */
463
464#if VMDEBUG > 2
465 /* stack_dump_thobj(ec); */
466 vm_stack_dump_each(ec, ec->cfp, errout);
467
468 kprintf
469 ("--------------------------------------------------------------\n");
470#endif
471 return true;
472
473#if VMDEBUG > 2
474 error:
475 return false;
476#endif
477}
478
479 VALUE
480rb_vmdebug_thread_dump_state(FILE *errout, VALUE self)
481{
482 rb_thread_t *th = rb_thread_ptr(self);
483 rb_control_frame_t *cfp = th->ec->cfp;
484
485 kprintf("Thread state dump:\n");
486 kprintf("pc : %p, sp : %p\n", (void *)cfp->pc, (void *)cfp->sp);
487 kprintf("cfp: %p, ep : %p\n", (void *)cfp, (void *)cfp->ep);
488
489 error:
490 return Qnil;
491}
492
493#if defined __APPLE__
494# include <AvailabilityMacros.h>
495# if defined(MAC_OS_X_VERSION_10_5) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
496# define MCTX_SS_REG(reg) __ss.__##reg
497# else
498# define MCTX_SS_REG(reg) ss.reg
499# endif
500#endif
501
502#if defined(HAVE_BACKTRACE)
503# define USE_BACKTRACE 1
504# ifdef HAVE_LIBUNWIND
505# undef backtrace
506# define backtrace unw_backtrace
507# elif defined(__APPLE__) && defined(HAVE_LIBUNWIND_H) \
508 && defined(MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
509# define UNW_LOCAL_ONLY
510# include <libunwind.h>
511# include <sys/mman.h>
512# undef backtrace
513int
514backtrace(void **trace, int size)
515{
516 unw_cursor_t cursor; unw_context_t uc;
517 unw_word_t ip;
518 int n = 0;
519
520 unw_getcontext(&uc);
521 unw_init_local(&cursor, &uc);
522# if defined(__x86_64__)
523 while (unw_step(&cursor) > 0) {
524 unw_get_reg(&cursor, UNW_REG_IP, &ip);
525 trace[n++] = (void *)ip;
526 {
527 char buf[256];
528 unw_get_proc_name(&cursor, buf, 256, &ip);
529 if (strncmp("_sigtramp", buf, sizeof("_sigtramp")) == 0) {
530 goto darwin_sigtramp;
531 }
532 }
533 }
534 return n;
535darwin_sigtramp:
536 /* darwin's bundled libunwind doesn't support signal trampoline */
537 {
538 ucontext_t *uctx;
539 char vec[1];
540 int r;
541 /* get previous frame information from %rbx at _sigtramp and set values to cursor
542 * https://www.opensource.apple.com/source/Libc/Libc-825.25/i386/sys/_sigtramp.s
543 * https://www.opensource.apple.com/source/libunwind/libunwind-35.1/src/unw_getcontext.s
544 */
545 unw_get_reg(&cursor, UNW_X86_64_RBX, &ip);
546 uctx = (ucontext_t *)ip;
547 unw_set_reg(&cursor, UNW_X86_64_RAX, uctx->uc_mcontext->MCTX_SS_REG(rax));
548 unw_set_reg(&cursor, UNW_X86_64_RBX, uctx->uc_mcontext->MCTX_SS_REG(rbx));
549 unw_set_reg(&cursor, UNW_X86_64_RCX, uctx->uc_mcontext->MCTX_SS_REG(rcx));
550 unw_set_reg(&cursor, UNW_X86_64_RDX, uctx->uc_mcontext->MCTX_SS_REG(rdx));
551 unw_set_reg(&cursor, UNW_X86_64_RDI, uctx->uc_mcontext->MCTX_SS_REG(rdi));
552 unw_set_reg(&cursor, UNW_X86_64_RSI, uctx->uc_mcontext->MCTX_SS_REG(rsi));
553 unw_set_reg(&cursor, UNW_X86_64_RBP, uctx->uc_mcontext->MCTX_SS_REG(rbp));
554 unw_set_reg(&cursor, UNW_X86_64_RSP, 8+(uctx->uc_mcontext->MCTX_SS_REG(rsp)));
555 unw_set_reg(&cursor, UNW_X86_64_R8, uctx->uc_mcontext->MCTX_SS_REG(r8));
556 unw_set_reg(&cursor, UNW_X86_64_R9, uctx->uc_mcontext->MCTX_SS_REG(r9));
557 unw_set_reg(&cursor, UNW_X86_64_R10, uctx->uc_mcontext->MCTX_SS_REG(r10));
558 unw_set_reg(&cursor, UNW_X86_64_R11, uctx->uc_mcontext->MCTX_SS_REG(r11));
559 unw_set_reg(&cursor, UNW_X86_64_R12, uctx->uc_mcontext->MCTX_SS_REG(r12));
560 unw_set_reg(&cursor, UNW_X86_64_R13, uctx->uc_mcontext->MCTX_SS_REG(r13));
561 unw_set_reg(&cursor, UNW_X86_64_R14, uctx->uc_mcontext->MCTX_SS_REG(r14));
562 unw_set_reg(&cursor, UNW_X86_64_R15, uctx->uc_mcontext->MCTX_SS_REG(r15));
563 ip = uctx->uc_mcontext->MCTX_SS_REG(rip);
564
565 /* There are 4 cases for SEGV:
566 * (1) called invalid address
567 * (2) read or write invalid address
568 * (3) received signal
569 *
570 * Detail:
571 * (1) called invalid address
572 * In this case, saved ip is invalid address.
573 * It needs to just save the address for the information,
574 * skip the frame, and restore the frame calling the
575 * invalid address from %rsp.
576 * The problem is how to check whether the ip is valid or not.
577 * This code uses mincore(2) and assume the address's page is
578 * incore/referenced or not reflects the problem.
579 * Note that High Sierra's mincore(2) may return -128.
580 * (2) read or write invalid address
581 * saved ip is valid. just restart backtracing.
582 * (3) received signal in user space
583 * Same as (2).
584 * (4) received signal in kernel
585 * In this case saved ip points just after syscall, but registers are
586 * already overwritten by kernel. To fix register consistency,
587 * skip libc's kernel wrapper.
588 * To detect this case, just previous two bytes of ip is "\x0f\x05",
589 * syscall instruction of x86_64.
590 */
591 r = mincore((const void *)ip, 1, vec);
592 if (r || vec[0] <= 0 || memcmp((const char *)ip-2, "\x0f\x05", 2) == 0) {
593 /* if segv is caused by invalid call or signal received in syscall */
594 /* the frame is invalid; skip */
595 trace[n++] = (void *)ip;
596 ip = *(unw_word_t*)uctx->uc_mcontext->MCTX_SS_REG(rsp);
597 }
598
599 trace[n++] = (void *)ip;
600 unw_set_reg(&cursor, UNW_REG_IP, ip);
601 }
602 while (unw_step(&cursor) > 0) {
603 unw_get_reg(&cursor, UNW_REG_IP, &ip);
604 trace[n++] = (void *)ip;
605 }
606 return n;
607
608# else /* defined(__arm64__) */
609 /* Since Darwin arm64's _sigtramp is implemented as normal function,
610 * unwind can unwind frames without special code.
611 * https://github.com/apple/darwin-libplatform/blob/215b09856ab5765b7462a91be7076183076600df/src/setjmp/generic/sigtramp.c
612 */
613 while (unw_step(&cursor) > 0) {
614 unw_get_reg(&cursor, UNW_REG_IP, &ip);
615 // Strip Arm64's pointer authentication.
616 // https://developer.apple.com/documentation/security/preparing_your_app_to_work_with_pointer_authentication
617 // I wish I could use "ptrauth_strip()" but I get an error:
618 // "this target does not support pointer authentication"
619 trace[n++] = (void *)(ip & 0x7fffffffffffull);
620 }
621 return n;
622# endif
623}
624# elif defined(BROKEN_BACKTRACE)
625# undef USE_BACKTRACE
626# define USE_BACKTRACE 0
627# endif
628#else
629# define USE_BACKTRACE 0
630#endif
631
632#if USE_BACKTRACE
633# include <execinfo.h>
634#elif defined(_WIN32)
635# include <imagehlp.h>
636# ifndef SYMOPT_DEBUG
637# define SYMOPT_DEBUG 0x80000000
638# endif
639# ifndef MAX_SYM_NAME
640# define MAX_SYM_NAME 2000
641typedef struct {
642 DWORD64 Offset;
643 WORD Segment;
644 ADDRESS_MODE Mode;
645} ADDRESS64;
646typedef struct {
647 DWORD64 Thread;
648 DWORD ThCallbackStack;
649 DWORD ThCallbackBStore;
650 DWORD NextCallback;
651 DWORD FramePointer;
652 DWORD64 KiCallUserMode;
653 DWORD64 KeUserCallbackDispatcher;
654 DWORD64 SystemRangeStart;
655 DWORD64 KiUserExceptionDispatcher;
656 DWORD64 StackBase;
657 DWORD64 StackLimit;
658 DWORD64 Reserved[5];
659} KDHELP64;
660typedef struct {
661 ADDRESS64 AddrPC;
662 ADDRESS64 AddrReturn;
663 ADDRESS64 AddrFrame;
664 ADDRESS64 AddrStack;
665 ADDRESS64 AddrBStore;
666 void *FuncTableEntry;
667 DWORD64 Params[4];
668 BOOL Far;
669 BOOL Virtual;
670 DWORD64 Reserved[3];
671 KDHELP64 KdHelp;
672} STACKFRAME64;
673typedef struct {
674 ULONG SizeOfStruct;
675 ULONG TypeIndex;
676 ULONG64 Reserved[2];
677 ULONG Index;
678 ULONG Size;
679 ULONG64 ModBase;
680 ULONG Flags;
681 ULONG64 Value;
682 ULONG64 Address;
683 ULONG Register;
684 ULONG Scope;
685 ULONG Tag;
686 ULONG NameLen;
687 ULONG MaxNameLen;
688 char Name[1];
689} SYMBOL_INFO;
690typedef struct {
691 DWORD SizeOfStruct;
692 void *Key;
693 DWORD LineNumber;
694 char *FileName;
695 DWORD64 Address;
696} IMAGEHLP_LINE64;
697typedef void *PREAD_PROCESS_MEMORY_ROUTINE64;
698typedef void *PFUNCTION_TABLE_ACCESS_ROUTINE64;
699typedef void *PGET_MODULE_BASE_ROUTINE64;
700typedef void *PTRANSLATE_ADDRESS_ROUTINE64;
701# endif
702
703struct dump_thead_arg {
704 DWORD tid;
705 FILE *errout;
706};
707
708static void
709dump_thread(void *arg)
710{
711 HANDLE dbghelp;
712 BOOL (WINAPI *pSymInitialize)(HANDLE, const char *, BOOL);
713 BOOL (WINAPI *pSymCleanup)(HANDLE);
714 BOOL (WINAPI *pStackWalk64)(DWORD, HANDLE, HANDLE, STACKFRAME64 *, void *, PREAD_PROCESS_MEMORY_ROUTINE64, PFUNCTION_TABLE_ACCESS_ROUTINE64, PGET_MODULE_BASE_ROUTINE64, PTRANSLATE_ADDRESS_ROUTINE64);
715 DWORD64 (WINAPI *pSymGetModuleBase64)(HANDLE, DWORD64);
716 BOOL (WINAPI *pSymFromAddr)(HANDLE, DWORD64, DWORD64 *, SYMBOL_INFO *);
717 BOOL (WINAPI *pSymGetLineFromAddr64)(HANDLE, DWORD64, DWORD *, IMAGEHLP_LINE64 *);
718 HANDLE (WINAPI *pOpenThread)(DWORD, BOOL, DWORD);
719 DWORD tid = ((struct dump_thead_arg *)arg)->tid;
720 FILE *errout = ((struct dump_thead_arg *)arg)->errout;
721 HANDLE ph;
722 HANDLE th;
723
724 dbghelp = LoadLibrary("dbghelp.dll");
725 if (!dbghelp) return;
726 pSymInitialize = (BOOL (WINAPI *)(HANDLE, const char *, BOOL))GetProcAddress(dbghelp, "SymInitialize");
727 pSymCleanup = (BOOL (WINAPI *)(HANDLE))GetProcAddress(dbghelp, "SymCleanup");
728 pStackWalk64 = (BOOL (WINAPI *)(DWORD, HANDLE, HANDLE, STACKFRAME64 *, void *, PREAD_PROCESS_MEMORY_ROUTINE64, PFUNCTION_TABLE_ACCESS_ROUTINE64, PGET_MODULE_BASE_ROUTINE64, PTRANSLATE_ADDRESS_ROUTINE64))GetProcAddress(dbghelp, "StackWalk64");
729 pSymGetModuleBase64 = (DWORD64 (WINAPI *)(HANDLE, DWORD64))GetProcAddress(dbghelp, "SymGetModuleBase64");
730 pSymFromAddr = (BOOL (WINAPI *)(HANDLE, DWORD64, DWORD64 *, SYMBOL_INFO *))GetProcAddress(dbghelp, "SymFromAddr");
731 pSymGetLineFromAddr64 = (BOOL (WINAPI *)(HANDLE, DWORD64, DWORD *, IMAGEHLP_LINE64 *))GetProcAddress(dbghelp, "SymGetLineFromAddr64");
732 pOpenThread = (HANDLE (WINAPI *)(DWORD, BOOL, DWORD))GetProcAddress(GetModuleHandle("kernel32.dll"), "OpenThread");
733 if (pSymInitialize && pSymCleanup && pStackWalk64 && pSymGetModuleBase64 &&
734 pSymFromAddr && pSymGetLineFromAddr64 && pOpenThread) {
735 SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_DEBUG | SYMOPT_LOAD_LINES);
736 ph = GetCurrentProcess();
737 pSymInitialize(ph, NULL, TRUE);
738 th = pOpenThread(THREAD_SUSPEND_RESUME|THREAD_GET_CONTEXT, FALSE, tid);
739 if (th) {
740 if (SuspendThread(th) != (DWORD)-1) {
741 CONTEXT context;
742 memset(&context, 0, sizeof(context));
743 context.ContextFlags = CONTEXT_FULL;
744 if (GetThreadContext(th, &context)) {
745 char libpath[MAX_PATH];
746 char buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
747 SYMBOL_INFO *info = (SYMBOL_INFO *)buf;
748 DWORD mac;
749 STACKFRAME64 frame;
750 memset(&frame, 0, sizeof(frame));
751#if defined(_M_AMD64) || defined(__x86_64__)
752 mac = IMAGE_FILE_MACHINE_AMD64;
753 frame.AddrPC.Mode = AddrModeFlat;
754 frame.AddrPC.Offset = context.Rip;
755 frame.AddrFrame.Mode = AddrModeFlat;
756 frame.AddrFrame.Offset = context.Rbp;
757 frame.AddrStack.Mode = AddrModeFlat;
758 frame.AddrStack.Offset = context.Rsp;
759#elif defined(_M_ARM64) || defined(__aarch64__)
760 mac = IMAGE_FILE_MACHINE_ARM64;
761 frame.AddrPC.Mode = AddrModeFlat;
762 frame.AddrPC.Offset = context.Pc;
763 frame.AddrFrame.Mode = AddrModeFlat;
764 frame.AddrFrame.Offset = context.Fp;
765 frame.AddrStack.Mode = AddrModeFlat;
766 frame.AddrStack.Offset = context.Sp;
767#else /* i386 */
768 mac = IMAGE_FILE_MACHINE_I386;
769 frame.AddrPC.Mode = AddrModeFlat;
770 frame.AddrPC.Offset = context.Eip;
771 frame.AddrFrame.Mode = AddrModeFlat;
772 frame.AddrFrame.Offset = context.Ebp;
773 frame.AddrStack.Mode = AddrModeFlat;
774 frame.AddrStack.Offset = context.Esp;
775#endif
776
777 while (pStackWalk64(mac, ph, th, &frame, &context, NULL,
778 NULL, NULL, NULL)) {
779 DWORD64 addr = frame.AddrPC.Offset;
780 IMAGEHLP_LINE64 line;
781 DWORD64 displacement;
782 DWORD tmp;
783
784 if (addr == frame.AddrReturn.Offset || addr == 0 ||
785 frame.AddrReturn.Offset == 0)
786 break;
787
788 memset(buf, 0, sizeof(buf));
789 info->SizeOfStruct = sizeof(SYMBOL_INFO);
790 info->MaxNameLen = MAX_SYM_NAME;
791 if (pSymFromAddr(ph, addr, &displacement, info)) {
792 if (GetModuleFileName((HANDLE)(uintptr_t)pSymGetModuleBase64(ph, addr), libpath, sizeof(libpath)))
793 kprintf("%s", libpath);
794 kprintf("(%s+0x%"PRI_64_PREFIX"x)",
795 info->Name, displacement);
796 }
797 kprintf(" [0x%p]", (void *)(VALUE)addr);
798 memset(&line, 0, sizeof(line));
799 line.SizeOfStruct = sizeof(line);
800 if (pSymGetLineFromAddr64(ph, addr, &tmp, &line))
801 kprintf(" %s:%lu", line.FileName, line.LineNumber);
802 kprintf("\n");
803 }
804 }
805
806 error:
807 ResumeThread(th);
808 }
809 CloseHandle(th);
810 }
811 pSymCleanup(ph);
812 }
813 FreeLibrary(dbghelp);
814}
815#endif
816
817void
818rb_print_backtrace(FILE *errout)
819{
820#if USE_BACKTRACE
821#define MAX_NATIVE_TRACE 1024
822 static void *trace[MAX_NATIVE_TRACE];
823 int n = (int)backtrace(trace, MAX_NATIVE_TRACE);
824#if (defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)) && defined(HAVE_DLADDR) && !defined(__sparc)
825 rb_dump_backtrace_with_lines(n, trace, errout);
826#else
827 char **syms = backtrace_symbols(trace, n);
828 if (syms) {
829 int i;
830 for (i=0; i<n; i++) {
831 kprintf("%s\n", syms[i]);
832 }
833 free(syms);
834 }
835 error:
836 /* ignore errors at writing */;
837#endif
838#elif defined(_WIN32)
839 struct dump_thead_arg arg = {
840 .tid = GetCurrentThreadId(),
841 .errout = errout,
842 };
843 HANDLE th = (HANDLE)_beginthread(dump_thread, 0, &arg);
844 if (th != (HANDLE)-1)
845 WaitForSingleObject(th, INFINITE);
846#endif
847}
848
849#ifdef HAVE_LIBPROCSTAT
850struct procstat;
851struct kinfo_proc;
852static void procstat_vm(struct procstat *, struct kinfo_proc *, FILE *);
853#include "missing/procstat_vm.c"
854#endif
855
856#if defined __linux__
857# if defined(__x86_64__) || defined(__i386__)
858# define dump_machine_register(reg) (col_count = print_machine_register(errout, mctx->gregs[REG_##reg], #reg, col_count, 80))
859# elif defined(__aarch64__) || defined(__arm__) || defined(__riscv) || defined(__loongarch64)
860# define dump_machine_register(reg, regstr) (col_count = print_machine_register(errout, reg, regstr, col_count, 80))
861# endif
862#elif defined __APPLE__
863# if defined(__aarch64__)
864# define dump_machine_register(reg, regstr) (col_count = print_machine_register(errout, mctx->MCTX_SS_REG(reg), regstr, col_count, 80))
865# else
866# define dump_machine_register(reg) (col_count = print_machine_register(errout, mctx->MCTX_SS_REG(reg), #reg, col_count, 80))
867# endif
868#endif
869
870#ifdef dump_machine_register
871static int
872print_machine_register(FILE *errout, size_t reg, const char *reg_name, int col_count, int max_col)
873{
874 int ret;
875 char buf[64];
876 static const int size_width = sizeof(size_t) * CHAR_BIT / 4;
877
878 ret = snprintf(buf, sizeof(buf), " %3.3s: 0x%.*" PRIxSIZE, reg_name, size_width, reg);
879 if (col_count + ret > max_col) {
880 kputs("\n");
881 col_count = 0;
882 }
883 col_count += ret;
884 kputs(buf);
885 return col_count;
886
887 error:
888 return -1;
889}
890
891static bool
892rb_dump_machine_register(FILE *errout, const ucontext_t *ctx)
893{
894 int col_count = 0;
895 if (!ctx) return true;
896
897 kprintf("-- Machine register context "
898 "------------------------------------------------\n");
899
900# if defined __linux__
901 {
902 const mcontext_t *const mctx = &ctx->uc_mcontext;
903# if defined __x86_64__
904 dump_machine_register(RIP);
905 dump_machine_register(RBP);
906 dump_machine_register(RSP);
907 dump_machine_register(RAX);
908 dump_machine_register(RBX);
909 dump_machine_register(RCX);
910 dump_machine_register(RDX);
911 dump_machine_register(RDI);
912 dump_machine_register(RSI);
913 dump_machine_register(R8);
914 dump_machine_register(R9);
915 dump_machine_register(R10);
916 dump_machine_register(R11);
917 dump_machine_register(R12);
918 dump_machine_register(R13);
919 dump_machine_register(R14);
920 dump_machine_register(R15);
921 dump_machine_register(EFL);
922# elif defined __i386__
923 dump_machine_register(GS);
924 dump_machine_register(FS);
925 dump_machine_register(ES);
926 dump_machine_register(DS);
927 dump_machine_register(EDI);
928 dump_machine_register(ESI);
929 dump_machine_register(EBP);
930 dump_machine_register(ESP);
931 dump_machine_register(EBX);
932 dump_machine_register(EDX);
933 dump_machine_register(ECX);
934 dump_machine_register(EAX);
935 dump_machine_register(TRAPNO);
936 dump_machine_register(ERR);
937 dump_machine_register(EIP);
938 dump_machine_register(CS);
939 dump_machine_register(EFL);
940 dump_machine_register(UESP);
941 dump_machine_register(SS);
942# elif defined __aarch64__
943 dump_machine_register(mctx->regs[0], "x0");
944 dump_machine_register(mctx->regs[1], "x1");
945 dump_machine_register(mctx->regs[2], "x2");
946 dump_machine_register(mctx->regs[3], "x3");
947 dump_machine_register(mctx->regs[4], "x4");
948 dump_machine_register(mctx->regs[5], "x5");
949 dump_machine_register(mctx->regs[6], "x6");
950 dump_machine_register(mctx->regs[7], "x7");
951 dump_machine_register(mctx->regs[18], "x18");
952 dump_machine_register(mctx->regs[19], "x19");
953 dump_machine_register(mctx->regs[20], "x20");
954 dump_machine_register(mctx->regs[21], "x21");
955 dump_machine_register(mctx->regs[22], "x22");
956 dump_machine_register(mctx->regs[23], "x23");
957 dump_machine_register(mctx->regs[24], "x24");
958 dump_machine_register(mctx->regs[25], "x25");
959 dump_machine_register(mctx->regs[26], "x26");
960 dump_machine_register(mctx->regs[27], "x27");
961 dump_machine_register(mctx->regs[28], "x28");
962 dump_machine_register(mctx->regs[29], "x29");
963 dump_machine_register(mctx->sp, "sp");
964 dump_machine_register(mctx->fault_address, "fault_address");
965# elif defined __arm__
966 dump_machine_register(mctx->arm_r0, "r0");
967 dump_machine_register(mctx->arm_r1, "r1");
968 dump_machine_register(mctx->arm_r2, "r2");
969 dump_machine_register(mctx->arm_r3, "r3");
970 dump_machine_register(mctx->arm_r4, "r4");
971 dump_machine_register(mctx->arm_r5, "r5");
972 dump_machine_register(mctx->arm_r6, "r6");
973 dump_machine_register(mctx->arm_r7, "r7");
974 dump_machine_register(mctx->arm_r8, "r8");
975 dump_machine_register(mctx->arm_r9, "r9");
976 dump_machine_register(mctx->arm_r10, "r10");
977 dump_machine_register(mctx->arm_sp, "sp");
978 dump_machine_register(mctx->fault_address, "fault_address");
979# elif defined __riscv
980 dump_machine_register(mctx->__gregs[REG_SP], "sp");
981 dump_machine_register(mctx->__gregs[REG_S0], "s0");
982 dump_machine_register(mctx->__gregs[REG_S1], "s1");
983 dump_machine_register(mctx->__gregs[REG_A0], "a0");
984 dump_machine_register(mctx->__gregs[REG_A0+1], "a1");
985 dump_machine_register(mctx->__gregs[REG_A0+2], "a2");
986 dump_machine_register(mctx->__gregs[REG_A0+3], "a3");
987 dump_machine_register(mctx->__gregs[REG_A0+4], "a4");
988 dump_machine_register(mctx->__gregs[REG_A0+5], "a5");
989 dump_machine_register(mctx->__gregs[REG_A0+6], "a6");
990 dump_machine_register(mctx->__gregs[REG_A0+7], "a7");
991 dump_machine_register(mctx->__gregs[REG_S2], "s2");
992 dump_machine_register(mctx->__gregs[REG_S2+1], "s3");
993 dump_machine_register(mctx->__gregs[REG_S2+2], "s4");
994 dump_machine_register(mctx->__gregs[REG_S2+3], "s5");
995 dump_machine_register(mctx->__gregs[REG_S2+4], "s6");
996 dump_machine_register(mctx->__gregs[REG_S2+5], "s7");
997 dump_machine_register(mctx->__gregs[REG_S2+6], "s8");
998 dump_machine_register(mctx->__gregs[REG_S2+7], "s9");
999 dump_machine_register(mctx->__gregs[REG_S2+8], "s10");
1000 dump_machine_register(mctx->__gregs[REG_S2+9], "s11");
1001# elif defined __loongarch64
1002 dump_machine_register(mctx->__gregs[LARCH_REG_SP], "sp");
1003 dump_machine_register(mctx->__gregs[LARCH_REG_A0], "a0");
1004 dump_machine_register(mctx->__gregs[LARCH_REG_A0+1], "a1");
1005 dump_machine_register(mctx->__gregs[LARCH_REG_A0+2], "a2");
1006 dump_machine_register(mctx->__gregs[LARCH_REG_A0+3], "a3");
1007 dump_machine_register(mctx->__gregs[LARCH_REG_A0+4], "a4");
1008 dump_machine_register(mctx->__gregs[LARCH_REG_A0+5], "a5");
1009 dump_machine_register(mctx->__gregs[LARCH_REG_A0+6], "a6");
1010 dump_machine_register(mctx->__gregs[LARCH_REG_A0+7], "a7");
1011 dump_machine_register(mctx->__gregs[LARCH_REG_A0+8], "fp");
1012 dump_machine_register(mctx->__gregs[LARCH_REG_S0], "s0");
1013 dump_machine_register(mctx->__gregs[LARCH_REG_S1], "s1");
1014 dump_machine_register(mctx->__gregs[LARCH_REG_S2], "s2");
1015 dump_machine_register(mctx->__gregs[LARCH_REG_S0+3], "s3");
1016 dump_machine_register(mctx->__gregs[LARCH_REG_S0+4], "s4");
1017 dump_machine_register(mctx->__gregs[LARCH_REG_S0+5], "s5");
1018 dump_machine_register(mctx->__gregs[LARCH_REG_S0+6], "s6");
1019 dump_machine_register(mctx->__gregs[LARCH_REG_S0+7], "s7");
1020 dump_machine_register(mctx->__gregs[LARCH_REG_S0+8], "s8");
1021# endif
1022 }
1023# elif defined __APPLE__
1024 {
1025 const mcontext_t mctx = ctx->uc_mcontext;
1026# if defined __x86_64__
1027 dump_machine_register(rax);
1028 dump_machine_register(rbx);
1029 dump_machine_register(rcx);
1030 dump_machine_register(rdx);
1031 dump_machine_register(rdi);
1032 dump_machine_register(rsi);
1033 dump_machine_register(rbp);
1034 dump_machine_register(rsp);
1035 dump_machine_register(r8);
1036 dump_machine_register(r9);
1037 dump_machine_register(r10);
1038 dump_machine_register(r11);
1039 dump_machine_register(r12);
1040 dump_machine_register(r13);
1041 dump_machine_register(r14);
1042 dump_machine_register(r15);
1043 dump_machine_register(rip);
1044 dump_machine_register(rflags);
1045# elif defined __i386__
1046 dump_machine_register(eax);
1047 dump_machine_register(ebx);
1048 dump_machine_register(ecx);
1049 dump_machine_register(edx);
1050 dump_machine_register(edi);
1051 dump_machine_register(esi);
1052 dump_machine_register(ebp);
1053 dump_machine_register(esp);
1054 dump_machine_register(ss);
1055 dump_machine_register(eflags);
1056 dump_machine_register(eip);
1057 dump_machine_register(cs);
1058 dump_machine_register(ds);
1059 dump_machine_register(es);
1060 dump_machine_register(fs);
1061 dump_machine_register(gs);
1062# elif defined __aarch64__
1063 dump_machine_register(x[0], "x0");
1064 dump_machine_register(x[1], "x1");
1065 dump_machine_register(x[2], "x2");
1066 dump_machine_register(x[3], "x3");
1067 dump_machine_register(x[4], "x4");
1068 dump_machine_register(x[5], "x5");
1069 dump_machine_register(x[6], "x6");
1070 dump_machine_register(x[7], "x7");
1071 dump_machine_register(x[18], "x18");
1072 dump_machine_register(x[19], "x19");
1073 dump_machine_register(x[20], "x20");
1074 dump_machine_register(x[21], "x21");
1075 dump_machine_register(x[22], "x22");
1076 dump_machine_register(x[23], "x23");
1077 dump_machine_register(x[24], "x24");
1078 dump_machine_register(x[25], "x25");
1079 dump_machine_register(x[26], "x26");
1080 dump_machine_register(x[27], "x27");
1081 dump_machine_register(x[28], "x28");
1082 dump_machine_register(lr, "lr");
1083 dump_machine_register(fp, "fp");
1084 dump_machine_register(sp, "sp");
1085# endif
1086 }
1087# endif
1088 kprintf("\n\n");
1089 return true;
1090
1091 error:
1092 return false;
1093}
1094#else
1095# define rb_dump_machine_register(errout, ctx) ((void)0)
1096#endif /* dump_machine_register */
1097
1098bool
1099rb_vm_bugreport(const void *ctx, FILE *errout)
1100{
1101 const char *cmd = getenv("RUBY_ON_BUG");
1102 if (cmd) {
1103 char buf[0x100];
1104 snprintf(buf, sizeof(buf), "%s %"PRI_PIDT_PREFIX"d", cmd, getpid());
1105 int r = system(buf);
1106 if (r == -1) {
1107 snprintf(buf, sizeof(buf), "Launching RUBY_ON_BUG command failed.");
1108 }
1109 }
1110
1111 // Thread unsafe best effort attempt to stop printing the bug report in an
1112 // infinite loop. Can happen with corrupt Ruby stack.
1113 {
1114 static bool crashing = false;
1115 if (crashing) {
1116 kprintf("Crashed while printing bug report\n");
1117 return true;
1118 }
1119 crashing = true;
1120 }
1121
1122#ifdef __linux__
1123# define PROC_MAPS_NAME "/proc/self/maps"
1124#endif
1125#ifdef PROC_MAPS_NAME
1126 enum {other_runtime_info = 1};
1127#else
1128 enum {other_runtime_info = 0};
1129#endif
1130 const rb_vm_t *const vm = GET_VM();
1131 const rb_execution_context_t *ec = rb_current_execution_context(false);
1132
1133 if (vm && ec) {
1134 rb_vmdebug_stack_dump_raw(ec, ec->cfp, errout);
1135 rb_backtrace_print_as_bugreport(errout);
1136 kputs("\n");
1137 // If we get here, hopefully things are intact enough that
1138 // we can read these two numbers. It is an estimate because
1139 // we are reading without synchronization.
1140 kprintf("-- Threading information "
1141 "---------------------------------------------------\n");
1142 kprintf("Total ractor count: %u\n", vm->ractor.cnt);
1143 kprintf("Ruby thread count for this ractor: %u\n", rb_ec_ractor_ptr(ec)->threads.cnt);
1144 if (rb_fiber_scheduler_get() != Qnil) {
1145 kprintf("Note that the Fiber scheduler is enabled\n");
1146 }
1147 kputs("\n");
1148 }
1149
1150 rb_dump_machine_register(errout, ctx);
1151
1152#if USE_BACKTRACE || defined(_WIN32)
1153 kprintf("-- C level backtrace information "
1154 "-------------------------------------------\n");
1155 rb_print_backtrace(errout);
1156
1157
1158 kprintf("\n");
1159#endif /* USE_BACKTRACE */
1160
1161 if (other_runtime_info || vm) {
1162 kprintf("-- Other runtime information "
1163 "-----------------------------------------------\n\n");
1164 }
1165 if (vm && !rb_during_gc()) {
1166 int i;
1167 VALUE name;
1168 long len;
1169 const int max_name_length = 1024;
1170# define LIMITED_NAME_LENGTH(s) \
1171 (((len = RSTRING_LEN(s)) > max_name_length) ? max_name_length : (int)len)
1172
1173 name = vm->progname;
1174 if (name) {
1175 kprintf("* Loaded script: %.*s\n",
1176 LIMITED_NAME_LENGTH(name), RSTRING_PTR(name));
1177 kprintf("\n");
1178 }
1179 if (vm->loaded_features) {
1180 kprintf("* Loaded features:\n\n");
1181 for (i=0; i<RARRAY_LEN(vm->loaded_features); i++) {
1182 name = RARRAY_AREF(vm->loaded_features, i);
1183 if (RB_TYPE_P(name, T_STRING)) {
1184 kprintf(" %4d %.*s\n", i,
1185 LIMITED_NAME_LENGTH(name), RSTRING_PTR(name));
1186 }
1187 else if (RB_TYPE_P(name, T_CLASS) || RB_TYPE_P(name, T_MODULE)) {
1188 const char *const type = RB_TYPE_P(name, T_CLASS) ?
1189 "class" : "module";
1190 name = rb_search_class_path(rb_class_real(name));
1191 if (!RB_TYPE_P(name, T_STRING)) {
1192 kprintf(" %4d %s:<unnamed>\n", i, type);
1193 continue;
1194 }
1195 kprintf(" %4d %s:%.*s\n", i, type,
1196 LIMITED_NAME_LENGTH(name), RSTRING_PTR(name));
1197 }
1198 else {
1199 VALUE klass = rb_search_class_path(rb_obj_class(name));
1200 if (!RB_TYPE_P(klass, T_STRING)) {
1201 kprintf(" %4d #<%p:%p>\n", i,
1202 (void *)CLASS_OF(name), (void *)name);
1203 continue;
1204 }
1205 kprintf(" %4d #<%.*s:%p>\n", i,
1206 LIMITED_NAME_LENGTH(klass), RSTRING_PTR(klass),
1207 (void *)name);
1208 }
1209 }
1210 }
1211 kprintf("\n");
1212 }
1213
1214 {
1215#ifndef RUBY_ASAN_ENABLED
1216# ifdef PROC_MAPS_NAME
1217 {
1218 FILE *fp = fopen(PROC_MAPS_NAME, "r");
1219 if (fp) {
1220 kprintf("* Process memory map:\n\n");
1221
1222 while (!feof(fp)) {
1223 char buff[0x100];
1224 size_t rn = fread(buff, 1, 0x100, fp);
1225 if (fwrite(buff, 1, rn, errout) != rn)
1226 break;
1227 }
1228
1229 fclose(fp);
1230 kprintf("\n\n");
1231 }
1232 }
1233# endif /* __linux__ */
1234# ifdef HAVE_LIBPROCSTAT
1235# define MIB_KERN_PROC_PID_LEN 4
1236 int mib[MIB_KERN_PROC_PID_LEN];
1237 struct kinfo_proc kp;
1238 size_t len = sizeof(struct kinfo_proc);
1239 mib[0] = CTL_KERN;
1240 mib[1] = KERN_PROC;
1241 mib[2] = KERN_PROC_PID;
1242 mib[3] = getpid();
1243 if (sysctl(mib, MIB_KERN_PROC_PID_LEN, &kp, &len, NULL, 0) == -1) {
1244 kprintf("sysctl: %s\n", strerror(errno));
1245 }
1246 else {
1247 struct procstat *prstat = procstat_open_sysctl();
1248 kprintf("* Process memory map:\n\n");
1249 procstat_vm(prstat, &kp, errout);
1250 procstat_close(prstat);
1251 kprintf("\n");
1252 }
1253# endif /* __FreeBSD__ */
1254# ifdef __APPLE__
1255 vm_address_t addr = 0;
1256 vm_size_t size = 0;
1257 struct vm_region_submap_info map;
1258 mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT;
1259 natural_t depth = 0;
1260
1261 kprintf("* Process memory map:\n\n");
1262 while (1) {
1263 if (vm_region_recurse(mach_task_self(), &addr, &size, &depth,
1264 (vm_region_recurse_info_t)&map, &count) != KERN_SUCCESS) {
1265 break;
1266 }
1267
1268 if (map.is_submap) {
1269 // We only look at main addresses
1270 depth++;
1271 }
1272 else {
1273 kprintf("%lx-%lx %s%s%s", addr, (addr+size),
1274 ((map.protection & VM_PROT_READ) != 0 ? "r" : "-"),
1275 ((map.protection & VM_PROT_WRITE) != 0 ? "w" : "-"),
1276 ((map.protection & VM_PROT_EXECUTE) != 0 ? "x" : "-"));
1277# ifdef HAVE_LIBPROC_H
1278 char buff[PATH_MAX];
1279 if (proc_regionfilename(getpid(), addr, buff, sizeof(buff)) > 0) {
1280 kprintf(" %s", buff);
1281 }
1282# endif
1283 kprintf("\n");
1284 }
1285
1286 addr += size;
1287 size = 0;
1288 }
1289# endif
1290#endif
1291 }
1292 return true;
1293
1294 error:
1295 return false;
1296}
1297
1298bool
1299rb_vmdebug_stack_dump_all_threads(void)
1300{
1301 rb_thread_t *th = NULL;
1302 rb_ractor_t *r = GET_RACTOR();
1303 FILE *errout = stderr;
1304
1305 // TODO: now it only shows current ractor
1306 ccan_list_for_each(&r->threads.set, th, lt_node) {
1307#ifdef NON_SCALAR_THREAD_ID
1308 kprintf("th: %p, native_id: N/A\n", th);
1309#else
1310 kprintf("th: %p, native_id: %p\n", (void *)th, (void *)(uintptr_t)th->nt->thread_id);
1311#endif
1312 if (!rb_vmdebug_stack_dump_raw(th->ec, th->ec->cfp, errout)) goto error;
1313 }
1314 return true;
1315
1316 error:
1317 return false;
1318}
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
TYPE
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
T_IMEMO
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
T_MODULE
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
T_UNDEF
#define T_UNDEF
Old name of RUBY_T_UNDEF.
Definition value_type.h:82
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
T_CLASS
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:247
rb_inspect
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:680
rb_class_real
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:237
rb_during_gc
int rb_during_gc(void)
Queries if the GC is busy.
Definition gc.c:3726
rb_sym2str
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:971
len
int len
Length of the buffer.
Definition io.h:8
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
PRI_PIDT_PREFIX
#define PRI_PIDT_PREFIX
A rb_sprintf() format prefix to be used for a pid_t parameter.
Definition pid_t.h:38
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
StringValueCStr
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
errno
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
scheduler.h
Scheduler APIs.
rb_fiber_scheduler_get
VALUE rb_fiber_scheduler_get(void)
Queries the current scheduler of the current thread that is calling this function.
Definition scheduler.c:143
rb_env_t
Definition vm_core.h:1281
rb_proc_t
Definition vm_core.h:1268
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376