Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 _BACKTRACE_BACKTRACE_MAP_H |
| 18 | #define _BACKTRACE_BACKTRACE_MAP_H |
| 19 | |
| 20 | #include <stdint.h> |
Dan Albert | ac2fe7e | 2014-05-21 18:21:02 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Christopher Ferris | 0dbce45 | 2014-01-17 08:25:26 -0800 | [diff] [blame] | 22 | #ifdef USE_MINGW |
| 23 | // MINGW does not define these constants. |
| 24 | #define PROT_NONE 0 |
| 25 | #define PROT_READ 0x1 |
| 26 | #define PROT_WRITE 0x2 |
| 27 | #define PROT_EXEC 0x4 |
| 28 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 29 | #include <sys/mman.h> |
Christopher Ferris | 0dbce45 | 2014-01-17 08:25:26 -0800 | [diff] [blame] | 30 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 31 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 32 | #include <deque> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 33 | #include <string> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 34 | |
| 35 | struct backtrace_map_t { |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame^] | 36 | backtrace_map_t(): start(0), end(0), flags(0) {} |
| 37 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 38 | uintptr_t start; |
| 39 | uintptr_t end; |
| 40 | int flags; |
| 41 | std::string name; |
| 42 | }; |
| 43 | |
| 44 | class BacktraceMap { |
| 45 | public: |
Christopher Ferris | dda47b7 | 2014-08-04 17:08:46 -0700 | [diff] [blame] | 46 | // If uncached is true, then parse the current process map as of the call. |
| 47 | // Passing a map created with uncached set to true to Backtrace::Create() |
| 48 | // is unsupported. |
| 49 | static BacktraceMap* Create(pid_t pid, bool uncached = false); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 50 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 51 | virtual ~BacktraceMap(); |
| 52 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame^] | 53 | // Fill in the map data structure for the given address. |
| 54 | virtual void FillIn(uintptr_t addr, backtrace_map_t* map); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 55 | |
| 56 | // The flags returned are the same flags as used by the mmap call. |
| 57 | // The values are PROT_*. |
| 58 | int GetFlags(uintptr_t pc) { |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame^] | 59 | backtrace_map_t map; |
| 60 | FillIn(pc, &map); |
| 61 | if (IsValid(map)) { |
| 62 | return map.flags; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 63 | } |
| 64 | return PROT_NONE; |
| 65 | } |
| 66 | |
| 67 | bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; } |
| 68 | bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; } |
| 69 | bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; } |
| 70 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 71 | typedef std::deque<backtrace_map_t>::iterator iterator; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 72 | iterator begin() { return maps_.begin(); } |
| 73 | iterator end() { return maps_.end(); } |
| 74 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 75 | typedef std::deque<backtrace_map_t>::const_iterator const_iterator; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 76 | const_iterator begin() const { return maps_.begin(); } |
| 77 | const_iterator end() const { return maps_.end(); } |
| 78 | |
| 79 | virtual bool Build(); |
| 80 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame^] | 81 | static inline bool IsValid(const backtrace_map_t& map) { |
| 82 | return map.end > 0; |
| 83 | } |
| 84 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 85 | protected: |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 86 | BacktraceMap(pid_t pid); |
| 87 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 88 | virtual bool ParseLine(const char* line, backtrace_map_t* map); |
| 89 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 90 | std::deque<backtrace_map_t> maps_; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 91 | pid_t pid_; |
| 92 | }; |
| 93 | |
| 94 | #endif // _BACKTRACE_BACKTRACE_MAP_H |