<commit_before>/*
 * @FILE:	definations' functions 
 * @DESC:   
 * @Author: 
 * @Change:
 */

#include "phptrace_protocol.h"

int phptrace_ctrl_init(phptrace_ctrl_t *c)
{
	c->size = MAX_PROCESS_NUMBER;
	c->used = 0;
	c->ctrl_seg = phptrace_mmap_read(MMAP_CTRL_FILENAME);
	return (c->ctrl_seg.shmaddr != MAP_FAILED); 
}
void phptrace_ctrl_clean_all(phptrace_ctrl_t *c)
{
	memset(c->ctrl_seg.shmaddr, 0, c->ctrl_seg.size);
}
void phptrace_ctrl_clean_one(phptrace_ctrl_t *c, int pid)
{
	if (c && c->ctrl_seg.shmaddr)
		phptrace_ctrl_set(c, 0, pid); 
		//*((uint8_t *)(c->ctrl_seg.shmaddr + pid)) = 0;
}
void phptrace_ctrl_free(phptrace_ctrl_t *c, int pid)
{
	if (c && c->ctrl_seg.shmaddr != MAP_FAILED)
	{ 
		phptrace_ctrl_clean_one(c, pid);
		phptrace_unmap(&(c->ctrl_seg));
		free(c);
	}
}
int8_t phptrace_ctrl_heart_beat(phptrace_ctrl_t *c, int pid)
{ 
	int8_t s = -1;
	phptrace_ctrl_get(c, &s, pid);
	if (s != CTRL_STATE_CLOSE)
	{
		if (s == CTRL_STATE_PING) s = CTRL_STATE_PONG;
		else s = CTRL_STATE_PONG;
		phptrace_ctrl_set(c, (uint8_t)s, pid);
	}
	return s;
}

void phptrace_file_init(phptrace_file_t *f)
{
	//f->records = (phptrace_file_record_t *)malloc(sizeof(phptrace_file_record_t) * MIN_RECORDS_NUMBER);
	//if (!f->records)
	//{
	//	printf ("[error] alloc failed!\n");
	//	exit(-1);
	//}
	//f->num_records = 0;
	//f->size = MIN_RECORDS_NUMBER;
	
	f->num_records = 0;
	f->records_base = NULL;
	f->records_top = NULL;
}
void phptrace_file_free(phptrace_file_t *f)
{
	if (f)
	{
	//	if (f->records) free (f->records);
		free (f);
	}
}

void phptrace_file_record_clean_str(phptrace_file_record_t *r)
{
	if (r)
	{
		if (r->func_name) phptrace_str_free((r->func_name));
		if (r->params) phptrace_str_free((r->params));
		if (r->ret_values) phptrace_str_free((r->ret_values));
	}
}
/*
void phptrace_file_shrink_records_queue(phptrace_file_t *f)
{
	int qs, qe;
	if (f)
	{
		for (; f->records_state[f->qe] == 0;
}
*/

/* allocate a record and push it to the stack */
phptrace_file_record_t *phptrace_file_record_new()
{
	phptrace_file_record_t* r = (phptrace_file_record_t *)malloc(sizeof(phptrace_file_record_t));
	if (!r)
	{
		printf ("[error] malloc failed!\n");
		exit(-1);
	}
	return r;
}
void phptrace_file_record_free(phptrace_file_record_t *r)
{
	if (r)
	{
		/* free string members in r if needed */
		free(r);
	}
}

void phptrace_file_record_push(phptrace_file_t *f, phptrace_file_record_t *r)
{
	if (f)
	{
		r->prev = f->records_top;
		f->records_top = r;
		if (f->records_base == NULL)
			f->records_base = f->records_top;
	}
}

phptrace_file_record_t* phptrace_file_record_top(phptrace_file_t *f) 
{
	phptrace_file_record_t* r = NULL;
	if (f)
		r = f->records_top;
	return r;
}

phptrace_file_record_t* phptrace_file_record_pop(phptrace_file_t *f) 
{
	phptrace_file_record_t *r = NULL;
	if (f)
	{
		//phptrace_record_clean_str(&(f->records[f->qs]));
		r = f->records_top;
		if (r)
		{
			f->records_top = r->prev;
			r->prev = NULL;
			if (f->records_top == NULL)
				f->records_base = NULL;
		}
	}
	return r;
}

