blob: 11c21c6062e0787e86bcee181db40087edd20fee [file] [log] [blame]
Robert Swiecki0e673882016-03-31 18:50:29 +02001/*
2 *
3 * honggfuzz - routines dealing with subprocesses
4 * -----------------------------------------
5 *
6 * Author:
7 * Robert Swiecki <swiecki@google.com>
8 * Felix Gröbert <groebert@google.com>
9 *
10 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License. You may obtain
14 * a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
21 * implied. See the License for the specific language governing
22 * permissions and limitations under the License.
23 *
24 */
25
26#include "common.h"
27#include "subproc.h"
28
29#include <signal.h>
30#include <stdio.h>
31#include <string.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34
35#include "log.h"
36
37const char *subproc_StatusToStr(int status, char *str, size_t len)
38{
39 if (WIFEXITED(status)) {
40 snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status));
41 return str;
42 }
43
44 if (WIFSIGNALED(status)) {
45 snprintf(str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status),
46 strsignal(WTERMSIG(status)));
47 return str;
48 }
Robert Swiecki0e673882016-03-31 18:50:29 +020049 if (WIFCONTINUED(status)) {
50 snprintf(str, len, "CONTINUED");
51 return str;
52 }
53
54 if (!WIFSTOPPED(status)) {
55 snprintf(str, len, "UNKNOWN STATUS: %d", status);
56 return str;
57 }
58
59 /* Must be in a stopped state */
60 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
61 snprintf(str, len, "STOPPED (linux syscall): %d (%s)", WSTOPSIG(status),
62 strsignal(WSTOPSIG(status)));
63 return str;
64 }
Robert Swiecki2531d752016-04-19 14:00:17 +020065#if defined(PTRACE_EVENT_STOP)
Robert Swiecki0e673882016-03-31 18:50:29 +020066#define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16)
67 if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) {
68 switch (__LINUX_WPTRACEEVENT(status)) {
Robert Swiecki2531d752016-04-19 14:00:17 +020069 case PTRACE_EVENT_FORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020070 snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status),
71 strsignal(WSTOPSIG(status)));
72 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020073 case PTRACE_EVENT_VFORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020074 snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status),
75 strsignal(WSTOPSIG(status)));
76 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020077 case PTRACE_EVENT_CLONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020078 snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status),
79 strsignal(WSTOPSIG(status)));
80 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020081 case PTRACE_EVENT_EXEC:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020082 snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status),
83 strsignal(WSTOPSIG(status)));
84 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020085 case PTRACE_EVENT_VFORK_DONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020086 snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)",
87 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
88 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020089 case PTRACE_EVENT_EXIT:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020090 snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status),
91 strsignal(WSTOPSIG(status)));
92 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020093 case PTRACE_EVENT_SECCOMP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020094 snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status),
95 strsignal(WSTOPSIG(status)));
96 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020097 case PTRACE_EVENT_STOP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020098 snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki0e673882016-03-31 18:50:29 +020099 strsignal(WSTOPSIG(status)));
100 return str;
101 default:
102 snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)",
103 __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status)));
104 return str;
105 }
106 }
Robert Swiecki2531d752016-04-19 14:00:17 +0200107#endif /* defined(PTRACE_EVENT_STOP) */
Robert Swiecki0e673882016-03-31 18:50:29 +0200108
109 snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status),
110 strsignal(WSTOPSIG(status)));
111 return str;
112}