]> git.saurik.com Git - apple/dyld.git/blob - src/dyld_gdb.cpp
dyld-95.3.tar.gz
[apple/dyld.git] / src / dyld_gdb.cpp
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 #include "mach-o/dyld_images.h"
35
36 #define OLD_GDB_DYLD_INTERFACE __ppc__ || __i386__
37
38 // old gdb interface to dyld only supported on 32-bit ppc and i386
39 #if OLD_GDB_DYLD_INTERFACE
40
41 unsigned int gdb_dyld_version = 2;
42
43
44 /*
45 * gdb_dyld_state_changed() is a dummy routine called by dyld after images get
46 * added or removed/ Gdb is expected to set a break point at
47 * gdb_dyld_state_changed() then re-read dyld internal data as specified in
48 * the header file dyld_gdb.h
49 */
50 void gdb_dyld_state_changed()
51 {
52 // do nothing
53 }
54
55 #define NLIBRARY_IMAGES 200
56 #define NOBJECT_IMAGES 1
57
58
59 struct image {
60 const char* physical_name; // physical image name (file name)
61 uint32_t vmaddr_slide; // the slide from the staticly linked address
62 const mach_header* mh; // address of the mach header of the image
63 uint32_t valid; // TRUE if this is struct is valid
64 const char* name; // image name for reporting errors
65 };
66
67
68 struct library_images {
69 struct image images[NLIBRARY_IMAGES];
70 uint32_t nimages;
71 struct library_images* next_images;
72 };
73 struct object_images {
74 struct image images[NOBJECT_IMAGES];
75 uint32_t nimages;
76 struct library_images* next_images;
77 };
78
79 unsigned int gdb_nobject_images = NOBJECT_IMAGES;
80 unsigned int gdb_object_image_size = sizeof(image);
81 unsigned int gdb_nlibrary_images = NLIBRARY_IMAGES;
82 unsigned int gdb_library_image_size = sizeof(image);
83
84 extern "C" {
85 object_images object_images;// = { {}, 0 , NULL };
86 library_images library_images;// = { {}, 0 , NULL };
87 void send_event(const struct dyld_event* event);
88 }
89
90
91 enum dyld_event_type {
92 DYLD_IMAGE_ADDED = 0,
93 DYLD_IMAGE_REMOVED = 5
94 };
95
96 struct dyld_event {
97 enum dyld_event_type type;
98 const struct mach_header* header;
99 uintptr_t slide;
100 };
101
102
103 // gdb only notices changes bundles/dylibs loaded at runtime
104 // if the "send_event()" function in dyld is called...
105 void send_event(const struct dyld_event* event);
106 void (*send_event_ptr)(const struct dyld_event* event) = &send_event;
107
108 void addImageForgdb(const mach_header* mh, uintptr_t slide, const char* physicalPath, const char* logicalPath)
109 {
110 struct library_images* li = &library_images;
111 while ( li->nimages >= NLIBRARY_IMAGES ) {
112 if ( li->next_images == NULL ) {
113 struct library_images* li2 = new struct library_images();
114 li2->nimages = 0;
115 li2->next_images = NULL;
116 li->next_images = li2;
117 li = li2;
118 }
119 else {
120 li = li->next_images;
121 }
122 }
123 image* info = &li->images[li->nimages++];
124 info->physical_name = physicalPath;
125 info->vmaddr_slide = slide;
126 info->mh = mh;
127 info->valid = 1;
128 info->name = logicalPath;
129
130 // ping gdb about change
131 dyld_event event;
132 event.type = DYLD_IMAGE_ADDED;
133 event.header = mh;
134 event.slide = slide;
135
136 // we have to indirect through a function pointer to keep gcc-3.5 from inlining away the function call
137 // rdar://problem/3830560
138 (*send_event_ptr)(&event);
139 }
140
141 // move this to after use, otherwise gcc will see it has an empty implementation and
142 // optimize away the call site
143 void send_event(const struct dyld_event* event)
144 {
145 // This function exists to let gdb set a break point
146 // and catch libraries being added...
147 }
148
149
150 void removeImageForgdb(const mach_header* mh)
151 {
152 for (struct library_images* li = &library_images; li != NULL; li = li->next_images) {
153 for( uint32_t n=0; n < li->nimages; ++n) {
154 struct image* image = &li->images[n];
155 if ( image->mh == mh ) {
156 image->physical_name = NULL;
157 image->vmaddr_slide = 0;
158 image->mh = 0;
159 image->valid = 0;
160 image->name = NULL;
161 return;
162 }
163 }
164 }
165 }
166
167 #endif
168
169 static std::vector<dyld_image_info> sImageInfos;
170
171
172
173 void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[])
174 {
175 // make initial size large enought that we probably won't need to re-alloc it
176 if ( sImageInfos.size() == 0 )
177 sImageInfos.reserve(200);
178
179 // set infoArray to NULL to denote it is in-use
180 dyld_all_image_infos.infoArray = NULL;
181
182 // append all new images
183 for (uint32_t i=0; i < infoCount; ++i)
184 sImageInfos.push_back(info[i]);
185 dyld_all_image_infos.infoArrayCount = sImageInfos.size();
186
187 // set infoArray back to base address of vector
188 dyld_all_image_infos.infoArray = &sImageInfos[0];
189
190 // tell gdb that about the new images
191 dyld_all_image_infos.notification(dyld_image_adding, infoCount, info);
192 }
193
194 void removeImageFromAllImages(const struct mach_header* loadAddress)
195 {
196 dyld_image_info goingAway;
197
198 // set infoArray to NULL to denote it is in-use
199 dyld_all_image_infos.infoArray = NULL;
200
201 // remove image from infoArray
202 for (std::vector<dyld_image_info>::iterator it=sImageInfos.begin(); it != sImageInfos.end(); it++) {
203 if ( it->imageLoadAddress == loadAddress ) {
204 goingAway = *it;
205 sImageInfos.erase(it);
206 break;
207 }
208 }
209 dyld_all_image_infos.infoArrayCount = sImageInfos.size();
210
211 // set infoArray back to base address of vector
212 dyld_all_image_infos.infoArray = &sImageInfos[0];
213
214 // tell gdb that about the new images
215 dyld_all_image_infos.notification(dyld_image_removing, 1, &goingAway);
216 }
217
218
219 static void gdb_image_notifier(enum dyld_image_mode mode, uint32_t infoCount, const dyld_image_info info[])
220 {
221 // do nothing
222 // gdb sets a break point here to catch notifications
223 //dyld::log("dyld: gdb_image_notifier(%s, %d, ...)\n", mode ? "dyld_image_removing" : "dyld_image_adding", infoCount);
224 //for (uint32_t i=0; i < infoCount; ++i)
225 // dyld::log("dyld: %d loading at %p %s\n", i, info[i].imageLoadAddress, info[i].imageFilePath);
226 //for (uint32_t i=0; i < dyld_all_image_infos.infoArrayCount; ++i)
227 // dyld::log("dyld: %d loading at %p %s\n", i, dyld_all_image_infos.infoArray[i].imageLoadAddress, dyld_all_image_infos.infoArray[i].imageFilePath);
228 }
229
230
231
232 struct dyld_all_image_infos dyld_all_image_infos = { 1, 0, NULL, &gdb_image_notifier, false };
233
234 struct dyld_shared_cache_ranges dyld_shared_cache_ranges;
235
236