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
//! A library for parsing hprof files.
//!
//! See [parse_hprof] to get started, or see the examples in the repo.
//!
//! # Examples
//!
//! Iterating across all records to count how many of each record type there are (adapted from
//! the `analyze_hprof` example's `record-counts` subcommand):
//!
//! ```
//! use std::{fs, collections};
//! use jvm_hprof::{Hprof, parse_hprof, RecordTag, EnumIterable};
//!
//! fn count_records(file: fs::File) {
//!     let memmap = unsafe { memmap::MmapOptions::new().map(&file) }.unwrap();
//!
//!     let hprof: Hprof = parse_hprof(&memmap[..]).unwrap();
//!
//!     // start with zero counts for all types
//!     let mut counts = RecordTag::iter()
//!         .map(|r| (r, 0_u64))
//!         .collect::<collections::HashMap<RecordTag, u64>>();
//!
//!     // overwrite zeros with real counts for each record that exists in the hprof
//!     hprof
//!         .records_iter()
//!         .map(|r| r.unwrap().tag())
//!         .for_each(|tag| {
//!             counts.entry(tag).and_modify(|c| *c += 1).or_insert(1);
//!         });
//!
//!     let mut counts: Vec<(RecordTag, u64)> = counts
//!         .into_iter()
//!         .collect::<Vec<(jvm_hprof::RecordTag, u64)>>();
//!
//!     // highest count on top
//!     counts.sort_unstable_by_key(|&(_, count)| count);
//!     counts.reverse();
//!
//!     for (tag, count) in counts {
//!         println!("{:?}: {}", tag, count);
//!     }
//! }
//! ```
use getset::CopyGetters;
use nom::bytes::complete as bytes;
use nom::number::complete as number;
use std::cmp::Ordering;
use std::fmt::{Error, Formatter};
use std::{cmp, fmt};
use strum_macros;
use strum_macros::EnumIter;

pub mod heap_dump;
mod parsing_iterator;

use parsing_iterator::*;

/// Ids are used to identify many things in an hprof file: objects, classes, utf8 blobs, etc.
///
/// The on-disk representation of an Id depends on the relevant [IdSize].
#[derive(CopyGetters, Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct Id {
    // inflate 4-byte ids to 8-byte since if we have a small 32-bit heap, no worries about memory anyway
    #[get_copy = "pub"]
    id: u64,
}

impl From<u64> for Id {
    fn from(id: u64) -> Id {
        Id { id }
    }
}

impl fmt::Display for Id {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.id)
    }
}

impl fmt::UpperHex for Id {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        fmt::UpperHex::fmt(&self.id, f)
    }
}

/// An alternate means of identification used in parallel with [Id].
///
/// [LoadClass], for instance, has both a `class_obj_id` and a `serial`. In certain cases you might
/// need to use one or the other. If you were processing [StackFrame] records and wanted to print
/// human readable class names (which are available in [LoadClass], [StackFrame] uses
/// `class_serial`, whereas if you were inspecting a class's fields via [crate::heap_dump::Class],
/// that only has the class's `obj_id` available.
#[derive(CopyGetters, Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct Serial {
    /// The plain serial number.
    #[get_copy = "pub"]
    num: u32,
}

impl From<u32> for Serial {
    fn from(num: u32) -> Self {
        Serial { num }
    }
}

impl fmt::Display for Serial {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.num)
    }
}

impl fmt::UpperHex for Serial {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        fmt::UpperHex::fmt(&self.num, f)
    }
}

impl StatelessParserWithId for Id {
    fn parse(input: &[u8], id_size: IdSize) -> nom::IResult<&[u8], Self> {
        let (input, id) = match id_size {
            IdSize::U32 => number::be_u32(input).map(|(i, id)| (i, id as u64))?,
            IdSize::U64 => number::be_u64(input)?,
        };

        Ok((input, Id::from(id)))
    }
}

