lj_gdbjit.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. ** Client for the GDB JIT API.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #define lj_gdbjit_c
  6. #define LUA_CORE
  7. #include "lj_obj.h"
  8. #if LJ_HASJIT
  9. #include "lj_gc.h"
  10. #include "lj_err.h"
  11. #include "lj_debug.h"
  12. #include "lj_frame.h"
  13. #include "lj_buf.h"
  14. #include "lj_strfmt.h"
  15. #include "lj_jit.h"
  16. #include "lj_dispatch.h"
  17. /* This is not compiled in by default.
  18. ** Enable with -DLUAJIT_USE_GDBJIT in the Makefile and recompile everything.
  19. */
  20. #ifdef LUAJIT_USE_GDBJIT
  21. /* The GDB JIT API allows JIT compilers to pass debug information about
  22. ** JIT-compiled code back to GDB. You need at least GDB 7.0 or higher
  23. ** to see it in action.
  24. **
  25. ** This is a passive API, so it works even when not running under GDB
  26. ** or when attaching to an already running process. Alas, this implies
  27. ** enabling it always has a non-negligible overhead -- do not use in
  28. ** release mode!
  29. **
  30. ** The LuaJIT GDB JIT client is rather minimal at the moment. It gives
  31. ** each trace a symbol name and adds a source location and frame unwind
  32. ** information. Obviously LuaJIT itself and any embedding C application
  33. ** should be compiled with debug symbols, too (see the Makefile).
  34. **
  35. ** Traces are named TRACE_1, TRACE_2, ... these correspond to the trace
  36. ** numbers from -jv or -jdump. Use "break TRACE_1" or "tbreak TRACE_1" etc.
  37. ** to set breakpoints on specific traces (even ahead of their creation).
  38. **
  39. ** The source location for each trace allows listing the corresponding
  40. ** source lines with the GDB command "list" (but only if the Lua source
  41. ** has been loaded from a file). Currently this is always set to the
  42. ** location where the trace has been started.
  43. **
  44. ** Frame unwind information can be inspected with the GDB command
  45. ** "info frame". This also allows proper backtraces across JIT-compiled
  46. ** code with the GDB command "bt".
  47. **
  48. ** You probably want to add the following settings to a .gdbinit file
  49. ** (or add them to ~/.gdbinit):
  50. ** set disassembly-flavor intel
  51. ** set breakpoint pending on
  52. **
  53. ** Here's a sample GDB session:
  54. ** ------------------------------------------------------------------------
  55. $ cat >x.lua
  56. for outer=1,100 do
  57. for inner=1,100 do end
  58. end
  59. ^D
  60. $ luajit -jv x.lua
  61. [TRACE 1 x.lua:2]
  62. [TRACE 2 (1/3) x.lua:1 -> 1]
  63. $ gdb --quiet --args luajit x.lua
  64. (gdb) tbreak TRACE_1
  65. Function "TRACE_1" not defined.
  66. Temporary breakpoint 1 (TRACE_1) pending.
  67. (gdb) run
  68. Starting program: luajit x.lua
  69. Temporary breakpoint 1, TRACE_1 () at x.lua:2
  70. 2 for inner=1,100 do end
  71. (gdb) list
  72. 1 for outer=1,100 do
  73. 2 for inner=1,100 do end
  74. 3 end
  75. (gdb) bt
  76. #0 TRACE_1 () at x.lua:2
  77. #1 0x08053690 in lua_pcall [...]
  78. [...]
  79. #7 0x0806ff90 in main [...]
  80. (gdb) disass TRACE_1
  81. Dump of assembler code for function TRACE_1:
  82. 0xf7fd9fba <TRACE_1+0>: mov DWORD PTR ds:0xf7e0e2a0,0x1
  83. 0xf7fd9fc4 <TRACE_1+10>: movsd xmm7,QWORD PTR [edx+0x20]
  84. [...]
  85. 0xf7fd9ff8 <TRACE_1+62>: jmp 0xf7fd2014
  86. End of assembler dump.
  87. (gdb) tbreak TRACE_2
  88. Function "TRACE_2" not defined.
  89. Temporary breakpoint 2 (TRACE_2) pending.
  90. (gdb) cont
  91. Continuing.
  92. Temporary breakpoint 2, TRACE_2 () at x.lua:1
  93. 1 for outer=1,100 do
  94. (gdb) info frame
  95. Stack level 0, frame at 0xffffd7c0:
  96. eip = 0xf7fd9f60 in TRACE_2 (x.lua:1); saved eip 0x8053690
  97. called by frame at 0xffffd7e0
  98. source language unknown.
  99. Arglist at 0xffffd78c, args:
  100. Locals at 0xffffd78c, Previous frame's sp is 0xffffd7c0
  101. Saved registers:
  102. ebx at 0xffffd7ac, ebp at 0xffffd7b8, esi at 0xffffd7b0, edi at 0xffffd7b4,
  103. eip at 0xffffd7bc
  104. (gdb)
  105. ** ------------------------------------------------------------------------
  106. */
  107. /* -- GDB JIT API --------------------------------------------------------- */
  108. /* GDB JIT actions. */
  109. enum {
  110. GDBJIT_NOACTION = 0,
  111. GDBJIT_REGISTER,
  112. GDBJIT_UNREGISTER
  113. };
  114. /* GDB JIT entry. */
  115. typedef struct GDBJITentry {
  116. struct GDBJITentry *next_entry;
  117. struct GDBJITentry *prev_entry;
  118. const char *symfile_addr;
  119. uint64_t symfile_size;
  120. } GDBJITentry;
  121. /* GDB JIT descriptor. */
  122. typedef struct GDBJITdesc {
  123. uint32_t version;
  124. uint32_t action_flag;
  125. GDBJITentry *relevant_entry;
  126. GDBJITentry *first_entry;
  127. } GDBJITdesc;
  128. GDBJITdesc __jit_debug_descriptor = {
  129. 1, GDBJIT_NOACTION, NULL, NULL
  130. };
  131. /* GDB sets a breakpoint at this function. */
  132. void LJ_NOINLINE __jit_debug_register_code()
  133. {
  134. __asm__ __volatile__("");
  135. };
  136. /* -- In-memory ELF object definitions ------------------------------------ */
  137. /* ELF definitions. */
  138. typedef struct ELFheader {
  139. uint8_t emagic[4];
  140. uint8_t eclass;
  141. uint8_t eendian;
  142. uint8_t eversion;
  143. uint8_t eosabi;
  144. uint8_t eabiversion;
  145. uint8_t epad[7];
  146. uint16_t type;
  147. uint16_t machine;
  148. uint32_t version;
  149. uintptr_t entry;
  150. uintptr_t phofs;
  151. uintptr_t shofs;
  152. uint32_t flags;
  153. uint16_t ehsize;
  154. uint16_t phentsize;
  155. uint16_t phnum;
  156. uint16_t shentsize;
  157. uint16_t shnum;
  158. uint16_t shstridx;
  159. } ELFheader;
  160. typedef struct ELFsectheader {
  161. uint32_t name;
  162. uint32_t type;
  163. uintptr_t flags;
  164. uintptr_t addr;
  165. uintptr_t ofs;
  166. uintptr_t size;
  167. uint32_t link;
  168. uint32_t info;
  169. uintptr_t align;
  170. uintptr_t entsize;
  171. } ELFsectheader;
  172. #define ELFSECT_IDX_ABS 0xfff1
  173. enum {
  174. ELFSECT_TYPE_PROGBITS = 1,
  175. ELFSECT_TYPE_SYMTAB = 2,
  176. ELFSECT_TYPE_STRTAB = 3,
  177. ELFSECT_TYPE_NOBITS = 8
  178. };
  179. #define ELFSECT_FLAGS_WRITE 1
  180. #define ELFSECT_FLAGS_ALLOC 2
  181. #define ELFSECT_FLAGS_EXEC 4
  182. typedef struct ELFsymbol {
  183. #if LJ_64
  184. uint32_t name;
  185. uint8_t info;
  186. uint8_t other;
  187. uint16_t sectidx;
  188. uintptr_t value;
  189. uint64_t size;
  190. #else
  191. uint32_t name;
  192. uintptr_t value;
  193. uint32_t size;
  194. uint8_t info;
  195. uint8_t other;
  196. uint16_t sectidx;
  197. #endif
  198. } ELFsymbol;
  199. enum {
  200. ELFSYM_TYPE_FUNC = 2,
  201. ELFSYM_TYPE_FILE = 4,
  202. ELFSYM_BIND_LOCAL = 0 << 4,
  203. ELFSYM_BIND_GLOBAL = 1 << 4,
  204. };
  205. /* DWARF definitions. */
  206. #define DW_CIE_VERSION 1
  207. enum {
  208. DW_CFA_nop = 0x0,
  209. DW_CFA_offset_extended = 0x5,
  210. DW_CFA_def_cfa = 0xc,
  211. DW_CFA_def_cfa_offset = 0xe,
  212. DW_CFA_offset_extended_sf = 0x11,
  213. DW_CFA_advance_loc = 0x40,
  214. DW_CFA_offset = 0x80
  215. };
  216. enum {
  217. DW_EH_PE_udata4 = 3,
  218. DW_EH_PE_textrel = 0x20
  219. };
  220. enum {
  221. DW_TAG_compile_unit = 0x11
  222. };
  223. enum {
  224. DW_children_no = 0,
  225. DW_children_yes = 1
  226. };
  227. enum {
  228. DW_AT_name = 0x03,
  229. DW_AT_stmt_list = 0x10,
  230. DW_AT_low_pc = 0x11,
  231. DW_AT_high_pc = 0x12
  232. };
  233. enum {
  234. DW_FORM_addr = 0x01,
  235. DW_FORM_data4 = 0x06,
  236. DW_FORM_string = 0x08
  237. };
  238. enum {
  239. DW_LNS_extended_op = 0,
  240. DW_LNS_copy = 1,
  241. DW_LNS_advance_pc = 2,
  242. DW_LNS_advance_line = 3
  243. };
  244. enum {
  245. DW_LNE_end_sequence = 1,
  246. DW_LNE_set_address = 2
  247. };
  248. enum {
  249. #if LJ_TARGET_X86
  250. DW_REG_AX, DW_REG_CX, DW_REG_DX, DW_REG_BX,
  251. DW_REG_SP, DW_REG_BP, DW_REG_SI, DW_REG_DI,
  252. DW_REG_RA,
  253. #elif LJ_TARGET_X64
  254. /* Yes, the order is strange, but correct. */
  255. DW_REG_AX, DW_REG_DX, DW_REG_CX, DW_REG_BX,
  256. DW_REG_SI, DW_REG_DI, DW_REG_BP, DW_REG_SP,
  257. DW_REG_8, DW_REG_9, DW_REG_10, DW_REG_11,
  258. DW_REG_12, DW_REG_13, DW_REG_14, DW_REG_15,
  259. DW_REG_RA,
  260. #elif LJ_TARGET_ARM
  261. DW_REG_SP = 13,
  262. DW_REG_RA = 14,
  263. #elif LJ_TARGET_ARM64
  264. DW_REG_SP = 31,
  265. DW_REG_RA = 30,
  266. #elif LJ_TARGET_PPC
  267. DW_REG_SP = 1,
  268. DW_REG_RA = 65,
  269. DW_REG_CR = 70,
  270. #elif LJ_TARGET_MIPS
  271. DW_REG_SP = 29,
  272. DW_REG_RA = 31,
  273. #else
  274. #error "Unsupported target architecture"
  275. #endif
  276. };
  277. /* Minimal list of sections for the in-memory ELF object. */
  278. enum {
  279. GDBJIT_SECT_NULL,
  280. GDBJIT_SECT_text,
  281. GDBJIT_SECT_eh_frame,
  282. GDBJIT_SECT_shstrtab,
  283. GDBJIT_SECT_strtab,
  284. GDBJIT_SECT_symtab,
  285. GDBJIT_SECT_debug_info,
  286. GDBJIT_SECT_debug_abbrev,
  287. GDBJIT_SECT_debug_line,
  288. GDBJIT_SECT__MAX
  289. };
  290. enum {
  291. GDBJIT_SYM_UNDEF,
  292. GDBJIT_SYM_FILE,
  293. GDBJIT_SYM_FUNC,
  294. GDBJIT_SYM__MAX
  295. };
  296. /* In-memory ELF object. */
  297. typedef struct GDBJITobj {
  298. ELFheader hdr; /* ELF header. */
  299. ELFsectheader sect[GDBJIT_SECT__MAX]; /* ELF sections. */
  300. ELFsymbol sym[GDBJIT_SYM__MAX]; /* ELF symbol table. */
  301. uint8_t space[4096]; /* Space for various section data. */
  302. } GDBJITobj;
  303. /* Combined structure for GDB JIT entry and ELF object. */
  304. typedef struct GDBJITentryobj {
  305. GDBJITentry entry;
  306. size_t sz;
  307. GDBJITobj obj;
  308. } GDBJITentryobj;
  309. /* Template for in-memory ELF header. */
  310. static const ELFheader elfhdr_template = {
  311. .emagic = { 0x7f, 'E', 'L', 'F' },
  312. .eclass = LJ_64 ? 2 : 1,
  313. .eendian = LJ_ENDIAN_SELECT(1, 2),
  314. .eversion = 1,
  315. #if LJ_TARGET_LINUX
  316. .eosabi = 0, /* Nope, it's not 3. */
  317. #elif defined(__FreeBSD__)
  318. .eosabi = 9,
  319. #elif defined(__NetBSD__)
  320. .eosabi = 2,
  321. #elif defined(__OpenBSD__)
  322. .eosabi = 12,
  323. #elif defined(__DragonFly__)
  324. .eosabi = 0,
  325. #elif LJ_TARGET_SOLARIS
  326. .eosabi = 6,
  327. #else
  328. .eosabi = 0,
  329. #endif
  330. .eabiversion = 0,
  331. .epad = { 0, 0, 0, 0, 0, 0, 0 },
  332. .type = 1,
  333. #if LJ_TARGET_X86
  334. .machine = 3,
  335. #elif LJ_TARGET_X64
  336. .machine = 62,
  337. #elif LJ_TARGET_ARM
  338. .machine = 40,
  339. #elif LJ_TARGET_ARM64
  340. .machine = 183,
  341. #elif LJ_TARGET_PPC
  342. .machine = 20,
  343. #elif LJ_TARGET_MIPS
  344. .machine = 8,
  345. #else
  346. #error "Unsupported target architecture"
  347. #endif
  348. .version = 1,
  349. .entry = 0,
  350. .phofs = 0,
  351. .shofs = offsetof(GDBJITobj, sect),
  352. .flags = 0,
  353. .ehsize = sizeof(ELFheader),
  354. .phentsize = 0,
  355. .phnum = 0,
  356. .shentsize = sizeof(ELFsectheader),
  357. .shnum = GDBJIT_SECT__MAX,
  358. .shstridx = GDBJIT_SECT_shstrtab
  359. };
  360. /* -- In-memory ELF object generation ------------------------------------- */
  361. /* Context for generating the ELF object for the GDB JIT API. */
  362. typedef struct GDBJITctx {
  363. uint8_t *p; /* Pointer to next address in obj.space. */
  364. uint8_t *startp; /* Pointer to start address in obj.space. */
  365. GCtrace *T; /* Generate symbols for this trace. */
  366. uintptr_t mcaddr; /* Machine code address. */
  367. MSize szmcode; /* Size of machine code. */
  368. MSize spadjp; /* Stack adjustment for parent trace or interpreter. */
  369. MSize spadj; /* Stack adjustment for trace itself. */
  370. BCLine lineno; /* Starting line number. */
  371. const char *filename; /* Starting file name. */
  372. size_t objsize; /* Final size of ELF object. */
  373. GDBJITobj obj; /* In-memory ELF object. */
  374. } GDBJITctx;
  375. /* Add a zero-terminated string. */
  376. static uint32_t gdbjit_strz(GDBJITctx *ctx, const char *str)
  377. {
  378. uint8_t *p = ctx->p;
  379. uint32_t ofs = (uint32_t)(p - ctx->startp);
  380. do {
  381. *p++ = (uint8_t)*str;
  382. } while (*str++);
  383. ctx->p = p;
  384. return ofs;
  385. }
  386. /* Append a decimal number. */
  387. static void gdbjit_catnum(GDBJITctx *ctx, uint32_t n)
  388. {
  389. if (n >= 10) { uint32_t m = n / 10; n = n % 10; gdbjit_catnum(ctx, m); }
  390. *ctx->p++ = '0' + n;
  391. }
  392. /* Add a SLEB128 value. */
  393. static void gdbjit_sleb128(GDBJITctx *ctx, int32_t v)
  394. {
  395. uint8_t *p = ctx->p;
  396. for (; (uint32_t)(v+0x40) >= 0x80; v >>= 7)
  397. *p++ = (uint8_t)((v & 0x7f) | 0x80);
  398. *p++ = (uint8_t)(v & 0x7f);
  399. ctx->p = p;
  400. }
  401. /* Shortcuts to generate DWARF structures. */
  402. #define DB(x) (*p++ = (x))
  403. #define DI8(x) (*(int8_t *)p = (x), p++)
  404. #define DU16(x) (*(uint16_t *)p = (x), p += 2)
  405. #define DU32(x) (*(uint32_t *)p = (x), p += 4)
  406. #define DADDR(x) (*(uintptr_t *)p = (x), p += sizeof(uintptr_t))
  407. #define DUV(x) (p = (uint8_t *)lj_strfmt_wuleb128((char *)p, (x)))
  408. #define DSV(x) (ctx->p = p, gdbjit_sleb128(ctx, (x)), p = ctx->p)
  409. #define DSTR(str) (ctx->p = p, gdbjit_strz(ctx, (str)), p = ctx->p)
  410. #define DALIGNNOP(s) while ((uintptr_t)p & ((s)-1)) *p++ = DW_CFA_nop
  411. #define DSECT(name, stmt) \
  412. { uint32_t *szp_##name = (uint32_t *)p; p += 4; stmt \
  413. *szp_##name = (uint32_t)((p-(uint8_t *)szp_##name)-4); } \
  414. /* Initialize ELF section headers. */
  415. static void LJ_FASTCALL gdbjit_secthdr(GDBJITctx *ctx)
  416. {
  417. ELFsectheader *sect;
  418. *ctx->p++ = '\0'; /* Empty string at start of string table. */
  419. #define SECTDEF(id, tp, al) \
  420. sect = &ctx->obj.sect[GDBJIT_SECT_##id]; \
  421. sect->name = gdbjit_strz(ctx, "." #id); \
  422. sect->type = ELFSECT_TYPE_##tp; \
  423. sect->align = (al)
  424. SECTDEF(text, NOBITS, 16);
  425. sect->flags = ELFSECT_FLAGS_ALLOC|ELFSECT_FLAGS_EXEC;
  426. sect->addr = ctx->mcaddr;
  427. sect->ofs = 0;
  428. sect->size = ctx->szmcode;
  429. SECTDEF(eh_frame, PROGBITS, sizeof(uintptr_t));
  430. sect->flags = ELFSECT_FLAGS_ALLOC;
  431. SECTDEF(shstrtab, STRTAB, 1);
  432. SECTDEF(strtab, STRTAB, 1);
  433. SECTDEF(symtab, SYMTAB, sizeof(uintptr_t));
  434. sect->ofs = offsetof(GDBJITobj, sym);
  435. sect->size = sizeof(ctx->obj.sym);
  436. sect->link = GDBJIT_SECT_strtab;
  437. sect->entsize = sizeof(ELFsymbol);
  438. sect->info = GDBJIT_SYM_FUNC;
  439. SECTDEF(debug_info, PROGBITS, 1);
  440. SECTDEF(debug_abbrev, PROGBITS, 1);
  441. SECTDEF(debug_line, PROGBITS, 1);
  442. #undef SECTDEF
  443. }
  444. /* Initialize symbol table. */
  445. static void LJ_FASTCALL gdbjit_symtab(GDBJITctx *ctx)
  446. {
  447. ELFsymbol *sym;
  448. *ctx->p++ = '\0'; /* Empty string at start of string table. */
  449. sym = &ctx->obj.sym[GDBJIT_SYM_FILE];
  450. sym->name = gdbjit_strz(ctx, "JIT mcode");
  451. sym->sectidx = ELFSECT_IDX_ABS;
  452. sym->info = ELFSYM_TYPE_FILE|ELFSYM_BIND_LOCAL;
  453. sym = &ctx->obj.sym[GDBJIT_SYM_FUNC];
  454. sym->name = gdbjit_strz(ctx, "TRACE_"); ctx->p--;
  455. gdbjit_catnum(ctx, ctx->T->traceno); *ctx->p++ = '\0';
  456. sym->sectidx = GDBJIT_SECT_text;
  457. sym->value = 0;
  458. sym->size = ctx->szmcode;
  459. sym->info = ELFSYM_TYPE_FUNC|ELFSYM_BIND_GLOBAL;
  460. }
  461. /* Initialize .eh_frame section. */
  462. static void LJ_FASTCALL gdbjit_ehframe(GDBJITctx *ctx)
  463. {
  464. uint8_t *p = ctx->p;
  465. uint8_t *framep = p;
  466. /* Emit DWARF EH CIE. */
  467. DSECT(CIE,
  468. DU32(0); /* Offset to CIE itself. */
  469. DB(DW_CIE_VERSION);
  470. DSTR("zR"); /* Augmentation. */
  471. DUV(1); /* Code alignment factor. */
  472. DSV(-(int32_t)sizeof(uintptr_t)); /* Data alignment factor. */
  473. DB(DW_REG_RA); /* Return address register. */
  474. DB(1); DB(DW_EH_PE_textrel|DW_EH_PE_udata4); /* Augmentation data. */
  475. DB(DW_CFA_def_cfa); DUV(DW_REG_SP); DUV(sizeof(uintptr_t));
  476. #if LJ_TARGET_PPC
  477. DB(DW_CFA_offset_extended_sf); DB(DW_REG_RA); DSV(-1);
  478. #else
  479. DB(DW_CFA_offset|DW_REG_RA); DUV(1);
  480. #endif
  481. DALIGNNOP(sizeof(uintptr_t));
  482. )
  483. /* Emit DWARF EH FDE. */
  484. DSECT(FDE,
  485. DU32((uint32_t)(p-framep)); /* Offset to CIE. */
  486. DU32(0); /* Machine code offset relative to .text. */
  487. DU32(ctx->szmcode); /* Machine code length. */
  488. DB(0); /* Augmentation data. */
  489. /* Registers saved in CFRAME. */
  490. #if LJ_TARGET_X86
  491. DB(DW_CFA_offset|DW_REG_BP); DUV(2);
  492. DB(DW_CFA_offset|DW_REG_DI); DUV(3);
  493. DB(DW_CFA_offset|DW_REG_SI); DUV(4);
  494. DB(DW_CFA_offset|DW_REG_BX); DUV(5);
  495. #elif LJ_TARGET_X64
  496. DB(DW_CFA_offset|DW_REG_BP); DUV(2);
  497. DB(DW_CFA_offset|DW_REG_BX); DUV(3);
  498. DB(DW_CFA_offset|DW_REG_15); DUV(4);
  499. DB(DW_CFA_offset|DW_REG_14); DUV(5);
  500. /* Extra registers saved for JIT-compiled code. */
  501. DB(DW_CFA_offset|DW_REG_13); DUV(LJ_GC64 ? 10 : 9);
  502. DB(DW_CFA_offset|DW_REG_12); DUV(LJ_GC64 ? 11 : 10);
  503. #elif LJ_TARGET_ARM
  504. {
  505. int i;
  506. for (i = 11; i >= 4; i--) { DB(DW_CFA_offset|i); DUV(2+(11-i)); }
  507. }
  508. #elif LJ_TARGET_ARM64
  509. {
  510. int i;
  511. DB(DW_CFA_offset|31); DUV(2);
  512. for (i = 28; i >= 19; i--) { DB(DW_CFA_offset|i); DUV(3+(28-i)); }
  513. for (i = 15; i >= 8; i--) { DB(DW_CFA_offset|32|i); DUV(28-i); }
  514. }
  515. #elif LJ_TARGET_PPC
  516. {
  517. int i;
  518. DB(DW_CFA_offset_extended); DB(DW_REG_CR); DUV(55);
  519. for (i = 14; i <= 31; i++) {
  520. DB(DW_CFA_offset|i); DUV(37+(31-i));
  521. DB(DW_CFA_offset|32|i); DUV(2+2*(31-i));
  522. }
  523. }
  524. #elif LJ_TARGET_MIPS
  525. {
  526. int i;
  527. DB(DW_CFA_offset|30); DUV(2);
  528. for (i = 23; i >= 16; i--) { DB(DW_CFA_offset|i); DUV(26-i); }
  529. for (i = 30; i >= 20; i -= 2) { DB(DW_CFA_offset|32|i); DUV(42-i); }
  530. }
  531. #else
  532. #error "Unsupported target architecture"
  533. #endif
  534. if (ctx->spadjp != ctx->spadj) { /* Parent/interpreter stack frame size. */
  535. DB(DW_CFA_def_cfa_offset); DUV(ctx->spadjp);
  536. DB(DW_CFA_advance_loc|1); /* Only an approximation. */
  537. }
  538. DB(DW_CFA_def_cfa_offset); DUV(ctx->spadj); /* Trace stack frame size. */
  539. DALIGNNOP(sizeof(uintptr_t));
  540. )
  541. ctx->p = p;
  542. }
  543. /* Initialize .debug_info section. */
  544. static void LJ_FASTCALL gdbjit_debuginfo(GDBJITctx *ctx)
  545. {
  546. uint8_t *p = ctx->p;
  547. DSECT(info,
  548. DU16(2); /* DWARF version. */
  549. DU32(0); /* Abbrev offset. */
  550. DB(sizeof(uintptr_t)); /* Pointer size. */
  551. DUV(1); /* Abbrev #1: DW_TAG_compile_unit. */
  552. DSTR(ctx->filename); /* DW_AT_name. */
  553. DADDR(ctx->mcaddr); /* DW_AT_low_pc. */
  554. DADDR(ctx->mcaddr + ctx->szmcode); /* DW_AT_high_pc. */
  555. DU32(0); /* DW_AT_stmt_list. */
  556. )
  557. ctx->p = p;
  558. }
  559. /* Initialize .debug_abbrev section. */
  560. static void LJ_FASTCALL gdbjit_debugabbrev(GDBJITctx *ctx)
  561. {
  562. uint8_t *p = ctx->p;
  563. /* Abbrev #1: DW_TAG_compile_unit. */
  564. DUV(1); DUV(DW_TAG_compile_unit);
  565. DB(DW_children_no);
  566. DUV(DW_AT_name); DUV(DW_FORM_string);
  567. DUV(DW_AT_low_pc); DUV(DW_FORM_addr);
  568. DUV(DW_AT_high_pc); DUV(DW_FORM_addr);
  569. DUV(DW_AT_stmt_list); DUV(DW_FORM_data4);
  570. DB(0); DB(0); DB(0);
  571. ctx->p = p;
  572. }
  573. #define DLNE(op, s) (DB(DW_LNS_extended_op), DUV(1+(s)), DB((op)))
  574. /* Initialize .debug_line section. */
  575. static void LJ_FASTCALL gdbjit_debugline(GDBJITctx *ctx)
  576. {
  577. uint8_t *p = ctx->p;
  578. DSECT(line,
  579. DU16(2); /* DWARF version. */
  580. DSECT(header,
  581. DB(1); /* Minimum instruction length. */
  582. DB(1); /* is_stmt. */
  583. DI8(0); /* Line base for special opcodes. */
  584. DB(2); /* Line range for special opcodes. */
  585. DB(3+1); /* Opcode base at DW_LNS_advance_line+1. */
  586. DB(0); DB(1); DB(1); /* Standard opcode lengths. */
  587. /* Directory table. */
  588. DB(0);
  589. /* File name table. */
  590. DSTR(ctx->filename); DUV(0); DUV(0); DUV(0);
  591. DB(0);
  592. )
  593. DLNE(DW_LNE_set_address, sizeof(uintptr_t)); DADDR(ctx->mcaddr);
  594. if (ctx->lineno) {
  595. DB(DW_LNS_advance_line); DSV(ctx->lineno-1);
  596. }
  597. DB(DW_LNS_copy);
  598. DB(DW_LNS_advance_pc); DUV(ctx->szmcode);
  599. DLNE(DW_LNE_end_sequence, 0);
  600. )
  601. ctx->p = p;
  602. }
  603. #undef DLNE
  604. /* Undef shortcuts. */
  605. #undef DB
  606. #undef DI8
  607. #undef DU16
  608. #undef DU32
  609. #undef DADDR
  610. #undef DUV
  611. #undef DSV
  612. #undef DSTR
  613. #undef DALIGNNOP
  614. #undef DSECT
  615. /* Type of a section initializer callback. */
  616. typedef void (LJ_FASTCALL *GDBJITinitf)(GDBJITctx *ctx);
  617. /* Call section initializer and set the section offset and size. */
  618. static void gdbjit_initsect(GDBJITctx *ctx, int sect, GDBJITinitf initf)
  619. {
  620. ctx->startp = ctx->p;
  621. ctx->obj.sect[sect].ofs = (uintptr_t)((char *)ctx->p - (char *)&ctx->obj);
  622. initf(ctx);
  623. ctx->obj.sect[sect].size = (uintptr_t)(ctx->p - ctx->startp);
  624. }
  625. #define SECTALIGN(p, a) \
  626. ((p) = (uint8_t *)(((uintptr_t)(p) + ((a)-1)) & ~(uintptr_t)((a)-1)))
  627. /* Build in-memory ELF object. */
  628. static void gdbjit_buildobj(GDBJITctx *ctx)
  629. {
  630. GDBJITobj *obj = &ctx->obj;
  631. /* Fill in ELF header and clear structures. */
  632. memcpy(&obj->hdr, &elfhdr_template, sizeof(ELFheader));
  633. memset(&obj->sect, 0, sizeof(ELFsectheader)*GDBJIT_SECT__MAX);
  634. memset(&obj->sym, 0, sizeof(ELFsymbol)*GDBJIT_SYM__MAX);
  635. /* Initialize sections. */
  636. ctx->p = obj->space;
  637. gdbjit_initsect(ctx, GDBJIT_SECT_shstrtab, gdbjit_secthdr);
  638. gdbjit_initsect(ctx, GDBJIT_SECT_strtab, gdbjit_symtab);
  639. gdbjit_initsect(ctx, GDBJIT_SECT_debug_info, gdbjit_debuginfo);
  640. gdbjit_initsect(ctx, GDBJIT_SECT_debug_abbrev, gdbjit_debugabbrev);
  641. gdbjit_initsect(ctx, GDBJIT_SECT_debug_line, gdbjit_debugline);
  642. SECTALIGN(ctx->p, sizeof(uintptr_t));
  643. gdbjit_initsect(ctx, GDBJIT_SECT_eh_frame, gdbjit_ehframe);
  644. ctx->objsize = (size_t)((char *)ctx->p - (char *)obj);
  645. lj_assertX(ctx->objsize < sizeof(GDBJITobj), "GDBJITobj overflow");
  646. }
  647. #undef SECTALIGN
  648. /* -- Interface to GDB JIT API -------------------------------------------- */
  649. static int gdbjit_lock;
  650. static void gdbjit_lock_acquire()
  651. {
  652. while (__sync_lock_test_and_set(&gdbjit_lock, 1)) {
  653. /* Just spin; futexes or pthreads aren't worth the portability cost. */
  654. }
  655. }
  656. static void gdbjit_lock_release()
  657. {
  658. __sync_lock_release(&gdbjit_lock);
  659. }
  660. /* Add new entry to GDB JIT symbol chain. */
  661. static void gdbjit_newentry(lua_State *L, GDBJITctx *ctx)
  662. {
  663. /* Allocate memory for GDB JIT entry and ELF object. */
  664. MSize sz = (MSize)(sizeof(GDBJITentryobj) - sizeof(GDBJITobj) + ctx->objsize);
  665. GDBJITentryobj *eo = lj_mem_newt(L, sz, GDBJITentryobj);
  666. memcpy(&eo->obj, &ctx->obj, ctx->objsize); /* Copy ELF object. */
  667. eo->sz = sz;
  668. ctx->T->gdbjit_entry = (void *)eo;
  669. /* Link new entry to chain and register it. */
  670. eo->entry.prev_entry = NULL;
  671. gdbjit_lock_acquire();
  672. eo->entry.next_entry = __jit_debug_descriptor.first_entry;
  673. if (eo->entry.next_entry)
  674. eo->entry.next_entry->prev_entry = &eo->entry;
  675. eo->entry.symfile_addr = (const char *)&eo->obj;
  676. eo->entry.symfile_size = ctx->objsize;
  677. __jit_debug_descriptor.first_entry = &eo->entry;
  678. __jit_debug_descriptor.relevant_entry = &eo->entry;
  679. __jit_debug_descriptor.action_flag = GDBJIT_REGISTER;
  680. __jit_debug_register_code();
  681. gdbjit_lock_release();
  682. }
  683. /* Add debug info for newly compiled trace and notify GDB. */
  684. void lj_gdbjit_addtrace(jit_State *J, GCtrace *T)
  685. {
  686. GDBJITctx ctx;
  687. GCproto *pt = &gcref(T->startpt)->pt;
  688. TraceNo parent = T->ir[REF_BASE].op1;
  689. const BCIns *startpc = mref(T->startpc, const BCIns);
  690. ctx.T = T;
  691. ctx.mcaddr = (uintptr_t)T->mcode;
  692. ctx.szmcode = T->szmcode;
  693. ctx.spadjp = CFRAME_SIZE_JIT +
  694. (MSize)(parent ? traceref(J, parent)->spadjust : 0);
  695. ctx.spadj = CFRAME_SIZE_JIT + T->spadjust;
  696. lj_assertJ(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc,
  697. "start PC out of range");
  698. ctx.lineno = lj_debug_line(pt, proto_bcpos(pt, startpc));
  699. ctx.filename = proto_chunknamestr(pt);
  700. if (*ctx.filename == '@' || *ctx.filename == '=')
  701. ctx.filename++;
  702. else
  703. ctx.filename = "(string)";
  704. gdbjit_buildobj(&ctx);
  705. gdbjit_newentry(J->L, &ctx);
  706. }
  707. /* Delete debug info for trace and notify GDB. */
  708. void lj_gdbjit_deltrace(jit_State *J, GCtrace *T)
  709. {
  710. GDBJITentryobj *eo = (GDBJITentryobj *)T->gdbjit_entry;
  711. if (eo) {
  712. gdbjit_lock_acquire();
  713. if (eo->entry.prev_entry)
  714. eo->entry.prev_entry->next_entry = eo->entry.next_entry;
  715. else
  716. __jit_debug_descriptor.first_entry = eo->entry.next_entry;
  717. if (eo->entry.next_entry)
  718. eo->entry.next_entry->prev_entry = eo->entry.prev_entry;
  719. __jit_debug_descriptor.relevant_entry = &eo->entry;
  720. __jit_debug_descriptor.action_flag = GDBJIT_UNREGISTER;
  721. __jit_debug_register_code();
  722. gdbjit_lock_release();
  723. lj_mem_free(J2G(J), eo, eo->sz);
  724. }
  725. }
  726. #endif
  727. #endif