libdd-crashtracker 3.0.0

Detects program crashes and reports them to datadog backend.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

use crate::{
    crash_info::{CrashInfo, CrashInfoBuilder, ErrorKind, SigInfo, Span, StackFrame, Ucontext},
    receiver::debug_logger::{DebugLogger, ReceiverIssue},
    runtime_callback::RuntimeStack,
    shared::constants::*,
    CrashtrackerConfiguration, StackTrace,
};

use anyhow::Context;
use libdd_telemetry::data::LogLevel;
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};
use tokio::io::AsyncBufReadExt;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct RuntimeStackFrame {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    line: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    column: Option<u32>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    function: Vec<u8>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    type_name: Vec<u8>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    file: Vec<u8>,
}

impl From<RuntimeStackFrame> for StackFrame {
    fn from(value: RuntimeStackFrame) -> Self {
        let mut stack_frame = StackFrame::new();
        stack_frame.function = if value.function.is_empty() {
            None
        } else {
            Some(String::from_utf8_lossy(&value.function).to_string())
        };
        stack_frame.type_name = if value.type_name.is_empty() {
            None
        } else {
            Some(String::from_utf8_lossy(&value.type_name).to_string())
        };
        stack_frame.file = if value.file.is_empty() {
            None
        } else {
            Some(String::from_utf8_lossy(&value.file).to_string())
        };
        stack_frame.line = value.line;
        stack_frame.column = value.column;
        stack_frame
    }
}

/// The crashtracker collector sends data in blocks.
/// This enum tracks which block we're currently in, and, for multi-line blocks,
/// collects the partial data until the block is closed and it can be appended
/// to the CrashReport.
#[derive(Debug)]
pub(crate) enum StdinState {
    AdditionalTags,
    Config,
    Counters,
    Done,
    File(String, Vec<String>),
    Kind,
    Metadata,
    ProcInfo,
    SigInfo,
    SpanIds,
    StackTrace,
    TraceIds,
    Ucontext,
    Waiting,
    WholeStackTrace,
    // StackFrame is always emitted as one stream of all the frames but StackString
    // may have lines that we need to accumulate depending on runtime (e.g. Python)
    RuntimeStackFrame(Vec<StackFrame>),
    RuntimeStackString(Vec<String>),
    Message,
}

/// A state machine that processes data from the crash-tracker collector line by
/// line.  The crashtracker collector sends data in blocks, so we use a `state`
/// variable to track which block we're in and collect partial data.
/// Once we reach the end of a block, append the block's data to `crashinfo`.
fn process_line(
    builder: &mut CrashInfoBuilder,
    config: &mut Option<CrashtrackerConfiguration>,
    line: &str,
    state: StdinState,
    debug_logger: &DebugLogger,
) -> anyhow::Result<StdinState> {
    let next = match state {
        StdinState::AdditionalTags if line.starts_with(DD_CRASHTRACK_END_ADDITIONAL_TAGS) => {
            StdinState::Waiting
        }
        StdinState::AdditionalTags => {
            let additional_tags: Vec<String> = serde_json::from_str(line)?;
            builder.with_experimental_additional_tags(additional_tags)?;
            StdinState::AdditionalTags
        }

        StdinState::Config if line.starts_with(DD_CRASHTRACK_END_CONFIG) => StdinState::Waiting,
        StdinState::Config => {
            if config.is_some() {
                // The config might contain sensitive data, don't log it.
                eprintln!("Unexpected double config");
            }
            *config = Some(serde_json::from_str(line)?);
            StdinState::Config
        }

        StdinState::Counters if line.starts_with(DD_CRASHTRACK_END_COUNTERS) => StdinState::Waiting,
        StdinState::Counters => {
            let v: serde_json::Value = serde_json::from_str(line)?;
            let map = v.as_object().context("Expected map type value")?;
            anyhow::ensure!(map.len() == 1);
            let (key, val) = map
                .iter()
                .next()
                .context("we know there is one value here")?;
            let val = val.as_i64().context("Vals are ints")?;
            builder.with_counter(key.clone(), val)?;
            StdinState::Counters
        }

        StdinState::WholeStackTrace if line.starts_with(DD_CRASHTRACK_END_WHOLE_STACKTRACE) => {
            StdinState::Waiting
        }
        StdinState::WholeStackTrace => {
            let stacktrace: StackTrace = serde_json::from_str(line)?;
            builder.with_stack(stacktrace)?;
            StdinState::WholeStackTrace
        }

        StdinState::Done => {
            builder.with_log_message(
                format!("Unexpected line after crashreport is done: {line}"),
                true,
            )?;
            StdinState::Done
        }

        StdinState::File(filename, lines) if line.starts_with(DD_CRASHTRACK_END_FILE) => {
            builder.with_file_and_contents(filename, lines)?;
            StdinState::Waiting
        }
        StdinState::File(name, mut contents) => {
            contents.push(line.to_string());
            StdinState::File(name, contents)
        }

        StdinState::Kind if line.starts_with(DD_CRASHTRACK_END_KIND) => StdinState::Waiting,
        StdinState::Kind => {
            let kind: ErrorKind = serde_json::from_str(line)?;
            builder.with_kind(kind)?;
            StdinState::Kind
        }

        StdinState::Metadata if line.starts_with(DD_CRASHTRACK_END_METADATA) => StdinState::Waiting,
        StdinState::Metadata => {
            let metadata = serde_json::from_str(line)?;
            builder.with_metadata(metadata)?;
            StdinState::Metadata
        }

        StdinState::ProcInfo if line.starts_with(DD_CRASHTRACK_END_PROCINFO) => StdinState::Waiting,
        StdinState::ProcInfo => {
            let proc_info = serde_json::from_str(line)?;
            builder.with_proc_info(proc_info)?;
            StdinState::ProcInfo
        }
        StdinState::RuntimeStackFrame(frames)
            if line.starts_with(DD_CRASHTRACK_END_RUNTIME_STACK_FRAME) =>
        {
            let runtime_stack = RuntimeStack {
                format: "Datadog Runtime Callback 1.0".to_string(),
                frames,
                stacktrace_string: None,
            };
            builder.with_experimental_runtime_stack(runtime_stack)?;
            StdinState::Waiting
        }
        StdinState::RuntimeStackFrame(mut frames) => {
            let frame_json: RuntimeStackFrame = serde_json::from_str(line)?;
            frames.push(frame_json.into());
            StdinState::RuntimeStackFrame(frames)
        }
        StdinState::RuntimeStackString(lines)
            if line.starts_with(DD_CRASHTRACK_END_RUNTIME_STACK_STRING) =>
        {
            let runtime_stack = RuntimeStack {
                format: "Datadog Runtime Callback 1.0".to_string(),
                frames: vec![],
                stacktrace_string: Some(lines.join("\n")),
            };
            builder.with_experimental_runtime_stack(runtime_stack)?;
            StdinState::Waiting
        }
        StdinState::RuntimeStackString(mut lines) => {
            lines.push(line.to_string());
            StdinState::RuntimeStackString(lines)
        }
        StdinState::SigInfo if line.starts_with(DD_CRASHTRACK_END_SIGINFO) => StdinState::Waiting,
        StdinState::SigInfo => {
            let sig_info: SigInfo = serde_json::from_str(line)?;
            if !builder.has_message() {
                let message = format!(
                    "Process terminated with {:?} ({:?})",
                    sig_info.si_code_human_readable, sig_info.si_signo_human_readable
                );
                builder.with_message(message)?;
            }

            builder.with_timestamp_now()?;
            builder.with_sig_info(sig_info)?;
            builder.with_incomplete(true)?;
            StdinState::SigInfo
        }

        StdinState::Message if line.starts_with(DD_CRASHTRACK_END_MESSAGE) => StdinState::Waiting,
        StdinState::Message => {
            let unescaped = line.replace("\\n", "\n").replace("\\r", "\r");
            builder.with_message(unescaped)?;
            StdinState::Message
        }

        StdinState::SpanIds if line.starts_with(DD_CRASHTRACK_END_SPAN_IDS) => StdinState::Waiting,
        StdinState::SpanIds => {
            let span_ids: Vec<Span> = serde_json::from_str(line)?;
            builder.with_span_ids(span_ids)?;
            StdinState::SpanIds
        }

        StdinState::StackTrace if line.starts_with(DD_CRASHTRACK_END_STACKTRACE) => {
            builder.with_stack_set_complete()?;
            StdinState::Waiting
        }
        StdinState::StackTrace => {
            let frame = serde_json::from_str(line)?;
            builder.with_stack_frame(frame, true)?;
            StdinState::StackTrace
        }

        StdinState::TraceIds if line.starts_with(DD_CRASHTRACK_END_TRACE_IDS) => {
            StdinState::Waiting
        }
        StdinState::TraceIds => {
            let trace_ids: Vec<Span> = serde_json::from_str(line)?;
            builder.with_trace_ids(trace_ids)?;
            StdinState::TraceIds
        }
        StdinState::Ucontext if line.starts_with(DD_CRASHTRACK_END_UCONTEXT) => StdinState::Waiting,
        StdinState::Ucontext => {
            let ucontext: Ucontext = serde_json::from_str(line)?;
            builder.with_ucontext(ucontext)?;
            StdinState::Ucontext
        }

        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_ADDITIONAL_TAGS) => {
            StdinState::AdditionalTags
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_CONFIG) => StdinState::Config,
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_COUNTERS) => {
            StdinState::Counters
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_FILE) => {
            let (_, filename) = line.split_once(' ').unwrap_or(("", "MISSING_FILENAME"));
            StdinState::File(filename.to_string(), vec![])
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_KIND) => StdinState::Kind,
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_METADATA) => {
            StdinState::Metadata
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_PROCINFO) => {
            StdinState::ProcInfo
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_SIGINFO) => StdinState::SigInfo,
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_MESSAGE) => StdinState::Message,
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_SPAN_IDS) => {
            StdinState::SpanIds
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_STACKTRACE) => {
            StdinState::StackTrace
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_RUNTIME_STACK_STRING) => {
            StdinState::RuntimeStackString(vec![])
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_RUNTIME_STACK_FRAME) => {
            StdinState::RuntimeStackFrame(vec![])
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_TRACE_IDS) => {
            StdinState::TraceIds
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_UCONTEXT) => {
            StdinState::Ucontext
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_BEGIN_WHOLE_STACKTRACE) => {
            StdinState::WholeStackTrace
        }
        StdinState::Waiting if line.starts_with(DD_CRASHTRACK_DONE) => {
            builder.with_incomplete(false)?;
            StdinState::Done
        }
        StdinState::Waiting => {
            let msg = format!("Unexpected line while receiving crashreport: {line}");
            builder.with_log_message(msg.clone(), true)?;
            debug_logger.emit(
                ReceiverIssue::UnexpectedLine,
                &builder.uuid.to_string(),
                msg,
                LogLevel::Warn,
            );
            StdinState::Waiting
        }
    };
    Ok(next)
}

