3636#include " llvm/Support/LEB128.h"
3737#include " llvm/Support/Path.h"
3838#include " llvm/Support/ThreadPool.h"
39+ #include < iostream>
3940#include < vector>
4041
4142namespace llvm {
@@ -2182,6 +2183,10 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
21822183
21832184 if (const DWARFDebugLine::LineTable *LT =
21842185 ObjFile.Dwarf ->getLineTableForUnit (&Unit.getOrigUnit ())) {
2186+ // Search through Prologue
2187+ StringRef ObjName = ObjFile.FileName ;
2188+ llvm::outs () << " ObjName: " << ObjName << " \n " ;
2189+ bool ShouldDebug = ObjName.contains (" METAAppGroup.m" );
21852190
21862191 DWARFDebugLine::LineTable LineTable;
21872192
@@ -2207,6 +2212,18 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
22072212 for (size_t i = 0 ; i < LT->Rows .size (); i++)
22082213 InputRows.emplace_back (TrackedRow{LT->Rows [i], i, false });
22092214
2215+ if (ShouldDebug) {
2216+ llvm::outs () << " DEBUG: InputRows setup:\n " ;
2217+ for (size_t i = 0 ; i < InputRows.size (); i++) {
2218+ llvm::outs () << " [" << i << " ] OriginalRowIndex: "
2219+ << InputRows[i].OriginalRowIndex << " , Address: 0x"
2220+ << llvm::format_hex (InputRows[i].Row .Address .Address , 16 )
2221+ << " , Line: " << InputRows[i].Row .Line
2222+ << " , EndSequence: " << InputRows[i].Row .EndSequence
2223+ << " \n " ;
2224+ }
2225+ }
2226+
22102227 // This vector is the output line table (still in TrackedRow form).
22112228 std::vector<TrackedRow> OutputRows;
22122229 OutputRows.reserve (InputRows.size ());
@@ -2274,6 +2291,20 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
22742291 insertLineSequence (Seq, OutputRows);
22752292 }
22762293
2294+ if (ShouldDebug) {
2295+ llvm::outs () << " DEBUG: OutputRows after processing:\n " ;
2296+ for (size_t i = 0 ; i < OutputRows.size (); i++) {
2297+ llvm::outs () << " [" << i << " ] OriginalRowIndex: "
2298+ << OutputRows[i].OriginalRowIndex << " , Address: 0x"
2299+ << llvm::format_hex (OutputRows[i].Row .Address .Address ,
2300+ 16 )
2301+ << " , Line: " << OutputRows[i].Row .Line
2302+ << " , EndSequence: " << OutputRows[i].Row .EndSequence
2303+ << " , isStartSeqInOutput: "
2304+ << OutputRows[i].isStartSeqInOutput << " \n " ;
2305+ }
2306+ }
2307+
22772308 // Materialize the tracked rows into final DWARFDebugLine::Row objects.
22782309 LineTable.Rows .clear ();
22792310 LineTable.Rows .reserve (OutputRows.size ());
@@ -2308,6 +2339,15 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
23082339 LineTableMapping[Seq.StmtSeqOffset ] = Seq.FirstRowIndex ;
23092340 }
23102341
2342+ if (ShouldDebug) {
2343+ llvm::outs () << " DEBUG: LineTableMapping from parser:\n " ;
2344+ for (const auto &Entry : LineTableMapping) {
2345+ llvm::outs () << " StmtSeqOffset: 0x"
2346+ << llvm::format_hex (Entry.first , 8 )
2347+ << " -> FirstRowIndex: " << Entry.second << " \n " ;
2348+ }
2349+ }
2350+
23112351 // Second, manually find sequence boundaries and match them to the
23122352 // sorted attributes to handle sequences the parser might have missed.
23132353 auto StmtAttrs = Unit.getStmtSeqListAttributes ();
@@ -2316,12 +2356,27 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
23162356 return A.get () < B.get ();
23172357 });
23182358
2359+ if (ShouldDebug) {
2360+ llvm::outs () << " DEBUG: Sorted StmtAttrs:\n " ;
2361+ for (const auto &Attr : StmtAttrs) {
2362+ llvm::outs () << " StmtSeqOffset: 0x"
2363+ << llvm::format_hex (Attr.get (), 8 ) << " \n " ;
2364+ }
2365+ }
2366+
23192367 std::vector<size_t > SeqStartRows;
23202368 SeqStartRows.push_back (0 );
23212369 for (size_t i = 0 ; i < LT->Rows .size () - 1 ; ++i)
23222370 if (LT->Rows [i].EndSequence )
23232371 SeqStartRows.push_back (i + 1 );
23242372
2373+ if (ShouldDebug) {
2374+ llvm::outs () << " DEBUG: SeqStartRows:\n " ;
2375+ for (size_t i = 0 ; i < SeqStartRows.size (); ++i) {
2376+ llvm::outs () << " [" << i << " ]: " << SeqStartRows[i] << " \n " ;
2377+ }
2378+ }
2379+
23252380 // While SeqOffToOrigRow parsed from CU could be the ground truth,
23262381 // e.g.
23272382 //
@@ -2383,21 +2438,72 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
23832438 SeqStartIdxValidAndSmallerThanNext ()) {
23842439 SeqOffToOrigRow[StmtAttrs[StmtAttrIdx].get ()] =
23852440 SeqStartRows[SeqStartIdx];
2441+ if (ShouldDebug) {
2442+ llvm::outs ()
2443+ << " DEBUG: Adding dummy mapping: StmtSeqOffset 0x"
2444+ << llvm::format_hex (StmtAttrs[StmtAttrIdx].get (), 8 )
2445+ << " -> OrigRowIndex " << SeqStartRows[SeqStartIdx] << " \n " ;
2446+ }
23862447 ++StmtAttrIdx;
23872448 ++SeqStartIdx;
23882449 }
23892450 // One of the pointer points to the value at or past Next in the
23902451 // LineTableMapping, We move the pointer to re-align with the
23912452 // LineTableMapping
23922453 while (StmtIdxValidAndSmallerThanNext ()) {
2454+ if (ShouldDebug) {
2455+ llvm::outs ()
2456+ << " DEBUG: Skipping StmtAttr: 0x"
2457+ << llvm::format_hex (StmtAttrs[StmtAttrIdx].get (), 8 )
2458+ << " \n " ;
2459+ }
23932460 ++StmtAttrIdx;
23942461 }
23952462 while (SeqStartIdxValidAndSmallerThanNext ()) {
2463+ if (ShouldDebug) {
2464+ llvm::outs () << " DEBUG: Skipping SeqStartRow: "
2465+ << SeqStartRows[SeqStartIdx] << " \n " ;
2466+ }
23962467 ++SeqStartIdx;
23972468 }
23982469 // Use the LineTableMapping's result as the ground truth and move
23992470 // on.
2400- SeqOffToOrigRow[NextSeqOff] = NextRow;
2471+ if (NextSeqOff != DummyKey) {
2472+ SeqOffToOrigRow[NextSeqOff] = NextRow;
2473+ if (ShouldDebug) {
2474+ llvm::outs ()
2475+ << " DEBUG: Adding ground truth mapping: StmtSeqOffset 0x"
2476+ << llvm::format_hex (NextSeqOff, 8 ) << " -> OrigRowIndex "
2477+ << NextRow << " \n " ;
2478+ }
2479+ }
2480+ // It is possible that the first StmtAttrIdx/SeqStartIdx point to
2481+ // later entries in LineTableMapping. Therefore we only increment
2482+ // the pointers after we validate they are pointing to the `Next`
2483+ // entry. e.g. LineTableMapping SeqOff Row 0x08 9 <-
2484+ // NextSeqOff/NextRow 0x14 15
2485+ //
2486+ // StmtAttrs SeqStartRows
2487+ // 0x14 13 <- StmtAttrIdx/SeqStartIdx
2488+ // 0x16 15
2489+ // -- 17
2490+ if (StmtAttrIdx < StmtAttrs.size () &&
2491+ StmtAttrs[StmtAttrIdx].get () == NextSeqOff) {
2492+ ++StmtAttrIdx;
2493+ }
2494+ if (SeqStartIdx < SeqStartRows.size () &&
2495+ SeqStartRows[SeqStartIdx] == NextRow) {
2496+ ++SeqStartIdx;
2497+ }
2498+ }
2499+ }
2500+
2501+ if (ShouldDebug) {
2502+ llvm::outs () << " DEBUG: Final SeqOffToOrigRow map:\n " ;
2503+ for (const auto &Entry : SeqOffToOrigRow) {
2504+ llvm::outs () << " StmtSeqOffset: 0x"
2505+ << llvm::format_hex (Entry.first , 8 )
2506+ << " -> OrigRowIndex: " << Entry.second << " \n " ;
24012507 }
24022508 }
24032509
@@ -2406,34 +2512,67 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
24062512 for (size_t i = 0 ; i < OutputRows.size (); ++i)
24072513 OrigRowToNewRow[OutputRows[i].OriginalRowIndex ] = i;
24082514
2515+ if (ShouldDebug) {
2516+ llvm::outs () << " DEBUG: OrigRowToNewRow map:\n " ;
2517+ for (const auto &Entry : OrigRowToNewRow) {
2518+ llvm::outs () << " OrigRowIndex: " << Entry.first
2519+ << " -> NewRowIndex: " << Entry.second << " \n " ;
2520+ }
2521+ }
2522+
24092523 // Patch DW_AT_LLVM_stmt_sequence attributes in the compile unit DIE
24102524 // with the correct offset into the .debug_line section.
24112525 for (const auto &StmtSeq : Unit.getStmtSeqListAttributes ()) {
24122526 uint64_t OrigStmtSeq = StmtSeq.get ();
2527+ if (ShouldDebug) {
2528+ llvm::outs () << " DEBUG: Processing StmtSeq attribute with offset 0x"
2529+ << llvm::format_hex (OrigStmtSeq, 8 ) << " \n " ;
2530+ }
2531+
24132532 // 1. Get the original row index from the stmt list offset.
24142533 auto OrigRowIter = SeqOffToOrigRow.find (OrigStmtSeq);
24152534 // Check whether we have an output sequence for the StmtSeq offset.
24162535 // Some sequences are discarded by the DWARFLinker if they are invalid
24172536 // (empty).
24182537 if (OrigRowIter == SeqOffToOrigRow.end ()) {
2419- StmtSeq.set (UINT64_MAX);
2538+ if (ShouldDebug) {
2539+ llvm::outs () << " DEBUG: StmtSeq 0x"
2540+ << llvm::format_hex (OrigStmtSeq, 8 )
2541+ << " not found in SeqOffToOrigRow, setting to "
2542+ " OrigOffsetMissing\n " ;
2543+ }
2544+ StmtSeq.set (OrigOffsetMissing);
24202545 continue ;
24212546 }
24222547 size_t OrigRowIndex = OrigRowIter->second ;
2548+ if (ShouldDebug) {
2549+ llvm::outs () << " DEBUG: Found OrigRowIndex: " << OrigRowIndex
2550+ << " \n " ;
2551+ }
24232552
24242553 // 2. Get the new row index from the original row index.
24252554 auto NewRowIter = OrigRowToNewRow.find (OrigRowIndex);
24262555 if (NewRowIter == OrigRowToNewRow.end ()) {
24272556 // If the original row index is not found in the map, update the
24282557 // stmt_sequence attribute to the 'invalid offset' magic value.
2429- StmtSeq.set (UINT64_MAX);
2558+ if (ShouldDebug) {
2559+ llvm::outs () << " DEBUG: OrigRowIndex " << OrigRowIndex
2560+ << " not found in OrigRowToNewRow, setting to "
2561+ " NewOffsetMissing\n " ;
2562+ }
2563+ StmtSeq.set (NewOffsetMissing);
24302564 continue ;
24312565 }
24322566
24332567 // 3. Get the offset of the new row in the output .debug_line section.
24342568 assert (NewRowIter->second < OutputRowOffsets.size () &&
24352569 " New row index out of bounds" );
24362570 uint64_t NewStmtSeqOffset = OutputRowOffsets[NewRowIter->second ];
2571+ if (ShouldDebug) {
2572+ llvm::outs () << " DEBUG: Found NewRowIndex: " << NewRowIter->second
2573+ << " , setting to new offset 0x"
2574+ << llvm::format_hex (NewStmtSeqOffset, 8 ) << " \n " ;
2575+ }
24372576
24382577 // 4. Patch the stmt_list attribute with the new offset.
24392578 StmtSeq.set (NewStmtSeqOffset);
0 commit comments