2 * Copyright (c) 2016 Apple Inc. All rights reserved.
5 typedef char *kobject_description_t
[512];
20 err_mach(kern_return_t kr
, const struct region
*r
, const char *fmt
, ...)
27 printf("%016llx-%016llx ", R_ADDR(r
), R_ENDADDR(r
));
32 printf(": failed: %s (0x%x)", mach_error_string(kr
), kr
);
33 switch (err_get_system(kr
)) {
34 case err_get_system(err_mach_ipc
):
35 /* 0x10000000 == (4 << 26) */
36 printf(" => fatal\n");
47 vprintvr(const struct vm_range
*vr
, const char *restrict fmt
, va_list ap
)
50 printf("%016llx-%016llx ", V_ADDR(vr
), V_ENDADDR(vr
));
55 printvr(const struct vm_range
*vr
, const char *fmt
, ...)
59 vprintvr(vr
, fmt
, ap
);
64 printr(const struct region
*r
, const char *fmt
, ...)
68 vprintvr(R_RANGE(r
), fmt
, ap
);
73 * Print power-of-1024 sizes in human-readable form
76 str_hsize(hsize_str_t hstr
, uint64_t size
)
78 humanize_number(hstr
, sizeof (hsize_str_t
) - 1, size
, "",
79 HN_AUTOSCALE
, HN_B
| HN_NOSPACE
| HN_DECIMAL
| HN_IEC_PREFIXES
);
84 * Print VM protections in human-readable form
87 str_prot(const vm_prot_t prot
)
89 static const char *pstr
[] = {
91 [VM_PROT_READ
] = "r--",
92 [VM_PROT_WRITE
] = "-w-",
93 [VM_PROT_READ
|VM_PROT_WRITE
] = "rw-",
94 [VM_PROT_EXECUTE
] = "--x",
95 [VM_PROT_READ
|VM_PROT_EXECUTE
] = "r-x",
96 [VM_PROT_WRITE
|VM_PROT_EXECUTE
] = "-wx",
97 [VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
] = "rwx"
99 return pstr
[prot
& 7];
102 // c.f. VMUVMRegion.m
107 static const char *sstr
[] = {
110 [SM_PRIVATE
] = "sm=prv",
111 [SM_EMPTY
] = "sm=nul",
112 [SM_SHARED
] = "sm=ali",
113 [SM_TRUESHARED
] = "sm=shm",
114 [SM_PRIVATE_ALIASED
] = "sm=zer",
115 [SM_SHARED_ALIASED
] = "sm=s/a",
116 [SM_LARGE_PAGE
] = "sm=lpg",
118 if ((unsigned)sm
< sizeof (sstr
) / sizeof (sstr
[0]))
124 str_purgable(int pu
, int sm
)
128 static const char *pstr
[] = {
129 [VM_PURGABLE_NONVOLATILE
] = "p=n",
130 [VM_PURGABLE_VOLATILE
] = "p=v",
131 [VM_PURGABLE_EMPTY
] = "p=e",
132 [VM_PURGABLE_DENY
] = " ",
134 if ((unsigned)pu
< sizeof (pstr
) / sizeof (pstr
[0]))
140 * c.f. VMURegionTypeDescriptionForTagShareProtAndPager.
143 str_tag(tag_str_t tstr
, int tag
, int share_mode
, vm_prot_t curprot
, int external_pager
)
150 rtype
= "mapped file";
151 else if (SM_TRUESHARED
== share_mode
)
152 rtype
= "shared memory";
154 rtype
= "VM_allocate";
156 case VM_MEMORY_MALLOC
:
157 if (VM_PROT_NONE
== curprot
)
158 rtype
= "MALLOC guard page";
159 else if (SM_EMPTY
== share_mode
)
162 rtype
= "MALLOC metadata";
164 case VM_MEMORY_STACK
:
165 if (VM_PROT_NONE
== curprot
)
166 rtype
= "Stack guard";
170 #if defined(CONFIG_DEBUG) || defined(CONFIG_GCORE_MAP)
171 case VM_MEMORY_MALLOC_SMALL
:
172 rtype
= "MALLOC_SMALL";
174 case VM_MEMORY_MALLOC_LARGE
:
175 rtype
= "MALLOC_LARGE";
177 case VM_MEMORY_MALLOC_HUGE
:
178 rtype
= "MALLOC_HUGE";
183 case VM_MEMORY_REALLOC
:
184 rtype
= "MALLOC_REALLOC";
186 case VM_MEMORY_MALLOC_TINY
:
187 rtype
= "MALLOC_TINY";
189 case VM_MEMORY_MALLOC_LARGE_REUSABLE
:
190 rtype
= "MALLOC_LARGE_REUSABLE";
192 case VM_MEMORY_MALLOC_LARGE_REUSED
:
193 rtype
= "MALLOC_LARGE";
195 case VM_MEMORY_ANALYSIS_TOOL
:
196 rtype
= "Performance tool data";
198 case VM_MEMORY_MALLOC_NANO
:
199 rtype
= "MALLOC_NANO";
201 case VM_MEMORY_MACH_MSG
:
202 rtype
= "Mach message";
204 case VM_MEMORY_IOKIT
:
207 case VM_MEMORY_GUARD
:
210 case VM_MEMORY_SHARED_PMAP
:
211 rtype
= "shared pmap";
213 case VM_MEMORY_DYLIB
:
216 case VM_MEMORY_OBJC_DISPATCHERS
:
217 rtype
= "ObjC dispatching code";
219 case VM_MEMORY_UNSHARED_PMAP
:
220 rtype
= "unshared pmap";
222 case VM_MEMORY_APPKIT
:
225 case VM_MEMORY_FOUNDATION
:
226 rtype
= "Foundation";
228 case VM_MEMORY_COREGRAPHICS
:
229 rtype
= "CoreGraphics";
231 case VM_MEMORY_CORESERVICES
:
232 rtype
= "CoreServices";
237 case VM_MEMORY_COREDATA
:
240 case VM_MEMORY_COREDATA_OBJECTIDS
:
241 rtype
= "CoreData Object IDs";
244 rtype
= "ATS (font support)";
246 case VM_MEMORY_LAYERKIT
:
247 rtype
= "CoreAnimation";
249 case VM_MEMORY_CGIMAGE
:
252 case VM_MEMORY_TCMALLOC
:
253 rtype
= "WebKit Malloc";
255 case VM_MEMORY_COREGRAPHICS_DATA
:
256 rtype
= "CG raster data";
258 case VM_MEMORY_COREGRAPHICS_SHARED
:
259 rtype
= "CG shared images";
261 case VM_MEMORY_COREGRAPHICS_FRAMEBUFFERS
:
262 rtype
= "CG framebuffers";
264 case VM_MEMORY_COREGRAPHICS_BACKINGSTORES
:
265 rtype
= "CG backingstores";
268 rtype
= "dyld private memory";
270 case VM_MEMORY_DYLD_MALLOC
:
271 rtype
= "dyld malloc memory";
273 case VM_MEMORY_SQLITE
:
274 rtype
= "SQlite page cache";
276 case VM_MEMORY_JAVASCRIPT_CORE
:
277 rtype
= "WebAssembly memory";
279 case VM_MEMORY_JAVASCRIPT_JIT_EXECUTABLE_ALLOCATOR
:
280 rtype
= "JS JIT generated code";
282 case VM_MEMORY_JAVASCRIPT_JIT_REGISTER_FILE
:
283 rtype
= "JS VM register file";
286 rtype
= "OpenGL GLSL";
288 case VM_MEMORY_OPENCL
:
291 case VM_MEMORY_COREIMAGE
:
294 case VM_MEMORY_WEBCORE_PURGEABLE_BUFFERS
:
295 rtype
= "WebCore purgable data";
297 case VM_MEMORY_IMAGEIO
:
300 case VM_MEMORY_COREPROFILE
:
301 rtype
= "CoreProfile";
303 case VM_MEMORY_ASSETSD
:
304 rtype
= "Assets Library";
306 case VM_MEMORY_OS_ALLOC_ONCE
:
307 rtype
= "OS Alloc Once";
309 case VM_MEMORY_LIBDISPATCH
:
310 rtype
= "Dispatch continuations";
312 case VM_MEMORY_ACCELERATE
:
313 rtype
= "Accelerate framework";
315 case VM_MEMORY_COREUI
:
316 rtype
= "CoreUI image data";
318 case VM_MEMORY_COREUIFILE
:
319 rtype
= "CoreUI image file";
321 case VM_MEMORY_GENEALOGY
:
322 rtype
= "Activity Tracing";
324 case VM_MEMORY_RAWCAMERA
:
327 case VM_MEMORY_CORPSEINFO
:
328 rtype
= "Process Corpse Info";
331 rtype
= "Apple System Log";
333 case VM_MEMORY_SWIFT_RUNTIME
:
334 rtype
= "Swift runtime";
336 case VM_MEMORY_SWIFT_METADATA
:
337 rtype
= "Swift metadata";
342 case VM_MEMORY_SCENEKIT
:
345 case VM_MEMORY_SKYWALK
:
346 rtype
= "Skywalk Networking";
354 snprintf(tstr
, sizeof (tag_str_t
), "%s", rtype
);
356 snprintf(tstr
, sizeof (tag_str_t
), "tag #%d", tag
);
361 str_tagr(tag_str_t tstr
, const struct region
*r
) {
362 return str_tag(tstr
, r
->r_info
.user_tag
, r
->r_info
.share_mode
, r
->r_info
.protection
, r
->r_info
.external_pager
);
366 * Put two strings together separated by a '+' sign
367 * If the string gets too long, then add an ellipsis and
368 * stop concatenating it.
371 strconcat(const char *s0
, const char *s1
, size_t maxlen
)
373 const char ellipsis
[] = "...";
374 const char junction
[] = ", ";
375 const size_t s0len
= strlen(s0
);
376 size_t nmlen
= s0len
+ strlen(s1
) + strlen(junction
) + 1;
377 if (maxlen
> strlen(ellipsis
) && nmlen
> maxlen
) {
378 if (strcmp(s0
+ s0len
- strlen(ellipsis
), ellipsis
) == 0)
381 nmlen
= s0len
+ strlen(s1
) + strlen(junction
) + 1;
383 char *p
= malloc(nmlen
);
385 strlcpy(p
, s0
, nmlen
);
386 strlcat(p
, junction
, nmlen
);
387 strlcat(p
, s1
, nmlen
);
393 simple_namehash(const char *nm
)
395 unsigned long result
= 5381;
397 while (0 != (c
= *nm
++))
398 result
= (result
* 33) ^ c
;
399 return result
; /* modified djb2 */
403 bounded_pwrite(int fd
, const void *addr
, size_t size
, off_t off
, bool *nocache
, ssize_t
*nwrittenp
)
405 if (opt
->sizebound
&& off
+ (off_t
)size
> opt
->sizebound
)
408 bool oldnocache
= *nocache
;
409 if (size
>= opt
->ncthresh
&& !oldnocache
)
410 *nocache
= 0 == fcntl(fd
, F_NOCACHE
, 1);
411 else if (size
< opt
->ncthresh
&& oldnocache
)
412 *nocache
= 0 != fcntl(fd
, F_NOCACHE
, 0);
413 if (OPTIONS_DEBUG(opt
, 3) && oldnocache
^ *nocache
)
414 printf("F_NOCACHE now %sabled on fd %d\n", *nocache
? "en" : "dis", fd
);
416 const ssize_t nwritten
= pwrite(fd
, addr
, size
, off
);
420 *nwrittenp
= nwritten
;