|
| 1 | +#!/usr/bin/python2 |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +This is the main script of arnalyzer, which: |
| 6 | +- parse the options |
| 7 | +- pre-process the trace data file |
| 8 | +- call a specific script to do analysis |
| 9 | +""" |
| 10 | + |
| 11 | +import sys |
| 12 | +import getopt |
| 13 | +import os |
| 14 | +from vmexit_analyze import analyze_vm_exit |
| 15 | + |
| 16 | +def usage(): |
| 17 | + """print the usage of the script |
| 18 | + Args: NA |
| 19 | + Returns: None |
| 20 | + Raises: NA |
| 21 | + """ |
| 22 | + print ''' |
| 23 | + [Usage] acrnalyze.py [options] [value] ... |
| 24 | +
|
| 25 | + [options] |
| 26 | + -h: print this message |
| 27 | + -i, --ifile=[string]: input file |
| 28 | + -o, --ofile=[string]: output file |
| 29 | + --vm_exit: to generate vm_exit report |
| 30 | + ''' |
| 31 | + |
| 32 | +def do_analysis(ifile, ofile, analyzer): |
| 33 | + """do the specific analysis |
| 34 | +
|
| 35 | + Args: |
| 36 | + ifile: input trace data file |
| 37 | + ofile: output analysis report file |
| 38 | + analyzer: a function do the specific analysis |
| 39 | + Returns: |
| 40 | + None |
| 41 | + Raises: |
| 42 | + NA |
| 43 | + """ |
| 44 | + analyzer(ifile, ofile) |
| 45 | + |
| 46 | +# pre process to make sure the trace data start and end with VMENTER |
| 47 | +def pre_process(ifile, evstr): |
| 48 | + """invoke sh script to preprocess the data file |
| 49 | +
|
| 50 | + Args: |
| 51 | + ifile: input trace data file |
| 52 | + evstr: event string, after the processing, the data file should |
| 53 | + start and end with the string |
| 54 | + Returns: |
| 55 | + status of sh script execution |
| 56 | + Raises: |
| 57 | + NA |
| 58 | + """ |
| 59 | + status = os.system('bash pre_process.sh %s %s' % (ifile, evstr)) |
| 60 | + return status |
| 61 | + |
| 62 | +def main(argv): |
| 63 | + """Main enterance function |
| 64 | +
|
| 65 | + Args: |
| 66 | + argv: arguments string |
| 67 | + Returns: |
| 68 | + None |
| 69 | + Raises: |
| 70 | + GetoptError |
| 71 | + """ |
| 72 | + inputfile = '' |
| 73 | + outputfile = '' |
| 74 | + opts_short = "hi:o:" |
| 75 | + opts_long = ["ifile=", "ofile=", "vm_exit"] |
| 76 | + |
| 77 | + try: |
| 78 | + opts, args = getopt.getopt(argv, opts_short, opts_long) |
| 79 | + except getopt.GetoptError: |
| 80 | + usage() |
| 81 | + sys.exit(1) |
| 82 | + |
| 83 | + for opt, arg in opts: |
| 84 | + if opt == '-h': |
| 85 | + usage() |
| 86 | + sys.exit() |
| 87 | + elif opt in ("-i", "--ifile"): |
| 88 | + inputfile = arg |
| 89 | + elif opt in ("-o", "--ofile"): |
| 90 | + outputfile = arg |
| 91 | + elif opt == "--vm_exit": |
| 92 | + analyzer = analyze_vm_exit |
| 93 | + else: |
| 94 | + assert False, "unhandled option" |
| 95 | + |
| 96 | + assert inputfile != '', "input file is required" |
| 97 | + assert outputfile != '', "output file is required" |
| 98 | + assert analyzer != '', 'MUST contain one of analyzer: ''vm_exit' |
| 99 | + |
| 100 | + status = pre_process(inputfile, 'VM_ENTER') |
| 101 | + if status == 0: |
| 102 | + do_analysis(inputfile, outputfile, analyzer) |
| 103 | + else: |
| 104 | + print "Invalid trace data file %s" % (inputfile) |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main(sys.argv[1:]) |
0 commit comments