11#include "ruby/internal/config.h"
22# include <mach/vm_map.h>
23# include <mach/mach_init.h>
25# define vm_region_recurse vm_region_recurse_64
34#include "internal/gc.h"
35#include "internal/variable.h"
36#include "internal/vm.h"
39#include "ractor_core.h"
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))
47const char *rb_method_type_name(rb_method_type_t
type);
50#define kprintf(...) if (fprintf(errout, __VA_ARGS__) < 0) goto error
51#define kputs(s) if (fputs(s, errout) < 0) goto error
57 ptrdiff_t ep = cfp->ep - ec->vm_stack;
58 char ep_in_heap =
' ';
59 char posbuf[MAX_POSBUF+1];
61 const char *magic, *iseq_name =
"-", *selfstr =
"-", *biseq_name =
"-";
66 if (ep < 0 || (
size_t)ep > ec->vm_stack_size) {
67 ep = (ptrdiff_t)cfp->ep;
71 switch (VM_FRAME_TYPE(cfp)) {
72 case VM_FRAME_MAGIC_TOP:
75 case VM_FRAME_MAGIC_METHOD:
78 case VM_FRAME_MAGIC_CLASS:
81 case VM_FRAME_MAGIC_BLOCK:
84 case VM_FRAME_MAGIC_CFUNC:
87 case VM_FRAME_MAGIC_IFUNC:
90 case VM_FRAME_MAGIC_EVAL:
93 case VM_FRAME_MAGIC_RESCUE:
96 case VM_FRAME_MAGIC_DUMMY:
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>";
122 iseq_name = RSTRING_PTR(tmp);
123 snprintf(posbuf, MAX_POSBUF,
":%s", iseq_name);
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);
133 snprintf(posbuf, MAX_POSBUF,
"%s:%d", RSTRING_PTR(rb_iseq_path(iseq)), line);
137 iseq_name =
"<dummy_frame>";
141 else if (me != NULL) {
142 iseq_name = rb_id2name(me->def->original_id);
143 snprintf(posbuf, MAX_POSBUF,
":%s", iseq_name);
147 kprintf(
"c:%04"PRIdPTRDIFF
" ",
153 kprintf(
"p:%04"PRIdPTRDIFF
" ", pc);
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);
159 kprintf(
" %s", posbuf);
161 if (VM_FRAME_FINISHED_P(cfp)) {
162 kprintf(
" [FINISH]");
166 kprintf(
"iseq: %-24s ", iseq_name);
167 kprintf(
"self: %-24s ", selfstr);
168 kprintf(
"%-1s ", biseq_name);
177 if (IMEMO_TYPE_P(me, imemo_ment)) {
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));
186 kprintf(
" me is corrupted (%s)\n", rb_raw_obj_info(buff, 0x100, (
VALUE)me));
190 kprintf(
" self: %s\n", rb_raw_obj_info(buff, 0x100, cfp->self));
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;
198 rb_id2name(ISEQ_BODY(iseq)->local_table[i]),
199 rb_raw_obj_info(buff, 0x100, argv[i]));
214 const VALUE *ep = cfp->ep;
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);
222 if (ec->vm_stack <= t && t < sp) {
223 kprintf(
" (= %ld)", (
long)((
VALUE *)GC_GUARDED_PTR_REF((
VALUE)t) - ec->vm_stack));
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);
247rb_vmdebug_stack_dump_raw_current(
void)
250 return rb_vmdebug_stack_dump_raw(ec, ec->cfp, stderr);
257 kprintf(
"-- env --------------------\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");
267 env = rb_vm_env_prev_env(env);
269 kprintf(
"---------------------------\n");
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);
295rb_vmdebug_stack_dump_th(
VALUE thval,
FILE *errout)
298 return rb_vmdebug_stack_dump_raw(target_th->ec, target_th->ec->cfp, errout);
308 const VALUE *bp = prev_cfp->sp + ISEQ_BODY(cfp->iseq)->local_table_size + VM_ENV_DATA_SIZE;
310 if (ISEQ_BODY(cfp->iseq)->type == ISEQ_TYPE_METHOD || VM_FRAME_BMETHOD_P(cfp)) {
319 int i, argc = 0, local_table_size = 0;
322 const VALUE *ep = cfp->ep;
324 if (VM_FRAME_RUBYFRAME_P(cfp)) {
326 argc = ISEQ_BODY(iseq)->param.lead_num;
327 local_table_size = ISEQ_BODY(iseq)->local_table_size;
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)
341 const VALUE *ptr = ep - local_table_size;
343 control_frame_dump(ec, cfp, errout);
345 for (i = 0; i < argc; i++) {
350 for (; i < local_table_size - 1; i++) {
356 ptr = vm_base_ptr(cfp);
357 for (; ptr < sp; ptr++, i++) {
358 switch (
TYPE(*ptr)) {
369 kprintf(
" stack %2d: %8s (%"PRIdPTRDIFF
")\n", i,
StringValueCStr(rstr),
370 (ptr - ec->vm_stack));
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);
382 rb_bug(
"unsupported frame type: %08lx", VM_FRAME_TYPE(cfp));
392 ptrdiff_t ep = cfp->ep - ec->vm_stack;
395 if (VM_FRAME_RUBYFRAME_P(cfp)) {
396 pc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
399 if (ep < 0 || (
size_t)ep > ec->vm_stack_size) {
404 kprintf(
" [PC] %04"PRIdPTRDIFF
", [SP] %04"PRIdPTRDIFF
", [EP] %04"PRIdPTRDIFF
", [CFP] %04"PRIdPTRDIFF
"\n",
405 pc, (cfp->sp - ec->vm_stack), ep, cfpi);
413rb_vmdebug_thread_dump_regs(
VALUE thval,
FILE *errout)
415 return rb_vmdebug_debug_print_register(rb_thread_ptr(thval)->ec, errout);
424 ptrdiff_t pc = _pc - ISEQ_BODY(iseq)->iseq_encoded;
427 for (i=0; i<(int)VM_CFP_CNT(ec, cfp); i++) {
431 if(0) kprintf(
"[%03ld] ", (
long)(cfp->sp - ec->vm_stack));
435 const VALUE *iseq_original = rb_iseq_original_iseq((
rb_iseq_t *)iseq);
437 rb_iseq_disasm_insn(0, iseq_original, (
size_t)pc, iseq, 0);
443 rb_vmdebug_debug_print_register(errout, ec);
455 if (!rb_vmdebug_stack_dump_raw(ec, cfp, errout))
goto errout;
460 rb_vmdebug_debug_print_register(errout, ec);
466 vm_stack_dump_each(ec, ec->cfp, errout);
469 (
"--------------------------------------------------------------\n");
480rb_vmdebug_thread_dump_state(
FILE *errout,
VALUE self)
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);
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
498# define MCTX_SS_REG(reg) ss.reg
502#if defined(HAVE_BACKTRACE)
503# define USE_BACKTRACE 1
504# ifdef HAVE_LIBUNWIND
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>
514backtrace(
void **trace,
int size)
516 unw_cursor_t cursor; unw_context_t 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;
528 unw_get_proc_name(&cursor, buf, 256, &ip);
529 if (strncmp(
"_sigtramp", buf,
sizeof(
"_sigtramp")) == 0) {
530 goto darwin_sigtramp;
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);
591 r = mincore((
const void *)ip, 1, vec);
592 if (r || vec[0] <= 0 || memcmp((
const char *)ip-2,
"\x0f\x05", 2) == 0) {
595 trace[n++] = (
void *)ip;
596 ip = *(unw_word_t*)uctx->uc_mcontext->MCTX_SS_REG(rsp);
599 trace[n++] = (
void *)ip;
600 unw_set_reg(&cursor, UNW_REG_IP, ip);
602 while (unw_step(&cursor) > 0) {
603 unw_get_reg(&cursor, UNW_REG_IP, &ip);
604 trace[n++] = (
void *)ip;
613 while (unw_step(&cursor) > 0) {
614 unw_get_reg(&cursor, UNW_REG_IP, &ip);
619 trace[n++] = (
void *)(ip & 0x7fffffffffffull);
624# elif defined(BROKEN_BACKTRACE)
626# define USE_BACKTRACE 0
629# define USE_BACKTRACE 0
633# include <execinfo.h>
635# include <imagehlp.h>
637# define SYMOPT_DEBUG 0x80000000
640# define MAX_SYM_NAME 2000
648 DWORD ThCallbackStack;
649 DWORD ThCallbackBStore;
652 DWORD64 KiCallUserMode;
653 DWORD64 KeUserCallbackDispatcher;
654 DWORD64 SystemRangeStart;
655 DWORD64 KiUserExceptionDispatcher;
662 ADDRESS64 AddrReturn;
665 ADDRESS64 AddrBStore;
666 void *FuncTableEntry;
697typedef void *PREAD_PROCESS_MEMORY_ROUTINE64;
698typedef void *PFUNCTION_TABLE_ACCESS_ROUTINE64;
699typedef void *PGET_MODULE_BASE_ROUTINE64;
700typedef void *PTRANSLATE_ADDRESS_ROUTINE64;
703struct dump_thead_arg {
709dump_thread(
void *arg)
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;
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);
740 if (SuspendThread(th) != (DWORD)-1) {
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;
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;
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;
777 while (pStackWalk64(mac, ph, th, &frame, &context, NULL,
779 DWORD64 addr = frame.AddrPC.Offset;
780 IMAGEHLP_LINE64 line;
781 DWORD64 displacement;
784 if (addr == frame.AddrReturn.Offset || addr == 0 ||
785 frame.AddrReturn.Offset == 0)
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);
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);
813 FreeLibrary(dbghelp);
818rb_print_backtrace(
FILE *errout)
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);
827 char **syms = backtrace_symbols(trace, n);
830 for (i=0; i<n; i++) {
831 kprintf(
"%s\n", syms[i]);
839 struct dump_thead_arg arg = {
840 .tid = GetCurrentThreadId(),
843 HANDLE th = (HANDLE)_beginthread(dump_thread, 0, &arg);
844 if (th != (HANDLE)-1)
845 WaitForSingleObject(th, INFINITE);
849#ifdef HAVE_LIBPROCSTAT
852static void procstat_vm(
struct procstat *,
struct kinfo_proc *,
FILE *);
853#include "missing/procstat_vm.c"
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))
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))
866# define dump_machine_register(reg) (col_count = print_machine_register(errout, mctx->MCTX_SS_REG(reg), #reg, col_count, 80))
870#ifdef dump_machine_register
872print_machine_register(
FILE *errout,
size_t reg,
const char *reg_name,
int col_count,
int max_col)
876 static const int size_width =
sizeof(size_t) * CHAR_BIT / 4;
878 ret = snprintf(buf,
sizeof(buf),
" %3.3s: 0x%.*" PRIxSIZE, reg_name, size_width, reg);
879 if (col_count + ret > max_col) {
892rb_dump_machine_register(
FILE *errout,
const ucontext_t *ctx)
895 if (!ctx)
return true;
897 kprintf(
"-- Machine register context "
898 "------------------------------------------------\n");
900# if defined __linux__
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");
1023# elif defined __APPLE__
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");
1095# define rb_dump_machine_register(errout, ctx) ((void)0)
1099rb_vm_bugreport(
const void *ctx,
FILE *errout)
1101 const char *cmd = getenv(
"RUBY_ON_BUG");
1105 int r = system(buf);
1107 snprintf(buf,
sizeof(buf),
"Launching RUBY_ON_BUG command failed.");
1114 static bool crashing =
false;
1116 kprintf(
"Crashed while printing bug report\n");
1123# define PROC_MAPS_NAME "/proc/self/maps"
1125#ifdef PROC_MAPS_NAME
1126 enum {other_runtime_info = 1};
1128 enum {other_runtime_info = 0};
1130 const rb_vm_t *
const vm = GET_VM();
1134 rb_vmdebug_stack_dump_raw(ec, ec->cfp, errout);
1135 rb_backtrace_print_as_bugreport(errout);
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);
1145 kprintf(
"Note that the Fiber scheduler is enabled\n");
1150 rb_dump_machine_register(errout, ctx);
1152#if USE_BACKTRACE || defined(_WIN32)
1153 kprintf(
"-- C level backtrace information "
1154 "-------------------------------------------\n");
1155 rb_print_backtrace(errout);
1161 if (other_runtime_info || vm) {
1162 kprintf(
"-- Other runtime information "
1163 "-----------------------------------------------\n\n");
1165 if (vm && !rb_during_gc()) {
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)
1173 name = vm->progname;
1175 kprintf(
"* Loaded script: %.*s\n",
1176 LIMITED_NAME_LENGTH(name), RSTRING_PTR(name));
1179 if (vm->loaded_features) {
1180 kprintf(
"* Loaded features:\n\n");
1181 for (i=0; i<
RARRAY_LEN(vm->loaded_features); i++) {
1184 kprintf(
" %4d %.*s\n", i,
1185 LIMITED_NAME_LENGTH(name), RSTRING_PTR(name));
1192 kprintf(
" %4d %s:<unnamed>\n", i,
type);
1195 kprintf(
" %4d %s:%.*s\n", i,
type,
1196 LIMITED_NAME_LENGTH(name), RSTRING_PTR(name));
1201 kprintf(
" %4d #<%p:%p>\n", i,
1202 (
void *)
CLASS_OF(name), (
void *)name);
1205 kprintf(
" %4d #<%.*s:%p>\n", i,
1206 LIMITED_NAME_LENGTH(klass), RSTRING_PTR(klass),
1215#ifndef RUBY_ASAN_ENABLED
1216# ifdef PROC_MAPS_NAME
1218 FILE *fp = fopen(PROC_MAPS_NAME,
"r");
1220 kprintf(
"* Process memory map:\n\n");
1224 size_t rn = fread(buff, 1, 0x100, fp);
1225 if (fwrite(buff, 1, rn, errout) != rn)
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);
1241 mib[2] = KERN_PROC_PID;
1243 if (sysctl(mib, MIB_KERN_PROC_PID_LEN, &kp, &
len, NULL, 0) == -1) {
1244 kprintf(
"sysctl: %s\n", strerror(
errno));
1247 struct procstat *prstat = procstat_open_sysctl();
1248 kprintf(
"* Process memory map:\n\n");
1249 procstat_vm(prstat, &kp, errout);
1250 procstat_close(prstat);
1255 vm_address_t addr = 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;
1261 kprintf(
"* Process memory map:\n\n");
1263 if (vm_region_recurse(mach_task_self(), &addr, &size, &depth,
1264 (vm_region_recurse_info_t)&map, &count) != KERN_SUCCESS) {
1268 if (map.is_submap) {
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);
1299rb_vmdebug_stack_dump_all_threads(
void)
1303 FILE *errout = stderr;
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);
1310 kprintf(
"th: %p, native_id: %p\n", (
void *)th, (
void *)(uintptr_t)th->nt->thread_id);
1312 if (!rb_vmdebug_stack_dump_raw(th->ec, th->ec->cfp, errout))
goto error;
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define T_STRING
Old name of RUBY_T_STRING.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define CLASS_OF
Old name of rb_class_of.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_UNDEF
Old name of RUBY_T_UNDEF.
#define Qnil
Old name of RUBY_Qnil.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
int len
Length of the buffer.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define PRI_PIDT_PREFIX
A rb_sprintf() format prefix to be used for a pid_t parameter.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define errno
Ractor-aware version of errno.
VALUE rb_fiber_scheduler_get(void)
Queries the current scheduler of the current thread that is calling this function.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.