Browse Source

Makes the log function & macros variadic

Yann Weber 1 year ago
parent
commit
35a420d301
4 changed files with 58 additions and 38 deletions
  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 View File

33
 	if((res = malloc(sizeof(*res))) == NULL)
33
 	if((res = malloc(sizeof(*res))) == NULL)
34
 	{
34
 	{
35
 		err = errno;
35
 		err = errno;
36
-		perror("Unable to allocate env");
36
+		asmsh_log_perror("Unable to allocate env");
37
 		errno = err;
37
 		errno = err;
38
 		return NULL;
38
 		return NULL;
39
 	}
39
 	}
110
 		if(errno)
110
 		if(errno)
111
 		{
111
 		{
112
 			err = errno;
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
 			errno = err;
114
 			errno = err;
115
 			return -1;
115
 			return -1;
116
 		}
116
 		}
118
 		if(ptrace(PTRACE_POKETEXT, env->pid, wr_addr, data) < 0)
118
 		if(ptrace(PTRACE_POKETEXT, env->pid, wr_addr, data) < 0)
119
 		{
119
 		{
120
 			err = errno;
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
 			errno = err;
122
 			errno = err;
123
 			return -1;
123
 			return -1;
124
 		}
124
 		}
147
 		if(ptrace(PTRACE_POKETEXT, env->pid, env->code_write_ptr, data) < 0)
147
 		if(ptrace(PTRACE_POKETEXT, env->pid, env->code_write_ptr, data) < 0)
148
 		{
148
 		{
149
 			err = errno;
149
 			err = errno;
150
-			perror("Unable to poketext");
150
+			asmsh_log_perror("Unable to poketext");
151
 			errno = err;
151
 			errno = err;
152
 			return -1;
152
 			return -1;
153
 		}
153
 		}
174
 	if(ptrace(PTRACE_SINGLESTEP, env->pid, NULL, 0) < 0)
174
 	if(ptrace(PTRACE_SINGLESTEP, env->pid, NULL, 0) < 0)
175
 	{
175
 	{
176
 		err = errno;
176
 		err = errno;
177
-		perror("Unable to ptrace singlestep");
177
+		asmsh_log_perror("Unable to ptrace singlestep");
178
 		goto err;
178
 		goto err;
179
 	}
179
 	}
180
 	if(waitpid(env->pid, &env->status, 0) < 0)
180
 	if(waitpid(env->pid, &env->status, 0) < 0)
181
 	{
181
 	{
182
 		err = errno;
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
 		goto err;
184
 		goto err;
185
 	}
185
 	}
186
 	if(status) { *status = env->status; }
186
 	if(status) { *status = env->status; }
254
 	if(ptrace(PTRACE_GETREGS, asmenv->pid, NULL, &(asmenv->regs)) < 0)
254
 	if(ptrace(PTRACE_GETREGS, asmenv->pid, NULL, &(asmenv->regs)) < 0)
255
 	{
255
 	{
256
 		int err = errno;
256
 		int err = errno;
257
-		perror("ptrace getregs error");
257
+		asmsh_log_perror("ptrace getregs error");
258
 		errno = err;
258
 		errno = err;
259
 		return -1;
259
 		return -1;
260
 	}
260
 	}
277
 	if((env->pid = fork()) == -1)
277
 	if((env->pid = fork()) == -1)
278
 	{
278
 	{
279
 		err = errno;
279
 		err = errno;
280
-		perror("Unable to fork!");
280
+		asmsh_log_perror("Unable to fork!");
281
 		goto err_fork;
281
 		goto err_fork;
282
 	}
282
 	}
283
 	else if(env->pid == 0)
283
 	else if(env->pid == 0)
288
 	if(waitpid(env->pid, &wstatus, WUNTRACED) < 0)
288
 	if(waitpid(env->pid, &wstatus, WUNTRACED) < 0)
289
 	{
289
 	{
290
 		err=errno;
290
 		err=errno;
291
-		perror("Unable to wait for child process");
291
+		asmsh_log_perror("Unable to wait for child process");
292
 		goto err;
292
 		goto err;
293
 	}
293
 	}
