Ejemplo n.º 1
0
/*
 * Document-method: PinkTrace::StringArray.decode
 * call-seq:
 *   PinkTrace::StringArray.decode(pid, arg, index, [[maxlen=-1], [bitness=PinkTrace::Bitness::DEFAULT]]) -> String or nil
 *
 * This function decodes the member of the string array pointed by the address
 * +arg+. The +index+ argument specifies the index of the member in the string
 * array.
 *
 * Note: If the string array member was NULL, this function returns nil.
 */
VALUE
pinkrb_decode_strarray(int argc, VALUE *argv, VALUE mod)
{
    bool nil;
    pid_t pid;
    unsigned bit, ind;
    long arg;
    int maxlen;
    char *str;
    VALUE vpid, varg, vind, vmax, vbit, vret;

    switch (rb_scan_args(argc, argv, "32", &vpid, &varg, &vind, &vmax, &vbit)) {
    case 3:
        maxlen = -1;
        bit = PINKTRACE_BITNESS_DEFAULT;
        break;
    case 4:
        maxlen = NUM2INT(vmax);
        bit = PINKTRACE_BITNESS_DEFAULT;
        break;
    case 5:
        maxlen = NUM2INT(vmax);
        bit = FIX2UINT(vbit);
        break;
    default:
        abort();
    }
    pid = NUM2PIDT(vpid);
    arg = NUM2LONG(varg);
    ind = FIX2UINT(vind);

    if (maxlen < 0) {
        /* Use pink_decode_string_array_member_persistent() */
        errno = 0;
        str = pink_decode_string_array_member_persistent(pid, bit, arg, ind);
        if (!str) {
            if (errno)
                rb_sys_fail("pink_decode_string_array_member_persistent()");
            return Qnil;
        }

        vret = rb_str_new2(str);
        free(str);
        return vret;
    }

    /* Use pink_decode_string_array_member() */
    str = ALLOC_N(char, maxlen);
    if (!pink_decode_string_array_member(pid, bit, arg, ind, str, maxlen, &nil))
        rb_sys_fail("pink_decode_string_array_member()");
    if (nil) {
        free(str);
        return Qnil;
    }

    vret = rb_str_new2(str);
    if (str)
        free(str);
    return vret;
}
/* A very basic decoder for execve(2) system call. */
static void
decode_execve(pid_t pid, pink_bitness_t bitness)
{
	bool nil;
	unsigned i;
	long arg;
	char buf[MAX_STRING_LEN];
	const char *sep;

	if (!pink_decode_string(pid, bitness, 0, buf, MAX_STRING_LEN)) {
		perror("pink_decode_string");
		return;
	}
	if (!pink_util_get_arg(pid, bitness, 1, &arg)) {
		perror("pink_util_get_arg");
		return;
	}

	printf("execve(\"%s\", [", buf);

	for (i = 0, nil = false, sep = "";;sep = ", ") {
		if (!pink_decode_string_array_member(pid, bitness, arg, ++i, buf, MAX_STRING_LEN, &nil)) {
			perror("pink_decode_string_array_member");
			return;
		}

		printf("%s\"%s\"", sep, buf);

		if (nil) {
			printf("], envp[])");
			break;
		}
	}
}