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)
def xml_verify(target): """Pass the XML file through xmllint""" os.system('xmllint --path "tools/pyelf/dtd ../../dtd ../dtd" '\ '-o /dev/null --valid %s' % target) def gen_report(reportname, xml_file, n_objs, verbose): """Generate the named report from a given XML file""" try: parsed = Memstats_el.parse_xml_file(xml_file) except EzXMLError, text: print >> sys.stderr, text sys.exit(1) if REPORTS_SET.has_key(reportname): report = REPORTS_SET[reportname](parsed) report.generate(n_objs, verbose) else: print >> sys.stderr, 'Error: "%s" is not a valid report name.' % \ reportname print >> sys.stderr, 'Valid report names are:' for rep in REPORTS_SET: print >> sys.stderr, ' ' + rep sys.exit(1) def gen_diff_report(reportname, xml_file, diff_xml_file, n_objs, verbose): """Generate a diff version of the named report from the given XML files"""
return prog def xml_verify(target): """Pass the XML file through xmllint""" os.system('xmllint --path "tools/pyelf/dtd ../../dtd ../dtd" '\ '-o /dev/null --valid %s' % target) def gen_report(reportname, xml_file, n_objs, verbose): """Generate the named report from a given XML file""" try: parsed = Memstats_el.parse_xml_file(xml_file) except EzXMLError, text: print >> sys.stderr, text sys.exit(1) if REPORTS_SET.has_key(reportname): report = REPORTS_SET[reportname](parsed) report.generate(n_objs, verbose) else: print >> sys.stderr, 'Error: "%s" is not a valid report name.' % \ reportname print >> sys.stderr, 'Valid report names are:' for rep in REPORTS_SET: print >> sys.stderr, ' ' + rep sys.exit(1) def gen_diff_report(reportname, xml_file, diff_xml_file, n_objs, verbose): """Generate a diff version of the named report from the given XML files""" try: