官网参考:http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile
文件:
# demo_gdb.py
from numba import njit, gdb @njit(debug=True) def foo(a): b = a + 1 gdb() # instruct Numba to attach gdb at this location and pause execution c = a * 2.34 d = (a, b, c) print(a, b, c, d) r= foo(123) print(r)
shell
$ NUMBA_OPT=0 python demo_gdb.py Attaching to PID: 27157 GNU gdb (GDB) Red Hat Enterprise Linux 8.0.1-36.el7 ... Attaching to process 27157 ... Reading symbols from...done. 0x00007f0380c31550 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:81 81 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS) Breakpoint 1 at 0x7f036ac388f0: file numba/_helperlib.c, line 1090. Continuing. Breakpoint 1, numba_gdb_breakpoint () at numba/_helperlib.c:1090 1090 } (gdb) s Single stepping until exit from function _ZN5numba7targets8gdb_hook8hook_gdb12$3clocals$3e8impl$242E5Tuple, which has no line number information. __main__::foo$241(long long) () at demo_gdb.py:7 7 c = a * 2.34 (gdb) l 2 3 @njit(debug=True) 4 def foo(a): 5 b = a + 1 6 gdb() # instruct Numba to attach gdb at this location and pause execution 7 c = a * 2.34 8 d = (a, b, c) 9 print(a, b, c, d) 10 11 r= foo(123) (gdb) p a $1 = 123 (gdb) p b $2 = 124 (gdb) p c $3 = 0 (gdb) n 8 d = (a, b, c) (gdb) p c $4 = 287.81999999999999
文件:
from numba import njit, gdb_init import numpy as np @njit(debug=True) def foo(a, index): gdb_init() # instruct Numba to attach gdb at this location, but not to pause execution b = a + 1 c = a * 2.34 d = c[index] # access an address that is a) invalid b) out of the page print(a, b, c, d) bad_index = int(1e9) # this index is invalid z = np.arange(10) r = foo(z, bad_index) print(r)
执行:
$ python demo_gdb_segfault.py Attaching to PID: 5444 GNU gdb (GDB) Red Hat Enterprise Linux 8.0.1-36.el7 ... Attaching to process 5444 ... Reading symbols from...done. 0x00007f8d8010a550 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:81 81 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS) Breakpoint 1 at 0x7f8d6a1118f0: file numba/_helperlib.c, line 1090. Continuing. 0x00007fa7b810a41f in __main__::foo$241(Array , long long) () at demo_gdb_segfault.py:9 9 d = c[index] # access an address that is a) invalid b) out of the page (gdb) p index $1 = 1000000000 (gdb) p c $2 = "p\202\017\364\371U\000\000\000\000\000\000\000\000\000\000\n\000\000\000\000\000\000\000\b\000\000\000\000\000\000\000\240\202\017\364\371U\000\000\n\000\000\000\000\000\000\000\b\000\000\000\000\000\000" (gdb) whatis c type = {i8*, i8*, i64, i64, double*, [1 x i64], [1 x i64]} (gdb) x /32xb c 0x7ffd56195068: 0x70 0x82 0x0f 0xf4 0xf9 0x55 0x00 0x00 0x7ffd56195070: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x7ffd56195078: 0x0a 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x7ffd56195080: 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x00