2 * Copyright (c) 2016 Apple Inc. All rights reserved.
19 err_mach(kern_return_t kr
, const struct region
*r
, const char *fmt
, ...)
26 printf("%016llx-%016llx ", R_ADDR(r
), R_ENDADDR(r
));
31 printf(": failed: %s (0x%x)", mach_error_string(kr
), kr
);
32 switch (err_get_system(kr
)) {
33 case err_get_system(err_mach_ipc
):
34 /* 0x10000000 == (4 << 26) */
35 printf(" => fatal\n");
46 vprintvr(const struct vm_range
*vr
, const char *restrict fmt
, va_list ap
)
49 printf("%016llx-%016llx ", V_ADDR(vr
), V_ENDADDR(vr
));
54 printvr(const struct vm_range
*vr
, const char *fmt
, ...)
58 vprintvr(vr
, fmt
, ap
);
63 printr(const struct region
*r
, const char *fmt
, ...)
67 vprintvr(R_RANGE(r
), fmt
, ap
);
72 * Print power-of-1024 sizes in human-readable form
75 str_hsize(hsize_str_t hstr
, uint64_t size
)
77 humanize_number(hstr
, sizeof (hsize_str_t
) - 1, size
, "",
78 HN_AUTOSCALE
, HN_B
| HN_NOSPACE
| HN_DECIMAL
| HN_IEC_PREFIXES
);
83 * Print VM protections in human-readable form
86 str_prot(const vm_prot_t prot
)
88 static const char *pstr
[] = {
90 [VM_PROT_READ
] = "r--",
91 [VM_PROT_WRITE
] = "-w-",
92 [VM_PROT_READ
|VM_PROT_WRITE
] = "rw-",
93 [VM_PROT_EXECUTE
] = "--x",
94 [VM_PROT_READ
|VM_PROT_EXECUTE
] = "r-x",
95 [VM_PROT_WRITE
|VM_PROT_EXECUTE
] = "-wx",
96 [VM_PROT_READ
|VM_PROT_WRITE
|VM_PROT_EXECUTE
] = "rwx"
98 return pstr
[prot
& 7];
101 // c.f. VMUVMRegion.m
106 static const char *sstr
[] = {
109 [SM_PRIVATE
] = "sm=prv",
110 [SM_EMPTY
] = "sm=nul",
111 [SM_SHARED
] = "sm=ali",
112 [SM_TRUESHARED
] = "sm=shm",
113 [SM_PRIVATE_ALIASED
] = "sm=zer",
114 [SM_SHARED_ALIASED
] = "sm=s/a",
115 [SM_LARGE_PAGE
] = "sm=lpg",
117 if ((unsigned)sm
< sizeof (sstr
) / sizeof (sstr
[0]))
123 str_purgable(int pu
, int sm
)
127 static const char *pstr
[] = {
128 [VM_PURGABLE_NONVOLATILE
] = "p=n",
129 [VM_PURGABLE_VOLATILE
] = "p=v",
130 [VM_PURGABLE_EMPTY
] = "p=e",
131 [VM_PURGABLE_DENY
] = " ",
133 if ((unsigned)pu
< sizeof (pstr
) / sizeof (pstr
[0]))
139 * c.f. VMURegionTypeDescriptionForTagShareProtAndPager.
142 str_tag(tag_str_t tstr
, int tag
, int share_mode
, vm_prot_t curprot
, int external_pager
)
149 rtype
= "mapped file";
150 else if (SM_TRUESHARED
== share_mode
)
151 rtype
= "shared memory";
153 rtype
= "VM_allocate";
155 case VM_MEMORY_MALLOC
:
156 if (VM_PROT_NONE
== curprot
)
157 rtype
= "MALLOC guard page";
158 else if (SM_EMPTY
== share_mode
)
161 rtype
= "MALLOC metadata";
163 case VM_MEMORY_STACK
:
164 if (VM_PROT_NONE
== curprot
)
165 rtype
= "Stack guard";
169 #if defined(CONFIG_DEBUG) || defined(CONFIG_GCORE_MAP)
170 case VM_MEMORY_MALLOC_SMALL
:
171 rtype
= "MALLOC_SMALL";
173 case VM_MEMORY_MALLOC_LARGE
:
174 rtype
= "MALLOC_LARGE";
176 case VM_MEMORY_MALLOC_HUGE
:
177 rtype
= "MALLOC_HUGE";
182 case VM_MEMORY_REALLOC
:
183 rtype
= "MALLOC_REALLOC";
185 case VM_MEMORY_MALLOC_TINY
:
186 rtype
= "MALLOC_TINY";
188 case VM_MEMORY_MALLOC_LARGE_REUSABLE
:
189 rtype
= "MALLOC_LARGE_REUSABLE";
191 case VM_MEMORY_MALLOC_LARGE_REUSED
:
192 rtype
= "MALLOC_LARGE";
194 case VM_MEMORY_ANALYSIS_TOOL
:
195 rtype
= "Performance tool data";
197 case VM_MEMORY_MALLOC_NANO
:
198 rtype
= "MALLOC_NANO";
200 case VM_MEMORY_MACH_MSG
:
201 rtype
= "Mach message";
203 case VM_MEMORY_IOKIT
:
206 case VM_MEMORY_GUARD
:
209 case VM_MEMORY_SHARED_PMAP
:
210 rtype
= "shared pmap";
212 case VM_MEMORY_DYLIB
:
215 case VM_MEMORY_OBJC_DISPATCHERS
:
216 rtype
= "ObjC dispatching code";
218 case VM_MEMORY_UNSHARED_PMAP
:
219 rtype
= "unshared pmap";
221 case VM_MEMORY_APPKIT
:
224 case VM_MEMORY_FOUNDATION
:
225 rtype
= "Foundation";
227 case VM_MEMORY_COREGRAPHICS
:
228 rtype
= "CoreGraphics";
230 case VM_MEMORY_CORESERVICES
:
231 rtype
= "CoreServices";
236 case VM_MEMORY_COREDATA
:
239 case VM_MEMORY_COREDATA_OBJECTIDS
:
240 rtype
= "CoreData Object IDs";
243 rtype
= "ATS (font support)";
245 case VM_MEMORY_LAYERKIT
:
246 rtype
= "CoreAnimation";
248 case VM_MEMORY_CGIMAGE
:
251 case VM_MEMORY_TCMALLOC
:
252 rtype
= "WebKit Malloc";
254 case VM_MEMORY_COREGRAPHICS_DATA
:
255 rtype
= "CG raster data";
257 case VM_MEMORY_COREGRAPHICS_SHARED
:
258 rtype
= "CG shared images";
260 case VM_MEMORY_COREGRAPHICS_FRAMEBUFFERS
:
261 rtype
= "CG framebuffers";
263 case VM_MEMORY_COREGRAPHICS_BACKINGSTORES
:
264 rtype
= "CG backingstores";
267 rtype
= "dyld private memory";
269 case VM_MEMORY_DYLD_MALLOC
:
270 rtype
= "dyld malloc memory";
272 case VM_MEMORY_SQLITE
:
273 rtype
= "SQlite page cache";
275 case VM_MEMORY_JAVASCRIPT_CORE
:
276 rtype
= "WebAssembly memory";
278 case VM_MEMORY_JAVASCRIPT_JIT_EXECUTABLE_ALLOCATOR
:
279 rtype
= "JS JIT generated code";
281 case VM_MEMORY_JAVASCRIPT_JIT_REGISTER_FILE
:
282 rtype
= "JS VM register file";
285 rtype
= "OpenGL GLSL";
287 case VM_MEMORY_OPENCL
:
290 case VM_MEMORY_COREIMAGE
:
293 case VM_MEMORY_WEBCORE_PURGEABLE_BUFFERS
:
294 rtype
= "WebCore purgable data";
296 case VM_MEMORY_IMAGEIO
:
299 case VM_MEMORY_COREPROFILE
:
300 rtype
= "CoreProfile";
302 case VM_MEMORY_ASSETSD
:
303 rtype
= "Assets Library";
305 case VM_MEMORY_OS_ALLOC_ONCE
:
306 rtype
= "OS Alloc Once";
308 case VM_MEMORY_LIBDISPATCH
:
309 rtype
= "Dispatch continuations";
311 case VM_MEMORY_ACCELERATE
:
312 rtype
= "Accelerate framework";
314 case VM_MEMORY_COREUI
:
315 rtype
= "CoreUI image data";
317 case VM_MEMORY_COREUIFILE
:
318 rtype
= "CoreUI image file";
320 case VM_MEMORY_GENEALOGY
:
321 rtype
= "Activity Tracing";
323 case VM_MEMORY_RAWCAMERA
:
326 case VM_MEMORY_CORPSEINFO
:
327 rtype
= "Process Corpse Info";
330 rtype
= "Apple System Log";
332 case VM_MEMORY_SWIFT_RUNTIME
:
333 rtype
= "Swift runtime";
335 case VM_MEMORY_SWIFT_METADATA
:
336 rtype
= "Swift metadata";
341 case VM_MEMORY_SCENEKIT
:
344 case VM_MEMORY_SKYWALK
:
345 rtype
= "Skywalk Networking";
353 snprintf(tstr
, sizeof (tag_str_t
), "%s", rtype
);
355 snprintf(tstr
, sizeof (tag_str_t
), "tag #%d", tag
);
360 str_tagr(tag_str_t tstr
, const struct region
*r
) {
361 return str_tag(tstr
, r
->r_info
.user_tag
, r
->r_info
.share_mode
, r
->r_info
.protection
, r
->r_info
.external_pager
);
365 * Put two strings together separated by a '+' sign
366 * If the string gets too long, then add an ellipsis and
367 * stop concatenating it.
370 strconcat(const char *s0
, const char *s1
, size_t maxlen
)
372 const char ellipsis
[] = "...";
373 const char junction
[] = ", ";
374 const size_t s0len
= strlen(s0
);
375 size_t nmlen
= s0len
+ strlen(s1
) + strlen(junction
) + 1;
376 if (maxlen
> strlen(ellipsis
) && nmlen
> maxlen
) {
377 if (strcmp(s0
+ s0len
- strlen(ellipsis
), ellipsis
) == 0)
380 nmlen
= s0len
+ strlen(s1
) + strlen(junction
) + 1;
382 char *p
= malloc(nmlen
);
384 strlcpy(p
, s0
, nmlen
);
385 strlcat(p
, junction
, nmlen
);
386 strlcat(p
, s1
, nmlen
);
392 simple_namehash(const char *nm
)
394 unsigned long result
= 5381;
396 while (0 != (c
= *nm
++))
397 result
= (result
* 33) ^ c
;
398 return result
; /* modified djb2 */
402 bounded_pwrite(int fd
, const void *addr
, size_t size
, off_t off
, bool *nocache
, ssize_t
*nwrittenp
)
404 if (opt
->sizebound
&& off
+ (off_t
)size
> opt
->sizebound
)
407 bool oldnocache
= *nocache
;
408 if (size
>= opt
->ncthresh
&& !oldnocache
)
409 *nocache
= 0 == fcntl(fd
, F_NOCACHE
, 1);
410 else if (size
< opt
->ncthresh
&& oldnocache
)
411 *nocache
= 0 != fcntl(fd
, F_NOCACHE
, 0);
412 if (OPTIONS_DEBUG(opt
, 3) && oldnocache
^ *nocache
)
413 printf("F_NOCACHE now %sabled on fd %d\n", *nocache
? "en" : "dis", fd
);
415 const ssize_t nwritten
= pwrite(fd
, addr
, size
, off
);
419 *nwrittenp
= nwritten
;