| ... | ... | @@ -10,6 +10,7 @@ const DW = std.dwarf; |
| 10 | 10 | const leb = std.leb; |
| 11 | 11 | const Allocator = mem.Allocator; |
| 12 | 12 | |
| 13 | const build_options = @import("build_options"); |
| 13 | 14 | const trace = @import("../../tracy.zig").trace; |
| 14 | 15 | const Module = @import("../../Module.zig"); |
| 15 | 16 | const Type = @import("../../type.zig").Type; |
| ... | ... | @@ -87,21 +88,21 @@ debug_aranges_section_dirty: bool = false, |
| 87 | 88 | debug_info_header_dirty: bool = false, |
| 88 | 89 | debug_line_header_dirty: bool = false, |
| 89 | 90 | |
| 90 | | pub const abbrev_compile_unit = 1; |
| 91 | | pub const abbrev_subprogram = 2; |
| 92 | | pub const abbrev_subprogram_retvoid = 3; |
| 93 | | pub const abbrev_base_type = 4; |
| 94 | | pub const abbrev_pad1 = 5; |
| 95 | | pub const abbrev_parameter = 6; |
| 91 | const abbrev_compile_unit = 1; |
| 92 | const abbrev_subprogram = 2; |
| 93 | const abbrev_subprogram_retvoid = 3; |
| 94 | const abbrev_base_type = 4; |
| 95 | const abbrev_pad1 = 5; |
| 96 | const abbrev_parameter = 6; |
| 96 | 97 | |
| 97 | 98 | /// The reloc offset for the virtual address of a function in its Line Number Program. |
| 98 | 99 | /// Size is a virtual address integer. |
| 99 | | pub const dbg_line_vaddr_reloc_index = 3; |
| 100 | const dbg_line_vaddr_reloc_index = 3; |
| 100 | 101 | /// The reloc offset for the virtual address of a function in its .debug_info TAG_subprogram. |
| 101 | 102 | /// Size is a virtual address integer. |
| 102 | | pub const dbg_info_low_pc_reloc_index = 1; |
| 103 | const dbg_info_low_pc_reloc_index = 1; |
| 103 | 104 | |
| 104 | | pub const min_nop_size = 2; |
| 105 | const min_nop_size = 2; |
| 105 | 106 | |
| 106 | 107 | /// You must call this function *after* `MachO.populateMissingMetadata()` |
| 107 | 108 | /// has been called to get a viable debug symbols output. |
| ... | ... | @@ -888,8 +889,304 @@ fn writeStringTable(self: *DebugSymbols) !void { |
| 888 | 889 | self.string_table_dirty = false; |
| 889 | 890 | } |
| 890 | 891 | |
| 892 | pub fn updateDeclLineNumber(self: *DebugSymbols, module: *Module, decl: *const Module.Decl) !void { |
| 893 | const tracy = trace(@src()); |
| 894 | defer tracy.end(); |
| 895 | |
| 896 | const container_scope = decl.scope.cast(Module.Scope.Container).?; |
| 897 | const tree = container_scope.file_scope.contents.tree; |
| 898 | const file_ast_decls = tree.root_node.decls(); |
| 899 | // TODO Look into improving the performance here by adding a token-index-to-line |
| 900 | // lookup table. Currently this involves scanning over the source code for newlines. |
| 901 | const fn_proto = file_ast_decls[decl.src_index].castTag(.FnProto).?; |
| 902 | const block = fn_proto.getBodyNode().?.castTag(.Block).?; |
| 903 | const line_delta = std.zig.lineDelta(tree.source, 0, tree.token_locs[block.lbrace].start); |
| 904 | const casted_line_off = @intCast(u28, line_delta); |
| 905 | |
| 906 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 907 | const shdr = &dwarf_segment.sections.items[self.debug_line_section_index.?]; |
| 908 | const file_pos = shdr.offset + decl.fn_link.macho.off + getRelocDbgLineOff(); |
| 909 | var data: [4]u8 = undefined; |
| 910 | leb.writeUnsignedFixed(4, &data, casted_line_off); |
| 911 | try self.file.pwriteAll(&data, file_pos); |
| 912 | } |
| 913 | |
| 914 | pub const DeclDebugBuffers = struct { |
| 915 | dbg_line_buffer: std.ArrayList(u8), |
| 916 | dbg_info_buffer: std.ArrayList(u8), |
| 917 | dbg_info_type_relocs: link.File.DbgInfoTypeRelocsTable, |
| 918 | }; |
| 919 | |
| 920 | /// Caller owns the returned memory. |
| 921 | pub fn initDeclDebugBuffers( |
| 922 | self: *DebugSymbols, |
| 923 | allocator: *Allocator, |
| 924 | module: *Module, |
| 925 | decl: *Module.Decl, |
| 926 | ) !DeclDebugBuffers { |
| 927 | const tracy = trace(@src()); |
| 928 | defer tracy.end(); |
| 929 | |
| 930 | var dbg_line_buffer = std.ArrayList(u8).init(allocator); |
| 931 | var dbg_info_buffer = std.ArrayList(u8).init(allocator); |
| 932 | var dbg_info_type_relocs: link.File.DbgInfoTypeRelocsTable = .{}; |
| 933 | |
| 934 | const typed_value = decl.typed_value.most_recent.typed_value; |
| 935 | switch (typed_value.ty.zigTypeTag()) { |
| 936 | .Fn => { |
| 937 | const zir_dumps = if (std.builtin.is_test) &[0][]const u8{} else build_options.zir_dumps; |
| 938 | if (zir_dumps.len != 0) { |
| 939 | for (zir_dumps) |fn_name| { |
| 940 | if (mem.eql(u8, mem.spanZ(decl.name), fn_name)) { |
| 941 | std.debug.print("\n{}\n", .{decl.name}); |
| 942 | typed_value.val.cast(Value.Payload.Function).?.func.dump(module.*); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | // For functions we need to add a prologue to the debug line program. |
| 948 | try dbg_line_buffer.ensureCapacity(26); |
| 949 | |
| 950 | const line_off: u28 = blk: { |
| 951 | if (decl.scope.cast(Module.Scope.Container)) |container_scope| { |
| 952 | const tree = container_scope.file_scope.contents.tree; |
| 953 | const file_ast_decls = tree.root_node.decls(); |
| 954 | // TODO Look into improving the performance here by adding a token-index-to-line |
| 955 | // lookup table. Currently this involves scanning over the source code for newlines. |
| 956 | const fn_proto = file_ast_decls[decl.src_index].castTag(.FnProto).?; |
| 957 | const block = fn_proto.getBodyNode().?.castTag(.Block).?; |
| 958 | const line_delta = std.zig.lineDelta(tree.source, 0, tree.token_locs[block.lbrace].start); |
| 959 | break :blk @intCast(u28, line_delta); |
| 960 | } else if (decl.scope.cast(Module.Scope.ZIRModule)) |zir_module| { |
| 961 | const byte_off = zir_module.contents.module.decls[decl.src_index].inst.src; |
| 962 | const line_delta = std.zig.lineDelta(zir_module.source.bytes, 0, byte_off); |
| 963 | break :blk @intCast(u28, line_delta); |
| 964 | } else { |
| 965 | unreachable; |
| 966 | } |
| 967 | }; |
| 968 | |
| 969 | dbg_line_buffer.appendSliceAssumeCapacity(&[_]u8{ |
| 970 | DW.LNS_extended_op, |
| 971 | @sizeOf(u64) + 1, |
| 972 | DW.LNE_set_address, |
| 973 | }); |
| 974 | // This is the "relocatable" vaddr, corresponding to `code_buffer` index `0`. |
| 975 | assert(dbg_line_vaddr_reloc_index == dbg_line_buffer.items.len); |
| 976 | dbg_line_buffer.items.len += @sizeOf(u64); |
| 977 | |
| 978 | dbg_line_buffer.appendAssumeCapacity(DW.LNS_advance_line); |
| 979 | // This is the "relocatable" relative line offset from the previous function's end curly |
| 980 | // to this function's begin curly. |
| 981 | assert(getRelocDbgLineOff() == dbg_line_buffer.items.len); |
| 982 | // Here we use a ULEB128-fixed-4 to make sure this field can be overwritten later. |
| 983 | leb.writeUnsignedFixed(4, dbg_line_buffer.addManyAsArrayAssumeCapacity(4), line_off); |
| 984 | |
| 985 | dbg_line_buffer.appendAssumeCapacity(DW.LNS_set_file); |
| 986 | assert(getRelocDbgFileIndex() == dbg_line_buffer.items.len); |
| 987 | // Once we support more than one source file, this will have the ability to be more |
| 988 | // than one possible value. |
| 989 | const file_index = 1; |
| 990 | leb.writeUnsignedFixed(4, dbg_line_buffer.addManyAsArrayAssumeCapacity(4), file_index); |
| 991 | |
| 992 | // Emit a line for the begin curly with prologue_end=false. The codegen will |
| 993 | // do the work of setting prologue_end=true and epilogue_begin=true. |
| 994 | dbg_line_buffer.appendAssumeCapacity(DW.LNS_copy); |
| 995 | |
| 996 | // .debug_info subprogram |
| 997 | const decl_name_with_null = decl.name[0 .. mem.lenZ(decl.name) + 1]; |
| 998 | try dbg_info_buffer.ensureCapacity(dbg_info_buffer.items.len + 27 + decl_name_with_null.len); |
| 999 | |
| 1000 | const fn_ret_type = typed_value.ty.fnReturnType(); |
| 1001 | const fn_ret_has_bits = fn_ret_type.hasCodeGenBits(); |
| 1002 | if (fn_ret_has_bits) { |
| 1003 | dbg_info_buffer.appendAssumeCapacity(abbrev_subprogram); |
| 1004 | } else { |
| 1005 | dbg_info_buffer.appendAssumeCapacity(abbrev_subprogram_retvoid); |
| 1006 | } |
| 1007 | // These get overwritten after generating the machine code. These values are |
| 1008 | // "relocations" and have to be in this fixed place so that functions can be |
| 1009 | // moved in virtual address space. |
| 1010 | assert(dbg_info_low_pc_reloc_index == dbg_info_buffer.items.len); |
| 1011 | dbg_info_buffer.items.len += @sizeOf(u64); // DW.AT_low_pc, DW.FORM_addr |
| 1012 | assert(getRelocDbgInfoSubprogramHighPC() == dbg_info_buffer.items.len); |
| 1013 | dbg_info_buffer.items.len += 4; // DW.AT_high_pc, DW.FORM_data4 |
| 1014 | if (fn_ret_has_bits) { |
| 1015 | const gop = try dbg_info_type_relocs.getOrPut(allocator, fn_ret_type); |
| 1016 | if (!gop.found_existing) { |
| 1017 | gop.entry.value = .{ |
| 1018 | .off = undefined, |
| 1019 | .relocs = .{}, |
| 1020 | }; |
| 1021 | } |
| 1022 | try gop.entry.value.relocs.append(allocator, @intCast(u32, dbg_info_buffer.items.len)); |
| 1023 | dbg_info_buffer.items.len += 4; // DW.AT_type, DW.FORM_ref4 |
| 1024 | } |
| 1025 | dbg_info_buffer.appendSliceAssumeCapacity(decl_name_with_null); // DW.AT_name, DW.FORM_string |
| 1026 | mem.writeIntLittle(u32, dbg_info_buffer.addManyAsArrayAssumeCapacity(4), line_off + 1); // DW.AT_decl_line, DW.FORM_data4 |
| 1027 | dbg_info_buffer.appendAssumeCapacity(file_index); // DW.AT_decl_file, DW.FORM_data1 |
| 1028 | }, |
| 1029 | else => { |
| 1030 | // TODO implement .debug_info for global variables |
| 1031 | }, |
| 1032 | } |
| 1033 | |
| 1034 | return DeclDebugBuffers{ |
| 1035 | .dbg_info_buffer = dbg_info_buffer, |
| 1036 | .dbg_line_buffer = dbg_line_buffer, |
| 1037 | .dbg_info_type_relocs = dbg_info_type_relocs, |
| 1038 | }; |
| 1039 | } |
| 1040 | |
| 1041 | pub fn commitDeclDebugInfo( |
| 1042 | self: *DebugSymbols, |
| 1043 | allocator: *Allocator, |
| 1044 | module: *Module, |
| 1045 | decl: *Module.Decl, |
| 1046 | debug_buffers: *DeclDebugBuffers, |
| 1047 | target: std.Target, |
| 1048 | ) !void { |
| 1049 | const tracy = trace(@src()); |
| 1050 | defer tracy.end(); |
| 1051 | |
| 1052 | var dbg_line_buffer = &debug_buffers.dbg_line_buffer; |
| 1053 | var dbg_info_buffer = &debug_buffers.dbg_info_buffer; |
| 1054 | var dbg_info_type_relocs = &debug_buffers.dbg_info_type_relocs; |
| 1055 | |
| 1056 | const symbol = self.base.local_symbols.items[decl.link.macho.local_sym_index]; |
| 1057 | const text_block = &decl.link.macho; |
| 1058 | // If the Decl is a function, we need to update the __debug_line program. |
| 1059 | const typed_value = decl.typed_value.most_recent.typed_value; |
| 1060 | switch (typed_value.ty.zigTypeTag()) { |
| 1061 | .Fn => { |
| 1062 | // Perform the relocations based on vaddr. |
| 1063 | { |
| 1064 | const ptr = dbg_line_buffer.items[dbg_line_vaddr_reloc_index..][0..8]; |
| 1065 | mem.writeIntLittle(u64, ptr, symbol.n_value); |
| 1066 | } |
| 1067 | { |
| 1068 | const ptr = dbg_info_buffer.items[dbg_info_low_pc_reloc_index..][0..8]; |
| 1069 | mem.writeIntLittle(u64, ptr, symbol.n_value); |
| 1070 | } |
| 1071 | { |
| 1072 | const ptr = dbg_info_buffer.items[getRelocDbgInfoSubprogramHighPC()..][0..4]; |
| 1073 | mem.writeIntLittle(u32, ptr, @intCast(u32, text_block.size)); |
| 1074 | } |
| 1075 | |
| 1076 | try dbg_line_buffer.appendSlice(&[_]u8{ DW.LNS_extended_op, 1, DW.LNE_end_sequence }); |
| 1077 | |
| 1078 | // Now we have the full contents and may allocate a region to store it. |
| 1079 | |
| 1080 | // This logic is nearly identical to the logic below in `updateDeclDebugInfo` for |
| 1081 | // `TextBlock` and the .debug_info. If you are editing this logic, you |
| 1082 | // probably need to edit that logic too. |
| 1083 | |
| 1084 | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 1085 | const debug_line_sect = &dwarf_segment.sections.items[self.debug_line_section_index.?]; |
| 1086 | const src_fn = &decl.fn_link.macho; |
| 1087 | src_fn.len = @intCast(u32, dbg_line_buffer.items.len); |
| 1088 | if (self.dbg_line_fn_last) |last| { |
| 1089 | if (src_fn.next) |next| { |
| 1090 | // Update existing function - non-last item. |
| 1091 | if (src_fn.off + src_fn.len + min_nop_size > next.off) { |
| 1092 | // It grew too big, so we move it to a new location. |
| 1093 | if (src_fn.prev) |prev| { |
| 1094 | _ = self.dbg_line_fn_free_list.put(allocator, prev, {}) catch {}; |
| 1095 | prev.next = src_fn.next; |
| 1096 | } |
| 1097 | next.prev = src_fn.prev; |
| 1098 | src_fn.next = null; |
| 1099 | // Populate where it used to be with NOPs. |
| 1100 | const file_pos = debug_line_sect.offset + src_fn.off; |
| 1101 | try self.pwriteDbgLineNops(0, &[0]u8{}, src_fn.len, file_pos); |
| 1102 | // TODO Look at the free list before appending at the end. |
| 1103 | src_fn.prev = last; |
| 1104 | last.next = src_fn; |
| 1105 | self.dbg_line_fn_last = src_fn; |
| 1106 | |
| 1107 | src_fn.off = last.off + (last.len * alloc_num / alloc_den); |
| 1108 | } |
| 1109 | } else if (src_fn.prev == null) { |
| 1110 | // Append new function. |
| 1111 | // TODO Look at the free list before appending at the end. |
| 1112 | src_fn.prev = last; |
| 1113 | last.next = src_fn; |
| 1114 | self.dbg_line_fn_last = src_fn; |
| 1115 | |
| 1116 | src_fn.off = last.off + (last.len * alloc_num / alloc_den); |
| 1117 | } |
| 1118 | } else { |
| 1119 | // This is the first function of the Line Number Program. |
| 1120 | self.dbg_line_fn_first = src_fn; |
| 1121 | self.dbg_line_fn_last = src_fn; |
| 1122 | |
| 1123 | src_fn.off = self.dbgLineNeededHeaderBytes(module) * alloc_num / alloc_den; |
| 1124 | } |
| 1125 | |
| 1126 | const last_src_fn = self.dbg_line_fn_last.?; |
| 1127 | const needed_size = last_src_fn.off + last_src_fn.len; |
| 1128 | if (needed_size != debug_line_sect.size) { |
| 1129 | if (needed_size > dwarf_segment.allocatedSize(debug_line_sect.offset)) { |
| 1130 | const new_offset = dwarf_segment.findFreeSpace(needed_size, 1, null); |
| 1131 | const existing_size = last_src_fn.off; |
| 1132 | |
| 1133 | log.debug("moving __debug_line section: {} bytes from 0x{x} to 0x{x}", .{ |
| 1134 | existing_size, |
| 1135 | debug_line_sect.offset, |
| 1136 | new_offset, |
| 1137 | }); |
| 1138 | |
| 1139 | const amt = try self.file.copyRangeAll(debug_line_sect.offset, self.file, new_offset, existing_size); |
| 1140 | if (amt != existing_size) return error.InputOutput; |
| 1141 | debug_line_sect.offset = @intCast(u32, new_offset); |
| 1142 | debug_line_sect.addr = dwarf_segment.inner.vmaddr + new_offset - dwarf_segment.inner.fileoff; |
| 1143 | } |
| 1144 | debug_line_sect.size = needed_size; |
| 1145 | self.load_commands_dirty = true; // TODO look into making only the one section dirty |
| 1146 | self.debug_line_header_dirty = true; |
| 1147 | } |
| 1148 | const prev_padding_size: u32 = if (src_fn.prev) |prev| src_fn.off - (prev.off + prev.len) else 0; |
| 1149 | const next_padding_size: u32 = if (src_fn.next) |next| next.off - (src_fn.off + src_fn.len) else 0; |
| 1150 | |
| 1151 | // We only have support for one compilation unit so far, so the offsets are directly |
| 1152 | // from the .debug_line section. |
| 1153 | const file_pos = debug_line_sect.offset + src_fn.off; |
| 1154 | try self.pwriteDbgLineNops(prev_padding_size, dbg_line_buffer.items, next_padding_size, file_pos); |
| 1155 | |
| 1156 | // .debug_info - End the TAG_subprogram children. |
| 1157 | try dbg_info_buffer.append(0); |
| 1158 | }, |
| 1159 | else => {}, |
| 1160 | } |
| 1161 | |
| 1162 | // Now we emit the .debug_info types of the Decl. These will count towards the size of |
| 1163 | // the buffer, so we have to do it before computing the offset, and we can't perform the actual |
| 1164 | // relocations yet. |
| 1165 | var it = dbg_info_type_relocs.iterator(); |
| 1166 | while (it.next()) |entry| { |
| 1167 | entry.value.off = @intCast(u32, dbg_info_buffer.items.len); |
| 1168 | try self.addDbgInfoType(entry.key, dbg_info_buffer, target); |
| 1169 | } |
| 1170 | |
| 1171 | try self.updateDeclDebugInfoAllocation(allocator, text_block, @intCast(u32, dbg_info_buffer.items.len)); |
| 1172 | |
| 1173 | // Now that we have the offset assigned we can finally perform type relocations. |
| 1174 | it = dbg_info_type_relocs.iterator(); |
| 1175 | while (it.next()) |entry| { |
| 1176 | for (entry.value.relocs.items) |off| { |
| 1177 | mem.writeIntLittle( |
| 1178 | u32, |
| 1179 | dbg_info_buffer.items[off..][0..4], |
| 1180 | text_block.dbg_info_off + entry.value.off, |
| 1181 | ); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | try self.writeDeclDebugInfo(text_block, dbg_info_buffer.items); |
| 1186 | } |
| 1187 | |
| 891 | 1188 | /// Asserts the type has codegen bits. |
| 892 | | pub fn addDbgInfoType( |
| 1189 | fn addDbgInfoType( |
| 893 | 1190 | self: *DebugSymbols, |
| 894 | 1191 | ty: Type, |
| 895 | 1192 | dbg_info_buffer: *std.ArrayList(u8), |
| ... | ... | @@ -931,7 +1228,7 @@ pub fn addDbgInfoType( |
| 931 | 1228 | } |
| 932 | 1229 | } |
| 933 | 1230 | |
| 934 | | pub fn updateDeclDebugInfoAllocation( |
| 1231 | fn updateDeclDebugInfoAllocation( |
| 935 | 1232 | self: *DebugSymbols, |
| 936 | 1233 | allocator: *Allocator, |
| 937 | 1234 | text_block: *TextBlock, |
| ... | ... | @@ -986,7 +1283,7 @@ pub fn updateDeclDebugInfoAllocation( |
| 986 | 1283 | } |
| 987 | 1284 | } |
| 988 | 1285 | |
| 989 | | pub fn writeDeclDebugInfo(self: *DebugSymbols, text_block: *TextBlock, dbg_info_buf: []const u8) !void { |
| 1286 | fn writeDeclDebugInfo(self: *DebugSymbols, text_block: *TextBlock, dbg_info_buf: []const u8) !void { |
| 990 | 1287 | const tracy = trace(@src()); |
| 991 | 1288 | defer tracy.end(); |
| 992 | 1289 | |
| ... | ... | @@ -1057,19 +1354,19 @@ fn makeDebugString(self: *DebugSymbols, allocator: *Allocator, bytes: []const u8 |
| 1057 | 1354 | |
| 1058 | 1355 | /// The reloc offset for the line offset of a function from the previous function's line. |
| 1059 | 1356 | /// It's a fixed-size 4-byte ULEB128. |
| 1060 | | pub fn getRelocDbgLineOff() usize { |
| 1357 | fn getRelocDbgLineOff() usize { |
| 1061 | 1358 | return dbg_line_vaddr_reloc_index + @sizeOf(u64) + 1; |
| 1062 | 1359 | } |
| 1063 | 1360 | |
| 1064 | | pub fn getRelocDbgFileIndex() usize { |
| 1361 | fn getRelocDbgFileIndex() usize { |
| 1065 | 1362 | return getRelocDbgLineOff() + 5; |
| 1066 | 1363 | } |
| 1067 | 1364 | |
| 1068 | | pub fn getRelocDbgInfoSubprogramHighPC() u32 { |
| 1365 | fn getRelocDbgInfoSubprogramHighPC() u32 { |
| 1069 | 1366 | return dbg_info_low_pc_reloc_index + @sizeOf(u64); |
| 1070 | 1367 | } |
| 1071 | 1368 | |
| 1072 | | pub fn dbgLineNeededHeaderBytes(self: DebugSymbols, module: *Module) u32 { |
| 1369 | fn dbgLineNeededHeaderBytes(self: DebugSymbols, module: *Module) u32 { |
| 1073 | 1370 | const directory_entry_format_count = 1; |
| 1074 | 1371 | const file_name_entry_format_count = 1; |
| 1075 | 1372 | const directory_count = 1; |
| ... | ... | @@ -1092,7 +1389,7 @@ fn dbgInfoNeededHeaderBytes(self: DebugSymbols) u32 { |
| 1092 | 1389 | /// are less than 126,976 bytes (if this limit is ever reached, this function can be |
| 1093 | 1390 | /// improved to make more than one pwritev call, or the limit can be raised by a fixed |
| 1094 | 1391 | /// amount by increasing the length of `vecs`). |
| 1095 | | pub fn pwriteDbgLineNops( |
| 1392 | fn pwriteDbgLineNops( |
| 1096 | 1393 | self: *DebugSymbols, |
| 1097 | 1394 | prev_padding_size: usize, |
| 1098 | 1395 | buf: []const u8, |
| ... | ... | @@ -1170,7 +1467,7 @@ pub fn pwriteDbgLineNops( |
| 1170 | 1467 | |
| 1171 | 1468 | /// Writes to the file a buffer, prefixed and suffixed by the specified number of |
| 1172 | 1469 | /// bytes of padding. |
| 1173 | | pub fn pwriteDbgInfoNops( |
| 1470 | fn pwriteDbgInfoNops( |
| 1174 | 1471 | self: *DebugSymbols, |
| 1175 | 1472 | prev_padding_size: usize, |
| 1176 | 1473 | buf: []const u8, |
| ... | ... | @@ -1239,25 +1536,3 @@ pub fn pwriteDbgInfoNops( |
| 1239 | 1536 | |
| 1240 | 1537 | try self.file.pwritevAll(vecs[0..vec_index], offset - prev_padding_size); |
| 1241 | 1538 | } |
| 1242 | | |
| 1243 | | pub fn updateDeclLineNumber(self: *DebugSymbols, module: *Module, decl: *const Module.Decl) !void { |
| 1244 | | const tracy = trace(@src()); |
| 1245 | | defer tracy.end(); |
| 1246 | | |
| 1247 | | const container_scope = decl.scope.cast(Module.Scope.Container).?; |
| 1248 | | const tree = container_scope.file_scope.contents.tree; |
| 1249 | | const file_ast_decls = tree.root_node.decls(); |
| 1250 | | // TODO Look into improving the performance here by adding a token-index-to-line |
| 1251 | | // lookup table. Currently this involves scanning over the source code for newlines. |
| 1252 | | const fn_proto = file_ast_decls[decl.src_index].castTag(.FnProto).?; |
| 1253 | | const block = fn_proto.getBodyNode().?.castTag(.Block).?; |
| 1254 | | const line_delta = std.zig.lineDelta(tree.source, 0, tree.token_locs[block.lbrace].start); |
| 1255 | | const casted_line_off = @intCast(u28, line_delta); |
| 1256 | | |
| 1257 | | const dwarf_segment = &self.load_commands.items[self.dwarf_segment_cmd_index.?].Segment; |
| 1258 | | const shdr = &dwarf_segment.sections.items[self.debug_line_section_index.?]; |
| 1259 | | const file_pos = shdr.offset + decl.fn_link.macho.off + getRelocDbgLineOff(); |
| 1260 | | var data: [4]u8 = undefined; |
| 1261 | | leb.writeUnsignedFixed(4, &data, casted_line_off); |
| 1262 | | try self.file.pwriteAll(&data, file_pos); |
| 1263 | | } |