xref: /aosp_15_r20/external/pigweed/pw_symbolizer/py/symbolizer_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <cinttypes>
16 #include <cstdint>
17 #include <cstdio>
18 
19 namespace pw::symbolizer::test {
20 
21 struct Fish {
22   bool has_legs;
23   bool tastes_like_chicken;
24 };
25 
26 Fish gerald;
27 
28 }  // namespace pw::symbolizer::test
29 
PrintExpectedObjSymbol(void * address,const char * expected)30 void PrintExpectedObjSymbol(void* address, const char* expected) {
31   uintptr_t val = reinterpret_cast<uintptr_t>(address);
32   printf("{\"Address\":%" PRIuPTR ",\"Expected\":\"%s\",\"IsObj\":true}\n",
33          val,
34          expected);
35 }
36 
37 namespace {
38 
GoWild()39 void GoWild() {
40   volatile int counter = 0;
41   for (int i = 0; i < 123; i++) {
42     counter += i * counter;
43   }
44 }
45 
46 }  // namespace
47 
48 namespace another::one {
49 
PrintExpectedFuncSymbol(void * address,const char * expected,int line)50 void PrintExpectedFuncSymbol(void* address, const char* expected, int line) {
51   uintptr_t val = reinterpret_cast<uintptr_t>(address);
52   printf("{\"Address\":%" PRIuPTR
53          ",\"Expected\":\"%s\",\"Line\": %d,\"IsObj\":false}\n",
54          val,
55          expected,
56          line);
57 }
58 
59 }  // namespace another::one
60 
61 extern "C" long pw_extern_long = 42;
62 
main()63 int main() {
64   PrintExpectedObjSymbol(&pw::symbolizer::test::gerald,
65                          "pw::symbolizer::test::gerald");
66   PrintExpectedObjSymbol(&pw::symbolizer::test::gerald.tastes_like_chicken,
67                          "pw::symbolizer::test::gerald");
68   another::one::PrintExpectedFuncSymbol(
69       reinterpret_cast<void*>(&another::one::PrintExpectedFuncSymbol),
70       "another::one::PrintExpectedFuncSymbol(void*, char const*, int)",
71       50);
72 
73   another::one::PrintExpectedFuncSymbol(
74       reinterpret_cast<void*>(&GoWild), "(anonymous namespace)::GoWild()", 39);
75 
76   PrintExpectedObjSymbol(&pw_extern_long, "pw_extern_long");
77 
78   another::one::PrintExpectedFuncSymbol(
79       reinterpret_cast<void*>(&main), "main", 63);
80   return 0;
81 }
82