]> git.saurik.com Git - apple/dyld.git/blob - src/dyld_gdb.cpp
7d79aade137ea0d290d8682b6a1d295bec59318d
[apple/dyld.git] / src / dyld_gdb.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2009 Apple 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 #include "ImageLoader.h"
36
37
38 static std::vector<dyld_image_info> sImageInfos;
39
40 void addImagesToAllImages(uint32_t infoCount, const dyld_image_info info[])
41 {
42 // make initial size large enought that we probably won't need to re-alloc it
43 if ( sImageInfos.size() == 0 )
44 sImageInfos.reserve(INITIAL_IMAGE_COUNT);
45
46 // set infoArray to NULL to denote it is in-use
47 dyld_all_image_infos.infoArray = NULL;
48
49 // append all new images
50 for (uint32_t i=0; i < infoCount; ++i)
51 sImageInfos.push_back(info[i]);
52 dyld_all_image_infos.infoArrayCount = sImageInfos.size();
53
54 // set infoArray back to base address of vector
55 dyld_all_image_infos.infoArray = &sImageInfos[0];
56
57 // tell gdb that about the new images
58 dyld_all_image_infos.notification(dyld_image_adding, infoCount, info);
59 }
60
61 void removeImageFromAllImages(const struct mach_header* loadAddress)
62 {
63 dyld_image_info goingAway;
64
65 // set infoArray to NULL to denote it is in-use
66 dyld_all_image_infos.infoArray = NULL;
67
68 // remove image from infoArray
69 for (std::vector<dyld_image_info>::iterator it=sImageInfos.begin(); it != sImageInfos.end(); it++) {
70 if ( it->imageLoadAddress == loadAddress ) {
71 goingAway = *it;
72 sImageInfos.erase(it);
73 break;
74 }
75 }
76 dyld_all_image_infos.infoArrayCount = sImageInfos.size();
77
78 // set infoArray back to base address of vector
79 dyld_all_image_infos.infoArray = &sImageInfos[0];
80
81 // tell gdb that about the new images
82 dyld_all_image_infos.notification(dyld_image_removing, 1, &goingAway);
83 }
84
85
86 static void gdb_image_notifier(enum dyld_image_mode mode, uint32_t infoCount, const dyld_image_info info[])
87 {
88 // do nothing
89 // gdb sets a break point here to catch notifications
90 //dyld::log("dyld: gdb_image_notifier(%s, %d, ...)\n", mode ? "dyld_image_removing" : "dyld_image_adding", infoCount);
91 //for (uint32_t i=0; i < infoCount; ++i)
92 // dyld::log("dyld: %d loading at %p %s\n", i, info[i].imageLoadAddress, info[i].imageFilePath);
93 //for (uint32_t i=0; i < dyld_all_image_infos.infoArrayCount; ++i)
94 // dyld::log("dyld: %d loading at %p %s\n", i, dyld_all_image_infos.infoArray[i].imageLoadAddress, dyld_all_image_infos.infoArray[i].imageFilePath);
95 }
96
97 void setAlImageInfosHalt(const char* message, uintptr_t flags)
98 {
99 dyld_all_image_infos.errorMessage = message;
100 dyld_all_image_infos.terminationFlags = flags;
101 }
102
103
104 extern void* __dso_handle;
105 #define STR(s) # s
106 #define XSTR(s) STR(s)
107
108 struct dyld_all_image_infos dyld_all_image_infos __attribute__ ((section ("__DATA,__all_image_info")))
109 = { 7, 0, NULL, &gdb_image_notifier, false, false, (const mach_header*)&__dso_handle, NULL,
110 XSTR(DYLD_VERSION) , NULL, 0, 0 };
111
112 struct dyld_shared_cache_ranges dyld_shared_cache_ranges;
113
114
115