/// Hprof ids can be 32 or 64 bit, depending on the system and JVM that the hprof was captured on.
///
/// This controls how ids are parsed, and can generally be otherwise ignored.
#[derive(Debug, Clone, Copy)]
pub enum IdSize {
    U32,
    U64,
}

impl IdSize {
    fn size_in_bytes(&self) -> usize {
        match self {
            IdSize::U32 => 4,
            IdSize::U64 => 8,
        }
    }
}

/// The top level of data loaded from an .hprof file.
// https://github.com/openjdk/jdk/blob/08822b4e0526fe001c39fe08e241b849eddf481d/src/hotspot/share/services/heapDumper.cpp
#[derive(CopyGetters)]
pub struct Hprof<'a> {
    #[get_copy = "pub"]
    header: Header<'a>,
    records: &'a [u8],
}

impl<'a> Hprof<'a> {
    /// Iterate over the [Record] data in the hprof.
    ///
    /// Iteration is cheap, as each [Record] defers parsing the bulk of its data until later.
    pub fn records_iter(&self) -> Records<'a> {
        Records {
            remaining: self.records,
            id_size: self.header.id_size,
        }
    }
}

/// Entry point for parsing.
///
/// This is intended to be used with a memory mapped hprof file, or, for small heap dumps that can
/// fit comfortably in memory, just an in-memory buffer.
pub fn parse_hprof(input: &[u8]) -> ParseResult<Hprof> {
    let (input, header) = Header::parse(input)?;

    Ok(Hprof {
        header,
        records: input,
    })
}

/// Basic metadata about the hprof
#[derive(CopyGetters, Copy, Clone)]
pub struct Header<'a> {
    label: &'a [u8],
    #[get_copy = "pub"]
    id_size: IdSize,
    /// The timestamp for the hprof as the number of millis since epoch
    #[get_copy = "pub"]
    timestamp_millis: u64,
}

impl<'a> Header<'a> {
    pub fn label(&self) -> Result<&'a str, std::str::Utf8Error> {
        std::str::from_utf8(self.label)
    }

    fn parse(input: &[u8]) -> nom::IResult<&[u8], Header> {
        // https://github.com/openjdk/jdk/blob/08822b4e0526fe001c39fe08e241b849eddf481d/src/hotspot/share/services/heapDumper.cpp#L63
        let (input, label) = bytes::take_until(&b"\0"[..])(input)?;
        let (input, _) = bytes::take_while_m_n(1, 1, |b| b == 0)(input)?;

        // TODO confirm endianness
        let (input, id_size_num) = number::be_u32(input)?;
        let (input, epoch_hi) = number::be_u32(input)?;
        let (input, epoch_lo) = number::be_u32(input)?;

        let epoch_timestamp = ((epoch_hi as u64) << 32) + (epoch_lo as u64);

        let id_size = match id_size_num {
            4 => IdSize::U32,
            8 => IdSize::U64,
            _ => panic!("unexpected size {}", id_size_num), // TODO
        };

        Ok((
            input,
            Header {
                label,
                id_size,
                timestamp_millis: epoch_timestamp,
            },
        ))
    }
}

impl<'a> fmt::Debug for Header<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        f.debug_struct("Header")
            .field("label", &self.label())
            .field("timestamp_millis", &self.timestamp_millis())
            .field("id_size", &self.id_size())
            .finish()
    }
}

/// Iterator over the [Record] data in an hprof.
pub struct Records<'a> {
    remaining: &'a [u8],
    id_size: IdSize,
}

impl<'a> Iterator for Records<'a> {
    type Item = ParseResult<'a, Record<'a>>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.remaining.is_empty() {
            return None;
        }

        let res = Record::parse(self.remaining, self.id_size);
        match res {
            Ok((input, record)) => {
                self.remaining = input;
                Some(Ok(record))
            }
            Err(e) => Some(Err(e)),
        }
    }
}

/// The next level down from the [Hprof] in the hierarchy of data.
///
/// See [RecordTag] for the different types of data that can be in a Record.
///
/// # Performance
///
/// Records are generic and lightweight: the parsing done to get a Record is just loading a couple
/// of bytes to get the tag and timestamp, and a slice that contains the "body" of the record, so
/// iterating across all Records without inspecting the body of each one is cheap. Even huge heap
/// dumps might have only tens of thousands of Records.
///
/// Since iterating records is very fast but parsing the contents may not be (e.g. a sequence of
/// 2GiB [HeapDumpSegment] records), records are especially amenable to parallel processing, e.g.
/// with rayon's `par_bridge()`.
///
/// # Examples
///
/// Because enum variants in Rust can't (yet) have methods that exist only for one variant, parsing
/// the body of the record is done via `as_*` methods like `as_utf_8()`. If the tag of a given
/// record is `[RecordTag::Utf8]`, then `as_utf_8()` will return `Some(...)`, and all other `as_...`
/// will return `None`, and so forth. So, in practice, this might look like:
///
/// ```
/// use jvm_hprof::{Record, RecordTag, Utf8};
///
/// fn print_utf8(record: &Record) {
///     match record.tag() {
///         RecordTag::Utf8 => {
///             // unwrap strips the Option(),
///             // which we know is Some because of the tag
///             let utf8: Utf8 = record.as_utf_8().unwrap()
///                 // apply real error handling as needed
///                 .expect("parsing error -- corrupt hprof? Bug in parser?");
///
///             // build a str from the bytes, validating UTF-8
///             let utf_str: &str = utf8.text_as_str()
///                 .expect("Surely the JVM wouldn't write a Utf8 record with invalid UTF-8");
///
///             println!("utf8 contents: {}", utf_str);
///         }
///         _ => println!("tag was {:?}, not utf8", record.tag())
///     }
/// }
/// ```
#[derive(CopyGetters, Copy, Clone)]
pub struct Record<'a> {
    /// The tag, which determines which of the `as_*` methods it is suitable to call.
    #[get_copy = "pub"]
    tag: RecordTag,
    /// Microseconds since the timestamp in the header
    #[get_copy = "pub"]
    micros_since_header_ts: u32,
    id_size: IdSize,
    body: &'a [u8],
}

impl<'a> Record<'a> {
    /// Returns `Some` if the tag is [RecordTag::Utf8] and `None` otherwise.
    pub fn as_utf_8(&self) -> Option<ParseResult<Utf8<'a>>> {
        match self.tag {
            RecordTag::Utf8 => Some(Utf8::parse(self.body, self.id_size)),
            _ => None,
        }
    }

    /// Returns `Some` if the tag is [RecordTag::LoadClass] and `None` otherwise.
    pub fn as_load_class(&self) -> Option<ParseResult<LoadClass>> {
        match self.tag {
            RecordTag::LoadClass => Some(LoadClass::parse(self.body, self.id_size)),
            _ => None,
        }
    }

    /// Returns `Some` if the tag is [RecordTag::StackFrame] and `None` otherwise.
    pub fn as_stack_frame(&self) -> Option<ParseResult<StackFrame>> {
        match self.tag {
            RecordTag::StackFrame => Some(StackFrame::parse(self.body, self.id_size)),
            _ => None,
        }
    }

    /// Returns `Some` if the tag is [RecordTag::StackTrace] and `None` otherwise.
    pub fn as_stack_trace(&self) -> Option<ParseResult<StackTrace<'a>>> {
        match self.tag {
            RecordTag::StackTrace => Some(StackTrace::parse(self.body, self.id_size)),
            _ => None,
        }
    }

    /// Returns `Some` if the tag is [RecordTag::HeapDump] or [RecordTag::HeapDumpSegment] and
    /// `None` otherwise.
    pub fn as_heap_dump_segment(&self) -> Option<ParseResult<HeapDumpSegment<'a>>> {
        match self.tag {
            RecordTag::HeapDump | RecordTag::HeapDumpSegment => {
                Some(HeapDumpSegment::parse(self.body, self.id_size))
            }
            _ => None,
        }
    }

    fn parse(input: &[u8], id_size: IdSize) -> nom::IResult<&[u8], Record> {
        // https://github.com/openjdk/jdk/blob/08822b4e0526fe001c39fe08e241b849eddf481d/src/hotspot/share/services/heapDumper.cpp#L76
        let (input, tag_byte) = bytes::take(1_usize)(input)?;

        let tag = match tag_byte[0] {
            0x01 => RecordTag::Utf8,
            0x02 => RecordTag::LoadClass,
            0x03 => RecordTag::UnloadClass,
            0x04 => RecordTag::StackFrame,
            0x05 => RecordTag::StackTrace,
            0x06 => RecordTag::AllocSites,
            0x07 => RecordTag::HeapSummary,
            0x0A => RecordTag::StartThread,
            0x0B => RecordTag::EndThread,
            0x0C => RecordTag::HeapDump,
            0x0D => RecordTag::CpuSamples,
            0x0E => RecordTag::ControlSettings,
            0x1C => RecordTag::HeapDumpSegment,
            0x2C => RecordTag::HeapDumpEnd,
            _ => panic!("unexpected tag: {:#X?}", tag_byte[0]),
        };

        let (input, micros) = number::be_u32(input)?;
        let (input, len) = number::be_u32(input)?;
        let (input, body) = bytes::take(len)(input)?;

        Ok((
            input,
            Record {
                tag,
                micros_since_header_ts: micros,
                id_size,
                body,
            },
        ))
    }
}

/// Indicates what type of data is contained in a particular [Record].
///
/// Each variant has a matching struct. Calling [Record::as_utf_8] on a `Record` with tag
/// `RecordTag::Utf8`, for instance, will produce a [Utf8] struct.
// Since this enum has no data, add EnumIter to allow enumerating across the variants
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Hash, EnumIter)]
pub enum RecordTag {
    /// See [Utf8]
    Utf8,
    /// See [LoadClass]
    LoadClass,
    /// Unused?
    UnloadClass,
    /// See [StackFrame]
    StackFrame,
    /// See [StackTrace]
    StackTrace,
    /// Unused?
    AllocSites,
    /// Unused?
    StartThread,
    /// Unused?
    EndThread,
    /// Unused?
    HeapSummary,
    /// See [HeapDumpSegment]
    HeapDump,
    /// Unused?
    CpuSamples,
    /// Unused?
    ControlSettings,
    /// See [HeapDumpSegment]
    HeapDumpSegment,
    /// Denotes the end of a heap dump
    HeapDumpEnd,
}

impl RecordTag {
    fn tag_byte(&self) -> u8 {
        match self {
            RecordTag::Utf8 => 0x01,
            RecordTag::LoadClass => 0x02,
            RecordTag::UnloadClass => 0x03,
            RecordTag::StackFrame => 0x04,
            RecordTag::StackTrace => 0x05,
            RecordTag::AllocSites => 0x06,
            RecordTag::HeapSummary => 0x07,
            RecordTag::StartThread => 0x0A,
            RecordTag::EndThread => 0x0B,
            RecordTag::HeapDump => 0x0C,
            RecordTag::CpuSamples => 0x0D,
            RecordTag::ControlSettings => 0x0E,
            RecordTag::HeapDumpSegment => 0x1C,
            RecordTag::HeapDumpEnd => 0x2C,
        }
    }
}

impl cmp::Ord for RecordTag {
    fn cmp(&self, other: &Self) -> Ordering {
        self.tag_byte().cmp(&other.tag_byte())
    }
}

/// Contents of a [Record] with tag [RecordTag::Utf8].
#[derive(CopyGetters, Copy, Clone)]
pub struct Utf8<'a> {
    #[get_copy = "pub"]
    name_id: Id,
    #[get_copy = "pub"]
    text: &'a [u8],
}

impl<'a> Utf8<'a> {
    fn parse(input: &[u8], id_size: IdSize) -> ParseResult<Utf8> {
        // https://github.com/openjdk/jdk/blob/08822b4e0526fe001c39fe08e241b849eddf481d/src/hotspot/share/services/heapDumper.cpp#L88
        let (input, id) = Id::parse(input, id_size)?;

        Ok(Utf8 {
            name_id: id,
            text: input,
        })
    }

    /// Note that in practice, there are nonzero Utf8 records with invalid UTF-8 bytes.
    pub fn text_as_str(&self) -> Result<&'a str, std::str::Utf8Error> {
        std::str::from_utf8(self.text)
    }
}

/// Contents of a [Record] with tag [RecordTag::LoadClass].
#[derive(CopyGetters, Copy, Clone)]
pub struct LoadClass {
    #[get_copy = "pub"]
    class_serial: Serial,
    #[get_copy = "pub"]
    class_obj_id: Id,
    #[get_copy = "pub"]
    stack_trace_serial: Serial,
    #[get_copy = "pub"]
    class_name_id: Id,
}

impl LoadClass {
    fn parse(input: &[u8], id_size: IdSize) -> ParseResult<LoadClass> {
        // https://github.com/openjdk/jdk/blob/08822b4e0526fe001c39fe08e241b849eddf481d/src/hotspot/share/services/heapDumper.cpp#L93
        let (input, class_serial) = number::be_u32(input)?;
        let (input, class_obj_id) = Id::parse(input, id_size)?;
        let (input, stack_trace_serial) = number::be_u32(input)?;
        let (_input, class_name_id) = Id::parse(input, id_size)?;

        Ok(LoadClass {
            class_serial: class_serial.into(),
            class_obj_id,
            stack_trace_serial: stack_trace_serial.into(),
            class_name_id,
        })
    }
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
struct UnloadClass {
    class_serial: Serial,
}

/// Contents of a [Record] with tag [RecordTag::StackFrame].
#[derive(CopyGetters, Clone)]
pub struct StackFrame {
    #[get_copy = "pub"]
    id: Id,
    #[get_copy = "pub"]
    method_name_id: Id,
    #[get_copy = "pub"]
    method_signature_id: Id,
    #[get_copy = "pub"]
    source_file_name_id: Id,
    #[get_copy = "pub"]
    class_serial: Serial,
    #[get_copy = "pub"]
    line_num: LineNum,
}

impl StackFrame {
    fn parse(input: &[u8], id_size: IdSize) -> ParseResult<Self> {
        // https://github.com/openjdk/jdk/blob/08822b4e0526fe001c39fe08e241b849eddf481d/src/hotspot/share/services/heapDumper.cpp#L104
        let (input, id) = Id::parse(input, id_size)?;
        let (input, method_name_id) = Id::parse(input, id_size)?;
        let (input, method_signature_id) = Id::parse(input, id_size)?;
        // TODO Option?
        let (input, source_file_name_id) = Id::parse(input, id_size)?;
        let (input, class_serial) = number::be_u32(input)?;
        let (_input, line_num) = LineNum::parse(input)?;

        Ok(StackFrame {
            id,
            method_name_id,
            method_signature_id,
            source_file_name_id,
            class_serial: class_serial.into(),
            line_num,
        })
    }
}

/// Contents of a [Record] with tag [RecordTag::StackTrace].
#[derive(CopyGetters, Clone)]
pub struct StackTrace<'a> {
    id_size: IdSize,
    #[get_copy = "pub"]
    stack_trace_serial: Serial,
    #[get_copy = "pub"]
    thread_serial: Serial,
    num_frame_ids: u32,
    frame_ids: &'a [u8],
}

impl<'a> StackTrace<'a> {
    fn parse(input: &[u8], id_size: crate::IdSize) -> ParseResult<StackTrace> {
        // https://github.com/openjdk/jdk/blob/08822b4e0526fe001c39fe08e241b849eddf481d/src/hotspot/share/services/heapDumper.cpp#L116
        let (input, stack_trace_serial) = number::be_u32(input)?;
        let (input, thread_serial) = number::be_u32(input)?;
        let (input, num_frame_ids) = number::be_u32(input)?;

        Ok(StackTrace {
            id_size,
            stack_trace_serial: stack_trace_serial.into(),
            thread_serial: thread_serial.into(),
            num_frame_ids,
            frame_ids: input,
        })
    }

    pub fn frame_ids(&self) -> Ids {
        Ids {
            iter: ParsingIterator::new_stateless_id_size(
                self.id_size,
                self.frame_ids,
                self.num_frame_ids,
            ),
        }
    }
}

/// Heap allocation sites, obtained after GC
// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
struct AllocSites {
    flags: AllocSitesFlags,
    cutoff_ratio: u32,
    total_live_bytes: u32,
    total_live_instances: u32,
    total_bytes_allocated: u64,
    total_instances_allocated: u64,
    // num_sites: u4
    // TODO iterator over following AllocSite instances
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
struct StartThread {
    thread_serial: Serial,
    thread_id: Id,
    stack_trace_serial: Serial,
    thread_name_id: Id,
    thread_group_name_id: Id,
    thread_group_parent_name_id: Id,
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
struct EndThread {
    thread_serial: Serial,
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
struct HeapSummary {
    total_live_bytes: u32,
}

/// Contents of a [Record] with tag [RecordTag::HeapDump] or [RecordTag::HeapDumpSegment].
///
/// Contains many [heap_dump::SubRecord]s.
///
/// See the [heap_dump] module.
pub struct HeapDumpSegment<'a> {
    id_size: IdSize,
    records: &'a [u8],
}

impl<'a> HeapDumpSegment<'a> {
    fn parse(input: &[u8], id_size: IdSize) -> ParseResult<HeapDumpSegment> {
        Ok(HeapDumpSegment {
            id_size,
            records: input,
        })
    }

    /// Iterate over the [heap_dump::SubRecord]s in this [Record].
    pub fn sub_records(&self) -> SubRecords<'a> {
        SubRecords {
            id_size: self.id_size,
            remaining: self.records,
        }
    }
}

/// Iterator over [heap_dump::SubRecord] data.
pub struct SubRecords<'a> {
    id_size: IdSize,
    remaining: &'a [u8],
}

impl<'a> Iterator for SubRecords<'a> {
    type Item = ParseResult<'a, heap_dump::SubRecord<'a>>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.remaining.is_empty() {
            return None;
        }

        let res = heap_dump::SubRecord::parse(self.remaining, self.id_size);
        match res {
            Ok((input, record)) => {
                self.remaining = input;
                Some(Ok(record))
            }
            Err(e) => Some(Err(e)),
        }
    }
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
struct CpuSamples {
    num_samples: u32,
    num_traces: u32,
    // TODO iterator over samples
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
struct ControlSettings {
    bits: u32,
    stack_trace_depth: u16,
}

/// A line referenced from a stack frame.
#[derive(Copy, Clone, Debug)]
pub enum LineNum {
    /// A line in a source file
    Normal(u32),
    Unknown,
    CompiledMethod,
    NativeMethod,
}

impl LineNum {
    fn parse(input: &[u8]) -> nom::IResult<&[u8], Self> {
        // https://github.com/openjdk/jdk/blob/08822b4e0526fe001c39fe08e241b849eddf481d/src/hotspot/share/services/heapDumper.cpp#L111
        let (input, num) = number::be_i32(input)?;

        Ok((
            input,
            match num {
                num if num > 0 => LineNum::Normal(num as u32),
                -1 => LineNum::Unknown,
                -2 => LineNum::CompiledMethod,
                -3 => LineNum::NativeMethod,
                _ => panic!("Invalid line num {}", num), // TODO
            },
        ))
    }
}

impl fmt::Display for LineNum {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        match self {
            LineNum::Normal(n) => write!(f, "{}", n),
            LineNum::Unknown => write!(f, "Unknown"),
            LineNum::CompiledMethod => write!(f, "CompiledMethod"),
            LineNum::NativeMethod => write!(f, "NativeMethod"),
        }
    }
}

#[derive(Copy, Clone, Debug)]
struct AllocSitesFlags {
    bits: u16,
}

impl AllocSitesFlags {
    // TODO referenced in heapDumper.cpp, but not actually written?
    #[allow(unused)]
    fn mode(&self) -> AllocSitesFlagsMode {
        // TODO naming, correctness?
        if self.bits & 0x001 > 0 {
            AllocSitesFlagsMode::Incremental
        } else {
            AllocSitesFlagsMode::Complete
        }
    }

    // TODO referenced in heapDumper.cpp, but not actually written?
    #[allow(unused)]
    fn sorting(&self) -> AllocSitesFlagsSorting {
        // TODO
        if self.bits & 0x002 > 0 {
            AllocSitesFlagsSorting::Allocation
        } else {
            AllocSitesFlagsSorting::Live
        }
    }

    // TODO referenced in heapDumper.cpp, but not actually written?
    #[allow(unused)]
    fn force_gc(&self) -> bool {
        self.bits & 0x0004 > 0
    }
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
enum AllocSitesFlagsMode {
    Incremental,
    Complete,
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
enum AllocSitesFlagsSorting {
    Allocation,
    Live,
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
enum ObjOrArrayType {
    Object,
    ObjectArray,
    BooleanArray,
    CharArray,
    FloatArray,
    DoubleArray,
    ByteArray,
    ShortArray,
    IntArray,
    LongArray,
}

impl ObjOrArrayType {
    // TODO referenced in heapDumper.cpp, but not actually written?
    #[allow(unused)]
    fn from_num(num: u8) -> ObjOrArrayType {
        match num {
            0 => ObjOrArrayType::Object,
            2 => ObjOrArrayType::ObjectArray,
            4 => ObjOrArrayType::BooleanArray,
            5 => ObjOrArrayType::CharArray,
            6 => ObjOrArrayType::FloatArray,
            7 => ObjOrArrayType::DoubleArray,
            8 => ObjOrArrayType::ByteArray,
            9 => ObjOrArrayType::ShortArray,
            10 => ObjOrArrayType::IntArray,
            11 => ObjOrArrayType::LongArray,
            _ => panic!("Unknown type num {}", num),
        }
    }
}

// TODO referenced in heapDumper.cpp, but not actually written?
#[allow(unused)]
struct AllocSite {
    is_array: ObjOrArrayType,
    /// May be zero during startup
    class_serial: Serial,
    stack_trace_serial: Serial,
    num_bytes_alive: u32,
    num_instances_alive: u32,
    num_bytes_allocated: u32,
    num_instances_allocated: u32,
}

/// Iterator that parses ids.
pub struct Ids<'a> {
    iter: ParsingIterator<'a, Id, IdSizeParserWrapper<Id>>,
}

impl<'a> Iterator for Ids<'a> {
    type Item = ParseResult<'a, Id>;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

type ParseResult<'e, T> = Result<T, nom::Err<(&'e [u8], nom::error::ErrorKind)>>;

/// Allow iterating over enum variants for enums that have `#[derive(EnumIter)]`.
///
/// Wrapper around `strum`'s `IntoEnumIter` so that users don't need to know about `strum`
pub trait EnumIterable {
    type Iterator: Iterator<Item = Self>;

    fn iter() -> Self::Iterator;
}

impl<T: strum::IntoEnumIterator> EnumIterable for T {
    type Iterator = T::Iterator;

    fn iter() -> Self::Iterator {
        T::iter()
    }
}