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"
36 // old gdb interface to dyld only supported on 32-bit ppc and i386 (not ppc64_
37 #if OLD_GDB_DYLD_INTERFACE
39 unsigned int gdb_dyld_version
= 2;
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
48 void gdb_dyld_state_changed()
53 #define NLIBRARY_IMAGES 200
54 #define NOBJECT_IMAGES 1
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
66 struct library_images
{
67 struct image images
[NLIBRARY_IMAGES
];
69 struct library_images
* next_images
;
71 struct object_images
{
72 struct image images
[NOBJECT_IMAGES
];
74 struct library_images
* next_images
;
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
);
83 object_images object_images
= { {}, 0 , NULL
};
84 library_images library_images
= { {}, 0 , NULL
};
85 void send_event(const struct dyld_event
* event
);
89 enum dyld_event_type
{
91 DYLD_IMAGE_REMOVED
= 5
95 enum dyld_event_type type
;
96 const struct mach_header
* header
;
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
;
106 void addImageForgdb(const mach_header
* mh
, uintptr_t slide
, const char* physicalPath
, const char* logicalPath
)
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();
113 li2
->next_images
= NULL
;
114 li
->next_images
= li2
;
118 li
= li
->next_images
;
121 image
* info
= &li
->images
[li
->nimages
++];
122 info
->physical_name
= physicalPath
;
123 info
->vmaddr_slide
= slide
;
126 info
->name
= logicalPath
;
128 // ping gdb about change
130 event
.type
= DYLD_IMAGE_ADDED
;
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
);
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
)
143 // This function exists to let gdb set a break point
144 // and catch libraries being added...
148 void removeImageForgdb(const mach_header
* mh
)
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;
167 static std::vector
<dyld_image_info
> sImageInfos
;
171 void addImagesToAllImages(uint32_t infoCount
, const dyld_image_info info
[])
173 // set infoArray to NULL to denote it is in-use
174 dyld_all_image_infos
.infoArray
= NULL
;
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();
181 // set infoArray back to base address of vector
182 dyld_all_image_infos
.infoArray
= &sImageInfos
[0];
184 // tell gdb that about the new images
185 dyld_all_image_infos
.notification(dyld_image_adding
, infoCount
, info
);
188 void removeImageFromAllImages(const struct mach_header
* loadAddress
)
190 dyld_image_info goingAway
;
192 // set infoArray to NULL to denote it is in-use
193 dyld_all_image_infos
.infoArray
= NULL
;
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
) {
199 sImageInfos
.erase(it
);
203 dyld_all_image_infos
.infoArrayCount
= sImageInfos
.size();
205 // set infoArray back to base address of vector
206 dyld_all_image_infos
.infoArray
= &sImageInfos
[0];
208 // tell gdb that about the new images
209 dyld_all_image_infos
.notification(dyld_image_removing
, 1, &goingAway
);
213 static void gdb_image_notifier(enum dyld_image_mode mode
, uint32_t infoCount
, const dyld_image_info info
[])
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);
224 struct dyld_all_image_infos dyld_all_image_infos
= { 1, 0, NULL
, &gdb_image_notifier
, false };