/// Listens to `stream`, reading it line by line, until
/// 1. A crash-report is received, in which case it is processed for upload, and we return
///    Some(CrashInfo)
/// 2. `stdin` closes without a crash report (i.e. if the parent terminated normally). In this case
///    we return "None".
///
/// Borrows `stream` rather than consuming it. The crashing process blocks on
/// POLLHUP from this connection, so closing it here would release that process
/// before the caller has symbolized the report, and symbolization reads
/// `/proc/<pid>/maps`. The caller decides when to close.
pub(crate) async fn receive_report_from_stream(
    timeout: Duration,
    stream: &mut (impl AsyncBufReadExt + std::marker::Unpin),
) -> anyhow::Result<Option<(CrashtrackerConfiguration, CrashInfo)>> {
    let mut builder = CrashInfoBuilder::new();
    let mut stdin_state = StdinState::Waiting;
    let mut config: Option<CrashtrackerConfiguration> = None;
    // Usable before the collector sends anything, so a receiver that never gets
    // a config or metadata block can still report why. Upgraded in the loop as
    // those blocks arrive.
    let mut debug_logger = DebugLogger::new(None, None);

    let mut crash_ping_sent = false;

    let mut lines = stream.lines();
    let mut deadline = None;
    // Start the timeout counter when the deadline when the first crash message is recieved
    let mut remaining_timeout = Duration::MAX;

    //TODO: This assumes that the input is valid UTF-8.
    loop {
        // Re-point the debug logger at the real endpoint and application once
        // the config and metadata blocks arrive. Cheap no-op until they do.
        debug_logger.update(config.as_ref(), builder.metadata.as_ref());

        // We need to wait until at least we receive config, metadata, and kind (on non-Windows
        // platforms) before sending the crash ping
        if !crash_ping_sent && builder.is_ping_ready() {
            if let Some(ref config_ref) = config {
                let config_clone = config_ref.clone();
                crash_ping_sent = true;
                // Spawn crash ping sending in a separate task
                let crash_ping = builder.build_crash_ping()?;

                tokio::task::spawn(async move {
                    if let Err(e) = crash_ping
                        .upload_to_endpoint_async(config_clone.endpoint())
                        .await
                    {
                        eprintln!("Failed to send crash ping: {e}");
                    }
                });
            } else {
                eprintln!("No config found, skipping crash ping");
            }
        }
        let next_line = tokio::time::timeout(remaining_timeout, lines.next_line()).await;
        let Ok(next_line) = next_line else {
            builder.with_log_message(format!("Timeout: {next_line:?}"), true)?;
            debug_logger.emit(
                ReceiverIssue::Timeout,
                &builder.uuid.to_string(),
                format!("Timeout while waiting for crash report input: {next_line:?}"),
                LogLevel::Warn,
            );
            break;
        };
        let Ok(next_line) = next_line else {
            builder.with_log_message(format!("IO Error: {next_line:?}"), true)?;
            // We ignore error from uploading the log to telemetry, because what are we going to do?
            // If upload is failing, its not worth the effort to retry the request so we should just
            // continue on. At least we will get the log message in the crash info
            debug_logger.emit(
                ReceiverIssue::IoError,
                &builder.uuid.to_string(),
                format!("IO error while reading crash report input: {next_line:?}"),
                LogLevel::Warn,
            );
            break;
        };
        let Some(next_line) = next_line else { break };

        match process_line(
            &mut builder,
            &mut config,
            &next_line,
            stdin_state,
            &debug_logger,
        ) {
            Ok(next_state) => {
                stdin_state = next_state;
                if matches!(stdin_state, StdinState::Done) {
                    break;
                }
            }
            Err(e) => {
                // If the input is corrupted, stop and salvage what we can
                builder.with_log_message(
                    format!("Unable to process line: {next_line}. Error: {e}"),
                    true,
                )?;
                debug_logger.emit(
                    ReceiverIssue::ProcessLine,
                    &builder.uuid.to_string(),
                    format!("Unable to process line: {next_line}. Error: {e}"),
                    LogLevel::Warn,
                );
                break;
            }
        }

        if let Some(deadline) = deadline {
            // The clock was already ticking, update the remaining time
            remaining_timeout = deadline - Instant::now()
        } else {
            // We've recieved the first message from the collector, start the clock ticking.
            deadline = Some(Instant::now() + timeout);
            remaining_timeout = timeout;
        }
    }

    if !builder.has_data() {
        // Nothing arrived at all, so there is no crash report to build and no
        // config to upload it with. The env-derived logger is all we have, and
        // this log is the only signal that the receiver ran and got nothing.
        // Waited on rather than spawned: we return right after, and the caller
        // drops the runtime, which would cancel a pending spawned task.
        debug_logger
            .emit_and_wait(
                ReceiverIssue::NoData,
                &builder.uuid.to_string(),
                "Receiver received no data".to_string(),
                LogLevel::Warn,
            )
            .await;
        return Ok(None);
    }

    enrich_thread_name(&mut builder)?;
    builder.with_os_info_this_machine()?;

    // Without a config, we don't even know the endpoint to transmit to.  Not much to do to recover.
    let config = config.context("Missing crashtracker configuration")?;

    for filename in config.additional_files() {
        if let Err(e) = builder.with_file(filename.clone()) {
            builder.with_log_message(e.to_string(), true)?;
            debug_logger.emit(
                ReceiverIssue::AttachAdditionalFile,
                &builder.uuid.to_string(),
                format!("Unable to attach additional file {filename:?}: {e}"),
                LogLevel::Warn,
            );
        }
    }

    // Thread collection is budgeted against the *remaining* receiver timeout;
    // whatever time is left after reading the crash data from stdin.
    // This makes sure the total receiver lifetime is bounded by the configured
    // timeout, and we always emit whatever threads were collected before
    // the deadline rather than silently discarding them.
    #[cfg(target_os = "linux")]
    if config.collect_all_threads() {
        if let Some(proc_info) = builder.proc_info.as_ref() {
            let parent_pid = proc_info.pid;
            let crashing_tid = proc_info.tid;
            // If we never received a first line (deadline is None) use zero so
            // collection is skipped; there is nothing to attach to anyway.
            let remaining_budget = deadline
                .map(|d| d.saturating_duration_since(Instant::now()))
                .unwrap_or(Duration::ZERO);
            if let Err(e) = collect_and_add_thread_contexts(
                &mut builder,
                &config,
                parent_pid,
                crashing_tid,
                remaining_budget,
            ) {
                let _ = builder
                    .with_log_message(format!("Failed to collect thread contexts: {e}"), true);
            }
        }
    }

    let crash_info = builder.build()?;

    if crash_info.incomplete {
        debug_logger.emit(
            ReceiverIssue::IncompleteStacktrace,
            &crash_info.uuid,
            "CrashInfo stacktrace incomplete".to_string(),
            LogLevel::Warn,
        );
    }

    Ok(Some((config, crash_info)))
}

