<commit_before>/*
  Copyright (c) 2006-2009 Gluster, Inc. <http://www.gluster.com>
  This file is part of GlusterFS.

  GlusterFS is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3 of the License,
  or (at your option) any later version.

  GlusterFS is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see
  <http://www.gnu.org/licenses/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

/**
 * xlators/debug/trace :
 *    This translator logs all the arguments to the fops/mops and also 
 *    their _cbk functions, which later passes the call to next layer. 
 *    Very helpful translator for debugging.
 */

#include <time.h>
#include <errno.h>
#include "glusterfs.h"
#include "xlator.h"
#include "common-utils.h"

#define ERR_EINVAL_NORETURN(cond)                \
do                                               \
  {						 \
    if ((cond))					 \
      {						 \
	gf_log ("ERROR", GF_LOG_ERROR,   	 \
		"%s: %s: (%s) is true", 	 \
		__FILE__, __FUNCTION__, #cond);	 \
      }                                          \
  } while (0)

typedef struct trace_private {
	int32_t debug_flag;
} trace_private_t;

struct {
	char *name;
	int enabled;
} trace_fop_names[GF_FOP_MAXVALUE];

static char *
trace_stat_to_str (struct stat *stbuf)
{
        char *statstr = NULL;
	char atime_buf[256] = {0,};
        char mtime_buf[256] = {0,};
        char ctime_buf[256] = {0,};
        int  asprint_ret_value = 0;

        strftime (atime_buf, 256, "[%b %d %H:%M:%S]",
                  localtime (&stbuf->st_atime));
        strftime (mtime_buf, 256, "[%b %d %H:%M:%S]",
                  localtime (&stbuf->st_mtime));
        strftime (ctime_buf, 256, "[%b %d %H:%M:%S]",
                  localtime (&stbuf->st_ctime));

        asprint_ret_value = asprintf (&statstr,
                                      "st_ino=%"PRIu64", st_mode=%o, st_nlink=%"GF_PRI_NLINK", "
                                      "st_uid=%d, st_gid=%d, st_size=%"PRId64", st_blocks=%"PRId64
                                      ", st_atime=%s, st_mtime=%s, st_ctime=%s",
                                      stbuf->st_ino, stbuf->st_mode, stbuf->st_nlink, stbuf->st_uid,
                                      stbuf->st_gid, stbuf->st_size, stbuf->st_blocks, atime_buf,
                                      mtime_buf, ctime_buf);

        if (asprint_ret_value < 0)
                statstr = NULL;

        return statstr;
}

int32_t 
trace_create_cbk (call_frame_t *frame,
		  void *cookie,
		  xlator_t *this,
		  int32_t op_ret,
		  int32_t op_errno,
		  fd_t *fd,
		  inode_t *inode,
		  struct stat *buf,
                  struct stat *preparent,
                  struct stat *postparent)
{
        char *statstr = NULL, *preparentstr = NULL, *postparentstr = NULL;

	ERR_EINVAL_NORETURN (!this);

	if (trace_fop_names[GF_FOP_CREATE].enabled) {
		if (op_ret >= 0) {
                        statstr = trace_stat_to_str (buf);
                        preparentstr = trace_stat_to_str (preparent);
                        postparentstr = trace_stat_to_str (postparent);

			gf_log (this->name, GF_LOG_NORMAL,
				"%"PRId64": (op_ret=%d, fd=%p, ino=%"PRIu64" "
				"*stbuf {%s}, *preparent {%s}, *postparent = "
                                "{%s})",
				frame->root->unique, op_ret, fd, inode->ino,
                                statstr, preparentstr, postparentstr);

                        if (statstr)
                                FREE (statstr);
                        if (preparent)
                                FREE (preparent);
                        if (postparent)
                                FREE (postparent);
		} else {
			gf_log (this->name, GF_LOG_NORMAL, 
				"%"PRId64": (op_ret=%d, op_errno=%d)",
				frame->root->unique, op_ret, op_errno);
		}    
	}

	STACK_UNWIND (frame, op_ret, op_errno, fd, inode, buf,
                      preparent, postparent);
	return 0;
}

int32_t 
trace_open_cbk (call_frame_t *frame,
		void *cookie,
		xlator_t *this,
		int32_t op_ret,
		int32_t op_errno,
		fd_t *fd)
{
	ERR_EINVAL_NORETURN (!this);

	if (trace_fop_names[GF_FOP_OPEN].enabled) {
		gf_log (this->name, GF_LOG_NORMAL, 
			"%"PRId64": (op_ret=%d, op_errno=%d, *fd=%p)",
			frame->root->unique, op_ret, op_errno, fd);
	}<commit_msg>fix a crash in debug/trace<commit_after>/*
  Copyright (c) 2006-2009 Gluster, Inc. <http://www.gluster.com>
  This file is part of GlusterFS.

  GlusterFS is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3 of the License,
  or (at your option) any later version.

  GlusterFS is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see
  <http://www.gnu.org/licenses/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

/**
 * xlators/debug/trace :
 *    This translator logs all the arguments to the fops/mops and also 
 *    their _cbk functions, which later passes the call to next layer. 
 *    Very helpful translator for debugging.
 */

#include <time.h>
#include <errno.h>
#include "glusterfs.h"
#include "xlator.h"
#include "common-utils.h"

#define ERR_EINVAL_NORETURN(cond)                \
do                                               \
  {						 \
    if ((cond))					 \
      {						 \
	gf_log ("ERROR", GF_LOG_ERROR,   	 \
		"%s: %s: (%s) is true", 	 \
		__FILE__, __FUNCTION__, #cond);	 \
      }                                          \
  } while (0)

typedef struct trace_private {
	int32_t debug_flag;
} trace_private_t;

struct {
	char *name;
	int enabled;
} trace_fop_names[GF_FOP_MAXVALUE];

static char *
trace_stat_to_str (struct stat *stbuf)
{
        char *statstr = NULL;
	char atime_buf[256] = {0,};
        char mtime_buf[256] = {0,};
        char ctime_buf[256] = {0,};
        int  asprint_ret_value = 0;

        strftime (atime_buf, 256, "[%b %d %H:%M:%S]",
                  localtime (&stbuf->st_atime));
        strftime (mtime_buf, 256, "[%b %d %H:%M:%S]",
                  localtime (&stbuf->st_mtime));
        strftime (ctime_buf, 256, "[%b %d %H:%M:%S]",
                  localtime (&stbuf->st_ctime));

        asprint_ret_value = asprintf (&statstr,
                                      "st_ino=%"PRIu64", st_mode=%o, st_nlink=%"GF_PRI_NLINK", "
                                      "st_uid=%d, st_gid=%d, st_size=%"PRId64", st_blocks=%"PRId64
                                      ", st_atime=%s, st_mtime=%s, st_ctime=%s",
                                      stbuf->st_ino, stbuf->st_mode, stbuf->st_nlink, stbuf->st_uid,
                                      stbuf->st_gid, stbuf->st_size, stbuf->st_blocks, atime_buf,
                                      mtime_buf, ctime_buf);

        if (asprint_ret_value < 0)
                statstr = NULL;

        return statstr;
}

int32_t 
trace_create_cbk (call_frame_t *frame,
		  void *cookie,
		  xlator_t *this,
		  int32_t op_ret,
		  int32_t op_errno,
		  fd_t *fd,
		  inode_t *inode,
		  struct stat *buf,
                  struct stat *preparent,
                  struct stat *postparent)
{
        char *statstr = NULL, *preparentstr = NULL, *postparentstr = NULL;

	ERR_EINVAL_NORETURN (!this);

	if (trace_fop_names[GF_FOP_CREATE].enabled) {
		if (op_ret >= 0) {
                        statstr = trace_stat_to_str (buf);
                        preparentstr = trace_stat_to_str (preparent);
                        postparentstr = trace_stat_to_str (postparent);

			gf_log (this->name, GF_LOG_NORMAL,
				"%"PRId64": (op_ret=%d, fd=%p, ino=%"PRIu64" "
				"*stbuf {%s}, *preparent {%s}, *postparent = "
                                "{%s})",
				frame->root->unique, op_ret, fd, inode->ino,
                                statstr, preparentstr, postparentstr);

                        if (statstr)
                                FREE (statstr);
                        if (preparentstr)
                                FREE (preparentstr);
                        if (postparentstr)
                                FREE (postparentstr);
		} else {
			gf_log (this->name, GF_LOG_NORMAL, 
				"%"PRId64": (op_ret=%d, op_errno=%d)",
				frame->root->unique, op_ret, op_errno);
		}    
	}

	STACK_UNWIND (frame, op_ret, op_errno, fd, inode, buf,
                      preparent, postparent);
	return 0;
}

int32_t 
trace_open_cbk (call_frame_t *frame,
		void *cookie,
		xlator_t *this,
		int32_t op_ret,
		int32_t op_errno,
		fd_t *fd)
{
	ERR_EINVAL_NORETURN (!this);

	if (trace_fop_names[GF_FOP_OPEN].enabled) {
		gf_log (this->name, GF_LOG_NORMAL, 
			"%"PRId64": (op_ret=%d, op_errno=%d, *fd=%p)",
			frame->root->unique, op_ret, op_errno, fd);
	}
{'fraction_non_alphanumeric': 0.11600555971346092, 'fraction_numerical': 0.013471613386079332, 'mean_word_length': 1.9582542694497154, 'pattern_counts': {'":': 6, '<': 13, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5754495', 'n_tokens_mistral': 3373, 'n_tokens_neox': 3069, 'n_words': 784}