lj_gdbjit.c 21 KB

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