Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | |
| 6 | #include <ctype.h> |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
| 10 | #include "util.h" |
| 11 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 12 | #include "libconstants.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 13 | #include "libsyscalls.h" |
| 14 | |
Mike Frysinger | fccb4c9 | 2013-10-19 02:42:07 -0400 | [diff] [blame] | 15 | /* |
| 16 | * These are syscalls used by the syslog() C library call. You can find them |
| 17 | * by running a simple test program. See below for x86_64 behavior: |
| 18 | * $ cat test.c |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 19 | * #include <syslog.h> |
Mike Frysinger | fccb4c9 | 2013-10-19 02:42:07 -0400 | [diff] [blame] | 20 | * main() { syslog(0, "foo"); } |
| 21 | * $ gcc test.c -static |
| 22 | * $ strace ./a.out |
| 23 | * ... |
| 24 | * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection |
| 25 | * connect(...) <- important |
| 26 | * sendto(...) <- important |
| 27 | * exit_group(0) <- finish! |
| 28 | */ |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 29 | #if defined(__x86_64__) |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 30 | #if defined(__ANDROID__) |
| 31 | const char *log_syscalls[] = {"socket", "connect", "fcntl", "writev"}; |
Alex Deymo | 7c6899c | 2016-01-27 18:24:19 -0800 | [diff] [blame] | 32 | #else |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 33 | const char *log_syscalls[] = {"connect", "sendto"}; |
| 34 | #endif |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 35 | #elif defined(__i386__) |
Jeff Vander Stoep | 1482f20 | 2016-01-07 11:21:31 -0800 | [diff] [blame] | 36 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 37 | const char *log_syscalls[] = {"socketcall", "writev", "fcntl64", |
| 38 | "clock_gettime"}; |
Jeff Vander Stoep | 1482f20 | 2016-01-07 11:21:31 -0800 | [diff] [blame] | 39 | #else |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 40 | const char *log_syscalls[] = {"socketcall", "time"}; |
Jeff Vander Stoep | 1482f20 | 2016-01-07 11:21:31 -0800 | [diff] [blame] | 41 | #endif |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 42 | #elif defined(__arm__) |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 43 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 44 | const char *log_syscalls[] = {"clock_gettime", "connect", "fcntl64", "socket", |
| 45 | "writev"}; |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 46 | #else |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 47 | const char *log_syscalls[] = {"connect", "gettimeofday", "send"}; |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 48 | #endif |
| 49 | #elif defined(__aarch64__) |
| 50 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 51 | const char *log_syscalls[] = {"connect", "fcntl", "sendto", "socket", "writev"}; |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 52 | #else |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 53 | const char *log_syscalls[] = {"connect", "send"}; |
Jeff Vander Stoep | d38f399 | 2015-12-02 10:54:51 -0800 | [diff] [blame] | 54 | #endif |
Jorge Lucangeli Obes | b98ad29 | 2016-01-25 15:07:30 -0800 | [diff] [blame] | 55 | #elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \ |
| 56 | defined(__sparc__) || defined(__mips__) |
| 57 | const char *log_syscalls[] = {"connect", "send"}; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 58 | #else |
| 59 | #error "Unsupported platform" |
| 60 | #endif |
| 61 | |
| 62 | const size_t log_syscalls_len = sizeof(log_syscalls)/sizeof(log_syscalls[0]); |
| 63 | |
Luis Hector Chavez | 2122455 | 2015-06-27 18:10:39 +0000 | [diff] [blame] | 64 | long int parse_single_constant(char *constant_str, char **endptr); |
| 65 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 66 | int lookup_syscall(const char *name) |
| 67 | { |
| 68 | const struct syscall_entry *entry = syscall_table; |
| 69 | for (; entry->name && entry->nr >= 0; ++entry) |
| 70 | if (!strcmp(entry->name, name)) |
| 71 | return entry->nr; |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | const char *lookup_syscall_name(int nr) |
| 76 | { |
| 77 | const struct syscall_entry *entry = syscall_table; |
| 78 | for (; entry->name && entry->nr >= 0; ++entry) |
| 79 | if (entry->nr == nr) |
| 80 | return entry->name; |
| 81 | return NULL; |
| 82 | } |
| 83 | |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 84 | long int parse_constant(char *constant_str, char **endptr) |
| 85 | { |
Luis Hector Chavez | 2122455 | 2015-06-27 18:10:39 +0000 | [diff] [blame] | 86 | long int value = 0; |
| 87 | char *group, *lastpos = constant_str; |
| 88 | char *original_constant_str = constant_str; |
| 89 | |
| 90 | /* |
| 91 | * Try to parse constants separated by pipes. Note that since |
| 92 | * |constant_str| is an atom, there can be no spaces between the |
| 93 | * constant and the pipe. Constants can be either a named constant |
| 94 | * defined in libconstants.gen.c or a number parsed with strtol. |
| 95 | * |
| 96 | * If there is an error parsing any of the constants, the whole process |
| 97 | * fails. |
| 98 | */ |
| 99 | while ((group = tokenize(&constant_str, "|")) != NULL) { |
| 100 | char *end = group; |
| 101 | value |= parse_single_constant(group, &end); |
| 102 | if (end == group) { |
| 103 | lastpos = original_constant_str; |
| 104 | value = 0; |
| 105 | break; |
| 106 | } |
| 107 | lastpos = end; |
| 108 | } |
| 109 | if (endptr) |
| 110 | *endptr = lastpos; |
| 111 | return value; |
| 112 | } |
| 113 | |
| 114 | long int parse_single_constant(char *constant_str, char **endptr) |
| 115 | { |
Luis Hector Chavez | 40b2574 | 2013-09-22 19:44:06 -0700 | [diff] [blame] | 116 | const struct constant_entry *entry = constant_table; |
| 117 | for (; entry->name; ++entry) { |
| 118 | if (!strcmp(entry->name, constant_str)) { |
| 119 | if (endptr) |
| 120 | *endptr = constant_str + strlen(constant_str); |
| 121 | |
| 122 | return entry->value; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return strtol(constant_str, endptr, 0); |
| 127 | } |
| 128 | |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 129 | char *strip(char *s) |
| 130 | { |
| 131 | char *end; |
| 132 | while (*s && isblank(*s)) |
| 133 | s++; |
| 134 | end = s + strlen(s) - 1; |
| 135 | while (end >= s && *end && (isblank(*end) || *end == '\n')) |
| 136 | end--; |
| 137 | *(end + 1) = '\0'; |
| 138 | return s; |
| 139 | } |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 140 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 141 | char *tokenize(char **stringp, const char *delim) |
| 142 | { |
Jorge Lucangeli Obes | 66cfc14 | 2012-11-30 15:42:52 -0800 | [diff] [blame] | 143 | char *ret = NULL; |
| 144 | |
| 145 | /* If the string is NULL or empty, there are no tokens to be found. */ |
| 146 | if (stringp == NULL || *stringp == NULL || **stringp == '\0') |
| 147 | return NULL; |
| 148 | |
| 149 | /* |
| 150 | * If the delimiter is NULL or empty, |
| 151 | * the full string makes up the only token. |
| 152 | */ |
| 153 | if (delim == NULL || *delim == '\0') { |
| 154 | ret = *stringp; |
| 155 | *stringp = NULL; |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | char *found; |
| 160 | while (**stringp != '\0') { |
| 161 | found = strstr(*stringp, delim); |
| 162 | |
| 163 | if (!found) { |
| 164 | /* |
| 165 | * The delimiter was not found, so the full string |
| 166 | * makes up the only token, and we're done. |
| 167 | */ |
| 168 | ret = *stringp; |
| 169 | *stringp = NULL; |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | if (found != *stringp) { |
| 174 | /* There's a non-empty token before the delimiter. */ |
| 175 | *found = '\0'; |
| 176 | ret = *stringp; |
| 177 | *stringp = found + strlen(delim); |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * The delimiter was found at the start of the string, |
| 183 | * skip it and keep looking for a non-empty token. |
| 184 | */ |
| 185 | *stringp += strlen(delim); |
| 186 | } |
| 187 | |
| 188 | return ret; |
| 189 | } |