@@ -14,6 +14,8 @@ const NT_X86_XSTATE: u32 = 0x202;
1414const AT_ENTRY : u64 = 9 ;
1515const AT_NULL : u64 = 0 ;
1616
17+ /// Structure to hold the crash dump context
18+ /// This structure contains the information needed to create a core dump
1719#[ derive( Debug ) ]
1820pub ( crate ) struct CrashDumpContext < ' a > {
1921 regions : & ' a [ MemoryRegion ] ,
@@ -38,6 +40,8 @@ impl<'a> CrashDumpContext<'a> {
3840 }
3941}
4042
43+ /// Structure that contains the process information for the core dump
44+ /// This serves as a source of information for `elfcore`'s [`CoreDumpBuilder`]
4145struct GuestView {
4246 regions : Vec < VaRegion > ,
4347 threads : Vec < ThreadView > ,
@@ -46,6 +50,7 @@ struct GuestView {
4650
4751impl GuestView {
4852 fn new ( ctx : & CrashDumpContext ) -> Self {
53+ // Map the regions to the format `CoreDumpBuilder` expects
4954 let regions = ctx
5055 . regions
5156 . iter ( )
@@ -64,6 +69,7 @@ impl GuestView {
6469 } )
6570 . collect ( ) ;
6671
72+ // The xsave state is checked as it can be empty
6773 let mut components = vec ! [ ] ;
6874 if !ctx. xsave . is_empty ( ) {
6975 components. push ( ArchComponentState {
@@ -73,6 +79,10 @@ impl GuestView {
7379 data : ctx. xsave . clone ( ) ,
7480 } ) ;
7581 }
82+
83+ // Create the thread view
84+ // The thread view contains the information about the thread
85+ // NOTE: Some of these fields are not used in the current implementation
7686 let thread = ThreadView {
7787 flags : 0 , // Kernel flags for the process
7888 tid : 1 ,
@@ -145,6 +155,9 @@ impl ProcessInfoSource for GuestView {
145155 }
146156}
147157
158+ /// Structure that reads the guest memory
159+ /// This structure serves as a custom memory reader for `elfcore`'s
160+ /// [`CoreDumpBuilder`]
148161struct GuestMemReader {
149162 regions : Vec < MemoryRegion > ,
150163}
@@ -164,6 +177,7 @@ impl ReadProcessMemory for GuestMemReader {
164177 buf : & mut [ u8 ] ,
165178 ) -> std:: result:: Result < usize , CoreError > {
166179 for r in self . regions . iter ( ) {
180+ // Check if the base address is within the guest region
167181 if base >= r. guest_region . start && base < r. guest_region . end {
168182 let offset = base - r. guest_region . start ;
169183 let region_slice = unsafe {
@@ -181,6 +195,8 @@ impl ReadProcessMemory for GuestMemReader {
181195
182196 // Only copy the amount that fits in both buffers
183197 buf[ ..copy_size] . copy_from_slice ( & region_slice[ offset..offset + copy_size] ) ;
198+
199+ // Return the number of bytes copied
184200 return std:: result:: Result :: Ok ( copy_size) ;
185201 }
186202 }
@@ -227,13 +243,13 @@ pub(crate) fn crashdump_to_tempfile(hv: &dyn Hypervisor) -> Result<()> {
227243 . write ( & temp_file)
228244 . map_err ( |e| new_error ! ( "Failed to write core dump: {:?}" , e) ) ?;
229245
230- // Persist the file with a .dmp extension
231- let persist_path = temp_file. path ( ) . with_extension ( "dmp" ) ;
246+ let persist_path = temp_file. path ( ) . with_extension ( "elf" ) ;
232247 temp_file
233248 . persist ( & persist_path)
234249 . map_err ( |e| new_error ! ( "Failed to persist core dump file: {:?}" , e) ) ?;
235250
236251 let path_string = persist_path. to_string_lossy ( ) . to_string ( ) ;
252+
237253 println ! ( "Core dump created successfully: {}" , path_string) ;
238254 log:: error!( "Core dump file: {}" , path_string) ;
239255
0 commit comments