Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 37 | const 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 Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 49 | 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 Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 65 | #if defined(PTRACE_EVENT_STOP) |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 66 | #define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16) |
| 67 | if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) { |
| 68 | switch (__LINUX_WPTRACEEVENT(status)) { |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 69 | case PTRACE_EVENT_FORK: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 70 | snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status), |
| 71 | strsignal(WSTOPSIG(status))); |
| 72 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 73 | case PTRACE_EVENT_VFORK: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 74 | snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status), |
| 75 | strsignal(WSTOPSIG(status))); |
| 76 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 77 | case PTRACE_EVENT_CLONE: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 78 | snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status), |
| 79 | strsignal(WSTOPSIG(status))); |
| 80 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 81 | case PTRACE_EVENT_EXEC: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 82 | snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status), |
| 83 | strsignal(WSTOPSIG(status))); |
| 84 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 85 | case PTRACE_EVENT_VFORK_DONE: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 86 | snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)", |
| 87 | WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
| 88 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 89 | case PTRACE_EVENT_EXIT: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 90 | snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status), |
| 91 | strsignal(WSTOPSIG(status))); |
| 92 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 93 | case PTRACE_EVENT_SECCOMP: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 94 | snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status), |
| 95 | strsignal(WSTOPSIG(status))); |
| 96 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 97 | case PTRACE_EVENT_STOP: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 98 | snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status), |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 99 | 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 Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 107 | #endif /* defined(PTRACE_EVENT_STOP) */ |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 108 | |
| 109 | snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), |
| 110 | strsignal(WSTOPSIG(status))); |
| 111 | return str; |
| 112 | } |