OSDN Git Service

Merge "Whitelist __NR__newselect." am: 8916a91eb3
[android-x86/bionic.git] / linker / linker_gdb_support.cpp
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include "linker_gdb_support.h"
30
31 #include <pthread.h>
32
33 #include "private/ScopedPthreadMutexLocker.h"
34
35 // This function is an empty stub where GDB locates a breakpoint to get notified
36 // about linker activity.
37 extern "C"
38 void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
39
40 r_debug _r_debug =
41     {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
42
43 static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
44 static link_map* r_debug_tail = nullptr;
45
46 void insert_link_map_into_debug_map(link_map* map) {
47   // Stick the new library at the end of the list.
48   // gdb tends to care more about libc than it does
49   // about leaf libraries, and ordering it this way
50   // reduces the back-and-forth over the wire.
51   if (r_debug_tail != nullptr) {
52     r_debug_tail->l_next = map;
53     map->l_prev = r_debug_tail;
54     map->l_next = nullptr;
55   } else {
56     _r_debug.r_map = map;
57     map->l_prev = nullptr;
58     map->l_next = nullptr;
59   }
60   r_debug_tail = map;
61 }
62
63 void remove_link_map_from_debug_map(link_map* map) {
64   if (r_debug_tail == map) {
65     r_debug_tail = map->l_prev;
66   }
67
68   if (map->l_prev) {
69     map->l_prev->l_next = map->l_next;
70   }
71   if (map->l_next) {
72     map->l_next->l_prev = map->l_prev;
73   }
74 }
75
76 void notify_gdb_of_load(link_map* map) {
77   ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
78
79   _r_debug.r_state = r_debug::RT_ADD;
80   rtld_db_dlactivity();
81
82   insert_link_map_into_debug_map(map);
83
84   _r_debug.r_state = r_debug::RT_CONSISTENT;
85   rtld_db_dlactivity();
86 }
87
88 void notify_gdb_of_unload(link_map* map) {
89   ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
90
91   _r_debug.r_state = r_debug::RT_DELETE;
92   rtld_db_dlactivity();
93
94   remove_link_map_from_debug_map(map);
95
96   _r_debug.r_state = r_debug::RT_CONSISTENT;
97   rtld_db_dlactivity();
98 }
99
100 void notify_gdb_of_libraries() {
101   _r_debug.r_state = r_debug::RT_ADD;
102   rtld_db_dlactivity();
103   _r_debug.r_state = r_debug::RT_CONSISTENT;
104   rtld_db_dlactivity();
105 }
106