294
 	if(!WIFSTOPPED(wstatus))
294
 	if(!WIFSTOPPED(wstatus))
300
 	if(ptrace(PTRACE_ATTACH, env->pid, 0, 0) == -1)
300
 	if(ptrace(PTRACE_ATTACH, env->pid, 0, 0) == -1)
301
 	{
301
 	{
302
 		err=errno;
302
 		err=errno;
303
-		perror("Unable to attach to child process");
303
+		asmsh_log_perror("Unable to attach to child process");
304
 		goto err;
304
 		goto err;
305
 	}
305
 	}
306
 
306
 
307
 	if(waitpid(env->pid, &wstatus, 0) < 0)
307
 	if(waitpid(env->pid, &wstatus, 0) < 0)
308
 	{
308
 	{
309
 		err=errno;
309
 		err=errno;
310
-		perror("Unable to wait for child process");
310
+		asmsh_log_perror("Unable to wait for child process");
311
 		goto err;
311
 		goto err;
312
 	}
312
 	}
313
 	if(!WIFSTOPPED(wstatus))
313
 	if(!WIFSTOPPED(wstatus))
321
 	if(ptrace(PTRACE_SETOPTIONS, env->pid, NULL, PTRACE_O_TRACEEXEC) < 0)
321
 	if(ptrace(PTRACE_SETOPTIONS, env->pid, NULL, PTRACE_O_TRACEEXEC) < 0)
322
 	{
322
 	{
323
 		err = errno;
323
 		err = errno;
324
-		perror("ptrace setoptions failed");
324
+		asmsh_log_perror("ptrace setoptions failed");
325
 		goto err;
325
 		goto err;
326
 	}
326
 	}
327
 
327
 
330
 		if(ptrace(PTRACE_CONT, env->pid, NULL, 0) < 0)
330
 		if(ptrace(PTRACE_CONT, env->pid, NULL, 0) < 0)
331
 		{
331
 		{
332
 			err = errno;
332
 			err = errno;
333
-			perror("ptrace CONT failed after attach");
333
+			asmsh_log_perror("ptrace CONT failed after attach");
334
 			goto err;
334
 			goto err;
335
 		}
335
 		}
336
 
336
 
337
 		if(waitpid(env->pid, &wstatus, 0) < 0)
337
 		if(waitpid(env->pid, &wstatus, 0) < 0)
338
 		{
338
 		{
339
 			err = errno;
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
 			goto err;
341
 			goto err;
342
 		}
342
 		}
343
 		if(wstatus >> 8 != (SIGTRAP | (PTRACE_EVENT_EXEC<<8)) && \
343
 		if(wstatus >> 8 != (SIGTRAP | (PTRACE_EVENT_EXEC<<8)) && \
353
 		if(ptrace(PTRACE_SYSCALL, env->pid, NULL, 0) < 0)
353
 		if(ptrace(PTRACE_SYSCALL, env->pid, NULL, 0) < 0)
354
 		{
354
 		{
355
 			err = errno;
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
 			goto err;
358
 			goto err;
359
 		}
359
 		}
360
 		if(waitpid(env->pid, &wstatus, 0) < 0)
360
 		if(waitpid(env->pid, &wstatus, 0) < 0)
361
 		{
361
 		{
362
 			err = errno;
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
 			goto err;
364
 			goto err;
365
 		}
365
 		}
366
 		if(wstatus != 1407 && wstatus != 263551)
366
 		if(wstatus != 1407 && wstatus != 263551)
437
 	{
437
 	{
438
 		int err = errno;
438
 		int err = errno;
439
 		perror("Unable to strdupa asmsh childpath :/");
439
 		perror("Unable to strdupa asmsh childpath :/");
440
+		//asmsh_log_perror("Unable to strdupa asmsh childpath :/");
441
+		///! @todo dump logs before exit !
440
 		exit(err?err:-1);
442
 		exit(err?err:-1);
441
 	}
443
 	}
442
 
444
 
444
 	execve(childpath, argv, envp);
446
 	execve(childpath, argv, envp);
445
 	int err = errno;
447
 	int err = errno;
446
 	perror("Unable to execve");
448
 	perror("Unable to execve");
449
+	//asmsh_log_perror("Child is unable to execve");
450
+	///! @todo dump logs before exit !
447
 	exit(err?err:-1);
451
 	exit(err?err:-1);
448
 }
452
 }
