/* Reads in an unsigned 16-bit integer. */
u_int16_t read_16(FILE *classfile)
{
	int b1, b2;
	b1 = fgetc(classfile);
	if(b1 == EOF)
		eof_error();
	b2 = fgetc(classfile);
	if(b2 == EOF)
		eof_error();
	return (u_int16_t)((b1 << 8) | b2);
}
/* Reads in an unsigned 8-bit integer. */
u_int8_t read_8(FILE *classfile)
{
	int b = fgetc(classfile);
	if(b == EOF)
		eof_error();
	return (u_int8_t)b;
}
Exemple #3
0
std::shared_ptr<buf_t>   i3_recv(const int32_t  sockfd) {
	auto buff = std::make_shared<buf_t>(0);
	const uint32_t  header_size = sizeof(header_t);

	{
		uint8_t*  header = (uint8_t*)buff->header;
		uint32_t  readed = 0;
		while (readed < header_size) {
			int  n = read(sockfd, header + readed, header_size - readed);
			if (n == -1) {
				throw errno_error(auss_t() << "Failed to read header from socket 0x" << std::hex << sockfd);
			}
			if (n == 0) {
				throw eof_error("Unexpected EOF while reading header");
			}

			readed += n;
		}
	}

	if (g_i3_ipc_magic != std::string(buff->header->magic, g_i3_ipc_magic.length())) {
		throw invalid_header_error("Invalid magic in reply");
	}

	buff->realloc_payload_to_header();

	{
		uint32_t  readed = 0;
		int n;
		while (readed < buff->header->size) {
			if ((n = read(sockfd, buff->payload + readed, buff->header->size - readed)) == -1) {
				if (errno == EINTR || errno == EAGAIN)
					continue;
				throw errno_error(auss_t() << "Failed to read payload from socket 0x" << std::hex << sockfd);
			}

			readed += n;
		}
	}

	return buff;
}
Exemple #4
0
static PyObject *
logreader_tp_iternext(LogReaderObject *self)
{
    int c;
    int what;
    int err = ERR_NONE;
    int lineno = -1;
    int fileno = -1;
    int tdelta = -1;
    PyObject *s1 = NULL, *s2 = NULL;
    PyObject *result = NULL;
#if 0
    unsigned char b0, b1;
#endif

    if (self->logfp == NULL) {
        PyErr_SetString(ProfilerError,
                        "cannot iterate over closed LogReader object");
        return NULL;
    }

restart:
    /* decode the record type */
    if ((c = fgetc(self->logfp)) == EOF) {
        fclose(self->logfp);
        self->logfp = NULL;
        return NULL;
    }
    what = c & WHAT_OTHER;
    if (what == WHAT_OTHER)
        what = c; /* need all the bits for type */
    else
        ungetc(c, self->logfp); /* type byte includes packed int */

    switch (what) {
    case WHAT_ENTER:
        err = unpack_packed_int(self, &fileno, 2);
        if (!err) {
            err = unpack_packed_int(self, &lineno, 0);
            if (self->frametimings && !err)
                err = unpack_packed_int(self, &tdelta, 0);
        }
        break;
    case WHAT_EXIT:
        err = unpack_packed_int(self, &tdelta, 2);
        break;
    case WHAT_LINENO:
        err = unpack_packed_int(self, &lineno, 2);
        if (self->linetimings && !err)
            err = unpack_packed_int(self, &tdelta, 0);
        break;
    case WHAT_ADD_INFO:
        err = unpack_add_info(self);
        break;
    case WHAT_DEFINE_FILE:
        err = unpack_packed_int(self, &fileno, 0);
        if (!err) {
            err = unpack_string(self, &s1);
            if (!err) {
                Py_INCREF(Py_None);
                s2 = Py_None;
            }
        }
        break;
    case WHAT_DEFINE_FUNC:
        err = unpack_packed_int(self, &fileno, 0);
        if (!err) {
            err = unpack_packed_int(self, &lineno, 0);
            if (!err)
                err = unpack_string(self, &s1);
        }
        break;
    case WHAT_LINE_TIMES:
        if ((c = fgetc(self->logfp)) == EOF)
            err = ERR_EOF;
        else {
            self->linetimings = c ? 1 : 0;
            goto restart;
        }
        break;
    case WHAT_FRAME_TIMES:
        if ((c = fgetc(self->logfp)) == EOF)
            err = ERR_EOF;
        else {
            self->frametimings = c ? 1 : 0;
            goto restart;
        }
        break;
    default:
        err = ERR_BAD_RECTYPE;
    }
    if (err == ERR_BAD_RECTYPE) {
        PyErr_SetString(PyExc_ValueError,
                        "unknown record type in log file");
    }
    else if (err == ERR_EOF) {
        eof_error(self);
    }
    else if (!err) {
        result = PyTuple_New(4);
        if (result == NULL)
            return NULL;
        PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
        PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
        if (s1 == NULL)
            PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
        else
            PyTuple_SET_ITEM(result, 1, s1);
        if (s2 == NULL)
            PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
        else
            PyTuple_SET_ITEM(result, 3, s2);
    }
    /* The only other case is err == ERR_EXCEPTION, in which case the
     * exception is already set.
     */
#if 0
    b0 = self->buffer[self->index];
    b1 = self->buffer[self->index + 1];
    if (b0 & 1) {
        /* This is a line-number event. */
        what = PyTrace_LINE;
        lineno = ((b0 & ~1) << 7) + b1;
        self->index += 2;
    }
    else {
        what = (b0 & 0x0E) >> 1;
        tdelta = ((b0 & 0xF0) << 4) + b1;
        if (what == PyTrace_CALL) {
            /* we know there's a 2-byte file ID & 2-byte line number */
            fileno = ((self->buffer[self->index + 2] << 8)
                      + self->buffer[self->index + 3]);
            lineno = ((self->buffer[self->index + 4] << 8)
                      + self->buffer[self->index + 5]);
            self->index += 6;
        }
        else
            self->index += 2;
    }
#endif
    return result;
}