From: Dimitry Ivanov Date: Thu, 18 Feb 2016 00:08:03 +0000 (-0800) Subject: Move gdb support functions to a separate file X-Git-Tag: android-x86-7.1-r1~92^2~2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=ed70f6a2a03c3d172d3770039e65dc262d5b4075;p=android-x86%2Fbionic.git Move gdb support functions to a separate file Move gdb support functions and variables to linker_gdb_support.h/cpp Bug: http://b/27533895 Change-Id: I96c6592a7055715b18f1137367470fe80987263f (cherry picked from commit 6b788eeff2ea0019849517e796b762ae790ca142) --- diff --git a/linker/Android.mk b/linker/Android.mk index 162088804..4a4ca5c5a 100644 --- a/linker/Android.mk +++ b/linker/Android.mk @@ -11,6 +11,7 @@ LOCAL_SRC_FILES := \ linker_allocator.cpp \ linker_block_allocator.cpp \ linker_dlwarning.cpp \ + linker_gdb_support.cpp \ linker_libc_support.c \ linker_mapped_file_fragment.cpp \ linker_memory.cpp \ diff --git a/linker/debugger.cpp b/linker/debugger.cpp index 670d1eacc..d4c7928f5 100644 --- a/linker/debugger.cpp +++ b/linker/debugger.cpp @@ -27,6 +27,7 @@ */ #include "linker.h" +#include "linker_gdb_support.h" #include #include diff --git a/linker/linker.cpp b/linker/linker.cpp index 4b8e1b135..6d3cc632f 100644 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -51,6 +51,7 @@ #include "linker.h" #include "linker_block_allocator.h" +#include "linker_gdb_support.h" #include "linker_debug.h" #include "linker_dlwarning.h" #include "linker_sleb128.h" @@ -272,75 +273,20 @@ size_t linker_get_error_buffer_size() { return sizeof(__linker_dl_err_buf); } -// This function is an empty stub where GDB locates a breakpoint to get notified -// about linker activity. -extern "C" -void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(); - -static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER; -static r_debug _r_debug = - {1, nullptr, reinterpret_cast(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0}; - -static link_map* r_debug_tail = nullptr; - -static void insert_link_map_into_debug_map(link_map* map) { - // Stick the new library at the end of the list. - // gdb tends to care more about libc than it does - // about leaf libraries, and ordering it this way - // reduces the back-and-forth over the wire. - if (r_debug_tail != nullptr) { - r_debug_tail->l_next = map; - map->l_prev = r_debug_tail; - map->l_next = nullptr; - } else { - _r_debug.r_map = map; - map->l_prev = nullptr; - map->l_next = nullptr; - } - r_debug_tail = map; -} - -static void insert_soinfo_into_debug_map(soinfo* info) { - // Copy the necessary fields into the debug structure. - link_map* map = &(info->link_map_head); - map->l_addr = info->load_bias; - // link_map l_name field is not const. - map->l_name = const_cast(info->get_realpath()); - map->l_ld = info->dynamic; - - insert_link_map_into_debug_map(map); -} - -static void remove_soinfo_from_debug_map(soinfo* info) { - link_map* map = &(info->link_map_head); - - if (r_debug_tail == map) { - r_debug_tail = map->l_prev; - } - - if (map->l_prev) { - map->l_prev->l_next = map->l_next; - } - if (map->l_next) { - map->l_next->l_prev = map->l_prev; - } -} - static void notify_gdb_of_load(soinfo* info) { if (info->is_main_executable()) { // GDB already knows about the main executable return; } - ScopedPthreadMutexLocker locker(&g__r_debug_mutex); - - _r_debug.r_state = r_debug::RT_ADD; - rtld_db_dlactivity(); + link_map* map = &(info->link_map_head); - insert_soinfo_into_debug_map(info); + map->l_addr = info->load_bias; + // link_map l_name field is not const. + map->l_name = const_cast(info->get_realpath()); + map->l_ld = info->dynamic; - _r_debug.r_state = r_debug::RT_CONSISTENT; - rtld_db_dlactivity(); + notify_gdb_of_load(map); } static void notify_gdb_of_unload(soinfo* info) { @@ -349,22 +295,7 @@ static void notify_gdb_of_unload(soinfo* info) { return; } - ScopedPthreadMutexLocker locker(&g__r_debug_mutex); - - _r_debug.r_state = r_debug::RT_DELETE; - rtld_db_dlactivity(); - - remove_soinfo_from_debug_map(info); - - _r_debug.r_state = r_debug::RT_CONSISTENT; - rtld_db_dlactivity(); -} - -void notify_gdb_of_libraries() { - _r_debug.r_state = r_debug::RT_ADD; - rtld_db_dlactivity(); - _r_debug.r_state = r_debug::RT_CONSISTENT; - rtld_db_dlactivity(); + notify_gdb_of_unload(&(info->link_map_head)); } bool android_namespace_t::is_accessible(const std::string& file) { diff --git a/linker/linker.h b/linker/linker.h index 389c5b362..104d61528 100644 --- a/linker/linker.h +++ b/linker/linker.h @@ -440,7 +440,6 @@ int do_dladdr(const void* addr, Dl_info* info); void debuggerd_init(); extern "C" abort_msg_t* g_abort_message; -extern "C" void notify_gdb_of_libraries(); char* linker_get_error_buffer(); size_t linker_get_error_buffer_size(); diff --git a/linker/linker_gdb_support.cpp b/linker/linker_gdb_support.cpp new file mode 100644 index 000000000..de7408740 --- /dev/null +++ b/linker/linker_gdb_support.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2016 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. + */ + +#include "linker_gdb_support.h" + +#include + +#include "private/ScopedPthreadMutexLocker.h" + +// This function is an empty stub where GDB locates a breakpoint to get notified +// about linker activity. +extern "C" +void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(); + +r_debug _r_debug = + {1, nullptr, reinterpret_cast(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0}; + +static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER; +static link_map* r_debug_tail = nullptr; + +void insert_link_map_into_debug_map(link_map* map) { + // Stick the new library at the end of the list. + // gdb tends to care more about libc than it does + // about leaf libraries, and ordering it this way + // reduces the back-and-forth over the wire. + if (r_debug_tail != nullptr) { + r_debug_tail->l_next = map; + map->l_prev = r_debug_tail; + map->l_next = nullptr; + } else { + _r_debug.r_map = map; + map->l_prev = nullptr; + map->l_next = nullptr; + } + r_debug_tail = map; +} + +void remove_link_map_from_debug_map(link_map* map) { + if (r_debug_tail == map) { + r_debug_tail = map->l_prev; + } + + if (map->l_prev) { + map->l_prev->l_next = map->l_next; + } + if (map->l_next) { + map->l_next->l_prev = map->l_prev; + } +} + +void notify_gdb_of_load(link_map* map) { + ScopedPthreadMutexLocker locker(&g__r_debug_mutex); + + _r_debug.r_state = r_debug::RT_ADD; + rtld_db_dlactivity(); + + insert_link_map_into_debug_map(map); + + _r_debug.r_state = r_debug::RT_CONSISTENT; + rtld_db_dlactivity(); +} + +void notify_gdb_of_unload(link_map* map) { + ScopedPthreadMutexLocker locker(&g__r_debug_mutex); + + _r_debug.r_state = r_debug::RT_DELETE; + rtld_db_dlactivity(); + + remove_link_map_from_debug_map(map); + + _r_debug.r_state = r_debug::RT_CONSISTENT; + rtld_db_dlactivity(); +} + +void notify_gdb_of_libraries() { + _r_debug.r_state = r_debug::RT_ADD; + rtld_db_dlactivity(); + _r_debug.r_state = r_debug::RT_CONSISTENT; + rtld_db_dlactivity(); +} + diff --git a/linker/linker_gdb_support.h b/linker/linker_gdb_support.h new file mode 100644 index 000000000..2a590ba43 --- /dev/null +++ b/linker/linker_gdb_support.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2016 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 __LINKER_GDB_SUPPORT_H +#define __LINKER_GDB_SUPPORT_H + +#include +#include + +__BEGIN_DECLS + +void insert_link_map_into_debug_map(link_map* map); +void remove_link_map_from_debug_map(link_map* map); +void notify_gdb_of_load(link_map* map); +void notify_gdb_of_unload(link_map* map); +void notify_gdb_of_libraries(); + +extern struct r_debug _r_debug; + +__END_DECLS + +#endif