8221569: JFR tool produces incorrect output when both --categories and --events are specified
authorcito
Fri, 10 May 2019 20:56:37 +0200
changeset 54815 1e83e1a600cb
parent 54814 207b7bf04f49
child 54816 b27f33bef884
8221569: JFR tool produces incorrect output when both --categories and --events are specified Reviewed-by: mgronlun Contributed-by: chihiro.ito@oracle.com, erik.gahlin@oracle.com
src/jdk.jfr/share/classes/jdk/jfr/internal/tool/Print.java
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/tool/Print.java	Fri May 10 11:18:45 2019 -0400
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/tool/Print.java	Fri May 10 20:56:37 2019 +0200
@@ -107,13 +107,23 @@
         int stackDepth = 5;
         EventPrintWriter eventWriter = null;
         int optionCount = options.size();
+        boolean foundEventFilter = false;
+        boolean foundCategoryFilter = false;
         while (optionCount > 0) {
             if (acceptFilterOption(options, "--events")) {
+                if (foundEventFilter) {
+                    throw new UserSyntaxException("use --events event1,event2,event3 to include multiple events");
+                }
+                foundEventFilter = true;
                 String filter = options.remove();
                 warnForWildcardExpansion("--events", filter);
                 eventFilter = addEventFilter(filter, eventFilter);
             }
             if (acceptFilterOption(options, "--categories")) {
+                if (foundCategoryFilter) {
+                    throw new UserSyntaxException("use --categories category1,category2 to include multiple categories");
+                }
+                foundCategoryFilter = true;
                 String filter = options.remove();
                 warnForWildcardExpansion("--categories", filter);
                 eventFilter = addCategoryFilter(filter, eventFilter);
@@ -137,6 +147,8 @@
             }
             if (optionCount == options.size()) {
                 // No progress made
+                checkCommonError(options, "--event", "--events");
+                checkCommonError(options, "--category", "--categories");
                 throw new UserSyntaxException("unknown option " + options.peek());
             }
             optionCount = options.size();
@@ -157,6 +169,12 @@
         pw.flush();
     }
 
+    private void checkCommonError(Deque<String> options, String typo, String correct) throws UserSyntaxException {
+       if (typo.equals(options.peek())) {
+           throw new UserSyntaxException("unknown option " + typo + ", did you mean " + correct + "?");
+       }
+    }
+
     private static boolean acceptFormatterOption(Deque<String> options, EventPrintWriter eventWriter, String expected) throws UserSyntaxException {
         if (expected.equals(options.peek())) {
             if (eventWriter != null) {
@@ -179,7 +197,7 @@
 
     private static Predicate<EventType> addCategoryFilter(String filterText, Predicate<EventType> eventFilter) throws UserSyntaxException {
         List<String> filters = explodeFilter(filterText);
-        return recurseIfPossible(eventType -> {
+        Predicate<EventType> newFilter = recurseIfPossible(eventType -> {
             for (String category : eventType.getCategoryNames()) {
                 for (String filter : filters) {
                     if (match(category, filter)) {
@@ -192,6 +210,7 @@
             }
             return false;
         });
+        return eventFilter == null ? newFilter : eventFilter.or(newFilter);
     }
 
     private static String acronomify(String multipleWords) {
@@ -210,7 +229,7 @@
 
     private static Predicate<EventType> addEventFilter(String filterText, final Predicate<EventType> eventFilter) throws UserSyntaxException {
         List<String> filters = explodeFilter(filterText);
-        return recurseIfPossible(eventType -> {
+        Predicate<EventType> newFilter = recurseIfPossible(eventType -> {
             for (String filter : filters) {
                 String fullEventName = eventType.getName();
                 if (match(fullEventName, filter)) {
@@ -223,6 +242,7 @@
             }
             return false;
         });
+        return eventFilter == null ? newFilter : eventFilter.or(newFilter);
     }
 
     private static boolean match(String text, String filter) {