def print_cmd(args): """Display an ELF file. args contains command line arguments passed from sys.argv. These are parsed using option parser.""" parser = OptionParser("%prog print [options] file", add_help_option=0) parser.add_option("-H", "--help", action="help") parser.add_option("-a", "--all", action="store_true", dest="all", help="Print all information") parser.add_option("-h", "--header", action="store_true", dest="header", help="Print ELF header") parser.add_option("-l", "--pheaders", action="store_true", dest="pheaders", help="Print ELF sections headers") parser.add_option("-S", "--sheaders", action="store_true", dest="sheaders", help="Print ELF sections headers") parser.add_option("-k", "--kconfig", action="store_true", dest="kconfig", help="Print L4 kernel config data structure (Default)") parser.add_option("-B", "--bootinfo", action="store_true", dest="bootinfo", help="Print L4 Bootinfo") parser.add_option("-m", "--segnames", action="store_true", dest="segnames", help="Print segment names") parser.add_option("-s", "--syms", action="store_true", dest="symbols", help="Print the symbol table") parser.add_option("-r", "--relocs", action="store_true", dest="relocs", help="Display the relocations (if present)") parser.add_option("-W", "--wide", action="store_true", dest="wide", default=False, help="Allow output width to exceed 80 characters") parser.add_option("-e", "--elfweaver-info", action="store_true", dest="elfweaverinfo", help="Print elfweaver info section") (options, args) = parser.parse_args(args) if len(args) != 1: parser.error("incorrect number of arguments") if options.all: options.header = options.pheaders = options.sheaders = \ options.kconfig = options.bootinfo = \ options.elfweaverinfo = options.segnames = True elf = PreparedElfFile(filename=args[0]) if options.header: elf.get_elf_header().output(sys.stdout) if options.sheaders: print_sheaders(elf, not options.header, sys.stdout) if options.pheaders: print_pheaders(elf, not options.header, sys.stdout) if options.symbols: print_symbol_table(elf, sys.stdout, options.wide) if options.relocs: print_relocations(elf, sys.stdout, options.wide) if options.kconfig: kern_ver = check_api_versions(elf) if kern_ver == NANO_KERNEL_API_VERSION: kconfig = find_nano_heap(elf) else: # kern_ver == MICRO_KERNEL_API_VERSION: kconfig = find_kernel_config(elf) print if kconfig: kconfig.output(sys.stdout) else: print "There is no kernel configuration in this file." if kern_ver != NANO_KERNEL_API_VERSION: print initscript = find_init_script(elf) if initscript: initscript.output(sys.stdout) else: print "There is no initialisation script in this file." if options.elfweaverinfo: print elfweaverinfo = find_elfweaver_info(elf) if elfweaverinfo: elfweaverinfo.output(sys.stdout) else: print "There is no elfweaver info section in this file." if options.bootinfo: print bootinfo = find_bootinfo(elf) if bootinfo: bootinfo.output(sys.stdout) else: print "There is no Bootinfo section in this file." if options.segnames: segcount = 0 print segnames = get_segnames(elf) if segnames: print "Segment Name" for segname in segnames.strings[1:]: idx = segname.index('\x00') segname = segname[:idx] segname = segname.strip() if segname != "": print " %02d %s" % (segcount, segname) segcount += 1 else: # TODO - use the first section in each segment # as the segment name and print that. print "There is no .segnames section in this file" return 0
def memstats_cmd(args): """Memory Usage Statistics program implementation""" parser = OptionParser("%prog memstats [options] file [file]", add_help_option=0) parser.add_option("-H", "--help", action="help") parser.add_option("-x", "--xml", action="store_true", dest="output_xml", help="Produces an XML file containing memory statistics" \ " in the same directory as the supplied ELF.") parser.add_option("-y", "--verify", action="store_true", dest="verify_xml", help="Verify the XML file.") parser.add_option("-r", "--report", action="store", dest="report", metavar="NAME", help="Parse a memory statistics XML file and output "\ "the named report. Valid reports are: '%s'." % "', '".join(REPORTS_SET.keys())) parser.add_option("-v", "--verbose", action="store_true", dest="verbose_report", help="Produce the verbose form of the requested report.") parser.add_option("-l", "--largest-num", action="store", dest="n_objs", type="int", help="Print, at most, the largest N_OBJS " \ "text and code objects.") parser.add_option("-d", "--diff", action="store_true", dest="output_diff", help="Used on a report, to indicate there are two " "files and the differences should be reported.") parser.add_option("-R", "--repository", action="store", dest="repository", default=None, help="Include the version control repository in the " "XML file.") parser.add_option("-c", "--changeset", action="store", dest="changeset", default=None, help="Include the version control changeset in the " "XML file.") (options, args) = parser.parse_args(args) IdList.reset() Heap.reset() if len(args) < 1 or args[0] is "": parser.error("Missing file argument(s)") input_file = args[0] if not os.path.exists(input_file): print >> sys.stderr, 'Error: File "%s" does not exist.' % input_file return 1 if options.output_xml: elf = PreparedElfFile(filename=input_file) if not elf.segments: print >> sys.stderr, \ 'Error: ELF file "%s" contains no segments.' % input_file return 1 if options.output_diff and not options.report: parser.error("Diff only makes sense on a report.") return # -x implies input is an ELF # -t implies input is XML if options.output_xml: notes_sec = notes.find_elfweaver_notes(elf) memstats = Memstats(elf.machine, notes_sec.cpu, notes_sec.poolname, notes_sec.memsecs, notes_sec.cell_names, notes_sec.space_names, notes_sec.mappings) programs = [] parse_segments(elf, programs, elf.segments, memstats) xml_file = os.path.join(os.path.dirname(input_file), "memstats.xml") hg_stats = (options.repository, options.changeset) memstats.set_revision(hg_stats) kern_ver = check_api_versions(elf) if kern_ver == MICRO_KERNEL_API_VERSION: memstats.env.env_type = "Micro" import weaver.kernel_micro_elf script = weaver.kernel_micro_elf.find_init_script(elf, memstats) elif kern_ver == NANO_KERNEL_API_VERSION: memstats.env.env_type = "Nano" import weaver.kernel_nano_elf heap = weaver.kernel_nano_elf.find_nano_heap(elf) heap.decode(memstats) else: print >> sys.stderr, "Unknown kernel type." return 1 xml_f = open(xml_file, "w") xml_f.write(memstats.format()) xml_f.close() if options.verify_xml: xml_verify(xml_file) if options.report: if options.output_xml: input_file = xml_file if options.output_diff: if len(args) != 2: parser.error("Report diffs require two files.") diff_file = args[1] if options.verify_xml: xml_verify(input_file) xml_verify(diff_file) gen_diff_report(options.report, input_file, diff_file, options.n_objs, options.verbose_report) else: if options.verify_xml: xml_verify(input_file) gen_report(options.report, input_file, options.n_objs, options.verbose_report)