out-of-tree kernel {module, exploit} development tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

log.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2019 Mikhail Klementev. All rights reserved.
  2. // Use of this source code is governed by a AGPLv3 license
  3. // (or later) that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "database/sql"
  7. "encoding/json"
  8. "fmt"
  9. "log"
  10. "math"
  11. "os"
  12. "github.com/olekukonko/tablewriter"
  13. "gopkg.in/logrusorgru/aurora.v2"
  14. "code.dumpstack.io/tools/out-of-tree/config"
  15. )
  16. func logLogEntry(l logEntry) {
  17. distroInfo := fmt.Sprintf("%s-%s {%s}", l.DistroType,
  18. l.DistroRelease, l.KernelRelease)
  19. artifactInfo := fmt.Sprintf("{[%s] %s}", l.Type, l.Name)
  20. colored := ""
  21. if l.Type == config.KernelExploit {
  22. colored = aurora.Sprintf("[%4d %4s] [%s] %40s %40s: %s %s",
  23. l.ID, l.Tag, l.Timestamp, artifactInfo, distroInfo,
  24. genOkFail("BUILD", l.Build.Ok),
  25. genOkFail("LPE", l.Test.Ok))
  26. } else {
  27. colored = aurora.Sprintf("[%4d %4s] [%s] %40s %40s: %s %s %s",
  28. l.ID, l.Tag, l.Timestamp, artifactInfo, distroInfo,
  29. genOkFail("BUILD", l.Build.Ok),
  30. genOkFail("INSMOD", l.Run.Ok),
  31. genOkFail("TEST", l.Test.Ok))
  32. }
  33. additional := ""
  34. if l.KernelPanic {
  35. additional = "(panic)"
  36. } else if l.KilledByTimeout {
  37. additional = "(timeout)"
  38. }
  39. if additional != "" {
  40. fmt.Println(colored, additional)
  41. } else {
  42. fmt.Println(colored)
  43. }
  44. }
  45. func logHandler(db *sql.DB, path, tag string, num int, rate bool) (err error) {
  46. var les []logEntry
  47. ka, kaErr := config.ReadArtifactConfig(path + "/.out-of-tree.toml")
  48. if kaErr == nil {
  49. log.Println(".out-of-tree.toml found, filter by artifact name")
  50. les, err = getAllArtifactLogs(db, tag, num, ka)
  51. } else {
  52. les, err = getAllLogs(db, tag, num)
  53. }
  54. if err != nil {
  55. return
  56. }
  57. s := "\nS"
  58. if rate {
  59. if kaErr != nil {
  60. err = kaErr
  61. return
  62. }
  63. s = fmt.Sprintf("{[%s] %s} Overall s", ka.Type, ka.Name)
  64. les, err = getAllArtifactLogs(db, tag, math.MaxInt64, ka)
  65. if err != nil {
  66. return
  67. }
  68. } else {
  69. for _, l := range les {
  70. logLogEntry(l)
  71. }
  72. }
  73. success := 0
  74. for _, l := range les {
  75. if l.Test.Ok {
  76. success++
  77. }
  78. }
  79. overall := float64(success) / float64(len(les))
  80. fmt.Printf("%success rate: %.04f (~%.0f%%)\n",
  81. s, overall, overall*100)
  82. return
  83. }
  84. func logDumpHandler(db *sql.DB, id int) (err error) {
  85. var l logEntry
  86. if id > 0 {
  87. l, err = getLogByID(db, id)
  88. } else {
  89. l, err = getLastLog(db)
  90. }
  91. if err != nil {
  92. return
  93. }
  94. fmt.Println("ID:", l.ID)
  95. fmt.Println("Date:", l.Timestamp)
  96. fmt.Println("Tag:", l.Tag)
  97. fmt.Println()
  98. fmt.Println("Type:", l.Type.String())
  99. fmt.Println("Name:", l.Name)
  100. fmt.Println()
  101. fmt.Println("Distro:", l.DistroType.String(), l.DistroRelease)
  102. fmt.Println("Kernel:", l.KernelRelease)
  103. fmt.Println()
  104. fmt.Println("Build ok:", l.Build.Ok)
  105. if l.Type == config.KernelModule {
  106. fmt.Println("Insmod ok:", l.Run.Ok)
  107. }
  108. fmt.Println("Test ok:", l.Test.Ok)
  109. fmt.Println()
  110. fmt.Printf("Build output:\n%s\n", l.Build.Output)
  111. fmt.Println()
  112. if l.Type == config.KernelModule {
  113. fmt.Printf("Insmod output:\n%s\n", l.Run.Output)
  114. fmt.Println()
  115. }
  116. fmt.Printf("Test output:\n%s\n", l.Test.Output)
  117. fmt.Println()
  118. fmt.Printf("Qemu stdout:\n%s\n", l.Stdout)
  119. fmt.Println()
  120. fmt.Printf("Qemu stderr:\n%s\n", l.Stderr)
  121. fmt.Println()
  122. return
  123. }
  124. type runstat struct {
  125. All, BuildOK, RunOK, TestOK, Timeout, Panic int
  126. }
  127. func getStats(db *sql.DB, path, tag string) (
  128. distros map[string]map[string]map[string]runstat, err error) {
  129. var les []logEntry
  130. ka, kaErr := config.ReadArtifactConfig(path + "/.out-of-tree.toml")
  131. if kaErr == nil {
  132. les, err = getAllArtifactLogs(db, tag, -1, ka)
  133. } else {
  134. les, err = getAllLogs(db, tag, -1)
  135. }
  136. if err != nil {
  137. return
  138. }
  139. distros = make(map[string]map[string]map[string]runstat)
  140. for _, l := range les {
  141. _, ok := distros[l.DistroType.String()]
  142. if !ok {
  143. distros[l.DistroType.String()] = make(map[string]map[string]runstat)
  144. }
  145. _, ok = distros[l.DistroType.String()][l.DistroRelease]
  146. if !ok {
  147. distros[l.DistroType.String()][l.DistroRelease] = make(map[string]runstat)
  148. }
  149. rs := distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease]
  150. rs.All++
  151. if l.Build.Ok {
  152. rs.BuildOK++
  153. }
  154. if l.Run.Ok {
  155. rs.RunOK++
  156. }
  157. if l.Test.Ok {
  158. rs.TestOK++
  159. }
  160. if l.KernelPanic {
  161. rs.Panic++
  162. }
  163. if l.KilledByTimeout {
  164. rs.Timeout++
  165. }
  166. distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease] = rs
  167. }
  168. return
  169. }
  170. func logJSONHandler(db *sql.DB, path, tag string) (err error) {
  171. distros, err := getStats(db, path, tag)
  172. if err != nil {
  173. return
  174. }
  175. bytes, err := json.Marshal(&distros)
  176. if err != nil {
  177. return
  178. }
  179. fmt.Println(string(bytes))
  180. return
  181. }
  182. func logMarkdownHandler(db *sql.DB, path, tag string) (err error) {
  183. distros, err := getStats(db, path, tag)
  184. if err != nil {
  185. return
  186. }
  187. table := tablewriter.NewWriter(os.Stdout)
  188. table.SetHeader([]string{"Distro", "Release", "Kernel", "Reliability"})
  189. table.SetBorders(tablewriter.Border{
  190. Left: true, Top: false, Right: true, Bottom: false})
  191. table.SetCenterSeparator("|")
  192. for distro, releases := range distros {
  193. for release, kernels := range releases {
  194. for kernel, stats := range kernels {
  195. all := float64(stats.All)
  196. ok := float64(stats.TestOK)
  197. r := fmt.Sprintf("%6.02f%%", (ok/all)*100)
  198. table.Append([]string{distro, release, kernel, r})
  199. }
  200. }
  201. }
  202. table.Render()
  203. return
  204. }