I get a crash in a situation where I'm trying to iterate over an empty array in a Perl script. The output is
Can't use an undefined value as an ARRAY reference at ...
The array is created by a module and I need to get a stacktrace to help find the root of the problem in this module.
However, when running the script with GDB and trying to get a backtrace, I get a "No stack." message. Indeed, after the "undefined value" error, there are some more lines in the script's output, so it seems like the script doesn't completely crash at this point and thus GDB tries to get its backtrace too late. But the script exits a little bit later (which it wouldn't, if the array had been well defined, so in the long run the error leads to a crash). I'm sorry for the fuzzy problem description, but the code is quite complex (for me) and I'm not sure what is actually happening there... Thus, I'm actually only interested in the following question:
Is there a way to enforce a fatal crash at a given position such that GDB will pick it up and generate a backtrace? Kind of tell GDB: "Here's the problematic variable, please show me a backtrace that lead to it"?
edit: Here is a backtrace including C library information, which I could get for a different crash:
(gdb) bt#0 XQueryExtension (dpy=0x0, name=0x7ffff7e18000 "X-Resource", major_opcode=0x7fffffffc0f4, first_event=0x7fffffffc0f8, first_error=0x7fffffffc0fc) at ../../src/QuExt.c:48#1 0x00007ffff53ac097 in XInitExtension (dpy=dpy@entry=0x0, name=name@entry=0x7ffff7e18000 "X-Resource") at ../../src/InitExt.c:59#2 0x00007ffff5205840 in XextAddDisplay ( extinfo=0x7ffff7e1a110 <_xres_ext_info_data>, dpy=dpy@entry=0x0, ext_name=ext_name@entry=0x7ffff7e18000 "X-Resource", hooks=hooks@entry=0x7ffff7e1a0a0 <xres_extension_hooks>, nevents=nevents@entry=0, data=data@entry=0x0) at ../../src/extutil.c:110#3 0x00007ffff7e163c9 in find_display (dpy=0x0) at /build/libxres-6CIuq8/libxres-1.2.1/src/XRes.c:41#4 0x00007ffff7e164a6 in XResQueryExtension (dpy=dpy@entry=0x0, event_base_return=event_base_return@entry=0x7fffffffc1c8, error_base_return=error_base_return@entry=0x7fffffffc1cc) at /build/libxres-6CIuq8/libxres-1.2.1/src/XRes.c:52#5 0x00007ffff5ed01dd in init_xres (self=0x55555a04a370) at ../libwnck/wnck-handle.c:190#6 wnck_handle_constructed (object=0x55555a04a370) at ../libwnck/wnck-handle.c:208#7 0x00007ffff6dc3bba in g_object_new_internal (class=0x55555a04a240, params=0x7fffffffc380, n_params=1) at ../../../gobject/gobject.c:2654#8 0x00007ffff6dc5bc3 in g_object_new_internal (n_params=1, --Type <RET> for more, q to quit, c to continue without paging--c params=0x7fffffffc380, class=0x55555a04a240) at ../../../gobject/gobject.c:2920#9 g_object_new_valist (object_type=<optimized out>, first_property_name=<optimized out>, var_args=var_args@entry=0x7fffffffc650) at ../../../gobject/gobject.c:2942#10 0x00007ffff6dc5f4f in g_object_new (object_type=<optimized out>, first_property_name=<optimized out>) at ../../../gobject/gobject.c:2415#11 0x00007ffff5ebfdfa in _wnck_get_handle () at ../libwnck/util.c:161#12 wnck_screen_get_default () at ../libwnck/screen.c:562#13 0x00007ffff7e2fb16 in ffi_call_unix64 () at ../src/x86/unix64.S:104#14 0x00007ffff7e2c3ef in ffi_call_int (cif=cif@entry=0x7fffffffc950, fn=<optimized out>, rvalue=<optimized out>, avalue=<optimized out>, closure=closure@entry=0x0) at ../src/x86/ffi64.c:673#15 0x00007ffff7e2f0be in ffi_call (cif=cif@entry=0x7fffffffc950, fn=fn@entry=0x7ffff5ebfdc0 <wnck_screen_get_default>, rvalue=rvalue@entry=0x7fffffffc930, avalue=<optimized out>) at ../src/x86/ffi64.c:710#16 0x00007ffff6bb2c2b in invoke_c_code (info=info@entry=0x55555a046f40, func_pointer=<optimized out>, sp=sp@entry=0x555558aa19e0, ax=ax@entry=1, items=items@entry=4, internal_stack_offset=internal_stack_offset@entry=4, package=<optimized out>, namespace=<optimized out>, function=<optimized out>, mark=<optimized out>) at /usr/src/libglib-object-introspection-perl-0.051-1build3/gperl-i11n-invoke-c.c:202#17 0x00007ffff6bb3aa8 in XS_Glib__Object__Introspection_invoke ( my_perl=<optimized out>, cv=<optimized out>) at /usr/src/libglib-object-introspection-perl-0.051-1build3/GObjectIntrospection.xs:1018#18 0x00005555556901da in ?? ()#19 0x000055555568570e in Perl_runops_standard ()#20 0x00005555555c48ea in Perl_call_sv ()#21 0x00007ffff6bb0aea in invoke_perl_code (cif=<optimized out>, resp=0x7fffffffd120, args=0x7fffffffcf70, userdata=<optimized out>) at /usr/src/libglib-object-introspection-perl-0.051-1build3/gperl-i11n-invoke-perl.c:169#22 0x00007ffff7e2f641 in ffi_closure_unix64_inner (cif=<optimized out>, fun=<optimized out>, user_data=<optimized out>, rvalue=<optimized out>, reg_args=<optimized out>, argp=0x7fffffffd150 "") at ../src/x86/ffi64.c:899#23 0x00007ffff7e2fd38 in ffi_closure_unix64 () at ../src/x86/unix64.S:303#24 0x00007ffff6dd26bd in _g_closure_invoke_va (param_types=0x0, n_params=<optimized out>, args=0x7fffffffd380, instance=0x55555a02a5f0, return_value=0x0, closure=0x555557390320) at ../../../gobject/gclosure.c:897#25 signal_emit_valist_unlocked (instance=instance@entry=0x55555a02a5f0, signal_id=signal_id@entry=239, detail=detail@entry=0, var_args=var_args@entry=0x7fffffffd380) at ../../../gobject/gsignal.c:3424#26 0x00007ffff6dd27c1 in g_signal_emit_valist (instance=0x55555a02a5f0, signal_id=239, detail=0, var_args=var_args@entry=0x7fffffffd380) at ../../../gobject/gsignal.c:3263#27 0x00007ffff6dd2883 in g_signal_emit ( instance=instance@entry=0x55555a02a5f0, signal_id=<optimized out>, detail=detail@entry=0) at ../../../gobject/gsignal.c:3583#28 0x00007ffff6a01f2e in g_application_register ( application=application@entry=0x55555a02a5f0, cancellable=cancellable@entry=0x0, error=error@entry=0x7fffffffd520) at ../../../gio/gapplication.c:2352#29 0x00007ffff6a024d0 in g_application_real_local_command_line ( application=0x55555a02a5f0, arguments=0x7fffffffd578, exit_status=0x7fffffffd574) at ../../../gio/gapplication.c:1155#30 0x00007ffff6a02823 in g_application_run (application=0x55555a02a5f0, argc=<optimized out>, argv=0x0) at ../../../gio/gapplication.c:2681#31 0x00007ffff7e2fb16 in ffi_call_unix64 () at ../src/x86/unix64.S:104#32 0x00007ffff7e2c3ef in ffi_call_int (cif=cif@entry=0x7fffffffd7e0, fn=<optimized out>, rvalue=<optimized out>, avalue=<optimized out>, closure=closure@entry=0x0) at ../src/x86/ffi64.c:673#33 0x00007ffff7e2f0be in ffi_call (cif=cif@entry=0x7fffffffd7e0, fn=fn@entry=0x7ffff6a02720 <g_application_run>, rvalue=rvalue@entry=0x7fffffffd7c0, avalue=<optimized out>) at ../src/x86/ffi64.c:710#34 0x00007ffff6bb2c2b in invoke_c_code (info=info@entry=0x555559f86a90, func_pointer=<optimized out>, sp=sp@entry=0x555558aa19e0, ax=ax@entry=1, items=items@entry=5, internal_stack_offset=internal_stack_offset@entry=4, package=<optimized out>, namespace=<optimized out>, function=<optimized out>, mark=<optimized out>) at /usr/src/libglib-object-introspection-perl-0.051-1build3/gperl-i11n-invoke-c.c:202#35 0x00007ffff6bb3aa8 in XS_Glib__Object__Introspection_invoke ( my_perl=<optimized out>, cv=<optimized out>) at /usr/src/libglib-object-introspection-perl-0.051-1build3/GObjectIntrospection.xs:1018#36 0x00005555556901da in ?? ()#37 0x000055555568570e in Perl_runops_standard ()#38 0x00005555555c3cfd in perl_run ()#39 0x000055555559963a in main ()