void *phptrace_mem_write_header(phptrace_file_header_t *header, void *mem){
    void *waitaddr = mem;
    /*skip the waitflag*/
    mem += sizeof(uint64_t);

    *((uint8_t *)mem) = header->version;
    mem += sizeof(uint8_t);

    *((uint8_t *)mem) = header->flag;
    mem += sizeof(uint8_t);

    *((uint64_t *)waitaddr) = header->magic_number;
    return mem; 
}
void *phptrace_mem_write_record(phptrace_file_record_t *record, void *mem){
    void *waitaddr = mem;
    /*skip the waitflag*/
    mem += sizeof(uint64_t);

    *((uint16_t *)mem) = record->level;
    mem += sizeof(uint16_t);

    *((uint64_t *)mem) = record->start_time;
    mem += sizeof(uint64_t);

    if(record->func_name){
        *((uint32_t *)mem) = record->func_name->len;
        mem += sizeof(uint32_t);
        memcpy(mem, record->func_name->data, record->func_name->len);
        mem += record->func_name->len;
    }else{
        *((uint32_t *)mem) = 0;
        mem += sizeof(uint32_t);
    }

    if(record->params){
        *((uint32_t *)mem) = record->params->len;
        mem += sizeof(uint32_t);
        memcpy(mem, record->params->data, record->params->len);
        mem += record->params->len;
    }else{
        *((uint32_t *)mem) = 0;
        mem += sizeof(uint32_t);
    }

    if(record->ret_values){
        *((uint32_t *)mem) = record->ret_values->len;
        memcpy(mem, record->ret_values->data, RET_VALUE_SIZE);
    }
    mem += RET_VALUE_SIZE;

    *((uint64_t *)mem) = record->time_cost;
    mem += sizeof(uint64_t);

    *((uint64_t *)waitaddr) = record->seq;
    return mem;
}

