diff options
author | Christopher Ferris <cferris@google.com> | 2014-01-17 15:07:18 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-01-17 15:07:18 +0000 |
commit | eb8d73b675d8d9613310b5d5e5865571b98d6f78 (patch) | |
tree | 332b1ded8e68dcc6d15c64d1303c5e3c0f9a5836 /include/backtrace | |
parent | 524b3a0c40fcca4b2259f317b2d098434998ce47 (diff) | |
parent | 46756821c4fe238f12a6e5ea18c356398f8d8795 (diff) | |
download | system_core-eb8d73b675d8d9613310b5d5e5865571b98d6f78.zip system_core-eb8d73b675d8d9613310b5d5e5865571b98d6f78.tar.gz system_core-eb8d73b675d8d9613310b5d5e5865571b98d6f78.tar.bz2 |
Merge "Rewrite libbacktrace to be all C++."
Diffstat (limited to 'include/backtrace')
-rw-r--r-- | include/backtrace/Backtrace.h | 72 | ||||
-rw-r--r-- | include/backtrace/BacktraceMap.h | 72 | ||||
-rw-r--r-- | include/backtrace/backtrace.h | 131 | ||||
-rw-r--r-- | include/backtrace/backtrace_constants.h | 30 |
4 files changed, 149 insertions, 156 deletions
diff --git a/include/backtrace/Backtrace.h b/include/backtrace/Backtrace.h index df40b87..f0fb0cd 100644 --- a/include/backtrace/Backtrace.h +++ b/include/backtrace/Backtrace.h @@ -17,10 +17,25 @@ #ifndef _BACKTRACE_BACKTRACE_H #define _BACKTRACE_BACKTRACE_H -#include <backtrace/backtrace.h> +#include <stdint.h> #include <string> +#include <vector> + +#include <backtrace/backtrace_constants.h> +#include <backtrace/BacktraceMap.h> + +struct backtrace_frame_data_t { + size_t num; // The current fame number. + uintptr_t pc; // The absolute pc. + uintptr_t sp; // The top of the stack. + size_t stack_size; // The size of the stack, zero indicate an unknown stack size. + const backtrace_map_t* map; // The map associated with the given pc. + std::string func_name; // The function name associated with this pc, NULL if not found. + uintptr_t func_offset; // pc relative to the start of the function, only valid if func_name is not NULL. +}; +// Forward declarations. class BacktraceImpl; class Backtrace { @@ -33,9 +48,9 @@ public: // If pid >= 0 and tid < 0, then the Backtrace object corresponds to a // different process. // Tracing a thread in a different process is not supported. - // If map_info is NULL, then create the map and manage it internally. - // If map_info is not NULL, the map is still owned by the caller. - static Backtrace* Create(pid_t pid, pid_t tid, backtrace_map_info_t* map_info = NULL); + // If map is NULL, then create the map and manage it internally. + // If map is not NULL, the map is still owned by the caller. + static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = NULL); virtual ~Backtrace(); @@ -46,13 +61,12 @@ public: // If the string is empty, then no valid function name was found. virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset); - // Get the name of the map associated with the given pc. If NULL is returned, - // then map_start is not set. Otherwise, map_start is the beginning of this - // map. - virtual const char* GetMapName(uintptr_t pc, uintptr_t* map_start); + // Find the map associated with the given pc. + virtual const backtrace_map_t* FindMap(uintptr_t pc); - // Finds the memory map associated with the given ptr. - virtual const backtrace_map_info_t* FindMapInfo(uintptr_t ptr); + // Take ownership of the BacktraceMap object associated with the backtrace. + // If this is called, the caller must handle deleting the object themselves. + virtual BacktraceMap* TakeMapOwnership(); // Read the data at a specific address. virtual bool ReadWord(uintptr_t ptr, uint32_t* out_value) = 0; @@ -62,35 +76,43 @@ public: virtual std::string FormatFrameData(size_t frame_num); virtual std::string FormatFrameData(const backtrace_frame_data_t* frame); - pid_t Pid() { return backtrace_.pid; } - pid_t Tid() { return backtrace_.tid; } - size_t NumFrames() { return backtrace_.num_frames; } - - const backtrace_t* GetBacktrace() { return &backtrace_; } + pid_t Pid() { return pid_; } + pid_t Tid() { return tid_; } + size_t NumFrames() { return frames_.size(); } const backtrace_frame_data_t* GetFrame(size_t frame_num) { - if (frame_num > NumFrames()) { + if (frame_num >= frames_.size()) { return NULL; } - return &backtrace_.frames[frame_num]; + return &frames_[frame_num]; } - const backtrace_map_info_t* GetMapList() { - return map_info_; - } + typedef std::vector<backtrace_frame_data_t>::iterator iterator; + iterator begin() { return frames_.begin(); } + iterator end() { return frames_.end(); } + + typedef std::vector<backtrace_frame_data_t>::const_iterator const_iterator; + const_iterator begin() const { return frames_.begin(); } + const_iterator end() const { return frames_.end(); } + + BacktraceMap* GetMap() { return map_; } protected: - Backtrace(BacktraceImpl* impl, pid_t pid, backtrace_map_info_t* map_info); + Backtrace(BacktraceImpl* impl, pid_t pid, BacktraceMap* map); virtual bool VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value); - BacktraceImpl* impl_; + bool BuildMap(); - backtrace_map_info_t* map_info_; + pid_t pid_; + pid_t tid_; - bool map_info_requires_delete_; + BacktraceMap* map_; + bool map_shared_; - backtrace_t backtrace_; + std::vector<backtrace_frame_data_t> frames_; + + BacktraceImpl* impl_; friend class BacktraceImpl; }; diff --git a/include/backtrace/BacktraceMap.h b/include/backtrace/BacktraceMap.h new file mode 100644 index 0000000..e5389c1 --- /dev/null +++ b/include/backtrace/BacktraceMap.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BACKTRACE_BACKTRACE_MAP_H +#define _BACKTRACE_BACKTRACE_MAP_H + +#include <stdint.h> +#include <sys/mman.h> + +#include <string> +#include <vector> + +struct backtrace_map_t { + uintptr_t start; + uintptr_t end; + int flags; + std::string name; +}; + +class BacktraceMap { +public: + BacktraceMap(pid_t pid); + virtual ~BacktraceMap(); + + // Get the map data structure for the given address. + const backtrace_map_t* Find(uintptr_t addr); + + // The flags returned are the same flags as used by the mmap call. + // The values are PROT_*. + int GetFlags(uintptr_t pc) { + const backtrace_map_t* map = Find(pc); + if (map) { + return map->flags; + } + return PROT_NONE; + } + + bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; } + bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; } + bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; } + + typedef std::vector<backtrace_map_t>::iterator iterator; + iterator begin() { return maps_.begin(); } + iterator end() { return maps_.end(); } + + typedef std::vector<backtrace_map_t>::const_iterator const_iterator; + const_iterator begin() const { return maps_.begin(); } + const_iterator end() const { return maps_.end(); } + + virtual bool Build(); + +protected: + virtual bool ParseLine(const char* line, backtrace_map_t* map); + + std::vector<backtrace_map_t> maps_; + pid_t pid_; +}; + +#endif // _BACKTRACE_BACKTRACE_MAP_H diff --git a/include/backtrace/backtrace.h b/include/backtrace/backtrace.h deleted file mode 100644 index cfcbf0f..0000000 --- a/include/backtrace/backtrace.h +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _BACKTRACE_H -#define _BACKTRACE_H - -#include <stdint.h> -#include <stdbool.h> -#include <sys/types.h> - -__BEGIN_DECLS - -// When the pid to be traced is set to this value, then trace the current -// process. If the tid value is not BACKTRACE_NO_TID, then the specified -// thread from the current process will be traced. -#define BACKTRACE_CURRENT_PROCESS -1 -// When the tid to be traced is set to this value, then trace the specified -// current thread of the specified pid. -#define BACKTRACE_CURRENT_THREAD -1 - -#define MAX_BACKTRACE_FRAMES 64 - -typedef struct backtrace_map_info { - struct backtrace_map_info* next; - uintptr_t start; - uintptr_t end; - bool is_readable; - bool is_writable; - bool is_executable; - char name[]; -} backtrace_map_info_t; - -typedef struct { - size_t num; /* The current fame number. */ - uintptr_t pc; /* The absolute pc. */ - uintptr_t sp; /* The top of the stack. */ - size_t stack_size; /* The size of the stack, zero indicate an unknown stack size. */ - const char* map_name; /* The name of the map to which this pc belongs, NULL indicates the pc doesn't belong to a known map. */ - uintptr_t map_offset; /* pc relative to the start of the map, only valid if map_name is not NULL. */ - char* func_name; /* The function name associated with this pc, NULL if not found. */ - uintptr_t func_offset; /* pc relative to the start of the function, only valid if func_name is not NULL. */ -} backtrace_frame_data_t; - -typedef struct { - backtrace_frame_data_t frames[MAX_BACKTRACE_FRAMES]; - size_t num_frames; - - pid_t pid; - pid_t tid; - backtrace_map_info_t* map_info_list; -} backtrace_t; - -typedef struct { - void* data; - const backtrace_t* backtrace; -} backtrace_context_t; - -/* Create a context for the backtrace data and gather the backtrace. - * If pid < 0, then gather the backtrace for the current process. - */ -bool backtrace_create_context( - backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames); - -/* The same as backtrace_create_context, except that it is assumed that - * the pid map has already been acquired and the caller will handle freeing - * the map data. - */ -bool backtrace_create_context_with_map( - backtrace_context_t* context, pid_t pid, pid_t tid, size_t num_ignore_frames, - backtrace_map_info_t* map_info); - -/* Gather the backtrace data for a pthread instead of a process. */ -bool backtrace_create_thread_context( - backtrace_context_t* context, pid_t tid, size_t num_ignore_frames); - -/* Free any memory allocated during the context create. */ -void backtrace_destroy_context(backtrace_context_t* context); - -/* Read data at a specific address for a process. */ -bool backtrace_read_word( - const backtrace_context_t* context, uintptr_t ptr, uint32_t* value); - -/* Get information about the map name associated with a pc. If NULL is - * returned, then map_start is not set. - */ -const char* backtrace_get_map_name( - const backtrace_context_t* context, uintptr_t pc, uintptr_t* map_start); - -/* Get the function name and offset given the pc. If NULL is returned, - * then func_offset is not set. The returned string is allocated using - * malloc and must be freed by the caller. - */ -char* backtrace_get_func_name( - const backtrace_context_t* context, uintptr_t pc, uintptr_t* func_offset); - -/* Loads memory map from /proc/<pid>/maps. If pid < 0, then load the memory - * map for the current process. - */ -backtrace_map_info_t* backtrace_create_map_info_list(pid_t pid); - -/* Frees memory associated with the map list. */ -void backtrace_destroy_map_info_list(backtrace_map_info_t* map_info_list); - -/* Finds the memory map that contains the specified pc. */ -const backtrace_map_info_t* backtrace_find_map_info( - const backtrace_map_info_t* map_info_list, uintptr_t pc); - -/* Create a formatted line of backtrace information for a single frame. */ -void backtrace_format_frame_data( - const backtrace_context_t* context, size_t frame_num, char* buf, - size_t buf_size); - -/* Get the backtrace data structure associated with the context. */ -const backtrace_t* backtrace_get_data(backtrace_context_t* context); - -__END_DECLS - -#endif /* _BACKTRACE_H */ diff --git a/include/backtrace/backtrace_constants.h b/include/backtrace/backtrace_constants.h new file mode 100644 index 0000000..f8c1575 --- /dev/null +++ b/include/backtrace/backtrace_constants.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BACKTRACE_BACKTRACE_CONSTANTS_H +#define _BACKTRACE_BACKTRACE_CONSTANTS_H + +// When the pid to be traced is set to this value, then trace the current +// process. If the tid value is not BACKTRACE_NO_TID, then the specified +// thread from the current process will be traced. +#define BACKTRACE_CURRENT_PROCESS -1 +// When the tid to be traced is set to this value, then trace the specified +// current thread of the specified pid. +#define BACKTRACE_CURRENT_THREAD -1 + +#define MAX_BACKTRACE_FRAMES 64 + +#endif // _BACKTRACE_BACKTRACE_CONSTANTS_H |