1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2005 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
29 #include <mach-o/loader.h>
33 #include "mach-o/dyld_gdb.h"
34 #include "mach-o/dyld_images.h"
36 #define OLD_GDB_DYLD_INTERFACE __ppc__ || __i386__
38 // old gdb interface to dyld only supported on 32-bit ppc and i386
39 #if OLD_GDB_DYLD_INTERFACE
41 unsigned int gdb_dyld_version
= 2;
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
50 void gdb_dyld_state_changed()
55 #define NLIBRARY_IMAGES 200
56 #define NOBJECT_IMAGES 1
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
68 struct library_images
{
69 struct image images
[NLIBRARY_IMAGES
];
71 struct library_images
* next_images
;
73 struct object_images
{
74 struct image images
[NOBJECT_IMAGES
];
76 struct library_images
* next_images
;
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
);
85 object_images object_images
;// = { {}, 0 , NULL };
86 library_images library_images
;// = { {}, 0 , NULL };
87 void send_event(const struct dyld_event
* event
);
91 enum dyld_event_type
{
93 DYLD_IMAGE_REMOVED
= 5
97 enum dyld_event_type type
;
98 const struct mach_header
* header
;
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
;
108 void addImageForgdb(const mach_header
* mh
, uintptr_t slide
, const char* physicalPath
, const char* logicalPath
)
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();
115 li2
->next_images
= NULL
;
116 li
->next_images
= li2
;
120 li
= li
->next_images
;
123 image
* info
= &li
->images
[li
->nimages
++];
124 info
->physical_name
= physicalPath
;
125 info
->vmaddr_slide
= slide
;
128 info
->name
= logicalPath
;
130 // ping gdb about change
132 event
.type
= DYLD_IMAGE_ADDED
;
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
);
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
)
145 // This function exists to let gdb set a break point
146 // and catch libraries being added...
150 void removeImageForgdb(const mach_header
* mh
)
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;
169 static std::vector
<dyld_image_info
> sImageInfos
;
173 void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[])
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);
179 // set infoArray to NULL to denote it is in-use
180 dyld_all_image_infos
.infoArray
= NULL
;
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();
187 // set infoArray back to base address of vector
188 dyld_all_image_infos
.infoArray
= &sImageInfos
[0];
190 // tell gdb that about the new images
191 dyld_all_image_infos
.notification(dyld_image_adding
, infoCount
, info
);
194 void removeImageFromAllImages(const struct mach_header
* loadAddress
)
196 dyld_image_info goingAway
;
198 // set infoArray to NULL to denote it is in-use
199 dyld_all_image_infos
.infoArray
= NULL
;
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
) {
205 sImageInfos
.erase(it
);
209 dyld_all_image_infos
.infoArrayCount
= sImageInfos
.size();
211 // set infoArray back to base address of vector
212 dyld_all_image_infos
.infoArray
= &sImageInfos
[0];
214 // tell gdb that about the new images
215 dyld_all_image_infos
.notification(dyld_image_removing
, 1, &goingAway
);
219 static void gdb_image_notifier(enum dyld_image_mode mode
, uint32_t infoCount
, const dyld_image_info info
[])
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);
232 struct dyld_all_image_infos dyld_all_image_infos
= { 1, 0, NULL
, &gdb_image_notifier
, false };
234 struct dyld_shared_cache_ranges dyld_shared_cache_ranges
;