449
 
453
 
450
 static char *asmsh_env_tmpexec()
454
 static char *asmsh_env_tmpexec()
451
 {
455
 {
452
 	char *ret = strdup("asmsh_child_XXXXXXXXX");
456
 	char *ret = strdup("asmsh_child_XXXXXXXXX");
457
+	int err;
453
 	if(!ret)
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
 		return NULL;
463
 		return NULL;
457
 	}
464
 	}
458
 	int tmpfd = mkstemp(ret);
465
 	int tmpfd = mkstemp(ret);
459
 	if(tmpfd < 0)
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
 		return NULL;
471
 		return NULL;
463
 	}
472
 	}
464
 	const int sz = &_binary_child_end - &_binary_child_start;
473
 	const int sz = &_binary_child_end - &_binary_child_start;
465
 	int rsz = write(tmpfd, &_binary_child_start, sz);
474
 	int rsz = write(tmpfd, &_binary_child_start, sz);
466
 	if(rsz<sz)
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
 		free(ret);
480
 		free(ret);
481
+		errno = err;
470
 		return NULL;
482
 		return NULL;
471
 	}
483
 	}
472
 	fchmod(tmpfd, 0555);
484
 	fchmod(tmpfd, 0555);

+ 1
- 1
asmsh.c View File

39
 
39
 
40
 	if(asmsh_init(&sh, CHILD_EXEC_PATH))
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
 		asmsh_logger_dprint(2, logger);
43
 		asmsh_logger_dprint(2, logger);
44
 		return 1;
44
 		return 1;
45
 	}
45
 	}

+ 9
- 4
logger.c View File

156
 }
156
 }
157
 
157
 
158
 int asmsh_log(asmsh_logger_t *logger, asmsh_loglevel_t lvl, const char caller[],
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
 	if(!logger)
161
 	if(!logger)
162
 	{
162
 	{
164
 	}
164
 	}
165
 	if(lvl < logger->min_level) { return 0; }
165
 	if(lvl < logger->min_level) { return 0; }
166
 
166
 
167
+	va_list ap;
167
 	size_t caller_len, msg_len, total_len;
168
 	size_t caller_len, msg_len, total_len;
168
 
169
 
169
 	caller_len = strlen(caller);
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
 	if(logger->msgs_alloc <= logger->msgs_sz + total_len)
176
 	if(logger->msgs_alloc <= logger->msgs_sz + total_len)
174
 	{
177
 	{
205
 	logger->nxt->msg = logger->nxt->caller + caller_len + 1;
208
 	logger->nxt->msg = logger->nxt->caller + caller_len + 1;
206
 
209
 
207
 	strncpy(logger->nxt->caller, caller, caller_len+1);
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
 	logger->nxt->nxt = (asmsh_log_msg_t*)((char*)logger->nxt->msg+total_len);
215
 	logger->nxt->nxt = (asmsh_log_msg_t*)((char*)logger->nxt->msg+total_len);
211
 	logger->nxt = logger->nxt->nxt;
216
 	logger->nxt = logger->nxt->nxt;

+ 16
- 13
logger.h View File

25
 #include <stdio.h>
25
 #include <stdio.h>
26
 #include <stdlib.h>
26
 #include <stdlib.h>
27
 #include <string.h>
27
 #include <string.h>
28
+#include <stdarg.h>
28
 #include <time.h>
29
 #include <time.h>
29
 
30
 
30
 ///! TODO varargs macro & log functions, perror etc.
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
 #define ASMSH_LOG_BUFFER_ALLOC 4096
47
 #define ASMSH_LOG_BUFFER_ALLOC 4096
45
 
48
 
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
 const char * asmsh_loglevel_name(asmsh_loglevel_t lvl);
135
 const char * asmsh_loglevel_name(asmsh_loglevel_t lvl);

Loading…
Cancel
Save