From 82c3743eb06c7e329ffa737c1669ef41639e9d85 Mon Sep 17 00:00:00 2001 From: Matthias Baesken Date: Wed, 13 Mar 2024 10:31:10 +0000 Subject: [PATCH] 8326496: [test] checkHsErrFileContent support printing hserr in error case Backport-of: a065eba56de01f4492123c6663ec0c3108d907a1 --- .../runtime/ErrorHandling/HsErrFileUtils.java | 65 +++++++++++++++++-- .../ErrorHandling/SecondaryErrorTest.java | 6 +- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/test/hotspot/jtreg/runtime/ErrorHandling/HsErrFileUtils.java b/test/hotspot/jtreg/runtime/ErrorHandling/HsErrFileUtils.java index 306ab44d18d..d2ca4c9ff55 100644 --- a/test/hotspot/jtreg/runtime/ErrorHandling/HsErrFileUtils.java +++ b/test/hotspot/jtreg/runtime/ErrorHandling/HsErrFileUtils.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. - * Copyright (c) 2022 SAP SE. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024 SAP SE. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -68,7 +68,7 @@ public static File openHsErrFileFromOutput(OutputAnalyzer output) { * @throws RuntimeException, {@link IOException} */ public static void checkHsErrFileContent(File f, Pattern[] patterns, boolean verbose) throws IOException { - checkHsErrFileContent(f, patterns, null, true, verbose); + checkHsErrFileContent(f, patterns, null, true, verbose, false); } /** @@ -80,11 +80,43 @@ public static void checkHsErrFileContent(File f, Pattern[] patterns, boolean ver * Order is irrelevant. * @param checkEndMarker If true, we check for the final "END" in an hs-err file; if it is missing it indicates * that hs-err file printing did not complete successfully. - * @param verbose If true, the content of the hs-err file is printed while matching. If false, only important - * information are printed. + * @param verbose If true, the content of the hs-err file is printed while matching. If false, only the matched patterns + * are printed. * @throws RuntimeException, {@link IOException} */ public static void checkHsErrFileContent(File f, Pattern[] positivePatterns, Pattern[] negativePatterns, boolean checkEndMarker, boolean verbose) throws IOException { + checkHsErrFileContent(f, positivePatterns, negativePatterns, checkEndMarker, verbose, false); + } + + /** + * Given an open hs-err file, read it line by line and check for existence of a set of patterns. Will fail + * if patterns are missing, or if the END marker is missing. + * @param f Input file + * @param patterns An array of patterns that need to match, in that order + * @param verbose If true, the content of the hs-err file is printed while matching. If false, only the matched patterns + * are printed. + * @param printHserrOnError If true, the content of the hs-err file is printed in case of a failing check + * @throws RuntimeException, {@link IOException} + */ + public static void checkHsErrFileContent(File f, Pattern[] patterns, boolean verbose, boolean printHserrOnError) throws IOException { + checkHsErrFileContent(f, patterns, null, true, verbose, printHserrOnError); + } + + /** + * Given an open hs-err file, read it line by line and check for various conditions. + * @param f input file + * @param positivePatterns Optional array of patterns that need to appear, in given order, in the file. Missing + * patterns cause the test to fail. + * @param negativePatterns Optional array of patterns that must not appear in the file; test fails if they do. + * Order is irrelevant. + * @param checkEndMarker If true, we check for the final "END" in an hs-err file; if it is missing it indicates + * that hs-err file printing did not complete successfully. + * @param verbose If true, the content of the hs-err file is printed while matching. If false, only the matched patterns + * are printed. + * @param printHserrOnError If true, the content of the hs-err file is printed in case of a failing check + * @throws RuntimeException, {@link IOException} + */ + public static void checkHsErrFileContent(File f, Pattern[] positivePatterns, Pattern[] negativePatterns, boolean checkEndMarker, boolean verbose, boolean printHserrOnError) throws IOException { try ( FileInputStream fis = new FileInputStream(f); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); @@ -123,6 +155,9 @@ public static void checkHsErrFileContent(File f, Pattern[] positivePatterns, Pat System.out.println(line); } System.out.println("^^^ Forbidden pattern found at line " + lineNo + ": " + negativePattern + "^^^"); + if (printHserrOnError) { + printHsErrFile(f); + } throw new RuntimeException("Forbidden pattern found at line " + lineNo + ": " + negativePattern); } } @@ -132,13 +167,33 @@ public static void checkHsErrFileContent(File f, Pattern[] positivePatterns, Pat } // If the current pattern is not null then it didn't match if (currentPositivePattern != null) { + if (printHserrOnError) { + printHsErrFile(f); + } throw new RuntimeException("hs-err file incomplete (first missing pattern: " + currentPositivePattern.pattern() + ")"); } if (checkEndMarker && !lastLine.equals("END.")) { + if (printHserrOnError) { + printHsErrFile(f); + } throw new RuntimeException("hs-err file incomplete (missing END marker.)"); } System.out.println("hs-err file " + f.getAbsolutePath() + " scanned successfully."); } } + private static void printHsErrFile(File f) throws IOException { + try ( + FileInputStream fis = new FileInputStream(f); + BufferedReader br = new BufferedReader(new InputStreamReader(fis)); + ) { + String line; + System.out.println("------------------------ hs-err file ------------------------"); + while ((line = br.readLine()) != null) { + System.out.println(line); + } + System.out.println("-------------------------------------------------------------"); + } + } + } diff --git a/test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java b/test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java index 012d5a5c16d..5b28e2a1d8b 100644 --- a/test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java +++ b/test/hotspot/jtreg/runtime/ErrorHandling/SecondaryErrorTest.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2014, 2022 SAP SE. All rights reserved. - * Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2024 SAP SE. All rights reserved. + * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -122,7 +122,7 @@ public static void main(String[] args) throws Exception { } Pattern[] pattern = patternlist.toArray(new Pattern[] {}); - HsErrFileUtils.checkHsErrFileContent(hs_err_file, pattern, false); + HsErrFileUtils.checkHsErrFileContent(hs_err_file, pattern, false, true); System.out.println("OK.");