36
36
#include " llvm/Support/LEB128.h"
37
37
#include " llvm/Support/Path.h"
38
38
#include " llvm/Support/ThreadPool.h"
39
+ #include < iostream>
39
40
#include < vector>
40
41
41
42
namespace llvm {
@@ -2182,6 +2183,10 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
2182
2183
2183
2184
if (const DWARFDebugLine::LineTable *LT =
2184
2185
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" );
2185
2190
2186
2191
DWARFDebugLine::LineTable LineTable;
2187
2192
@@ -2207,6 +2212,18 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
2207
2212
for (size_t i = 0 ; i < LT->Rows .size (); i++)
2208
2213
InputRows.emplace_back (TrackedRow{LT->Rows [i], i, false });
2209
2214
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
+
2210
2227
// This vector is the output line table (still in TrackedRow form).
2211
2228
std::vector<TrackedRow> OutputRows;
2212
2229
OutputRows.reserve (InputRows.size ());
@@ -2274,6 +2291,20 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
2274
2291
insertLineSequence (Seq, OutputRows);
2275
2292
}
2276
2293
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
+
2277
2308
// Materialize the tracked rows into final DWARFDebugLine::Row objects.
2278
2309
LineTable.Rows .clear ();
2279
2310
LineTable.Rows .reserve (OutputRows.size ());
@@ -2308,6 +2339,15 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
2308
2339
LineTableMapping[Seq.StmtSeqOffset ] = Seq.FirstRowIndex ;
2309
2340
}
2310
2341
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
+
2311
2351
// Second, manually find sequence boundaries and match them to the
2312
2352
// sorted attributes to handle sequences the parser might have missed.
2313
2353
auto StmtAttrs = Unit.getStmtSeqListAttributes ();
@@ -2316,12 +2356,27 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
2316
2356
return A.get () < B.get ();
2317
2357
});
2318
2358
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
+
2319
2367
std::vector<size_t > SeqStartRows;
2320
2368
SeqStartRows.push_back (0 );
2321
2369
for (size_t i = 0 ; i < LT->Rows .size () - 1 ; ++i)
2322
2370
if (LT->Rows [i].EndSequence )
2323
2371
SeqStartRows.push_back (i + 1 );
2324
2372
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
+
2325
2380
// While SeqOffToOrigRow parsed from CU could be the ground truth,
2326
2381
// e.g.
2327
2382
//
@@ -2383,21 +2438,72 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
2383
2438
SeqStartIdxValidAndSmallerThanNext ()) {
2384
2439
SeqOffToOrigRow[StmtAttrs[StmtAttrIdx].get ()] =
2385
2440
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
+ }
2386
2447
++StmtAttrIdx;
2387
2448
++SeqStartIdx;
2388
2449
}
2389
2450
// One of the pointer points to the value at or past Next in the
2390
2451
// LineTableMapping, We move the pointer to re-align with the
2391
2452
// LineTableMapping
2392
2453
while (StmtIdxValidAndSmallerThanNext ()) {
2454
+ if (ShouldDebug) {
2455
+ llvm::outs ()
2456
+ << " DEBUG: Skipping StmtAttr: 0x"
2457
+ << llvm::format_hex (StmtAttrs[StmtAttrIdx].get (), 8 )
2458
+ << " \n " ;
2459
+ }
2393
2460
++StmtAttrIdx;
2394
2461
}
2395
2462
while (SeqStartIdxValidAndSmallerThanNext ()) {
2463
+ if (ShouldDebug) {
2464
+ llvm::outs () << " DEBUG: Skipping SeqStartRow: "
2465
+ << SeqStartRows[SeqStartIdx] << " \n " ;
2466
+ }
2396
2467
++SeqStartIdx;
2397
2468
}
2398
2469
// Use the LineTableMapping's result as the ground truth and move
2399
2470
// 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 " ;
2401
2507
}
2402
2508
}
2403
2509
@@ -2406,34 +2512,67 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
2406
2512
for (size_t i = 0 ; i < OutputRows.size (); ++i)
2407
2513
OrigRowToNewRow[OutputRows[i].OriginalRowIndex ] = i;
2408
2514
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
+
2409
2523
// Patch DW_AT_LLVM_stmt_sequence attributes in the compile unit DIE
2410
2524
// with the correct offset into the .debug_line section.
2411
2525
for (const auto &StmtSeq : Unit.getStmtSeqListAttributes ()) {
2412
2526
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
+
2413
2532
// 1. Get the original row index from the stmt list offset.
2414
2533
auto OrigRowIter = SeqOffToOrigRow.find (OrigStmtSeq);
2415
2534
// Check whether we have an output sequence for the StmtSeq offset.
2416
2535
// Some sequences are discarded by the DWARFLinker if they are invalid
2417
2536
// (empty).
2418
2537
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);
2420
2545
continue ;
2421
2546
}
2422
2547
size_t OrigRowIndex = OrigRowIter->second ;
2548
+ if (ShouldDebug) {
2549
+ llvm::outs () << " DEBUG: Found OrigRowIndex: " << OrigRowIndex
2550
+ << " \n " ;
2551
+ }
2423
2552
2424
2553
// 2. Get the new row index from the original row index.
2425
2554
auto NewRowIter = OrigRowToNewRow.find (OrigRowIndex);
2426
2555
if (NewRowIter == OrigRowToNewRow.end ()) {
2427
2556
// If the original row index is not found in the map, update the
2428
2557
// 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);
2430
2564
continue ;
2431
2565
}
2432
2566
2433
2567
// 3. Get the offset of the new row in the output .debug_line section.
2434
2568
assert (NewRowIter->second < OutputRowOffsets.size () &&
2435
2569
" New row index out of bounds" );
2436
2570
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
+ }
2437
2576
2438
2577
// 4. Patch the stmt_list attribute with the new offset.
2439
2578
StmtSeq.set (NewStmtSeqOffset);
0 commit comments