src/lj_gdbjit.c - luajit-2.0-src

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** Client for the GDB JIT API.
  3. ** Copyright (C) 2005-2015 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_PPC
  264.   DW_REG_SP = 1,
  265.   DW_REG_RA = 65,
  266.   DW_REG_CR = 70,
  267. #elif LJ_TARGET_MIPS
  268.   DW_REG_SP = 29,
  269.   DW_REG_RA = 31,
  270. #else
  271. #error "Unsupported target architecture"
  272. #endif
  273. };

  274. /* Minimal list of sections for the in-memory ELF object. */
  275. enum {
  276.   GDBJIT_SECT_NULL,
  277.   GDBJIT_SECT_text,
  278.   GDBJIT_SECT_eh_frame,
  279.   GDBJIT_SECT_shstrtab,
  280.   GDBJIT_SECT_strtab,
  281.   GDBJIT_SECT_symtab,
  282.   GDBJIT_SECT_debug_info,
  283.   GDBJIT_SECT_debug_abbrev,
  284.   GDBJIT_SECT_debug_line,
  285.   GDBJIT_SECT__MAX
  286. };

  287. enum {
  288.   GDBJIT_SYM_UNDEF,
  289.   GDBJIT_SYM_FILE,
  290.   GDBJIT_SYM_FUNC,
  291.   GDBJIT_SYM__MAX
  292. };

  293. /* In-memory ELF object. */
  294. typedef struct GDBJITobj {
  295.   ELFheader hdr;                        /* ELF header. */
  296.   ELFsectheader sect[GDBJIT_SECT__MAX];        /* ELF sections. */
  297.   ELFsymbol sym[GDBJIT_SYM__MAX];        /* ELF symbol table. */
  298.   uint8_t space[4096];                        /* Space for various section data. */
  299. } GDBJITobj;

  300. /* Combined structure for GDB JIT entry and ELF object. */
  301. typedef struct GDBJITentryobj {
  302.   GDBJITentry entry;
  303.   size_t sz;
  304.   GDBJITobj obj;
  305. } GDBJITentryobj;

  306. /* Template for in-memory ELF header. */
  307. static const ELFheader elfhdr_template = {
  308.   .emagic = { 0x7f, 'E', 'L', 'F' },
  309.   .eclass = LJ_64 ? 2 : 1,
  310.   .eendian = LJ_ENDIAN_SELECT(1, 2),
  311.   .eversion = 1,
  312. #if LJ_TARGET_LINUX
  313.   .eosabi = 0/* Nope, it's not 3. */
  314. #elif defined(__FreeBSD__)
  315.   .eosabi = 9,
  316. #elif defined(__NetBSD__)
  317.   .eosabi = 2,
  318. #elif defined(__OpenBSD__)
  319.   .eosabi = 12,
  320. #elif defined(__DragonFly__)
  321.   .eosabi = 0,
  322. #elif (defined(__sun__) && defined(__svr4__))
  323.   .eosabi = 6,
  324. #else
  325.   .eosabi = 0,
  326. #endif
  327.   .eabiversion = 0,
  328.   .epad = { 0, 0, 0, 0, 0, 0, 0 },
  329.   .type = 1,
  330. #if LJ_TARGET_X86
  331.   .machine = 3,
  332. #elif LJ_TARGET_X64
  333.   .machine = 62,
  334. #elif LJ_TARGET_ARM
  335.   .machine = 40,
  336. #elif LJ_TARGET_PPC
  337.   .machine = 20,
  338. #elif LJ_TARGET_MIPS
  339.   .machine = 8,
  340. #else
  341. #error "Unsupported target architecture"
  342. #endif
  343.   .version = 1,
  344.   .entry = 0,
  345.   .phofs = 0,
  346.   .shofs = offsetof(GDBJITobj, sect),
  347.   .flags = 0,
  348.   .ehsize = sizeof(ELFheader),
  349.   .phentsize = 0,
  350.   .phnum = 0,
  351.   .shentsize = sizeof(ELFsectheader),
  352.   .shnum = GDBJIT_SECT__MAX,
  353.   .shstridx = GDBJIT_SECT_shstrtab
  354. };

  355. /* -- In-memory ELF object generation ------------------------------------- */

  356. /* Context for generating the ELF object for the GDB JIT API. */
  357. typedef struct GDBJITctx {
  358.   uint8_t *p;                /* Pointer to next address in obj.space. */
  359.   uint8_t *startp;        /* Pointer to start address in obj.space. */
  360.   GCtrace *T;                /* Generate symbols for this trace. */
  361.   uintptr_t mcaddr;        /* Machine code address. */
  362.   MSize szmcode;        /* Size of machine code. */
  363.   MSize spadjp;                /* Stack adjustment for parent trace or interpreter. */
  364.   MSize spadj;                /* Stack adjustment for trace itself. */
  365.   BCLine lineno;        /* Starting line number. */
  366.   const char *filename;        /* Starting file name. */
  367.   size_t objsize;        /* Final size of ELF object. */
  368.   GDBJITobj obj;        /* In-memory ELF object. */
  369. } GDBJITctx;

  370. /* Add a zero-terminated string. */
  371. static uint32_t gdbjit_strz(GDBJITctx *ctx, const char *str)
  372. {
  373.   uint8_t *p = ctx->p;
  374.   uint32_t ofs = (uint32_t)(p - ctx->startp);
  375.   do {
  376.     *p++ = (uint8_t)*str;
  377.   } while (*str++);
  378.   ctx->p = p;
  379.   return ofs;
  380. }

  381. /* Append a decimal number. */
  382. static void gdbjit_catnum(GDBJITctx *ctx, uint32_t n)
  383. {
  384.   if (n >= 10) { uint32_t m = n / 10; n = n % 10; gdbjit_catnum(ctx, m); }
  385.   *ctx->p++ = '0' + n;
  386. }

  387. /* Add a SLEB128 value. */
  388. static void gdbjit_sleb128(GDBJITctx *ctx, int32_t v)
  389. {
  390.   uint8_t *p = ctx->p;
  391.   for (; (uint32_t)(v+0x40) >= 0x80; v >>= 7)
  392.     *p++ = (uint8_t)((v & 0x7f) | 0x80);
  393.   *p++ = (uint8_t)(v & 0x7f);
  394.   ctx->p = p;
  395. }

  396. /* Shortcuts to generate DWARF structures. */
  397. #define DB(x)                (*p++ = (x))
  398. #define DI8(x)                (*(int8_t *)p = (x), p++)
  399. #define DU16(x)                (*(uint16_t *)p = (x), p += 2)
  400. #define DU32(x)                (*(uint32_t *)p = (x), p += 4)
  401. #define DADDR(x)        (*(uintptr_t *)p = (x), p += sizeof(uintptr_t))
  402. #define DUV(x)                (p = (uint8_t *)lj_strfmt_wuleb128((char *)p, (x)))
  403. #define DSV(x)                (ctx->p = p, gdbjit_sleb128(ctx, (x)), p = ctx->p)
  404. #define DSTR(str)        (ctx->p = p, gdbjit_strz(ctx, (str)), p = ctx->p)
  405. #define DALIGNNOP(s)        while ((uintptr_t)p & ((s)-1)) *p++ = DW_CFA_nop
  406. #define DSECT(name, stmt) \
  407.   { uint32_t *szp_##name = (uint32_t *)p; p += 4; stmt \
  408.     *szp_##name = (uint32_t)((p-(uint8_t *)szp_##name)-4); } \

  409. /* Initialize ELF section headers. */
  410. static void LJ_FASTCALL gdbjit_secthdr(GDBJITctx *ctx)
  411. {
  412.   ELFsectheader *sect;

  413.   *ctx->p++ = '\0'/* Empty string at start of string table. */

  414. #define SECTDEF(id, tp, al) \
  415.   sect = &ctx->obj.sect[GDBJIT_SECT_##id]; \
  416.   sect->name = gdbjit_strz(ctx, "." #id); \
  417.   sect->type = ELFSECT_TYPE_##tp; \
  418.   sect->align = (al)

  419.   SECTDEF(text, NOBITS, 16);
  420.   sect->flags = ELFSECT_FLAGS_ALLOC|ELFSECT_FLAGS_EXEC;
  421.   sect->addr = ctx->mcaddr;
  422.   sect->ofs = 0;
  423.   sect->size = ctx->szmcode;

  424.   SECTDEF(eh_frame, PROGBITS, sizeof(uintptr_t));
  425.   sect->flags = ELFSECT_FLAGS_ALLOC;

  426.   SECTDEF(shstrtab, STRTAB, 1);
  427.   SECTDEF(strtab, STRTAB, 1);

  428.   SECTDEF(symtab, SYMTAB, sizeof(uintptr_t));
  429.   sect->ofs = offsetof(GDBJITobj, sym);
  430.   sect->size = sizeof(ctx->obj.sym);
  431.   sect->link = GDBJIT_SECT_strtab;
  432.   sect->entsize = sizeof(ELFsymbol);
  433.   sect->info = GDBJIT_SYM_FUNC;

  434.   SECTDEF(debug_info, PROGBITS, 1);
  435.   SECTDEF(debug_abbrev, PROGBITS, 1);
  436.   SECTDEF(debug_line, PROGBITS, 1);

  437. #undef SECTDEF
  438. }

  439. /* Initialize symbol table. */
  440. static void LJ_FASTCALL gdbjit_symtab(GDBJITctx *ctx)
  441. {
  442.   ELFsymbol *sym;

  443.   *ctx->p++ = '\0'/* Empty string at start of string table. */

  444.   sym = &ctx->obj.sym[GDBJIT_SYM_FILE];
  445.   sym->name = gdbjit_strz(ctx, "JIT mcode");
  446.   sym->sectidx = ELFSECT_IDX_ABS;
  447.   sym->info = ELFSYM_TYPE_FILE|ELFSYM_BIND_LOCAL;

  448.   sym = &ctx->obj.sym[GDBJIT_SYM_FUNC];
  449.   sym->name = gdbjit_strz(ctx, "TRACE_"); ctx->p--;
  450.   gdbjit_catnum(ctx, ctx->T->traceno); *ctx->p++ = '\0';
  451.   sym->sectidx = GDBJIT_SECT_text;
  452.   sym->value = 0;
  453.   sym->size = ctx->szmcode;
  454.   sym->info = ELFSYM_TYPE_FUNC|ELFSYM_BIND_GLOBAL;
  455. }

  456. /* Initialize .eh_frame section. */
  457. static void LJ_FASTCALL gdbjit_ehframe(GDBJITctx *ctx)
  458. {
  459.   uint8_t *p = ctx->p;
  460.   uint8_t *framep = p;

  461.   /* Emit DWARF EH CIE. */
  462.   DSECT(CIE,
  463.     DU32(0);                        /* Offset to CIE itself. */
  464.     DB(DW_CIE_VERSION);
  465.     DSTR("zR");                        /* Augmentation. */
  466.     DUV(1);                        /* Code alignment factor. */
  467.     DSV(-(int32_t)sizeof(uintptr_t));  /* Data alignment factor. */
  468.     DB(DW_REG_RA);                /* Return address register. */
  469.     DB(1); DB(DW_EH_PE_textrel|DW_EH_PE_udata4);  /* Augmentation data. */
  470.     DB(DW_CFA_def_cfa); DUV(DW_REG_SP); DUV(sizeof(uintptr_t));
  471. #if LJ_TARGET_PPC
  472.     DB(DW_CFA_offset_extended_sf); DB(DW_REG_RA); DSV(-1);
  473. #else
  474.     DB(DW_CFA_offset|DW_REG_RA); DUV(1);
  475. #endif
  476.     DALIGNNOP(sizeof(uintptr_t));
  477.   )

  478.   /* Emit DWARF EH FDE. */
  479.   DSECT(FDE,
  480.     DU32((uint32_t)(p-framep));        /* Offset to CIE. */
  481.     DU32(0);                        /* Machine code offset relative to .text. */
  482.     DU32(ctx->szmcode);                /* Machine code length. */
  483.     DB(0);                        /* Augmentation data. */
  484.     /* Registers saved in CFRAME. */
  485. #if LJ_TARGET_X86
  486.     DB(DW_CFA_offset|DW_REG_BP); DUV(2);
  487.     DB(DW_CFA_offset|DW_REG_DI); DUV(3);
  488.     DB(DW_CFA_offset|DW_REG_SI); DUV(4);
  489.     DB(DW_CFA_offset|DW_REG_BX); DUV(5);
  490. #elif LJ_TARGET_X64
  491.     DB(DW_CFA_offset|DW_REG_BP); DUV(2);
  492.     DB(DW_CFA_offset|DW_REG_BX); DUV(3);
  493.     DB(DW_CFA_offset|DW_REG_15); DUV(4);
  494.     DB(DW_CFA_offset|DW_REG_14); DUV(5);
  495.     /* Extra registers saved for JIT-compiled code. */
  496.     DB(DW_CFA_offset|DW_REG_13); DUV(9);
  497.     DB(DW_CFA_offset|DW_REG_12); DUV(10);
  498. #elif LJ_TARGET_ARM
  499.     {
  500.       int i;
  501.       for (i = 11; i >= 4; i--) { DB(DW_CFA_offset|i); DUV(2+(11-i)); }
  502.     }
  503. #elif LJ_TARGET_PPC
  504.     {
  505.       int i;
  506.       DB(DW_CFA_offset_extended); DB(DW_REG_CR); DUV(55);
  507.       for (i = 14; i <= 31; i++) {
  508.         DB(DW_CFA_offset|i); DUV(37+(31-i));
  509.         DB(DW_CFA_offset|32|i); DUV(2+2*(31-i));
  510.       }
  511.     }
  512. #elif LJ_TARGET_MIPS
  513.     {
  514.       int i;
  515.       DB(DW_CFA_offset|30); DUV(2);
  516.       for (i = 23; i >= 16; i--) { DB(DW_CFA_offset|i); DUV(26-i); }
  517.       for (i = 30; i >= 20; i -= 2) { DB(DW_CFA_offset|32|i); DUV(42-i); }
  518.     }
  519. #else
  520. #error "Unsupported target architecture"
  521. #endif
  522.     if (ctx->spadjp != ctx->spadj) {  /* Parent/interpreter stack frame size. */
  523.       DB(DW_CFA_def_cfa_offset); DUV(ctx->spadjp);
  524.       DB(DW_CFA_advance_loc|1);  /* Only an approximation. */
  525.     }
  526.     DB(DW_CFA_def_cfa_offset); DUV(ctx->spadj);  /* Trace stack frame size. */
  527.     DALIGNNOP(sizeof(uintptr_t));
  528.   )

  529.   ctx->p = p;
  530. }

  531. /* Initialize .debug_info section. */
  532. static void LJ_FASTCALL gdbjit_debuginfo(GDBJITctx *ctx)
  533. {
  534.   uint8_t *p = ctx->p;

  535.   DSECT(info,
  536.     DU16(2);                        /* DWARF version. */
  537.     DU32(0);                        /* Abbrev offset. */
  538.     DB(sizeof(uintptr_t));        /* Pointer size. */

  539.     DUV(1);                        /* Abbrev #1: DW_TAG_compile_unit. */
  540.     DSTR(ctx->filename);        /* DW_AT_name. */
  541.     DADDR(ctx->mcaddr);                /* DW_AT_low_pc. */
  542.     DADDR(ctx->mcaddr + ctx->szmcode);  /* DW_AT_high_pc. */
  543.     DU32(0);                        /* DW_AT_stmt_list. */
  544.   )

  545.   ctx->p = p;
  546. }

  547. /* Initialize .debug_abbrev section. */
  548. static void LJ_FASTCALL gdbjit_debugabbrev(GDBJITctx *ctx)
  549. {
  550.   uint8_t *p = ctx->p;

  551.   /* Abbrev #1: DW_TAG_compile_unit. */
  552.   DUV(1); DUV(DW_TAG_compile_unit);
  553.   DB(DW_children_no);
  554.   DUV(DW_AT_name);        DUV(DW_FORM_string);
  555.   DUV(DW_AT_low_pc);        DUV(DW_FORM_addr);
  556.   DUV(DW_AT_high_pc);        DUV(DW_FORM_addr);
  557.   DUV(DW_AT_stmt_list);        DUV(DW_FORM_data4);
  558.   DB(0); DB(0);

  559.   ctx->p = p;
  560. }

  561. #define DLNE(op, s)        (DB(DW_LNS_extended_op), DUV(1+(s)), DB((op)))

  562. /* Initialize .debug_line section. */
  563. static void LJ_FASTCALL gdbjit_debugline(GDBJITctx *ctx)
  564. {
  565.   uint8_t *p = ctx->p;

  566.   DSECT(line,
  567.     DU16(2);                        /* DWARF version. */
  568.     DSECT(header,
  569.       DB(1);                        /* Minimum instruction length. */
  570.       DB(1);                        /* is_stmt. */
  571.       DI8(0);                        /* Line base for special opcodes. */
  572.       DB(2);                        /* Line range for special opcodes. */
  573.       DB(3+1);                        /* Opcode base at DW_LNS_advance_line+1. */
  574.       DB(0); DB(1); DB(1);        /* Standard opcode lengths. */
  575.       /* Directory table. */
  576.       DB(0);
  577.       /* File name table. */
  578.       DSTR(ctx->filename); DUV(0); DUV(0); DUV(0);
  579.       DB(0);
  580.     )

  581.     DLNE(DW_LNE_set_address, sizeof(uintptr_t)); DADDR(ctx->mcaddr);
  582.     if (ctx->lineno) {
  583.       DB(DW_LNS_advance_line); DSV(ctx->lineno-1);
  584.     }
  585.     DB(DW_LNS_copy);
  586.     DB(DW_LNS_advance_pc); DUV(ctx->szmcode);
  587.     DLNE(DW_LNE_end_sequence, 0);
  588.   )

  589.   ctx->p = p;
  590. }

  591. #undef DLNE

  592. /* Undef shortcuts. */
  593. #undef DB
  594. #undef DI8
  595. #undef DU16
  596. #undef DU32
  597. #undef DADDR
  598. #undef DUV
  599. #undef DSV
  600. #undef DSTR
  601. #undef DALIGNNOP
  602. #undef DSECT

  603. /* Type of a section initializer callback. */
  604. typedef void (LJ_FASTCALL *GDBJITinitf)(GDBJITctx *ctx);

  605. /* Call section initializer and set the section offset and size. */
  606. static void gdbjit_initsect(GDBJITctx *ctx, int sect, GDBJITinitf initf)
  607. {
  608.   ctx->startp = ctx->p;
  609.   ctx->obj.sect[sect].ofs = (uintptr_t)((char *)ctx->p - (char *)&ctx->obj);
  610.   initf(ctx);
  611.   ctx->obj.sect[sect].size = (uintptr_t)(ctx->p - ctx->startp);
  612. }

  613. #define SECTALIGN(p, a) \
  614.   ((p) = (uint8_t *)(((uintptr_t)(p) + ((a)-1)) & ~(uintptr_t)((a)-1)))

  615. /* Build in-memory ELF object. */
  616. static void gdbjit_buildobj(GDBJITctx *ctx)
  617. {
  618.   GDBJITobj *obj = &ctx->obj;
  619.   /* Fill in ELF header and clear structures. */
  620.   memcpy(&obj->hdr, &elfhdr_template, sizeof(ELFheader));
  621.   memset(&obj->sect, 0, sizeof(ELFsectheader)*GDBJIT_SECT__MAX);
  622.   memset(&obj->sym, 0, sizeof(ELFsymbol)*GDBJIT_SYM__MAX);
  623.   /* Initialize sections. */
  624.   ctx->p = obj->space;
  625.   gdbjit_initsect(ctx, GDBJIT_SECT_shstrtab, gdbjit_secthdr);
  626.   gdbjit_initsect(ctx, GDBJIT_SECT_strtab, gdbjit_symtab);
  627.   gdbjit_initsect(ctx, GDBJIT_SECT_debug_info, gdbjit_debuginfo);
  628.   gdbjit_initsect(ctx, GDBJIT_SECT_debug_abbrev, gdbjit_debugabbrev);
  629.   gdbjit_initsect(ctx, GDBJIT_SECT_debug_line, gdbjit_debugline);
  630.   SECTALIGN(ctx->p, sizeof(uintptr_t));
  631.   gdbjit_initsect(ctx, GDBJIT_SECT_eh_frame, gdbjit_ehframe);
  632.   ctx->objsize = (size_t)((char *)ctx->p - (char *)obj);
  633.   lua_assert(ctx->objsize < sizeof(GDBJITobj));
  634. }

  635. #undef SECTALIGN

  636. /* -- Interface to GDB JIT API -------------------------------------------- */

  637. /* Add new entry to GDB JIT symbol chain. */
  638. static void gdbjit_newentry(lua_State *L, GDBJITctx *ctx)
  639. {
  640.   /* Allocate memory for GDB JIT entry and ELF object. */
  641.   MSize sz = (MSize)(sizeof(GDBJITentryobj) - sizeof(GDBJITobj) + ctx->objsize);
  642.   GDBJITentryobj *eo = lj_mem_newt(L, sz, GDBJITentryobj);
  643.   memcpy(&eo->obj, &ctx->obj, ctx->objsize);  /* Copy ELF object. */
  644.   eo->sz = sz;
  645.   ctx->T->gdbjit_entry = (void *)eo;
  646.   /* Link new entry to chain and register it. */
  647.   eo->entry.prev_entry = NULL;
  648.   eo->entry.next_entry = __jit_debug_descriptor.first_entry;
  649.   if (eo->entry.next_entry)
  650.     eo->entry.next_entry->prev_entry = &eo->entry;
  651.   eo->entry.symfile_addr = (const char *)&eo->obj;
  652.   eo->entry.symfile_size = ctx->objsize;
  653.   __jit_debug_descriptor.first_entry = &eo->entry;
  654.   __jit_debug_descriptor.relevant_entry = &eo->entry;
  655.   __jit_debug_descriptor.action_flag = GDBJIT_REGISTER;
  656.   __jit_debug_register_code();
  657. }

  658. /* Add debug info for newly compiled trace and notify GDB. */
  659. void lj_gdbjit_addtrace(jit_State *J, GCtrace *T)
  660. {
  661.   GDBJITctx ctx;
  662.   GCproto *pt = &gcref(T->startpt)->pt;
  663.   TraceNo parent = T->ir[REF_BASE].op1;
  664.   const BCIns *startpc = mref(T->startpc, const BCIns);
  665.   ctx.T = T;
  666.   ctx.mcaddr = (uintptr_t)T->mcode;
  667.   ctx.szmcode = T->szmcode;
  668.   ctx.spadjp = CFRAME_SIZE_JIT +
  669.                (MSize)(parent ? traceref(J, parent)->spadjust : 0);
  670.   ctx.spadj = CFRAME_SIZE_JIT + T->spadjust;
  671.   lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc);
  672.   ctx.lineno = lj_debug_line(pt, proto_bcpos(pt, startpc));
  673.   ctx.filename = proto_chunknamestr(pt);
  674.   if (*ctx.filename == '@' || *ctx.filename == '=')
  675.     ctx.filename++;
  676.   else
  677.     ctx.filename = "(string)";
  678.   gdbjit_buildobj(&ctx);
  679.   gdbjit_newentry(J->L, &ctx);
  680. }

  681. /* Delete debug info for trace and notify GDB. */
  682. void lj_gdbjit_deltrace(jit_State *J, GCtrace *T)
  683. {
  684.   GDBJITentryobj *eo = (GDBJITentryobj *)T->gdbjit_entry;
  685.   if (eo) {
  686.     if (eo->entry.prev_entry)
  687.       eo->entry.prev_entry->next_entry = eo->entry.next_entry;
  688.     else
  689.       __jit_debug_descriptor.first_entry = eo->entry.next_entry;
  690.     if (eo->entry.next_entry)
  691.       eo->entry.next_entry->prev_entry = eo->entry.prev_entry;
  692.     __jit_debug_descriptor.relevant_entry = &eo->entry;
  693.     __jit_debug_descriptor.action_flag = GDBJIT_UNREGISTER;
  694.     __jit_debug_register_code();
  695.     lj_mem_free(J2G(J), eo, eo->sz);
  696.   }
  697. }

  698. #endif
  699. #endif