Fix unset ZapRelocationType for fixup (#18589)
[platform/upstream/coreclr.git] / Tools / DumplingHelper.py
1 import os
2 import platform
3 import urllib
4 import urllib2
5 import glob
6 import time
7 import sys
8 import subprocess
9 import string
10 import traceback
11
12 def get_timestamp():
13   print(time.time())
14
15 def install_dumpling():
16   try:
17     if (not os.path.isfile(dumplingPath)):
18       url = "https://dumpling.int-dot.net/api/client/dumpling.py"
19       scriptPath = os.path.dirname(os.path.realpath(__file__))
20       downloadLocation = scriptPath + "/dumpling.py"
21       response = urllib2.urlopen(url)
22       if response.getcode() == 200:
23         with open(downloadLocation, 'w') as f:
24           f.write(response.read())
25         subprocess.call([sys.executable, downloadLocation, "install", "--update"])
26       else:
27         raise urllib2.URLError("HTTP Status Code" + str(result.getcode()))
28
29     subprocess.call([sys.executable, dumplingPath, "install"])
30   except urllib2.HTTPError, e:
31     print("Dumpling cannot be installed from " + url + " due to: " + str(e).replace(':', '')) # Remove : to avoid looking like error format
32   except  urllib2.URLError, e:
33     print("Dumpling cannot be installed from " + url + " due to: " + str(e.reason))
34   except:
35     print("An unexpected error was encountered while installing dumpling.py: " + traceback.format_exc())
36
37 def ensure_installed():
38   if (not os.path.isfile(dumplingPath)):
39     print("Dumpling has not been installed yet. Please run \"DumplingHelper.py install_dumpling\" before collect_dumps.")
40     return False
41   else:
42     return True
43
44 def find_latest_dump(folder, startTimeStr):
45   startTime = float(startTimeStr)
46   globPattern = "/*";
47
48   # Outside of Windows, core files are generally dumped into the executable's directory,
49   # so it may have many other files in it. Filter those out.
50   if sys.platform != "win32":
51     globPattern = "/*core*"
52
53   allFiles = glob.glob(folder + globPattern);
54   if allFiles:
55     latestFile = max(allFiles, key=os.path.getmtime)
56     latestTime = os.path.getmtime(latestFile)
57     if (latestTime > startTime):
58       return latestFile
59   return None
60
61 def collect_dump(exitcodeStr, folder, startTimeStr, projectName, incpaths):
62   exitcode = int(exitcodeStr)
63
64   if (exitcode == 0):
65     sys.exit(exitcode)
66
67   if not ensure_installed():
68     sys.exit(exitcode)
69
70   if (not incpaths is None):
71     # Normalize incpaths so it can be passed to dumpling.py.
72     incpaths = incpaths.split(",")
73     incpaths = string.join(incpaths, " ")
74
75   # Find candidate crash dumps in the given folder.
76   print("Trying to find crash dumps for project: " + projectName)
77   file = find_latest_dump(folder, startTimeStr)
78   if (file is None):
79     print("No new dump file was found in " + folder)
80   else:
81     # File was found; upload it.
82     print("Uploading dump file: " + file)
83     procArgs = string.join([
84       sys.executable, dumplingPath, "upload",
85       "--dumppath", file,
86       "--noprompt",
87       "--triage", "none",
88       "--displayname", projectName,
89       "--properties", "STRESS_TESTID="+projectName
90       ], " ");
91     if (not incpaths is None):
92       procArgs = procArgs + " --incpaths " + incpaths
93
94     subprocess.call(procArgs, shell=True)
95
96   sys.exit(exitcode)
97
98 def print_usage():
99   print("DumplingHelper.py <command>")
100   print("Commands:")
101   print("  install_dumpling:")
102   print("      - Installs dumpling globally on the machine.")
103   print("  get_timestamp:")
104   print("      - Prints out the current timestamp of the machine.")
105   print("  collect_dump <exitcode> <folder> <starttime> <projectname> <incpaths>:")
106   print("      - Collects and uploads the latest dump (after start time) from the folder to the dumpling service.")
107
108 # Main
109 def main(argv):
110   if (len(argv) <= 1):
111     print_usage()
112     sys.exit(1)
113   if (argv[1] == "install_dumpling"):
114     install_dumpling()
115   elif (argv[1] == "get_timestamp"):
116     get_timestamp()
117   elif (argv[1] == "collect_dump"):
118     if (len(argv) == 6):
119       collect_dump(argv[2], argv[3], argv[4], argv[5], None)
120     elif (len(argv) == 7):
121       collect_dump(argv[2], argv[3], argv[4], argv[5], argv[6])
122     else:
123       print("Invalid number of arguments passed to collect_dump.")
124       sys.exit(1)
125   else:
126     print(argv[1] + " is not a valid command.")
127     print_usage()
128     sys.exit(1)
129
130 dumplingPath = os.path.expanduser("~/.dumpling/dumpling.py")
131 if __name__ == '__main__':
132   main(sys.argv)