1 /*
2  * Copyright (C) 2018 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 #ifndef _LIBUNWINDSTACK_LOCAL_UNWINDER_H
18 #define _LIBUNWINDSTACK_LOCAL_UNWINDER_H
19 
20 #include <pthread.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 #include <unwindstack/Error.h>
29 #include <unwindstack/Maps.h>
30 #include <unwindstack/Memory.h>
31 
32 namespace unwindstack {
33 
34 // Forward declarations.
35 class Elf;
36 struct MapInfo;
37 
38 struct LocalFrameData {
LocalFrameDataLocalFrameData39   LocalFrameData(MapInfo* map_info, uint64_t pc, uint64_t rel_pc, const std::string& function_name,
40                  uint64_t function_offset)
41       : map_info(map_info),
42         pc(pc),
43         rel_pc(rel_pc),
44         function_name(function_name),
45         function_offset(function_offset) {}
46 
47   MapInfo* map_info;
48   uint64_t pc;
49   uint64_t rel_pc;
50   std::string function_name;
51   uint64_t function_offset;
52 };
53 
54 // This is a specialized class that should only be used for doing local unwinds.
55 // The Unwind call can be made as multiple times on the same object, and it can
56 // be called by multiple threads at the same time.
57 // It is designed to be used in debugging circumstances to get a stack trace
58 // as fast as possible.
59 class LocalUnwinder {
60  public:
61   LocalUnwinder() = default;
LocalUnwinder(const std::vector<std::string> & skip_libraries)62   LocalUnwinder(const std::vector<std::string>& skip_libraries) : skip_libraries_(skip_libraries) {}
63   ~LocalUnwinder() = default;
64 
65   bool Init();
66 
67   bool Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames);
68 
69   bool ShouldSkipLibrary(const std::string& map_name);
70 
71   MapInfo* GetMapInfo(uint64_t pc);
72 
LastErrorCode()73   ErrorCode LastErrorCode() { return last_error_.code; }
LastErrorAddress()74   uint64_t LastErrorAddress() { return last_error_.address; }
75 
76  private:
77   pthread_rwlock_t maps_rwlock_;
78   std::unique_ptr<LocalUpdatableMaps> maps_ = nullptr;
79   std::shared_ptr<Memory> process_memory_;
80   std::vector<std::string> skip_libraries_;
81   ErrorData last_error_;
82 };
83 
84 }  // namespace unwindstack
85 
86 #endif  // _LIBUNWINDSTACK_LOCAL_UNWINDER_H
87