import time


def generateGDBScript(logFile="gdb.txt", inputFile="", runArgs=[]):
    """ Generates the GDB script needed for trace generation """
    gdbScript = open(logFile.replace(".txt",".script_%s" % str(time.time())[:-3]), "w")
    if len(runArgs) > 0 or len(inputFile) > 0:
        # Bundle all the arguments in one string
        args = " ".join([a for a in runArgs])#未使用
        # Beware of the "step" versus "stepi"
        gdbScript.write("set logging file %s\nset logging on\nset height 0\nset $_exitcode = -999\nset $_instructioncount = 0\nbreak __libc_start_main\nrun `< %s`\nwhile $_instructioncount <= 50000\n\tx/i $pc\n\tstepi\n\tif $_exitcode != -999\n\t\tset logging off\n\t\tquit\n\tend\n\tset $_instructioncount = $_instructioncount + 1\nend\nset logging off\nquit" % (logFile, inputFile))
    else:
        gdbScript.write("set logging file %s\nset logging on\nset height 0\nset $_exitcode = -999\nset $_instructioncount = 0\nbreak __libc_start_main\nrun\nwhile $_instructioncount <= 50000\n\tx/i $pc\n\tstepi\n\tif $_exitcode != -999\n\t\tset logging off\n\t\tquit\n\tend\n\tset $_instructioncount = $_instructioncount + 1\nend\nset logging off\nquit" % logFile)
    gdbScript.close()
    return gdbScript.name