#[cfg(target_os = "linux")]
fn collect_and_add_thread_contexts(
    builder: &mut CrashInfoBuilder,
    config: &CrashtrackerConfiguration,
    parent_pid: u32,
    crashing_tid: Option<u32>,
    budget: Duration,
) -> anyhow::Result<()> {
    use crate::crash_info::{StackTrace, ThreadData};
    use crate::receiver::ptrace_collector::stream_thread_contexts;

    let crashing_tid = crashing_tid.unwrap_or(0) as i32;
    let parent_pid = parent_pid as i32;

    let crash_site = builder.ucontext.as_ref().and_then(crash_site_registers);

    let mut collected_threads = Vec::new();

    let incomplete = stream_thread_contexts(
        parent_pid,
        crashing_tid,
        config.max_threads(),
        budget,
        |tid, captured_context| {
            let (name, state) = read_thread_stat(parent_pid, tid);
            let name = name.unwrap_or_else(|| tid.to_string());

            let mut stack = match captured_context {
                Some(ctx) => ctx.stack_trace.clone(),
                None => StackTrace::new_incomplete(),
            };

            let crashed = tid == crashing_tid;
            if crashed {
                if let Some((ip, sp)) = crash_site {
                    drop_frames_above_crash_site(&mut stack, ip, sp);
                }
            }

            collected_threads.push(ThreadData {
                crashed,
                name,
                stack,
                state,
            });
        },
    )?;

    if incomplete {
        let _ = builder.with_counter("threads_incomplete".to_string(), 1);
    }

    let _ = builder.with_threads(collected_threads);

    Ok(())
}

