📄 file_spliter.c
字号:
/* Copyright (C) 2005 Bull S.A. Contributed by Matthieu Castet <mat-c@users.sourceforge.net>, 2005. This Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This Library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */#if HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <string.h>#include <stdlib.h>#include <assert.h>#include "types.h"#include "glib_hash.h"#include "modules.h"extern struct export *export;struct desc { FILE *fd; FILE * father; int count;};static GHashTable* hashtable;static FILE *fd;static int type;static char *file;static int init (char *name, int mtype) { if (!name) mtype = SPLIT_STDOUT; type = mtype; switch (type) { case SPLIT_NULL: fd = fopen (name, "w"); if (fd == NULL) { perror ("fopen"); return -1; } export->init_file (fd, NULL); break; case SPLIT_STDOUT: fd = stdout; export->init_file (fd, NULL); break; case SPLIT_PID ... SPLIT_TID: file = strdup(name); hashtable = g_hash_table_new (g_str_hash, g_str_equal); break; default: return -1; } return 0;}static inline void copyfd (FILE *src, FILE *dest) { char buff [512]; int nb_read; /* copy the file from the beginning */ fflush (src); rewind (src); /* remove the written header */ fflush (dest); rewind (dest); while ((nb_read = fread (buff, 1, sizeof (buff), src)) > 0) fwrite(buff, nb_read, 1, dest);}static inline FILE *newfd (char *file, char *tmp, FILE *old_fd) { char tmp1 [strlen (file) + strlen (tmp) + 2]; struct desc *new_desc; FILE *ret; sprintf (tmp1, "%s-%s", file, tmp); ret = fopen (tmp1, "w+"); if (ret == NULL) { perror("fopen"); return ret; } export->init_file (ret, old_fd); new_desc = malloc (sizeof (struct desc)); new_desc->fd = ret; new_desc->father = old_fd; new_desc->count = 1; g_hash_table_insert (hashtable, strdup (tmp), new_desc); return ret;}static FILE * getfd (pid_t pid, char *tid, ptt_event_t event, int *list_int) { char tmp [255]; struct desc *des; switch (type) { case SPLIT_NULL: case SPLIT_STDOUT: return fd; case SPLIT_PID: case SPLIT_PID_FULL: sprintf (tmp, "%d", pid); break; case SPLIT_TID: sprintf (tmp, "%d-%s", pid, tid); break; } if ((type == SPLIT_TID) || (type == SPLIT_PID)) { fd = g_hash_table_lookup (hashtable, tmp); /* It is the first time we found this pid. create a new file */ if (fd == NULL) { char tmp1 [strlen (file) + strlen (tmp) + 2]; sprintf (tmp1, "%s-%s", file, tmp); fd = fopen (tmp1, "w"); export->init_file (fd, NULL); g_hash_table_insert (hashtable, strdup (tmp), fd); } return fd; } switch (event) { case EVT_PROG_FORK: /* insert it in the father */ fd = getfd (list_int [0], tid, EVT_NULL, NULL); newfd (file, tmp, fd); break; case EVT_START_USER_FUNC: des = g_hash_table_lookup (hashtable, tmp); if (des == NULL) return newfd (file, tmp, NULL); if (!des->father) fprintf(stderr, "%s : multiple EVT_START_USER_FUNC : no " "implemented...\n", tmp); des->father = NULL; fd = des->fd; break; default: des = g_hash_table_lookup (hashtable, tmp); assert (des != NULL); if (des->father) { copyfd (des->father, des->fd); des->father = NULL; } fd = des->fd; break; } return fd;}static void free_fd (gpointer key, gpointer value, gpointer user_data) { if ((type == SPLIT_TID) || (type == SPLIT_PID)) fclose ((FILE*) value); else { struct desc *des = (struct desc *) value; fclose (des->fd); free (des); } free (key);}static void close () { switch (type) { case SPLIT_NULL: fclose(fd); break; case SPLIT_STDOUT: fflush(fd); break; default: g_hash_table_foreach (hashtable, free_fd, NULL); g_hash_table_destroy (hashtable); break; }}struct file_spliter file_spliter = { .init = init, .getfd = getfd, .close = close};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -