@@ -498,6 +498,48 @@ impl JitMemory {
498498 self . handle_reloc ( dest, target) ;
499499 }
500500
501+ /// Dump the generated machine code as an objdump-style disassembly
502+ /// listing (the AArch64 counterpart of the x86-64 `dump_code`).
503+ ///
504+ /// The disassembler binary defaults to `objdump`, which is the native
505+ /// tool on an aarch64 host. When running the emulated tests on a
506+ /// non-aarch64 host, set the `OBJDUMP` environment variable to a
507+ /// cross-capable binutils (e.g. `aarch64-linux-gnu-objdump`) so the
508+ /// A64 stream is decoded correctly.
509+ pub fn dump_code ( & self ) -> Result < String , std:: io:: Error > {
510+ use std:: io:: Write ;
511+ use std:: process:: Command ;
512+ let asm = self . as_slice ( ) ;
513+ let mut file = tempfile:: NamedTempFile :: new ( ) ?;
514+ let ( start_pos, code_end, _end_pos) = self . code_block . last ( ) . unwrap ( ) ;
515+ file. write_all ( & asm[ start_pos. 0 ..code_end. 0 ] ) . unwrap ( ) ;
516+
517+ let objdump = std:: env:: var ( "OBJDUMP" ) . unwrap_or_else ( |_| "objdump" . to_string ( ) ) ;
518+ Command :: new ( objdump)
519+ . args ( [
520+ "-D" ,
521+ "-b" ,
522+ "binary" ,
523+ "-m" ,
524+ "aarch64" ,
525+ file. path ( ) . to_str ( ) . unwrap ( ) ,
526+ ] )
527+ . output ( )
528+ . map ( |o| {
529+ std:: str:: from_utf8 ( & o. stdout )
530+ . unwrap ( )
531+ . to_string ( )
532+ . split_inclusive ( '\n' )
533+ . filter ( |s| {
534+ s. len ( ) > 1
535+ && !s. contains ( "file format binary" )
536+ && !s. contains ( "Disassembly of section" )
537+ && !s. contains ( "<.data>" )
538+ } )
539+ . collect ( )
540+ } )
541+ }
542+
501543 /// Patch a single relocation `target` now that its label resolves to
502544 /// `(src_page, src_pos)`.
503545 pub ( crate ) fn write_reloc ( & mut self , src_page : Page , src_pos : Pos , target : TargetType ) {
0 commit comments