report-error.bbclass 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #
  2. # Collects debug information in order to create error report files.
  3. #
  4. # Copyright (C) 2013 Intel Corporation
  5. # Author: Andreea Brandusa Proca <andreea.b.proca@intel.com>
  6. #
  7. # SPDX-License-Identifier: MIT
  8. #
  9. ERR_REPORT_DIR ?= "${LOG_DIR}/error-report"
  10. def errorreport_getdata(e):
  11. import codecs
  12. logpath = e.data.getVar('ERR_REPORT_DIR')
  13. datafile = os.path.join(logpath, "error-report.txt")
  14. with codecs.open(datafile, 'r', 'utf-8') as f:
  15. data = f.read()
  16. return data
  17. def errorreport_savedata(e, newdata, file):
  18. import json
  19. import codecs
  20. logpath = e.data.getVar('ERR_REPORT_DIR')
  21. datafile = os.path.join(logpath, file)
  22. with codecs.open(datafile, 'w', 'utf-8') as f:
  23. json.dump(newdata, f, indent=4, sort_keys=True)
  24. return datafile
  25. def get_conf_data(e, filename):
  26. builddir = e.data.getVar('TOPDIR')
  27. filepath = os.path.join(builddir, "conf", filename)
  28. jsonstring = ""
  29. if os.path.exists(filepath):
  30. with open(filepath, 'r') as f:
  31. for line in f.readlines():
  32. if line.startswith("#") or len(line.strip()) == 0:
  33. continue
  34. else:
  35. jsonstring=jsonstring + line
  36. return jsonstring
  37. def get_common_data(e):
  38. data = {}
  39. data['machine'] = e.data.getVar("MACHINE")
  40. data['build_sys'] = e.data.getVar("BUILD_SYS")
  41. data['distro'] = e.data.getVar("DISTRO")
  42. data['target_sys'] = e.data.getVar("TARGET_SYS")
  43. data['branch_commit'] = str(oe.buildcfg.detect_branch(e.data)) + ": " + str(oe.buildcfg.detect_revision(e.data))
  44. data['bitbake_version'] = e.data.getVar("BB_VERSION")
  45. data['layer_version'] = get_layers_branch_rev(e.data)
  46. data['local_conf'] = get_conf_data(e, 'local.conf')
  47. data['auto_conf'] = get_conf_data(e, 'auto.conf')
  48. return data
  49. python errorreport_handler () {
  50. import json
  51. import codecs
  52. def nativelsb():
  53. nativelsbstr = e.data.getVar("NATIVELSBSTRING")
  54. # provide a bit more host info in case of uninative build
  55. if e.data.getVar('UNINATIVE_URL') != 'unset':
  56. return '/'.join([nativelsbstr, lsb_distro_identifier(e.data)])
  57. return nativelsbstr
  58. logpath = e.data.getVar('ERR_REPORT_DIR')
  59. datafile = os.path.join(logpath, "error-report.txt")
  60. if isinstance(e, bb.event.BuildStarted):
  61. bb.utils.mkdirhier(logpath)
  62. data = {}
  63. data = get_common_data(e)
  64. data['nativelsb'] = nativelsb()
  65. data['failures'] = []
  66. data['component'] = " ".join(e.getPkgs())
  67. lock = bb.utils.lockfile(datafile + '.lock')
  68. errorreport_savedata(e, data, "error-report.txt")
  69. bb.utils.unlockfile(lock)
  70. elif isinstance(e, bb.build.TaskFailed):
  71. task = e.task
  72. taskdata={}
  73. log = e.data.getVar('BB_LOGFILE')
  74. taskdata['recipe'] = e.data.expand("${PN}")
  75. taskdata['package'] = e.data.expand("${PF}")
  76. taskdata['task'] = task
  77. if log:
  78. try:
  79. with codecs.open(log, encoding='utf-8') as logFile:
  80. logdata = logFile.read()
  81. # Replace host-specific paths so the logs are cleaner
  82. for d in ("TOPDIR", "TMPDIR"):
  83. s = e.data.getVar(d)
  84. if s:
  85. logdata = logdata.replace(s, d)
  86. except:
  87. logdata = "Unable to read log file"
  88. else:
  89. logdata = "No Log"
  90. # server will refuse failures longer than param specified in project.settings.py
  91. # MAX_UPLOAD_SIZE = "5242880"
  92. # use lower value, because 650 chars can be spent in task, package, version
  93. max_logdata_size = 5242000
  94. # upload last max_logdata_size characters
  95. if len(logdata) > max_logdata_size:
  96. logdata = "..." + logdata[-max_logdata_size:]
  97. taskdata['log'] = logdata
  98. lock = bb.utils.lockfile(datafile + '.lock')
  99. jsondata = json.loads(errorreport_getdata(e))
  100. jsondata['failures'].append(taskdata)
  101. errorreport_savedata(e, jsondata, "error-report.txt")
  102. bb.utils.unlockfile(lock)
  103. elif isinstance(e, bb.event.NoProvider):
  104. bb.utils.mkdirhier(logpath)
  105. data = {}
  106. data = get_common_data(e)
  107. data['nativelsb'] = nativelsb()
  108. data['failures'] = []
  109. data['component'] = str(e._item)
  110. taskdata={}
  111. taskdata['log'] = str(e)
  112. taskdata['package'] = str(e._item)
  113. taskdata['task'] = "Nothing provides " + "'" + str(e._item) + "'"
  114. data['failures'].append(taskdata)
  115. lock = bb.utils.lockfile(datafile + '.lock')
  116. errorreport_savedata(e, data, "error-report.txt")
  117. bb.utils.unlockfile(lock)
  118. elif isinstance(e, bb.event.ParseError):
  119. bb.utils.mkdirhier(logpath)
  120. data = {}
  121. data = get_common_data(e)
  122. data['nativelsb'] = nativelsb()
  123. data['failures'] = []
  124. data['component'] = "parse"
  125. taskdata={}
  126. taskdata['log'] = str(e._msg)
  127. taskdata['task'] = str(e._msg)
  128. data['failures'].append(taskdata)
  129. lock = bb.utils.lockfile(datafile + '.lock')
  130. errorreport_savedata(e, data, "error-report.txt")
  131. bb.utils.unlockfile(lock)
  132. elif isinstance(e, bb.event.BuildCompleted):
  133. lock = bb.utils.lockfile(datafile + '.lock')
  134. jsondata = json.loads(errorreport_getdata(e))
  135. bb.utils.unlockfile(lock)
  136. failures = jsondata['failures']
  137. if(len(failures) > 0):
  138. filename = "error_report_" + e.data.getVar("BUILDNAME")+".txt"
  139. datafile = errorreport_savedata(e, jsondata, filename)
  140. bb.note("The errors for this build are stored in %s\nYou can send the errors to a reports server by running:\n send-error-report %s [-s server]" % (datafile, datafile))
  141. bb.note("The contents of these logs will be posted in public if you use the above command with the default server. Please ensure you remove any identifying or proprietary information when prompted before sending.")
  142. }
  143. addhandler errorreport_handler
  144. errorreport_handler[eventmask] = "bb.event.BuildStarted bb.event.BuildCompleted bb.build.TaskFailed bb.event.NoProvider bb.event.ParseError"