<commit_before>/*
** strace.c for strace in /home/kureuil/Work/strace
** 
** Made by <NAME>
** Login   <<EMAIL>>
** 
** Started on  Mon Apr  4 22:19:02 2016 <NAME>
** Last update Sun Apr 10 01:20:27 2016 <NAME>
*/

#define _GNU_SOURCE
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include <sys/reg.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <fcntl.h>
#include "strace.h"

int	strace_print_hexa(unsigned long long int value,
			  pid_t child,
			  const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  return (fprintf(stderr, "0x%llx", value));
}

int	strace_print_integer(unsigned long long int value,
			     pid_t child,
			     const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  return (fprintf(stderr, "%d", (int) value));
}

int	strace_print_pointer(unsigned long long int value,
			     pid_t child,
			     const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  if (value == 0)
    return (fprintf(stderr, "NULL"));
  else
    return (fprintf(stderr, "%#llx", value));
}

int	strace_print_long(unsigned long long int value,
			  pid_t child,
			  const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  return (fprintf(stderr, "%ld", (long) value));
}

int	strace_print_ulong(unsigned long long int value,
			   pid_t child,
			   const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  return (fprintf(stderr, "%lu", (unsigned long) value));
}

void	strace_string_read(char **strp,
			   unsigned long long int addr,
			   pid_t child)
{
  size_t	allocated;
  size_t	readb;
  unsigned long	tmp;

  allocated = 16;
  readb = 0;
  if ((*strp = realloc(*strp, allocated)) == NULL)
    return ;
  while (true)
    {
      if (readb > allocated)
	{
	  allocated <<= 1;
	  if ((*strp = realloc(*strp, allocated)) == NULL)
	    return ;
	}
      errno = 0;
      tmp = ptrace(PTRACE_PEEKDATA, child, addr + readb);
      if (errno)
	{
	  (*strp)[readb] = '\0';
	  return ;
	}
      memcpy(*strp + readb, &tmp, sizeof(tmp));
      if (memchr(&tmp, 0, sizeof(tmp)) != NULL)
	return ;
      readb += sizeof(tmp);
    }
}

int	strace_print_string(unsigned long long int value,
			    pid_t child,
			    const struct user_regs_struct *regs)
{
  char	*str;
  int	ret;

  (void) regs;<commit_msg>Fix allocation in string read<commit_after>/*
** strace.c for strace in /home/kureuil/Work/strace
** 
** Made by <NAME>
** Login   <<EMAIL>>
** 
** Started on  Mon Apr  4 22:19:02 2016 Arch Kureuil
** Last update Sun Apr 10 16:19:22 2016 Arch Kureuil
*/

#define _GNU_SOURCE
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include <sys/reg.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <fcntl.h>
#include "strace.h"

int	strace_print_hexa(unsigned long long int value,
			  pid_t child,
			  const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  return (fprintf(stderr, "0x%llx", value));
}

int	strace_print_integer(unsigned long long int value,
			     pid_t child,
			     const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  return (fprintf(stderr, "%d", (int) value));
}

int	strace_print_pointer(unsigned long long int value,
			     pid_t child,
			     const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  if (value == 0)
    return (fprintf(stderr, "NULL"));
  else
    return (fprintf(stderr, "%#llx", value));
}

int	strace_print_long(unsigned long long int value,
			  pid_t child,
			  const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  return (fprintf(stderr, "%ld", (long) value));
}

int	strace_print_ulong(unsigned long long int value,
			   pid_t child,
			   const struct user_regs_struct *regs)
{
  (void) child;
  (void) regs;
  return (fprintf(stderr, "%lu", (unsigned long) value));
}

void	strace_string_read(char **strp,
			   unsigned long long int addr,
			   pid_t child)
{
  size_t	allocated;
  size_t	readb;
  unsigned long	tmp;

  allocated = 16;
  readb = 0;
  if ((*strp = realloc(*strp, allocated)) == NULL)
    return ;
  while (true)
    {
      if ((readb + sizeof(tmp)) > allocated)
	{
	  allocated <<= 1;
	  if ((*strp = realloc(*strp, allocated)) == NULL)
	    return ;
	}
      errno = 0;
      tmp = ptrace(PTRACE_PEEKDATA, child, addr + readb);
      if (errno)
	{
	  (*strp)[readb] = '\0';
	  return ;
	}
      memcpy(*strp + readb, &tmp, sizeof(tmp));
      if (memchr(&tmp, 0, sizeof(tmp)) != NULL)
	return ;
      readb += sizeof(tmp);
    }
}

int	strace_print_string(unsigned long long int value,
			    pid_t child,
			    const struct user_regs_struct *regs)
{
  char	*str;
  int	ret;

  (void) regs;
{'fraction_non_alphanumeric': 0.14410925397472482, 'fraction_numerical': 0.013045250713412148, 'mean_word_length': 3.3081650570676033, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 39, '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': '5059575', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1872, 'n_words': 506}