]>
Commit | Line | Data |
---|---|---|
cf37c299 A |
1 | /* |
2 | * Copyright (c) 2015 Apple Inc. All rights reserved. | |
3 | */ | |
4 | ||
5 | #include "options.h" | |
6 | #include "utils.h" | |
7 | #include "region.h" | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <string.h> | |
11 | #include <strings.h> | |
12 | #include <stdlib.h> | |
13 | #include <stdarg.h> | |
14 | #include <unistd.h> | |
15 | #include <libutil.h> | |
16 | ||
17 | void | |
18 | err_mach(kern_return_t kr, const struct region *r, const char *fmt, ...) | |
19 | { | |
20 | va_list ap; | |
21 | va_start(ap, fmt); | |
22 | if (0 != kr) | |
23 | printf("%s: ", pgm); | |
24 | if (NULL != r) | |
25 | printf("%llx-%llx ", R_ADDR(r), R_ENDADDR(r)); | |
26 | vprintf(fmt, ap); | |
27 | va_end(ap); | |
28 | ||
29 | if (0 != kr) { | |
30 | printf(": %s (%x)", mach_error_string(kr), kr); | |
31 | switch (err_get_system(kr)) { | |
32 | case err_get_system(err_mach_ipc): | |
33 | /* 0x10000000 == (4 << 26) */ | |
34 | printf(" => fatal\n"); | |
35 | exit(127); | |
36 | default: | |
37 | putchar('\n'); | |
38 | break; | |
39 | } | |
40 | } else | |
41 | putchar('\n'); | |
42 | } | |
43 | ||
44 | void | |
45 | printr(const struct region *r, const char *fmt, ...) | |
46 | { | |
47 | va_list ap; | |
48 | va_start(ap, fmt); | |
49 | if (NULL != r) | |
50 | printf("%llx-%llx ", R_ADDR(r), R_ENDADDR(r)); | |
51 | vfprintf(stdout, fmt, ap); | |
52 | va_end(ap); | |
53 | } | |
54 | ||
55 | /* | |
56 | * Print power-of-1024 sizes in human-readable form | |
57 | */ | |
58 | const char * | |
59 | str_hsize(hsize_str_t hstr, uint64_t size) | |
60 | { | |
61 | humanize_number(hstr, sizeof (hsize_str_t) - 1, size, "", | |
62 | HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL | HN_IEC_PREFIXES); | |
63 | return hstr; | |
64 | } | |
65 | ||
66 | /* | |
67 | * Put two strings together separated by a '+' sign | |
68 | * If the string gets too long, then add an elipsis and | |
69 | * stop concatenating it. | |
70 | */ | |
71 | char * | |
72 | strconcat(const char *s0, const char *s1, size_t maxlen) | |
73 | { | |
74 | const char ellipsis[] = "..."; | |
75 | const char junction[] = ", "; | |
76 | const size_t s0len = strlen(s0); | |
77 | size_t nmlen = s0len + strlen(s1) + strlen(junction) + 1; | |
78 | if (maxlen > strlen(ellipsis) && nmlen > maxlen) { | |
79 | if (strcmp(s0 + s0len - strlen(ellipsis), ellipsis) == 0) | |
80 | return strdup(s0); | |
81 | s1 = ellipsis; | |
82 | nmlen = s0len + strlen(s1) + strlen(junction) + 1; | |
83 | } | |
84 | char *p = malloc(nmlen); | |
85 | if (p) { | |
86 | strlcpy(p, s0, nmlen); | |
87 | strlcat(p, junction, nmlen); | |
88 | strlcat(p, s1, nmlen); | |
89 | } | |
90 | return p; | |
91 | } |