ソースを参照

Makes the log function & macros variadic

Yann Weber 1年前
コミット
35a420d301
4個のファイルの変更58行の追加38行の削除
  1. 32
    20
      asm_env.c
  2. 1
    1
      asmsh.c
  3. 9
    4
      logger.c
  4. 16
    13
      logger.h

+ 32
- 20
asm_env.c ファイルの表示

@@ -33,7 +33,7 @@ asmsh_env_t* asmsh_env(const char *childpath)
33 33
 	if((res = malloc(sizeof(*res))) == NULL)
34 34
 	{
35 35
 		err = errno;
36
-		perror("Unable to allocate env");
36
+		asmsh_log_perror("Unable to allocate env");
37 37
 		errno = err;
38 38
 		return NULL;
39 39
 	}
@@ -110,7 +110,7 @@ int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf,
110 110
 		if(errno)
111 111
 		{
112 112
 			err = errno;
113
-			perror("Unable to peektext in order to allign write");
113
+			asmsh_log_perror("Unable to peektext in order to allign write");
114 114
 			errno = err;
115 115
 			return -1;
116 116
 		}
@@ -118,7 +118,7 @@ int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf,
118 118
 		if(ptrace(PTRACE_POKETEXT, env->pid, wr_addr, data) < 0)
119 119
 		{
120 120
 			err = errno;
121
-			perror("Unable to poketext in order to allign write");
121
+			asmsh_log_perror("Unable to poketext in order to allign write");
122 122
 			errno = err;
123 123
 			return -1;
124 124
 		}
