blob: 35581b9ac65dc2aedcb5b84ca76eae07ca22f2aa [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 }
65#define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16)
66 if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) {
67 switch (__LINUX_WPTRACEEVENT(status)) {
68 case 1: /* PTRACE_EVENT_FORK */
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020069 snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status),
70 strsignal(WSTOPSIG(status)));
71 return str;
72 case 2: /* PTRACE_EVENT_VFORK */
73 snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status),
74 strsignal(WSTOPSIG(status)));
75 return str;
76 case 3: /* PTRACE_EVENT_CLONE */
77 snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status),
78 strsignal(WSTOPSIG(status)));
79 return str;
80 case 4: /* PTRACE_EVENT_EXEC */
81 snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status),
82 strsignal(WSTOPSIG(status)));
83 return str;
84 case 5: /* PTRACE_EVENT_VFORK_DONE */
85 snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)",
86 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
87 return str;
88 case 6: /* PTRACE_EVENT_EXIT */
89 snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status),
90 strsignal(WSTOPSIG(status)));
91 return str;
92 case 7: /* PTRACE_EVENT_SECCOMP */
93 snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status),
94 strsignal(WSTOPSIG(status)));
95 return str;
96 case 128: /* PTRACE_EVENT_STOP */
97 snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki0e673882016-03-31 18:50:29 +020098 strsignal(WSTOPSIG(status)));
99 return str;
100 default:
101 snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)",
102 __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status)));
103 return str;
104 }
105 }
106
107 snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status),
108 strsignal(WSTOPSIG(status)));
109 return str;
110}