of_unittest_expect 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. #!/usr/bin/perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Copyright 2020, 2022 Sony Corporation
  5. #
  6. # Author: Frank Rowand
  7. # This program is meant to be an aid to reading the verbose output of
  8. # on the console log that results from executing the Linux kernel
  9. # devicetree unittest (drivers/of/unitest.c).
  10. $VUFX = "230211a";
  11. use strict 'refs';
  12. use strict subs;
  13. use Getopt::Long;
  14. use Text::Wrap;
  15. # strip off everything before final "/"
  16. (undef, $script_name) = split(/^.*\//, $0);
  17. # following /usr/include/sysexits.h
  18. $EX_OK=0;
  19. $EX_USAGE=64;
  20. #______________________________________________________________________________
  21. sub compare {
  22. my ($expect, $got) = @_;
  23. my $expect_next;
  24. my $expect_next_lit;
  25. my $got_next;
  26. my $type;
  27. while ($expect) {
  28. ($expect_next, $type) = split(/<</, $expect);
  29. ($type) = split(/>>/, $type);
  30. $expect =~ s/^.*?>>//; # '?' is non-greedy, minimal match
  31. # literal, ignore all metacharacters when used in a regex
  32. $expect_next_lit = quotemeta($expect_next);
  33. $got_next = $got;
  34. $got_next =~ s/^($expect_next_lit).*/\1/;
  35. $got =~ s/^$expect_next_lit//;
  36. if ($expect_next ne $got_next) {
  37. return 0;
  38. }
  39. if ($type eq "int") {
  40. if ($got =~ /^[+-]*[0-9]+/) {
  41. $got =~ s/^[+-]*[0-9]+//;
  42. } else {
  43. return 0;
  44. }
  45. } elsif ($type eq "hex") {
  46. if ($got =~ /^(0x)*[0-9a-f]+/) {
  47. $got =~ s/^(0x)*[0-9a-f]+//;
  48. } else {
  49. return 0;
  50. }
  51. } elsif ($type eq "all") {
  52. return 1;
  53. } elsif ($type eq "") {
  54. if ($expect_next ne $got_next) {
  55. return 0;
  56. } else {
  57. return 1;
  58. }
  59. } else {
  60. $internal_err++;
  61. print "** ERROR: special pattern not recognized: <<$type>>, CONSOLE_LOG line: $.\n";
  62. return 0;
  63. }
  64. }
  65. # should not get here
  66. $internal_err++;
  67. print "** ERROR: $script_name internal error, at end of compare(), CONSOLE_LOG line: $.\n";
  68. return 0;
  69. }
  70. #______________________________________________________________________________
  71. sub usage {
  72. # ***** when editing, be careful to not put tabs in the string printed:
  73. print STDERR
  74. "
  75. usage:
  76. $script_name CONSOLE_LOG
  77. -h print program usage
  78. --help print program usage
  79. --hide-expect suppress output of EXPECTed lines
  80. --line-num report line number of CONSOLE_LOG
  81. --no-expect-stats do not report EXPECT statistics
  82. --no-strip-ts do not strip leading console timestamps
  83. --verbose do not suppress EXPECT begin and end lines
  84. --version print program version and exit
  85. Process a console log for EXPECTed test related messages to either
  86. highlight expected devicetree unittest related messages or suppress
  87. the messages. Leading console timestamps will be stripped.
  88. Various unittests may trigger kernel messages from outside the
  89. unittest code. The unittest annotates that it expects the message
  90. to occur with an 'EXPECT \\ : text' (begin) before triggering the
  91. message, and an 'EXPECT / : text' (end) after triggering the message.
  92. If an expected message does not occur, that will be reported.
  93. For each expected message, the 'EXPECT \\ : text' (begin) and
  94. 'EXPECT / : text' (end), 'text' will contain the message text.
  95. If 'EXPECT \\' (begin) and 'EXPECT /' (end) lines do not contain
  96. matching 'text', that will be reported.
  97. If EXPECT lines are nested, 'EXPECT /' (end) lines must be in the
  98. reverse order of the corresponding 'EXPECT \\' (begin) lines.
  99. 'EXPECT \\ : text' (begin) and 'EXPECT / : text' (end) lines can
  100. contain special patterns in 'text':
  101. <<int>> matches: [+-]*[0-9]+
  102. <<hex>> matches: (0x)*[0-9a-f]+
  103. <<all>> matches: anything to end of line
  104. 'EXPECT \\' (begin) and 'EXPECT /' (end) lines are suppressed.
  105. A prefix is added to every line of output:
  106. 'ok ' Line matches an enclosing EXPECT begin/end pair
  107. '** ' Line reports $script_name warning or error
  108. '-> ' Line reports start or end of the unittests
  109. '>> ' Line reports a unittest test FAIL
  110. ' ' Lines that are not otherwise prefixed
  111. Issues detected in CONSOLE_LOG are reported to STDOUT, not to STDERR.
  112. Known Issues:
  113. --line-num causes the CONSOLE_LOG line number to be printed in 4 columns.
  114. If CONSOLE_LOG contains more than 9999 lines then more columns will be
  115. used to report the line number for lines greater than 9999 (eg for
  116. lines 10000 - 99999, 5 columns will be used).
  117. ";
  118. return {};
  119. }
  120. #______________________________________________________________________________
  121. #______________________________________________________________________________
  122. if (!GetOptions(
  123. "h" => \$help,
  124. "help" => \$help,
  125. "hide-expect" => \$hide_expect,
  126. "line-num" => \$print_line_num,
  127. "no-expect-stats" => \$no_expect_stats,
  128. "no-strip-ts" => \$no_strip_ts,
  129. "verbose" => \$verbose,
  130. "version" => \$version,
  131. )) {
  132. print STDERR "\n";
  133. print STDERR "ERROR processing command line options\n";
  134. print STDERR "\n";
  135. print STDERR "For help, type '$script_name --help'\n";
  136. print STDERR "\n";
  137. exit $EX_OK;
  138. }
  139. if ($no_strip_ts) {
  140. $strip_ts = 1;
  141. $no_strip_ts = 0;
  142. } else {
  143. $strip_ts = 0;
  144. $no_strip_ts = 1;
  145. }
  146. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  147. if ($help){
  148. &usage;
  149. exit $EX_OK;
  150. }
  151. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  152. if ($version) {
  153. print STDERR "\n$script_name $VUFX\n\n";
  154. print STDERR "\n";
  155. exit $EX_OK;
  156. }
  157. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  158. if ($#ARGV != 0) {
  159. # Limit input files to exactly one.
  160. #
  161. # 'while ($line = <ARGV>) {' in the code below supports multiple file
  162. # names on the command line, but the EXPECT statistics are reported
  163. # once for all input - it is not an expected use case to generate one
  164. # set of statistics for multiple input files.
  165. print STDERR "\n";
  166. print STDERR "Required arguments: CONSOLE_LOG\n";
  167. print STDERR "\n";
  168. exit $EX_USAGE;
  169. }
  170. #______________________________________________________________________________
  171. # Patterns to match 'EXPECT \ : ' (begin) and 'EXPECT / : ' (end)
  172. #
  173. # $exp_* are used as regex match patterns,
  174. # so '\\\\' in $exp_begin matches a single '\'
  175. # quotemeta() does not do the right thing in this case
  176. #
  177. # $pr_fmt is the prefix that unittest prints for every message
  178. $pr_fmt = "### dt-test ### ";
  179. $exp_begin = "${pr_fmt}EXPECT \\\\ : ";
  180. $exp_end = "${pr_fmt}EXPECT / : ";
  181. $expnot_begin = "${pr_fmt}EXPECT_NOT \\\\ : ";
  182. $expnot_end = "${pr_fmt}EXPECT_NOT / : ";
  183. $line_num = "";
  184. $timestamp = "";
  185. LINE:
  186. while ($line = <ARGV>) {
  187. chomp $line;
  188. $suppress_line = 0;
  189. $prefix = " "; ## 2 characters
  190. if ($strip_ts) {
  191. $timestamp = $line;
  192. if ($timestamp =~ /^\[\s*[0-9]+\.[0-9]*\] /) {
  193. ($timestamp, $null) = split(/]/, $line);
  194. $timestamp = $timestamp . "] ";
  195. } else {
  196. $timestamp = "";
  197. }
  198. }
  199. $line =~ s/^\[\s*[0-9]+\.[0-9]*\] //;
  200. # ----- find EXPECT begin
  201. if ($line =~ /^\s*$exp_begin/) {
  202. $data = $line;
  203. $data =~ s/^\s*$exp_begin//;
  204. push @exp_begin_stack, $data;
  205. if ($verbose) {
  206. if ($print_line_num) {
  207. $line_num = sprintf("%4s ", $.);
  208. }
  209. printf "%s %s%s%s\n", $prefix, $line_num, $timestamp, $line;
  210. }
  211. next LINE;
  212. }
  213. # ----- find EXPECT end
  214. if ($line =~ /^\s*$exp_end/) {
  215. $data = $line;
  216. $data =~ s/^\s*$exp_end//;
  217. if ($verbose) {
  218. if ($print_line_num) {
  219. $line_num = sprintf("%4s ", $.);
  220. }
  221. printf "%s %s%s%s\n", $prefix, $line_num, $timestamp, $line;
  222. }
  223. $found = 0;
  224. $no_begin = 0;
  225. if (@exp_found_or_begin > 0) {
  226. $begin = pop @exp_found_or_begin;
  227. if (compare($data, $begin)) {
  228. $found = 1;
  229. $exp_found++;
  230. }
  231. } elsif (@begin > 0) {
  232. $begin = pop @exp_begin_stack;
  233. } else {
  234. $no_begin = 1;
  235. }
  236. if ($no_begin) {
  237. $exp_missing_begin++;
  238. print "** ERROR: EXPECT end without matching EXPECT begin:\n";
  239. print " end ---> $line\n";
  240. } elsif (! $found) {
  241. if ($print_line_num) {
  242. $line_num = sprintf("%4s ", $.);
  243. }
  244. $exp_missing++;
  245. printf "** %s%s$script_name WARNING - not found ---> %s\n",
  246. $line_num, $timestamp, $data;
  247. } elsif (! compare($data, $begin) and ($data ne $begin)) {
  248. $exp_missing_end++;
  249. print "** ERROR: EXPECT end does not match EXPECT begin:\n";
  250. print " begin -> $begin\n";
  251. print " end ---> $line\n";
  252. }
  253. next LINE;
  254. }
  255. # ----- find EXPECT_NOT begin
  256. if ($line =~ /^\s*$expnot_begin/) {
  257. $data = $line;
  258. $data =~ s/^\s*$expnot_begin//;
  259. push @expnot_begin_stack, $data;
  260. if ($verbose) {
  261. if ($print_line_num) {
  262. $line_num = sprintf("%4s ", $.);
  263. }
  264. printf "%s %s%s%s\n", $prefix, $line_num, $timestamp, $line;
  265. }
  266. next LINE;
  267. }
  268. # ----- find EXPECT_NOT end
  269. if ($line =~ /^\s*$expnot_end/) {
  270. $data = $line;
  271. $data =~ s/^\s*$expnot_end//;
  272. if ($verbose) {
  273. if ($print_line_num) {
  274. $line_num = sprintf("%4s ", $.);
  275. }
  276. printf "%s %s%s%s\n", $prefix, $line_num, $timestamp, $line;
  277. }
  278. $found = 0;
  279. $no_begin = 0;
  280. if (@expnot_found_or_begin > 0) {
  281. $begin = pop @expnot_found_or_begin;
  282. if (compare($data, $begin)) {
  283. $found = 1;
  284. $expnot_found++;
  285. }
  286. } elsif (@expnot_begin_stack <= 0) {
  287. $no_begin = 1;
  288. }
  289. if ($no_begin) {
  290. $expnot_missing_begin++;
  291. print "** ERROR: EXPECT_NOT end without matching EXPECT_NOT begin:\n";
  292. print " end ---> $line\n";
  293. }
  294. if ($found) {
  295. if ($print_line_num) {
  296. $line_num = sprintf("%4s ", $.);
  297. }
  298. printf "** %s%s$script_name WARNING - next line matches EXPECT_NOT\n",
  299. $line_num, $timestamp;
  300. printf "** %s%s%s\n", $line_num, $timestamp, $line;
  301. } else {
  302. $expnot_missing++;
  303. }
  304. if (@expnot_begin_stack > 0) {
  305. $begin = pop @expnot_begin_stack;
  306. if (! compare($data, $begin) and ($data ne $begin)) {
  307. $expnot_missing_end++;
  308. print "** ERROR: EXPECT_NOT end does not match EXPECT_NOT begin:\n";
  309. print " begin -> $begin\n";
  310. print " end ---> $line\n";
  311. }
  312. }
  313. next LINE;
  314. }
  315. # ----- not an EXPECT line
  316. if (($line =~ /^${pr_fmt}start of unittest - you will see error messages$/) ||
  317. ($line =~ /^${pr_fmt}end of unittest - [0-9]+ passed, [0-9]+ failed$/ ) ) {
  318. $prefix = "->"; # 2 characters
  319. } elsif ($line =~ /^${pr_fmt}FAIL /) {
  320. $unittest_fail++;
  321. $prefix = ">>"; # 2 characters
  322. }
  323. $found = 0;
  324. foreach $begin (@exp_begin_stack) {
  325. if (compare($begin, $line)) {
  326. $found = 1;
  327. last;
  328. }
  329. }
  330. if ($found) {
  331. $begin = shift @exp_begin_stack;
  332. while (! compare($begin, $line)) {
  333. push @exp_found_or_begin, $begin;
  334. $begin = shift @exp_begin_stack;
  335. }
  336. push @exp_found_or_begin, $line;
  337. if ($hide_expect) {
  338. $suppress_line = 1;
  339. }
  340. $prefix = "ok"; # 2 characters
  341. }
  342. $found = 0;
  343. foreach $begin (@expnot_begin_stack) {
  344. if (compare($begin, $line)) {
  345. $found = 1;
  346. last;
  347. }
  348. }
  349. if ($found) {
  350. $begin = shift @begin;
  351. while (! compare($begin, $line)) {
  352. push @expnot_found_or_begin, $begin;
  353. $begin = shift @begin;
  354. }
  355. push @expnot_found_or_begin, $line;
  356. if ($hide_expect) {
  357. $suppress_line = 1;
  358. }
  359. $prefix = "**"; # 2 characters
  360. }
  361. if ($suppress_line) {
  362. next LINE;
  363. }
  364. if ($print_line_num) {
  365. $line_num = sprintf("%4s ", $.);
  366. }
  367. printf "%s %s%s%s\n", $prefix, $line_num, $timestamp, $line;
  368. }
  369. if (! $no_expect_stats) {
  370. print "\n";
  371. print "** EXPECT statistics:\n";
  372. print "**\n";
  373. printf "** non-zero values expected:\n";
  374. print "**\n";
  375. printf "** EXPECT found : %4i\n", $exp_found;
  376. printf "** EXPECT_NOT not found : %4i\n", $expnot_missing;
  377. print "**\n";
  378. printf "** zero values expected:\n";
  379. print "**\n";
  380. printf "** EXPECT not found : %4i\n", $exp_missing;
  381. printf "** missing EXPECT begin : %4i\n", $exp_missing_begin;
  382. printf "** missing EXPECT end : %4i\n", $exp_missing_end;
  383. print "**\n";
  384. printf "** EXPECT_NOT found : %4i\n", $expnot_found;
  385. printf "** missing EXPECT_NOT begin : %4i\n", $expnot_missing_begin;
  386. printf "** missing EXPECT_NOT end : %4i\n", $expnot_missing_end;
  387. print "**\n";
  388. printf "** unittest FAIL : %4i\n", $unittest_fail;
  389. printf "** internal error : %4i\n", $internal_err;
  390. }
  391. if (@exp_begin_stack) {
  392. print "** ERROR: EXPECT begin without matching EXPECT end:\n";
  393. print " This list may be misleading.\n";
  394. foreach $begin (@exp_begin_stack) {
  395. print " begin ---> $begin\n";
  396. }
  397. }
  398. if (@expnot_begin_stack) {
  399. print "** ERROR: EXPECT_NOT begin without matching EXPECT_NOT end:\n";
  400. print " This list may be misleading.\n";
  401. foreach $begin (@expnot_begin_stack) {
  402. print " begin ---> $begin\n";
  403. }
  404. }