@@ -147,7 +147,7 @@ int asmsh_env_write_mem(asmsh_env_t *env, void *addr, const unsigned char *buf,
147 147
 		if(ptrace(PTRACE_POKETEXT, env->pid, env->code_write_ptr, data) < 0)
148 148
 		{
149 149
 			err = errno;
150
-			perror("Unable to poketext");
150
+			asmsh_log_perror("Unable to poketext");
151 151
 			errno = err;
152 152
 			return -1;
153 153
 		}
@@ -174,13 +174,13 @@ int asmsh_env_step(asmsh_env_t *env, int *status)
174 174
 	if(ptrace(PTRACE_SINGLESTEP, env->pid, NULL, 0) < 0)
175 175
 	{
176 176
 		err = errno;
177
-		perror("Unable to ptrace singlestep");
177
+		asmsh_log_perror("Unable to ptrace singlestep");
178 178
 		goto err;
179 179
 	}
180 180
 	if(waitpid(env->pid, &env->status, 0) < 0)
181 181
 	{
182 182
 		err = errno;
183
-		perror("Unable to wait for child process to stop on step");
183
+		asmsh_log_perror("Unable to wait for child process to stop on step");
184 184
 		goto err;
185 185
 	}
186 186
 	if(status) { *status = env->status; }
@@ -254,7 +254,7 @@ int asmsh_env_update_regs(asmsh_env_t *asmenv)
254 254
 	if(ptrace(PTRACE_GETREGS, asmenv->pid, NULL, &(asmenv->regs)) < 0)
255 255
 	{
256 256
 		int err = errno;
257
-		perror("ptrace getregs error");
257
+		asmsh_log_perror("ptrace getregs error");
258 258
 		errno = err;
259 259
 		return -1;
260 260
 	}
@@ -277,7 +277,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
277 277
 	if((env->pid = fork()) == -1)
278 278
 	{
279 279
 		err = errno;
280
-		perror("Unable to fork!");
280
+		asmsh_log_perror("Unable to fork!");
281 281
 		goto err_fork;
282 282
 	}
283 283
 	else if(env->pid == 0)
@@ -288,7 +288,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
288 288
 	if(waitpid(env->pid, &wstatus, WUNTRACED) < 0)
289 289
 	{
290 290
 		err=errno;
291
-		perror("Unable to wait for child process");
291
+		asmsh_log_perror("Unable to wait for child process");
292 292
 		goto err;
293 293
 	}
294 294
 	if(!WIFSTOPPED(wstatus))
@@ -300,14 +300,14 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
300 300
 	if(ptrace(PTRACE_ATTACH, env->pid, 0, 0) == -1)
301 301
 	{
302 302
 		err=errno;
303
-		perror("Unable to attach to child process");
303
+		asmsh_log_perror("Unable to attach to child process");
304 304
 		goto err;
305 305
 	}
306 306
 
307 307
 	if(waitpid(env->pid, &wstatus, 0) < 0)
308 308
 	{
309 309
 		err=errno;
310
-		perror("Unable to wait for child process");
310
+		asmsh_log_perror("Unable to wait for child process");
311 311
 		goto err;
312 312
 	}
313 313
 	if(!WIFSTOPPED(wstatus))
@@ -321,7 +321,7 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
321 321
 	if(ptrace(PTRACE_SETOPTIONS, env->pid, NULL, PTRACE_O_TRACEEXEC) < 0)
322 322
 	{
323 323
 		err = errno;
324
-		perror("ptrace setoptions failed");
324
+		asmsh_log_perror("ptrace setoptions failed");
325 325
 		goto err;
326 326
 	}
327 327
 
@@ -330,14 +330,14 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
330 330
 		if(ptrace(PTRACE_CONT, env->pid, NULL, 0) < 0)
331 331
 		{
332 332
 			err = errno;
333
-			perror("ptrace CONT failed after attach");
333
+			asmsh_log_perror("ptrace CONT failed after attach");
334 334
 			goto err;
335 335
 		}
336 336
 
337 337
 		if(waitpid(env->pid, &wstatus, 0) < 0)
338 338
 		{
339 339
 			err = errno;
340
-			perror("Unable to wait for child process to stop after exec");
340
+			asmsh_log_perror("Unable to wait for child process to stop after exec");
341 341
 			goto err;
342 342
 		}
343 343
 		if(wstatus >> 8 != (SIGTRAP | (PTRACE_EVENT_EXEC<<8)) && \
@@ -353,14 +353,14 @@ static int _asmsh_env_spawn(asmsh_env_t *env)
353 353
 		if(ptrace(PTRACE_SYSCALL, env->pid, NULL, 0) < 0)
354 354
 		{
355 355
 			err = errno;
356
-			perror("Unable to ptrace syscall");
357
-			dprintf(2, "ptrace syscall failed on %d time\n", i+1);
356
+			asmsh_log_error("Unable to ptrace syscall on %dth time : %s",
357
+					i+1, strerror(errno));
358 358
 			goto err;
359 359
 		}
360 360
 		if(waitpid(env->pid, &wstatus, 0) < 0)
361 361
 		{
362 362
 			err = errno;
363
-			perror("Unable to wait for child process to stop on syscall");
363
+			asmsh_log_perror("Unable to wait for child process to stop on syscall");
364 364
 			goto err;
365 365
 		}
366 366
 		if(wstatus != 1407 && wstatus != 263551)
@@ -437,6 +437,8 @@ static void _asmsh_env_child(const char *childpath)
437 437
 	{
438 438
 		int err = errno;
439 439
 		perror("Unable to strdupa asmsh childpath :/");
440
+		//asmsh_log_perror("Unable to strdupa asmsh childpath :/");
441
+		///! @todo dump logs before exit !
440 442
 		exit(err?err:-1);
441 443
 	}
442 444
 
@@ -444,29 +446,39 @@ static void _asmsh_env_child(const char *childpath)
444 446
 	execve(childpath, argv, envp);
445 447
 	int err = errno;
446 448
 	perror("Unable to execve");
449
+	//asmsh_log_perror("Child is unable to execve");
450
+	///! @todo dump logs before exit !
447 451
 	exit(err?err:-1);
448 452
 }
449 453
 
450 454
 static char *asmsh_env_tmpexec()
451 455
 {
452 456
 	char *ret = strdup("asmsh_child_XXXXXXXXX");
457
+	int err;
453 458
 	if(!ret)
454 459
 	{
455
-		perror("getting a temporary file name");
460
+		err = errno;
461
+		asmsh_log_perror("getting a temporary file name");
462
+		errno = err;
456 463
 		return NULL;
457 464
 	}
458 465
 	int tmpfd = mkstemp(ret);
459 466
 	if(tmpfd < 0)
460 467
 	{
461
-		perror("Unable to mk temporary file");
468
+		err = errno;
469
+		asmsh_log_perror("Unable to mk temporary file");
470
+		errno = err;
462 471
 		return NULL;
463 472
 	}
464 473
 	const int sz = &_binary_child_end - &_binary_child_start;
465 474
 	int rsz = write(tmpfd, &_binary_child_start, sz);
466 475
 	if(rsz<sz)
467 476
 	{
468
-		perror("Unable to write the child executable");
477
+		// TODO : differenciate incomplete write & error !
478
+		err = errno;
479
+		asmsh_log_perror("Unable to write the child executable");
469 480
 		free(ret);
481
+		errno = err;
470 482
 		return NULL;
471 483
 	}
472 484
 	fchmod(tmpfd, 0555);

+ 1
- 1
asmsh.c ファイルの表示

@@ -39,7 +39,7 @@ int main(int argc, char *argv[], char *envp[])
39 39
 
40 40
 	if(asmsh_init(&sh, CHILD_EXEC_PATH))
41 41
 	{
42
-		perror("Unable to init shell");
42
+		asmsh_log_error("Unable to init shell : %s", strerror(errno));
43 43
 		asmsh_logger_dprint(2, logger);
44 44
 		return 1;
45 45
 	}

+ 9
- 4
logger.c ファイルの表示

@@ -156,7 +156,7 @@ int asmsh_logger_strdup(asmsh_logger_t *logger, char **result, size_t *res_sz)
156 156
 }
157 157
 
158 158
 int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[],
159
-		const char *msg)
159
+		const char *msg, ...)
160 160
 {
161 161
 	if(!logger)
162 162
 	{
@@ -164,11 +164,14 @@ int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[],
164 164
 	}
165 165
 	if(lvl < logger->min_level) { return 0; }
166 166
 
167
+	va_list ap;
167 168
 	size_t caller_len, msg_len, total_len;
168 169
 
169 170
 	caller_len = strlen(caller);
170
-	msg_len = strlen(msg);
171
-	total_len = msg_len  + caller_len + 2 + sizeof(asmsh_log_msg_t);
171
+	va_start(ap, msg);
172
+	msg_len = vsnprintf(NULL, 0, msg, ap) + 1;
173
+	va_end(ap);
174
+	total_len = msg_len  + caller_len + 1 + sizeof(asmsh_log_msg_t);
172 175
 
173 176
 	if(logger->msgs_alloc <= logger->msgs_sz + total_len)
174 177
 	{
@@ -205,7 +208,9 @@ int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[],
205 208
 	logger->nxt->msg = logger->nxt->caller + caller_len + 1;
206 209
 
207 210
 	strncpy(logger->nxt->caller, caller, caller_len+1);
208
-	strncpy(logger->nxt->msg, msg, msg_len+1);
211
+	va_start(ap, msg);
212
+	vsnprintf(logger->nxt->msg, msg_len, msg, ap);
213
+	va_end(ap);
209 214
 
210 215
 	logger->nxt->nxt = (asmsh_log_msg_t*)((char*)logger->nxt->msg+total_len);
211 216
 	logger->nxt = logger->nxt->nxt;

+ 16
- 13
logger.h ファイルの表示

@@ -25,21 +25,24 @@
25 25
 #include <stdio.h>
26 26
 #include <stdlib.h>
27 27
 #include <string.h>
28
+#include <stdarg.h>
28 29
 #include <time.h>
29 30
 
30 31
 ///! TODO varargs macro & log functions, perror etc.
31
-#define asmsh_log_trace(msg) \
32
-	asmsh_log(_default_logger, ASMSH_TRACE, __FUNCTION__, msg)
33
-#define asmsh_log_debug(msg) \
34
-	asmsh_log(_default_logger, ASMSH_DEBUG, __FUNCTION__, msg)
35
-#define asmsh_log_info(msg) \
36
-	asmsh_log(_default_logger, ASMSH_INFO, __FUNCTION__, msg)
37
-#define asmsh_log_warning(msg) \
38
-	asmsh_log(_default_logger, ASMSH_WARN, __FUNCTION__, msg)
39
-#define asmsh_log_error(msg) \
40
-	asmsh_log(_default_logger, ASMSH_ERR, __FUNCTION__, msg)
41
-#define asmsh_log_fatal(msg) \
42
-	asmsh_log(_default_logger, ASMSH_FATAL, __FUNCTION__, msg)
32
+#define asmsh_log_trace(msg_args...) \
33
+	asmsh_log(_default_logger, ASMSH_TRACE, __FUNCTION__, msg_args)
34
+#define asmsh_log_debug(msg_args...) \
35
+	asmsh_log(_default_logger, ASMSH_DEBUG, __FUNCTION__, msg_args)
36
+#define asmsh_log_info(msg_args...) \
37
+	asmsh_log(_default_logger, ASMSH_INFO, __FUNCTION__, msg_args)
38
+#define asmsh_log_warning(msg_args...) \
39
+	asmsh_log(_default_logger, ASMSH_WARN, __FUNCTION__, msg_args)
40
+#define asmsh_log_error(msg_args...) \
41
+	asmsh_log(_default_logger, ASMSH_ERR, __FUNCTION__, msg_args)
42
+#define asmsh_log_fatal(msg_args...) \
43
+	asmsh_log(_default_logger, ASMSH_FATAL, __FUNCTION__, msg_args)
44
+#define asmsh_log_perror(msg) \
45
+	asmsh_log_error("%s : %s", msg, strerror(errno));
43 46
 
44 47
 #define ASMSH_LOG_BUFFER_ALLOC 4096
45 48
 
@@ -126,7 +129,7 @@ static inline int asmsh_logger_stderr(asmsh_logger_t *logger) {
126 129
 }
127 130
 
128 131
 
129
-int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[], const char *msg);
132
+int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[], const char *msg, ...);
130 133
 
131 134
 
132 135
 const char * asmsh_loglevel_name(asmsh_loglevel_t lvl);

読み込み中…
キャンセル
保存