Yabin Cui | a9cfcde | 2020-06-29 14:04:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "tracing.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <android-base/strings.h> |
| 22 | |
Yabin Cui | 15dd5f7 | 2020-08-24 14:21:55 -0700 | [diff] [blame] | 23 | using namespace simpleperf; |
| 24 | |
Yabin Cui | 84c55ff | 2020-07-09 14:14:22 -0700 | [diff] [blame] | 25 | static void CheckAdjustFilter(const std::string& filter, bool use_quote, |
| 26 | const std::string& adjusted_filter, |
| 27 | const std::string used_field_str) { |
Yabin Cui | a9cfcde | 2020-06-29 14:04:44 -0700 | [diff] [blame] | 28 | FieldNameSet used_fields; |
Yabin Cui | 84c55ff | 2020-07-09 14:14:22 -0700 | [diff] [blame] | 29 | auto value = AdjustTracepointFilter(filter, use_quote, &used_fields); |
| 30 | ASSERT_TRUE(value.has_value()); |
| 31 | ASSERT_EQ(value.value(), adjusted_filter); |
| 32 | ASSERT_EQ(android::base::Join(used_fields, ","), used_field_str); |
| 33 | } |
| 34 | |
| 35 | TEST(tracing, adjust_tracepoint_filter) { |
| 36 | std::string filter = "((sig >= 1 && sig < 20) || sig == 32) && comm != \"bash\""; |
| 37 | CheckAdjustFilter(filter, true, filter, "comm,sig"); |
| 38 | CheckAdjustFilter(filter, false, "((sig >= 1 && sig < 20) || sig == 32) && comm != bash", |
| 39 | "comm,sig"); |
| 40 | |
| 41 | filter = "pid != 3 && !(comm ~ *bash)"; |
| 42 | CheckAdjustFilter(filter, true, "pid != 3 && !(comm ~ \"*bash\")", "comm,pid"); |
| 43 | CheckAdjustFilter(filter, false, filter, "comm,pid"); |
| 44 | |
| 45 | filter = "mask & 3"; |
| 46 | CheckAdjustFilter(filter, true, filter, "mask"); |
| 47 | CheckAdjustFilter(filter, false, filter, "mask"); |
| 48 | |
| 49 | filter = "addr > 0 && addr != 0xFFFFFFFFFFFFFFFF || value > -5"; |
| 50 | CheckAdjustFilter(filter, true, filter, "addr,value"); |
| 51 | CheckAdjustFilter(filter, false, filter, "addr,value"); |
Yabin Cui | a9cfcde | 2020-06-29 14:04:44 -0700 | [diff] [blame] | 52 | |
| 53 | // unmatched paren |
Yabin Cui | 84c55ff | 2020-07-09 14:14:22 -0700 | [diff] [blame] | 54 | FieldNameSet used_fields; |
| 55 | ASSERT_FALSE(AdjustTracepointFilter("(pid > 3", true, &used_fields).has_value()); |
| 56 | ASSERT_FALSE(AdjustTracepointFilter("pid > 3)", true, &used_fields).has_value()); |
Yabin Cui | a9cfcde | 2020-06-29 14:04:44 -0700 | [diff] [blame] | 57 | // unknown operator |
Yabin Cui | 84c55ff | 2020-07-09 14:14:22 -0700 | [diff] [blame] | 58 | ASSERT_FALSE(AdjustTracepointFilter("pid ^ 3", true, &used_fields).has_value()); |
Yabin Cui | a9cfcde | 2020-06-29 14:04:44 -0700 | [diff] [blame] | 59 | } |
Yabin Cui | 15dd5f7 | 2020-08-24 14:21:55 -0700 | [diff] [blame] | 60 | |
| 61 | namespace simpleperf { |
ThiƩbaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 62 | std::ostream& operator<<(std::ostream& os, const TracingField& field) { |
Yabin Cui | 15dd5f7 | 2020-08-24 14:21:55 -0700 | [diff] [blame] | 63 | os << "field (" << field.name << ", off " << field.offset << ", elem size " << field.elem_size |
| 64 | << ", elem_count " << field.elem_count << ", is_signed " << field.is_signed << ", is_dynamic " |
| 65 | << field.is_dynamic << ")"; |
| 66 | return os; |
| 67 | } |
| 68 | } // namespace simpleperf |
| 69 | |
| 70 | TEST(tracing, ParseTracingFormat) { |
| 71 | std::string data = |
| 72 | "name: sched_wakeup_new\n" |
| 73 | "ID: 94\n" |
| 74 | "format:\n" |
| 75 | "\tfield:unsigned short common_type; offset:0; size:2; signed:0;\n" |
| 76 | "\tfield:unsigned char common_flags; offset:2; size:1; signed:0;\n" |
| 77 | "\tfield:unsigned char common_preempt_count; offset:3; size:1; signed:0;\n" |
| 78 | "\tfield:int common_pid; offset:4; size:4; signed:1;\n" |
| 79 | "\n" |
| 80 | "\tfield:char comm[16]; offset:8; size:16; signed:1;\n" |
| 81 | "\tfield:__data_loc char[] name; offset:24; size:4; signed:1;\n"; |
| 82 | TracingFormat format = ParseTracingFormat(data); |
| 83 | ASSERT_EQ(format.name, "sched_wakeup_new"); |
| 84 | ASSERT_EQ(format.id, 94); |
| 85 | ASSERT_EQ(format.fields.size(), 6); |
| 86 | ASSERT_EQ(format.fields[0], TracingField({.name = "common_type", .offset = 0, .elem_size = 2})); |
| 87 | ASSERT_EQ(format.fields[1], TracingField({.name = "common_flags", .offset = 2, .elem_size = 1})); |
| 88 | ASSERT_EQ(format.fields[2], |
| 89 | TracingField({.name = "common_preempt_count", .offset = 3, .elem_size = 1})); |
| 90 | ASSERT_EQ(format.fields[3], |
| 91 | TracingField({.name = "common_pid", .offset = 4, .elem_size = 4, .is_signed = true})); |
| 92 | ASSERT_EQ( |
| 93 | format.fields[4], |
| 94 | TracingField( |
| 95 | {.name = "comm", .offset = 8, .elem_size = 1, .elem_count = 16, .is_signed = true})); |
| 96 | ASSERT_EQ(format.fields[5], TracingField({.name = "name", |
| 97 | .offset = 24, |
| 98 | .elem_size = 4, |
| 99 | .elem_count = 1, |
| 100 | .is_signed = true, |
| 101 | .is_dynamic = true})); |
| 102 | } |