void *phptrace_mem_fix_record(phptrace_file_record_t *record, void *mem){
    memcpy(mem, record->ret_values, 
            record->ret_values->len + sizeof(uint32_t) > RET_VALUE_SIZE <commit_msg>fixbug: set to zero because we maybe never write the return value<commit_after>/*
 * @FILE:	definations' functions 
 * @DESC:   
 * @Author: 
 * @Change:
 */

#include "phptrace_protocol.h"

int phptrace_ctrl_init(phptrace_ctrl_t *c)
{
	c->size = MAX_PROCESS_NUMBER;
	c->used = 0;
	c->ctrl_seg = phptrace_mmap_read(MMAP_CTRL_FILENAME);
	return (c->ctrl_seg.shmaddr != MAP_FAILED); 
}
void phptrace_ctrl_clean_all(phptrace_ctrl_t *c)
{
	memset(c->ctrl_seg.shmaddr, 0, c->ctrl_seg.size);
}
void phptrace_ctrl_clean_one(phptrace_ctrl_t *c, int pid)
{
	if (c && c->ctrl_seg.shmaddr)
		phptrace_ctrl_set(c, 0, pid); 
		//*((uint8_t *)(c->ctrl_seg.shmaddr + pid)) = 0;
}
void phptrace_ctrl_free(phptrace_ctrl_t *c, int pid)
{
	if (c && c->ctrl_seg.shmaddr != MAP_FAILED)
	{ 
		phptrace_ctrl_clean_one(c, pid);
		phptrace_unmap(&(c->ctrl_seg));
		free(c);
	}
}
int8_t phptrace_ctrl_heart_beat(phptrace_ctrl_t *c, int pid)
{ 
	int8_t s = -1;
	phptrace_ctrl_get(c, &s, pid);
	if (s != CTRL_STATE_CLOSE)
	{
		if (s == CTRL_STATE_PING) s = CTRL_STATE_PONG;
		else s = CTRL_STATE_PONG;
		phptrace_ctrl_set(c, (uint8_t)s, pid);
	}
	return s;
}

void phptrace_file_init(phptrace_file_t *f)
{
	//f->records = (phptrace_file_record_t *)malloc(sizeof(phptrace_file_record_t) * MIN_RECORDS_NUMBER);
	//if (!f->records)
	//{
	//	printf ("[error] alloc failed!\n");
	//	exit(-1);
	//}
	//f->num_records = 0;
	//f->size = MIN_RECORDS_NUMBER;
	
	f->num_records = 0;
	f->records_base = NULL;
	f->records_top = NULL;
}
void phptrace_file_free(phptrace_file_t *f)
{
	if (f)
	{
	//	if (f->records) free (f->records);
		free (f);
	}
}

void phptrace_file_record_clean_str(phptrace_file_record_t *r)
{
	if (r)
	{
		if (r->func_name) phptrace_str_free((r->func_name));
		if (r->params) phptrace_str_free((r->params));
		if (r->ret_values) phptrace_str_free((r->ret_values));
	}
}
/*
void phptrace_file_shrink_records_queue(phptrace_file_t *f)
{
	int qs, qe;
	if (f)
	{
		for (; f->records_state[f->qe] == 0;
}
*/

/* allocate a record and push it to the stack */
phptrace_file_record_t *phptrace_file_record_new()
{
	phptrace_file_record_t* r = (phptrace_file_record_t *)malloc(sizeof(phptrace_file_record_t));
	if (!r)
	{
		printf ("[error] malloc failed!\n");
		exit(-1);
	}
	return r;
}
void phptrace_file_record_free(phptrace_file_record_t *r)
{
	if (r)
	{
		/* free string members in r if needed */
		free(r);
	}
}

void phptrace_file_record_push(phptrace_file_t *f, phptrace_file_record_t *r)
{
	if (f)
	{
		r->prev = f->records_top;
		f->records_top = r;
		if (f->records_base == NULL)
			f->records_base = f->records_top;
	}
}

phptrace_file_record_t* phptrace_file_record_top(phptrace_file_t *f) 
{
	phptrace_file_record_t* r = NULL;
	if (f)
		r = f->records_top;
	return r;
}

phptrace_file_record_t* phptrace_file_record_pop(phptrace_file_t *f) 
{
	phptrace_file_record_t *r = NULL;
	if (f)
	{
		//phptrace_record_clean_str(&(f->records[f->qs]));
		r = f->records_top;
		if (r)
		{
			f->records_top = r->prev;
			r->prev = NULL;
			if (f->records_top == NULL)
				f->records_base = NULL;
		}
	}
	return r;
}

void *phptrace_mem_write_header(phptrace_file_header_t *header, void *mem){
    void *waitaddr = mem;
    /*skip the waitflag*/
    mem += sizeof(uint64_t);

    *((uint8_t *)mem) = header->version;
    mem += sizeof(uint8_t);

    *((uint8_t *)mem) = header->flag;
    mem += sizeof(uint8_t);

    *((uint64_t *)waitaddr) = header->magic_number;
    return mem; 
}
void *phptrace_mem_write_record(phptrace_file_record_t *record, void *mem){
    void *waitaddr = mem;
    /*skip the waitflag*/
    mem += sizeof(uint64_t);

    *((uint16_t *)mem) = record->level;
    mem += sizeof(uint16_t);

    *((uint64_t *)mem) = record->start_time;
    mem += sizeof(uint64_t);

    if(record->func_name){
        *((uint32_t *)mem) = record->func_name->len;
        mem += sizeof(uint32_t);
        memcpy(mem, record->func_name->data, record->func_name->len);
        mem += record->func_name->len;
    }else{
        *((uint32_t *)mem) = 0;
        mem += sizeof(uint32_t);
    }

    if(record->params){
        *((uint32_t *)mem) = record->params->len;
        mem += sizeof(uint32_t);
        memcpy(mem, record->params->data, record->params->len);
        mem += record->params->len;
    }else{
        *((uint32_t *)mem) = 0;
        mem += sizeof(uint32_t);
    }

    if(record->ret_values){
        *((uint32_t *)mem) = record->ret_values->len;
        memcpy(mem, record->ret_values->data, RET_VALUE_SIZE);
    }else{
        /* zero the reserved chunk because maybe 
         * we never have an opportunity to write 
         * the return value*/
        memset(mem, 0, RET_VALUE_SIZE);
    }
    mem += RET_VALUE_SIZE;

    *((uint64_t *)mem) = record->time_cost;
    mem += sizeof(uint64_t);

    *((uint64_t *)waitaddr) = record->seq;
    return mem;
}

void *phptrace_mem_fix_record(phptrace_file_record_t *record, void *mem){
    memcpy(mem, record->ret_values, 
            record->ret_values->len + sizeof(uint32_t) > RET_VALUE_SIZE 
{'fraction_non_alphanumeric': 0.1630578428382637, 'fraction_numerical': 0.012300498119345329, 'mean_word_length': 3.70492587278814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2454329', 'n_tokens_mistral': 4350, 'n_tokens_neox': 4130, 'n_words': 721}