/// The instruction and stack pointer the kernel saved when it delivered the fatal
/// signal.
///
/// Returns `None` when the report carries no usable register state: an unhandled
/// exception has no ucontext at all
#[cfg(target_os = "linux")]
fn crash_site_registers(ucontext: &Ucontext) -> Option<(u64, u64)> {
    let (ip_name, sp_name) = match ucontext.arch.as_str() {
        "x86_64" => ("rip", "rsp"),
        "aarch64" => ("pc", "sp"),
        _ => return None,
    };
    let ip = parse_hex_address(ucontext.registers.get(ip_name)?)?;
    let sp = parse_hex_address(ucontext.registers.get(sp_name)?)?;
    Some((ip, sp))
}

#[cfg(target_os = "linux")]
fn parse_hex_address(value: &str) -> Option<u64> {
    u64::from_str_radix(value.trim_start_matches("0x"), 16).ok()
}

/// Drop the crashtracker's own frames from the top of the crashing thread's stack.
///
/// The receiver unwinds the crashing thread from its live registers, and by then that
/// thread is parked inside our signal handler waiting for the receiver to finish. Its
/// stack therefore begins inside libdatadog rather than at the faulting instruction,
/// unlike `error.stack`, which libunwind seeds directly from the kernel-saved
/// registers and which consequently never contains these frames.
///
/// Those same registers pinpoint the faulting frame, so everything above the frame
/// matching them is ours. Instruction and stack pointer are matched as a pair rather
/// than discarding frames below a stack-pointer threshold, because the handler may run
/// on an alternate signal stack and such a stack is not guaranteed to be mapped below
/// the thread's main stack.
///
/// When no frame matches, the stack is left untouched. An unwind that never reached the
/// faulting frame is worth more intact than truncated on a guess.
#[cfg(target_os = "linux")]
fn drop_frames_above_crash_site(stack: &mut StackTrace, ip: u64, sp: u64) {
    let is_crash_site = |frame: &StackFrame| {
        frame.ip.as_deref().and_then(parse_hex_address) == Some(ip)
            && frame.sp.as_deref().and_then(parse_hex_address) == Some(sp)
    };

    if let Some(crash_site) = stack.frames.iter().position(is_crash_site) {
        stack.frames.drain(..crash_site);
    }
}

/// Read thread name and state from a single `/proc/{pid}/task/{tid}/stat` file.
///
/// The stat file format is: `pid (comm) state ...`
/// `comm` (the thread name) is enclosed between the first `(` and the last `)`
/// The state character immediately follows the closing `)`.
#[cfg(target_os = "linux")]
fn read_thread_stat(pid: i32, tid: i32) -> (Option<String>, Option<String>) {
    let content = match std::fs::read_to_string(format!("/proc/{pid}/task/{tid}/stat")) {
        Ok(c) => c,
        Err(_) => return (None, None),
    };

    let Some(name_start) = content.find('(') else {
        return (None, None);
    };
    let Some(name_end) = content.rfind(')') else {
        return (None, None);
    };

    let name = Some(content[name_start + 1..name_end].to_string());
    let state = content[name_end + 1..]
        .split_whitespace()
        .next()
        .map(|s| s.to_string());

    (name, state)
}

#[cfg(target_os = "linux")]
fn enrich_thread_name(builder: &mut CrashInfoBuilder) -> anyhow::Result<()> {
    use std::{fs, path::PathBuf};

    if builder.error.thread_name.is_some() {
        return Ok(());
    }
    let Some(proc_info) = builder.proc_info.as_ref() else {
        return Ok(());
    };
    let Some(tid) = proc_info.tid else {
        return Ok(());
    };
    let pid = proc_info.pid;
    let path = PathBuf::from(format!("/proc/{pid}/task/{tid}/comm"));
    let Ok(comm) = fs::read_to_string(&path) else {
        return Ok(());
    };
    let thread_name = comm.trim_end_matches('\n');
    if thread_name.is_empty() {
        return Ok(());
    }
    builder.with_thread_name(thread_name.to_string())?;
    Ok(())
}

#[cfg(not(target_os = "linux"))]
fn enrich_thread_name(_builder: &mut CrashInfoBuilder) -> anyhow::Result<()> {
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Reads from `socket` until `marker` shows up, then answers 200 so the
    /// uploader's request completes instead of waiting out its timeout.
    async fn serve_one_request(listener: tokio::net::TcpListener, marker: &str) -> String {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};

        let (mut socket, _) = listener.accept().await.expect("accept");
        let mut request = Vec::new();
        let mut chunk = [0u8; 4096];
        loop {
            let n = socket.read(&mut chunk).await.expect("read");
            if n == 0 {
                break;
            }
            request.extend_from_slice(&chunk[..n]);
            if String::from_utf8_lossy(&request).contains(marker) {
                break;
            }
        }
        let _ = socket
            .write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\n\r\n")
            .await;
        let _ = socket.flush().await;
        String::from_utf8_lossy(&request).to_string()
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_receive_report_no_data_sends_debug_log() {
        // Stand in for the agent, so the debug log has somewhere to land
        // without a config block telling the receiver where to send.
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        std::env::set_var(
            "DD_TRACE_AGENT_URL",
            format!("http://{}", listener.local_addr().unwrap()),
        );
        let server = tokio::spawn(async move {
            tokio::time::timeout(
                Duration::from_secs(5),
                serve_one_request(listener, "no_data"),
            )
            .await
            .expect("no telemetry request received")
        });

        let (sender, receiver) = tokio::net::UnixStream::pair().unwrap();
        // Close without sending anything, as a parent that exited normally does.
        drop(sender);

        let mut stream = tokio::io::BufReader::new(receiver);
        let report = receive_report_from_stream(Duration::from_secs(1), &mut stream)
            .await
            .unwrap();
        assert!(report.is_none());

        let request = server.await.unwrap();
        assert!(
            request.contains("receiver_issue:no_data"),
            "no_data tag missing from telemetry request: {request}"
        );
        assert!(
            request.contains("Receiver received no data"),
            "no_data message missing from telemetry request: {request}"
        );
    }

    #[test]
    fn test_stdin_state_waiting_to_message() {
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;

        let state = StdinState::Waiting;
        let line = DD_CRASHTRACK_BEGIN_MESSAGE;

        let next_state = process_line(
            &mut builder,
            &mut config,
            line,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();

        assert!(matches!(next_state, StdinState::Message));
    }

    #[test]
    fn test_stdin_state_message_content() {
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;

        // Enter message state
        let state = StdinState::Message;
        let message_line = "program panicked";

        let next_state = process_line(
            &mut builder,
            &mut config,
            message_line,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();

        // Should stay in message state
        assert!(matches!(next_state, StdinState::Message));

        // Verify message was stored
        assert!(builder.has_message());
    }

    #[test]
    fn test_stdin_state_message_to_waiting() {
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;

        let state = StdinState::Message;
        let line = DD_CRASHTRACK_END_MESSAGE;

        let next_state = process_line(
            &mut builder,
            &mut config,
            line,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();

        assert!(matches!(next_state, StdinState::Waiting));
    }

    #[test]
    fn test_message_state_with_empty_line() {
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;

        let state = StdinState::Message;
        let empty_line = "";

        let result = process_line(
            &mut builder,
            &mut config,
            empty_line,
            state,
            &DebugLogger::disabled(),
        );

        // Should handle empty line without error
        assert!(result.is_ok());
    }

    #[test]
    fn test_message_state_with_multiline_content() {
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;

        // First line of message
        let state = process_line(
            &mut builder,
            &mut config,
            "Line 1 of panic",
            StdinState::Message,
            &DebugLogger::disabled(),
        )
        .unwrap();

        // Should still be in message state
        assert!(matches!(state, StdinState::Message));

        // Note: Current implementation may only store last message
        // This test documents current behavior
    }

    #[test]
    fn test_message_state_full_workflow() {
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;

        // Start in waiting state
        let mut state = StdinState::Waiting;

        // Transition to message
        state = process_line(
            &mut builder,
            &mut config,
            DD_CRASHTRACK_BEGIN_MESSAGE,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::Message));

        // Add message content
        state = process_line(
            &mut builder,
            &mut config,
            "test panic message",
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::Message));
        assert!(builder.has_message());

        // End message
        state = process_line(
            &mut builder,
            &mut config,
            DD_CRASHTRACK_END_MESSAGE,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::Waiting));
    }

    #[test]
    fn test_stacktrace_empty_workflow() {
        // Test that receiving BEGIN_STACKTRACE followed by END_STACKTRACE
        // (with no frames) creates an empty but complete stack
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;

        let mut state = StdinState::Waiting;

        state = process_line(
            &mut builder,
            &mut config,
            DD_CRASHTRACK_BEGIN_STACKTRACE,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::StackTrace));

        // End stacktrace immediately (no frames)
        state = process_line(
            &mut builder,
            &mut config,
            DD_CRASHTRACK_END_STACKTRACE,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::Waiting));

        // Verify we have an empty but incomplete stack (no frames captured = stack unwinding
        // failed)
        let stack = builder.error.stack.as_ref().expect("Stack should exist");
        assert!(stack.frames.is_empty());
        assert!(
            stack.incomplete,
            "Stack should be marked incomplete when no frames were captured"
        );

        // Verify a log message was recorded about no frames
        assert!(builder
            .log_messages
            .as_ref()
            .map(|msgs| msgs
                .iter()
                .any(|msg| msg.contains("No native stack frames received")))
            .unwrap_or(false));
    }

    #[test]
    fn test_stacktrace_with_frames_workflow() {
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;

        let mut state = StdinState::Waiting;

        // Begin stacktrace
        state = process_line(
            &mut builder,
            &mut config,
            DD_CRASHTRACK_BEGIN_STACKTRACE,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::StackTrace));

        // Add a frame
        let frame_json = r#"{"ip":"0x1234"}"#;
        state = process_line(
            &mut builder,
            &mut config,
            frame_json,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::StackTrace));

        // End stacktrace
        state = process_line(
            &mut builder,
            &mut config,
            DD_CRASHTRACK_END_STACKTRACE,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::Waiting));

        // Verify we have a stack with one frame, marked complete
        let stack = builder.error.stack.as_ref().expect("Stack should exist");
        assert_eq!(stack.frames.len(), 1);
        assert!(!stack.incomplete, "Stack should be marked complete");
        assert_eq!(stack.frames[0].ip, Some("0x1234".to_string()));
    }

    #[test]
    fn test_message_with_escaped_sentinel_does_not_inject() {
        // Simulates what emit_message produces after sanitize_message_for_wire:
        // the sentinel strings are on a single escaped line, not separate lines.
        let mut builder = CrashInfoBuilder::new();
        let mut config = None;
        let mut state = StdinState::Waiting;

        // Enter message state
        state = process_line(
            &mut builder,
            &mut config,
            DD_CRASHTRACK_BEGIN_MESSAGE,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::Message));

        // Feed the sanitized content (newlines escaped, so it's one line)
        let sanitized_line = format!(
            "Exception 'Evil'\\n{}\\n{}\\n{{}}\\n{}",
            DD_CRASHTRACK_END_MESSAGE, DD_CRASHTRACK_BEGIN_CONFIG, DD_CRASHTRACK_END_CONFIG,
        );
        state = process_line(
            &mut builder,
            &mut config,
            &sanitized_line,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        // Must still be in Message state. the escaped sentinels are just text
        assert!(
            matches!(state, StdinState::Message),
            "escaped sentinels must not trigger state transitions"
        );

        // Now the real end sentinel
        state = process_line(
            &mut builder,
            &mut config,
            DD_CRASHTRACK_END_MESSAGE,
            state,
            &DebugLogger::disabled(),
        )
        .unwrap();
        assert!(matches!(state, StdinState::Waiting));

        // No config should have been injected
        assert!(
            config.is_none(),
            "no config section should have been parsed"
        );
        assert!(builder.has_message());
    }
}

