1 #if HAVE_CONFIG_H
2 #include "config.h"
3 #endif /* HAVE_CONFIG_H */
4
5 #define _GNU_SOURCE 1
6
7 #if HAVE_STDIO_H
8 #include <stdio.h>
9 #endif /* HAVE_STDIO_H */
10
11 #if HAVE_STDARG_H
12 #include <stdarg.h>
13 #endif /* HAVE_STDARG_H */
14
15 #if HAVE_STRING_H
16 #include <string.h>
17 #endif /* HAVE_STRING_H */
18
19 #if HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif /* HAVE_UNISTD_H */
22
23 #if HAVE_CTYPE_H
24 #include <ctype.h>
25 #endif
26
27 #if HAVE_ERRNO_H
28 #include <errno.h>
29 #endif /* HAVE_ERRNO_H */
30
31 #define CLOG_MAIN
32 #include "sys_util.h"
33
cgdb_calloc(size_t nmemb,size_t size)34 void *cgdb_calloc(size_t nmemb, size_t size)
35 {
36 void *t = calloc(nmemb, size);
37
38 if (t)
39 return t;
40
41 exit(-1);
42 }
43
cgdb_malloc(size_t size)44 void *cgdb_malloc(size_t size)
45 {
46 void *t = malloc(size);
47
48 if (t)
49 return t;
50
51 exit(-1);
52 }
53
cgdb_realloc(void * ptr,size_t size)54 void *cgdb_realloc(void *ptr, size_t size)
55 {
56 void *t = realloc(ptr, size);
57
58 if (t)
59 return t;
60
61 exit(-1);
62 }
63
cgdb_strdup(const char * s)64 char *cgdb_strdup(const char *s)
65 {
66 char *t = strdup(s);
67
68 if (t)
69 return t;
70
71 exit(-1);
72 }
73
cgdb_close(int fd)74 int cgdb_close(int fd)
75 {
76 int ret;
77
78 cgdb_close_start:
79 if ((ret = close(fd)) == -1 && errno == EINTR)
80 goto cgdb_close_start;
81 else if (ret == -1)
82 exit(-1);
83
84 return 0;
85 }
86
cgdb_string_to_int(const char * str,int * num)87 int cgdb_string_to_int(const char *str, int *num) {
88 int result = -1;
89
90 if (str && num) {
91 long int strtol_result;
92 char *end_ptr;
93
94 errno = 0;
95 strtol_result = strtol(str, &end_ptr, 10);
96 if (errno == 0 && str != end_ptr && *end_ptr == '\0') {
97 int convert_to_int_result = (int)strtol_result;
98 if (convert_to_int_result == strtol_result) {
99 *num = convert_to_int_result;
100 result = 0;
101 }
102 }
103 }
104
105 return result;
106 }
107
cgdb_hexstr_to_u64(const char * str,uint64_t * num)108 int cgdb_hexstr_to_u64(const char *str, uint64_t *num)
109 {
110 int result = -1;
111
112 if (str && num) {
113 uint64_t strtoull_result;
114 char *end_ptr;
115
116 errno = 0;
117 strtoull_result = strtoull(str, &end_ptr, 16);
118 if (errno == 0 && str != end_ptr &&
119 (*end_ptr == '\0' || *end_ptr == ' ')) {
120 *num = strtoull_result;
121 result = 0;
122 }
123 }
124
125 return result;
126 }
127
cgdb_supports_debugger_attach_detection()128 int cgdb_supports_debugger_attach_detection()
129 {
130 #ifdef HAVE_PROC_SELF_STATUS_FILE
131 return 1;
132 #else
133 return 0;
134 #endif
135 }
136
cgdb_is_debugger_attached()137 int cgdb_is_debugger_attached()
138 {
139 int result;
140 if (cgdb_supports_debugger_attach_detection()) {
141 int debugger_attached = 0;
142 static const char TracerPid[] = "TracerPid:";
143
144 FILE *fp = fopen("/proc/self/status", "r");
145 if ( fp ) {
146 ssize_t chars_read;
147 size_t line_len = 0;
148 char *line = NULL;
149
150 while ((chars_read = getline(&line, &line_len, fp)) != -1) {
151 char *tracer_pid = strstr(line, TracerPid);
152
153 if (tracer_pid) {
154 debugger_attached = !!atoi(tracer_pid + sizeof(TracerPid) - 1);
155 break;
156 }
157 }
158
159 free(line);
160 fclose(fp);
161 }
162
163 result = debugger_attached;
164 } else {
165 /* TODO: Implement for other platforms. */
166 result = -1;
167 }
168
169 return result;
170 }
171
get_file_size(FILE * file)172 long get_file_size(FILE *file)
173 {
174 if (fseek(file, 0, SEEK_END) != -1) {
175 long size;
176
177 size = ftell(file);
178 fseek(file, 0, SEEK_SET);
179
180 return size;
181 }
182
183 return -1;
184 }
185
log10_uint(unsigned int val)186 int log10_uint(unsigned int val)
187 {
188 if (val >= 1000000000u) return 9;
189 if (val >= 100000000u) return 8;
190 if (val >= 10000000u) return 7;
191 if (val >= 1000000u) return 6;
192 if (val >= 100000u) return 5;
193 if (val >= 10000u) return 4;
194 if (val >= 1000u) return 3;
195 if (val >= 100u) return 2;
196 if (val >= 10u) return 1;
197 return 0;
198 }
199
sys_aprintf(const char * fmt,...)200 char *sys_aprintf(const char *fmt, ...)
201 {
202 int n;
203 va_list ap;
204
205 va_start(ap, fmt);
206 n = vsnprintf(NULL, 0, fmt, ap) + 1;
207 va_end(ap);
208
209 if (n > 0 ) {
210 char *str = (char *)cgdb_malloc(n);
211
212 va_start(ap, fmt);
213 vsnprintf(str, n, fmt, ap);
214 va_end(ap);
215
216 return str;
217 }
218
219 return NULL;
220 }
221
sys_quote_nonprintables(const char * str,int len)222 std::string sys_quote_nonprintables(const char *str, int len)
223 {
224 int i;
225 std::string ret;
226
227 if (len == -1)
228 len = strlen(str);
229
230 for (i = 0; i < len; ++i)
231 {
232 const char *ch = NULL;
233
234 if (str[i] == '\r')
235 ch = "\\r";
236 else if (str[i] == '\n')
237 ch = "\\n";
238 else if (str[i] == '\032')
239 ch = "\\032";
240 else if (str[i] == '\033')
241 ch = "\\033";
242 else if (str[i] == '\b')
243 ch = "\\b";
244 else if (str[i] == '\t')
245 ch = "\\t";
246
247 if (ch) {
248 ret.push_back('(');
249 ret.append(ch);
250 ret.push_back(')');
251 } else
252 ret.push_back(str[i]);
253 }
254
255 return ret;
256 }
257