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> |
Christopher Ferris | 0dbce45 | 2014-01-17 08:25:26 -0800 | [diff] [blame] | 21 | #ifdef USE_MINGW |
| 22 | // MINGW does not define these constants. |
| 23 | #define PROT_NONE 0 |
| 24 | #define PROT_READ 0x1 |
| 25 | #define PROT_WRITE 0x2 |
| 26 | #define PROT_EXEC 0x4 |
| 27 | #else |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 28 | #include <sys/mman.h> |
Christopher Ferris | 0dbce45 | 2014-01-17 08:25:26 -0800 | [diff] [blame] | 29 | #endif |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 30 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 31 | #include <deque> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 32 | #include <string> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 33 | |
| 34 | struct backtrace_map_t { |
| 35 | uintptr_t start; |
| 36 | uintptr_t end; |
| 37 | int flags; |
| 38 | std::string name; |
| 39 | }; |
| 40 | |
| 41 | class BacktraceMap { |
| 42 | public: |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 43 | static BacktraceMap* Create(pid_t pid); |
| 44 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 45 | virtual ~BacktraceMap(); |
| 46 | |
| 47 | // Get the map data structure for the given address. |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 48 | virtual const backtrace_map_t* Find(uintptr_t addr); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 49 | |
| 50 | // The flags returned are the same flags as used by the mmap call. |
| 51 | // The values are PROT_*. |
| 52 | int GetFlags(uintptr_t pc) { |
| 53 | const backtrace_map_t* map = Find(pc); |
| 54 | if (map) { |
| 55 | return map->flags; |
| 56 | } |
| 57 | return PROT_NONE; |
| 58 | } |
| 59 | |
| 60 | bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; } |
| 61 | bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; } |
| 62 | bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; } |
| 63 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 64 | typedef std::deque<backtrace_map_t>::iterator iterator; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 65 | iterator begin() { return maps_.begin(); } |
| 66 | iterator end() { return maps_.end(); } |
| 67 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 68 | typedef std::deque<backtrace_map_t>::const_iterator const_iterator; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 69 | const_iterator begin() const { return maps_.begin(); } |
| 70 | const_iterator end() const { return maps_.end(); } |
| 71 | |
| 72 | virtual bool Build(); |
| 73 | |
| 74 | protected: |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 75 | BacktraceMap(pid_t pid); |
| 76 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 77 | virtual bool ParseLine(const char* line, backtrace_map_t* map); |
| 78 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 79 | std::deque<backtrace_map_t> maps_; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 80 | pid_t pid_; |
| 81 | }; |
| 82 | |
| 83 | #endif // _BACKTRACE_BACKTRACE_MAP_H |