#[cfg(all(test, target_os = "linux"))]
mod crashing_thread_tests {
    use super::*;
    use std::collections::HashMap;

    fn frame(ip: &str, sp: &str) -> StackFrame {
        StackFrame {
            ip: Some(ip.to_string()),
            sp: Some(sp.to_string()),
            ..StackFrame::new()
        }
    }

    fn ucontext(arch: &str, registers: &[(&str, &str)]) -> Ucontext {
        Ucontext {
            arch: arch.to_string(),
            registers: registers
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect::<HashMap<_, _>>(),
            raw: None,
        }
    }

    /// The prefix mirrors a real report: the thread is parked in `poll` inside the
    /// signal handler, and the frames beneath the trampoline are the actual crash.
    fn parked_in_handler() -> StackTrace {
        StackTrace::from_frames(
            vec![
                frame("0x7ddee3ca126f", "0x7ddee2f7cbe0"), // __libc_poll
                frame("0x7ddee2b4f356", "0x7ddee2f7cc10"), // ProcessHandle::finish
                frame("0x7ddee2b527db", "0x7ddee2f7cc80"), // handle_posix_sigaction
                frame("0x7ddee3be1050", "0x7ddee2f7d4c0"), // __restore_rt
                frame("0x7ddee3c2feec", "0x7ddee2f7da80"), // crash site
                frame("0x7ddee3be1050", "0x7ddee2f7dac0"),
                frame("0x7ddee2e9ccc0", "0x7ffe06975aa0"),
            ],
            false,
        )
    }

