]>
Commit | Line | Data |
---|---|---|
0959b6d4 A |
1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- |
2 | * | |
3 | * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved. | |
4 | * | |
5 | * @APPLE_LICENSE_HEADER_START@ | |
6 | * | |
7 | * This file contains Original Code and/or Modifications of Original Code | |
8 | * as defined in and that are subject to the Apple Public Source License | |
9 | * Version 2.0 (the 'License'). You may not use this file except in | |
10 | * compliance with the License. Please obtain a copy of the License at | |
11 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
12 | * file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
19 | * Please see the License for the specific language governing rights and | |
20 | * limitations under the License. | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | ||
25 | #include <stddef.h> | |
26 | #include <stdint.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <mach-o/loader.h> | |
30 | ||
31 | #include <vector> | |
32 | ||
33 | #include "mach-o/dyld_gdb.h" | |
34 | ||
35 | ||
36 | // old gdb interface to dyld only supported on 32-bit ppc and i386 (not ppc64_ | |
37 | #if OLD_GDB_DYLD_INTERFACE | |
38 | ||
39 | unsigned int gdb_dyld_version = 2; | |
40 | ||
41 | ||
42 | /* | |
43 | * gdb_dyld_state_changed() is a dummy routine called by dyld after images get | |
44 | * added or removed/ Gdb is expected to set a break point at | |
45 | * gdb_dyld_state_changed() then re-read dyld internal data as specified in | |
46 | * the header file dyld_gdb.h | |
47 | */ | |
48 | void gdb_dyld_state_changed() | |
49 | { | |
50 | // do nothing | |
51 | } | |
52 | ||
53 | #define NLIBRARY_IMAGES 200 | |
54 | #define NOBJECT_IMAGES 1 | |
55 | ||
56 | ||
57 | struct image { | |
58 | const char* physical_name; // physical image name (file name) | |
59 | uint32_t vmaddr_slide; // the slide from the staticly linked address | |
60 | const mach_header* mh; // address of the mach header of the image | |
61 | uint32_t valid; // TRUE if this is struct is valid | |
62 | const char* name; // image name for reporting errors | |
63 | }; | |
64 | ||
65 | ||
66 | struct library_images { | |
67 | struct image images[NLIBRARY_IMAGES]; | |
68 | uint32_t nimages; | |
69 | struct library_images* next_images; | |
70 | }; | |
71 | struct object_images { | |
72 | struct image images[NOBJECT_IMAGES]; | |
73 | uint32_t nimages; | |
74 | struct library_images* next_images; | |
75 | }; | |
76 | ||
77 | unsigned int gdb_nobject_images = NOBJECT_IMAGES; | |
78 | unsigned int gdb_object_image_size = sizeof(image); | |
79 | unsigned int gdb_nlibrary_images = NLIBRARY_IMAGES; | |
80 | unsigned int gdb_library_image_size = sizeof(image); | |
81 | ||
82 | extern "C" { | |
83 | object_images object_images = { {}, 0 , NULL }; | |
84 | library_images library_images = { {}, 0 , NULL }; | |
85 | void send_event(const struct dyld_event* event); | |
86 | } | |
87 | ||
88 | ||
89 | enum dyld_event_type { | |
90 | DYLD_IMAGE_ADDED = 0, | |
91 | DYLD_IMAGE_REMOVED = 5 | |
92 | }; | |
93 | ||
94 | struct dyld_event { | |
95 | enum dyld_event_type type; | |
96 | const struct mach_header* header; | |
97 | uintptr_t slide; | |
98 | }; | |
99 | ||
100 | ||
101 | // gdb only notices changes bundles/dylibs loaded at runtime | |
102 | // if the "send_event()" function in dyld is called... | |
103 | void send_event(const struct dyld_event* event); | |
104 | void (*send_event_ptr)(const struct dyld_event* event) = &send_event; | |
105 | ||
106 | void addImageForgdb(const mach_header* mh, uintptr_t slide, const char* physicalPath, const char* logicalPath) | |
107 | { | |
108 | struct library_images* li = &library_images; | |
109 | while ( li->nimages >= NLIBRARY_IMAGES ) { | |
110 | if ( li->next_images == NULL ) { | |
111 | struct library_images* li2 = new struct library_images(); | |
112 | li2->nimages = 0; | |
113 | li2->next_images = NULL; | |
114 | li->next_images = li2; | |
115 | li = li2; | |
116 | } | |
117 | else { | |
118 | li = li->next_images; | |
119 | } | |
120 | } | |
121 | image* info = &li->images[li->nimages++]; | |
122 | info->physical_name = physicalPath; | |
123 | info->vmaddr_slide = slide; | |
124 | info->mh = mh; | |
125 | info->valid = 1; | |
126 | info->name = logicalPath; | |
127 | ||
128 | // ping gdb about change | |
129 | dyld_event event; | |
130 | event.type = DYLD_IMAGE_ADDED; | |
131 | event.header = mh; | |
132 | event.slide = slide; | |
133 | ||
134 | // we have to indirect through a function pointer to keep gcc-3.5 from inlining away the function call | |
135 | // rdar://problem/3830560 | |
136 | (*send_event_ptr)(&event); | |
137 | } | |
138 | ||
139 | // move this to after use, otherwise gcc will see it has an empty implementation and | |
140 | // optimize away the call site | |
141 | void send_event(const struct dyld_event* event) | |
142 | { | |
143 | // This function exists to let gdb set a break point | |
144 | // and catch libraries being added... | |
145 | } | |
146 | ||
147 | ||
148 | void removeImageForgdb(const mach_header* mh) | |
149 | { | |
150 | for (struct library_images* li = &library_images; li != NULL; li = li->next_images) { | |
151 | for( uint32_t n=0; n < li->nimages; ++n) { | |
152 | struct image* image = &li->images[n]; | |
153 | if ( image->mh == mh ) { | |
154 | image->physical_name = NULL; | |
155 | image->vmaddr_slide = 0; | |
156 | image->mh = 0; | |
157 | image->valid = 0; | |
158 | image->name = NULL; | |
159 | return; | |
160 | } | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
165 | #endif | |
166 | ||
167 | static std::vector<dyld_image_info> sImageInfos; | |
168 | ||
169 | ||
170 | ||
171 | void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[]) | |
172 | { | |
173 | // set infoArray to NULL to denote it is in-use | |
174 | dyld_all_image_infos.infoArray = NULL; | |
175 | ||
176 | // append all new images | |
177 | for (uint32_t i=0; i < infoCount; ++i) | |
178 | sImageInfos.push_back(info[i]); | |
179 | dyld_all_image_infos.infoArrayCount = sImageInfos.size(); | |
180 | ||
181 | // set infoArray back to base address of vector | |
182 | dyld_all_image_infos.infoArray = &sImageInfos[0]; | |
183 | ||
184 | // tell gdb that about the new images | |
185 | dyld_all_image_infos.notification(dyld_image_adding, infoCount, info); | |
186 | } | |
187 | ||
188 | void removeImageFromAllImages(const struct mach_header* loadAddress) | |
189 | { | |
190 | dyld_image_info goingAway; | |
191 | ||
192 | // set infoArray to NULL to denote it is in-use | |
193 | dyld_all_image_infos.infoArray = NULL; | |
194 | ||
195 | // remove image from infoArray | |
196 | for (std::vector<dyld_image_info>::iterator it=sImageInfos.begin(); it != sImageInfos.end(); it++) { | |
197 | if ( it->imageLoadAddress == loadAddress ) { | |
198 | goingAway = *it; | |
199 | sImageInfos.erase(it); | |
200 | break; | |
201 | } | |
202 | } | |
203 | dyld_all_image_infos.infoArrayCount = sImageInfos.size(); | |
204 | ||
205 | // set infoArray back to base address of vector | |
206 | dyld_all_image_infos.infoArray = &sImageInfos[0]; | |
207 | ||
208 | // tell gdb that about the new images | |
209 | dyld_all_image_infos.notification(dyld_image_removing, 1, &goingAway); | |
210 | } | |
211 | ||
212 | ||
213 | static void gdb_image_notifier(enum dyld_image_mode mode, uint32_t infoCount, const dyld_image_info info[]) | |
214 | { | |
215 | // do nothing | |
216 | // gdb sets a break point here to catch notifications | |
217 | //fprintf(stderr, "dyld: gdb_image_notifier(%s, %d, ...)\n", mode ? "dyld_image_removing" : "dyld_image_adding", infoCount); | |
218 | //for (uint32_t i=0; i < dyld_all_image_infos.infoArrayCount; ++i) | |
219 | // fprintf(stderr, "dyld: %d loading at %p %s\n", i, dyld_all_image_infos.infoArray[i].imageLoadAddress, dyld_all_image_infos.infoArray[i].imageFilePath); | |
220 | } | |
221 | ||
222 | ||
223 | ||
224 | struct dyld_all_image_infos dyld_all_image_infos = { 1, 0, NULL, &gdb_image_notifier, false }; | |
225 | ||
226 | ||
227 |