xref: /aosp_15_r20/system/extras/simpleperf/tracing_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
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 
23 using namespace simpleperf;
24 
CheckAdjustFilter(const std::string & filter,bool use_quote,const std::string & adjusted_filter,const std::string used_field_str)25 static void CheckAdjustFilter(const std::string& filter, bool use_quote,
26                               const std::string& adjusted_filter,
27                               const std::string used_field_str) {
28   FieldNameSet used_fields;
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 // @CddTest = 6.1/C-0-2
TEST(tracing,adjust_tracepoint_filter)36 TEST(tracing, adjust_tracepoint_filter) {
37   std::string filter = "((sig >= 1 && sig < 20) || sig == 32) && comm != \"bash\"";
38   CheckAdjustFilter(filter, true, filter, "comm,sig");
39   CheckAdjustFilter(filter, false, "((sig >= 1 && sig < 20) || sig == 32) && comm != bash",
40                     "comm,sig");
41 
42   filter = "pid != 3 && !(comm ~ *bash)";
43   CheckAdjustFilter(filter, true, "pid != 3 && !(comm ~ \"*bash\")", "comm,pid");
44   CheckAdjustFilter(filter, false, filter, "comm,pid");
45 
46   filter = "mask & 3";
47   CheckAdjustFilter(filter, true, filter, "mask");
48   CheckAdjustFilter(filter, false, filter, "mask");
49 
50   filter = "addr > 0 && addr != 0xFFFFFFFFFFFFFFFF || value > -5";
51   CheckAdjustFilter(filter, true, filter, "addr,value");
52   CheckAdjustFilter(filter, false, filter, "addr,value");
53 
54   // unmatched paren
55   FieldNameSet used_fields;
56   ASSERT_FALSE(AdjustTracepointFilter("(pid > 3", true, &used_fields).has_value());
57   ASSERT_FALSE(AdjustTracepointFilter("pid > 3)", true, &used_fields).has_value());
58   // unknown operator
59   ASSERT_FALSE(AdjustTracepointFilter("pid ^ 3", true, &used_fields).has_value());
60 }
61 
62 namespace simpleperf {
operator <<(std::ostream & os,const TracingField & field)63 std::ostream& operator<<(std::ostream& os, const TracingField& field) {
64   os << "field (" << field.name << ", off " << field.offset << ", elem size " << field.elem_size
65      << ", elem_count " << field.elem_count << ", is_signed " << field.is_signed << ", is_dynamic "
66      << field.is_dynamic << ")";
67   return os;
68 }
69 }  // namespace simpleperf
70 
71 // @CddTest = 6.1/C-0-2
TEST(tracing,ParseTracingFormat)72 TEST(tracing, ParseTracingFormat) {
73   std::string data =
74       "name: sched_wakeup_new\n"
75       "ID: 94\n"
76       "format:\n"
77       "\tfield:unsigned short common_type;	offset:0;	size:2;	signed:0;\n"
78       "\tfield:unsigned char common_flags;	offset:2;	size:1;	signed:0;\n"
79       "\tfield:unsigned char common_preempt_count;	offset:3;	size:1;	signed:0;\n"
80       "\tfield:int common_pid;	offset:4;	size:4;	signed:1;\n"
81       "\n"
82       "\tfield:char comm[16];	offset:8;	size:16;	signed:1;\n"
83       "\tfield:__data_loc char[] name;	offset:24;	size:4;	signed:1;\n";
84   std::optional<TracingFormat> opt_format = ParseTracingFormat(data);
85   ASSERT_TRUE(opt_format.has_value());
86   const TracingFormat& format = opt_format.value();
87   ASSERT_EQ(format.name, "sched_wakeup_new");
88   ASSERT_EQ(format.id, 94);
89   ASSERT_EQ(format.fields.size(), 6);
90   ASSERT_EQ(format.fields[0], TracingField({.name = "common_type", .offset = 0, .elem_size = 2}));
91   ASSERT_EQ(format.fields[1], TracingField({.name = "common_flags", .offset = 2, .elem_size = 1}));
92   ASSERT_EQ(format.fields[2],
93             TracingField({.name = "common_preempt_count", .offset = 3, .elem_size = 1}));
94   ASSERT_EQ(format.fields[3],
95             TracingField({.name = "common_pid", .offset = 4, .elem_size = 4, .is_signed = true}));
96   ASSERT_EQ(
97       format.fields[4],
98       TracingField(
99           {.name = "comm", .offset = 8, .elem_size = 1, .elem_count = 16, .is_signed = true}));
100   ASSERT_EQ(format.fields[5], TracingField({.name = "name",
101                                             .offset = 24,
102                                             .elem_size = 4,
103                                             .elem_count = 1,
104                                             .is_signed = true,
105                                             .is_dynamic = true}));
106 }
107