    #[test]
    fn crash_site_frame_becomes_the_first_frame() {
        let mut stack = parked_in_handler();
        drop_frames_above_crash_site(&mut stack, 0x7ddee3c2feec, 0x7ddee2f7da80);

        assert_eq!(stack.frames.len(), 3);
        assert_eq!(stack.frames[0].ip.as_deref(), Some("0x7ddee3c2feec"));
        assert_eq!(stack.frames[0].sp.as_deref(), Some("0x7ddee2f7da80"));
    }

    /// The handler runs on an alternate signal stack, so its frames can sit at
    /// addresses either side of the thread's own stack. Only the exact crash-site
    /// frame may end the prefix.
    #[test]
    fn alternate_signal_stack_above_thread_stack_is_still_trimmed() {
        let mut stack = StackTrace::from_frames(
            vec![
                frame("0x1000", "0xffff0000"), // handler, on a higher-addressed alt stack
                frame("0x1010", "0xffff0040"),
                frame("0x2000", "0x7ffe0000"), // crash site, on the thread stack
                frame("0x2010", "0x7ffe0040"),
            ],
            false,
        );
        drop_frames_above_crash_site(&mut stack, 0x2000, 0x7ffe0000);

        assert_eq!(stack.frames.len(), 2);
        assert_eq!(stack.frames[0].ip.as_deref(), Some("0x2000"));
    }

    /// The ucontext block zero-pads its registers while unwound frames don't, so the
    /// two must be compared as numbers rather than as strings.
    #[test]
    fn zero_padded_registers_match_unpadded_frames() {
        let registers = crash_site_registers(&ucontext(
            "x86_64",
            &[("rip", "0x00007ddee3c2feec"), ("rsp", "0x00007ddee2f7da80")],
        ))
        .expect("x86_64 registers should parse");

        let mut stack = parked_in_handler();
        drop_frames_above_crash_site(&mut stack, registers.0, registers.1);

        assert_eq!(stack.frames.len(), 3);
    }

    #[test]
    fn stack_is_untouched_when_no_frame_matches() {
        let mut stack = parked_in_handler();
        let before = stack.frames.clone();
        drop_frames_above_crash_site(&mut stack, 0xdead, 0xbeef);

        assert_eq!(stack.frames, before);
    }

    #[test]
    fn stack_is_untouched_when_it_already_starts_at_the_crash_site() {
        let mut stack = StackTrace::from_frames(
            vec![frame("0x2000", "0x7ffe0000"), frame("0x2010", "0x7ffe0040")],
            false,
        );
        let before = stack.frames.clone();
        drop_frames_above_crash_site(&mut stack, 0x2000, 0x7ffe0000);

        assert_eq!(stack.frames, before);
    }

    /// A frame sharing only the instruction pointer is a different activation of the
    /// same function, not the crash site.
    #[test]
    fn matching_ip_alone_does_not_end_the_prefix() {
        let mut stack = StackTrace::from_frames(
            vec![
                frame("0x2000", "0x7ffe0000"), // recursive call, same ip
                frame("0x2000", "0x7ffe0040"), // crash site
            ],
            false,
        );
        drop_frames_above_crash_site(&mut stack, 0x2000, 0x7ffe0040);

        assert_eq!(stack.frames.len(), 1);
        assert_eq!(stack.frames[0].sp.as_deref(), Some("0x7ffe0040"));
    }

    #[test]
    fn registers_are_read_per_architecture() {
        assert_eq!(
            crash_site_registers(&ucontext("x86_64", &[("rip", "0x10"), ("rsp", "0x20")])),
            Some((0x10, 0x20))
        );
        assert_eq!(
            crash_site_registers(&ucontext("aarch64", &[("pc", "0x10"), ("sp", "0x20")])),
            Some((0x10, 0x20))
        );
        assert_eq!(
            crash_site_registers(&ucontext("riscv64", &[("pc", "0x10"), ("sp", "0x20")])),
            None
        );
        assert_eq!(
            crash_site_registers(&ucontext("x86_64", &[("rip", "0x10")])),
            None,
            "a ucontext missing the stack pointer yields no crash site"
        );
    }
}