"Fossies" - the Fresh Open Source Software Archive

Member "FunctionCheck-3.2.0/src/fcmanager/fc_com_manager.c" (26 May 2012, 7151 Bytes) of package /linux/privat/old/FunctionCheck-3.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * FunctionCheck profiler
    3  * (C) Copyright 2000-2012 Yannick Perret
    4  * 
    5  *  This program is free software; you can redistribute it and/or
    6  *  modify it under the terms of the GNU General Public License as
    7  *  published by the Free Software Foundation; either version 2 of the
    8  *  License, or (at your option) any later version.
    9  *
   10  *  This program is distributed in the hope that it will be useful,
   11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13  *  General Public License for more details.
   14  *
   15  *  You should have received a copy of the GNU General Public License
   16  *  along with this program; if not, write to the Free Software
   17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18  */
   19 
   20 /** fc_com.h: manage coms between profiled program and the manager **/
   21 
   22 #include <errno.h>
   23 #include "fc_global.h"
   24 #include "fc_com_manager.h"
   25 #include "fc_time.h"
   26 #include "fc_tools.h"
   27 #include "fc_fifo.h"
   28 
   29 /* the profile mode for other modules */
   30 int fc_mcom_mode = FC_MODE_SINGLE;
   31 
   32 /* FIFO for the com */
   33 static FC_FIFO fc_com_fifo = FC_FIFO_NDEF;
   34 static int fc_com_fifo_id = 0;
   35 
   36 /* the initial PID (for default value) */
   37 int fc_default_pid = 0;
   38 
   39 static int fc_com_pid = 0;
   40 
   41 /* real enter/exit for type 1 */
   42 int fc_mcom_read(void **f, void **s, unsigned long long *time, int *id, char *type,
   43         void **ptr, void **incoming, void **where, int *parent,
   44         unsigned int *size, unsigned int *align, char *name)
   45 {
   46     unsigned char* buffer;
   47 
   48     typedef struct structData
   49     {
   50         union
   51         {
   52             FC_CEnter CEnter;
   53             FC_CExit CExit;
   54             FC_CMalloc CMalloc;
   55             FC_CFree CFree;
   56             FC_CRealloc CRealloc;
   57             FC_CMemalign CMemalign;
   58             FC_CDlsym CDlsym;
   59             FC_CDlopen CDlopen;
   60             FC_CDlclose CDlclose;
   61             FC_CFork CFork;
   62             FC_CThread CThread;
   63             FC_CQuit CQuit;
   64             FC_CParent CParent;
   65             FC_CTime CTime;
   66         };
   67     } data;
   68 
   69     buffer = fc_fifo_read_single(fc_com_fifo, sizeof(char) /*type*/ + sizeof(int) /*pid,tid*/ + sizeof(data), fc_com_pid);
   70     if (buffer == 0)
   71     {
   72         return 0;
   73     }
   74 
   75     *type = buffer[0];
   76     buffer++;
   77 
   78     /* if needed read the ID */
   79     if (fc_mcom_mode == FC_MODE_SINGLE)
   80     {
   81         *id = fc_default_pid;
   82     }
   83     else
   84     {
   85         *id = *(unsigned int*)buffer;
   86         buffer += sizeof(unsigned int);
   87         if (*id == 0)
   88         {
   89             fc_message("invalid id (0)");
   90             goto CLEANUP;
   91         }
   92     }
   93 
   94     /* now read the data regards to the type */
   95     switch (*type)
   96     {
   97     case FC_TYPE_ENTER:
   98     {
   99         FC_CEnter* fcenter = (FC_CEnter*)buffer;
  100         *f = fcenter->to;
  101         *s = fcenter->from;
  102         *time = fcenter->time;
  103         break;
  104     }
  105     case FC_TYPE_EXIT:
  106     {
  107         FC_CExit* fcexit = (FC_CExit*)buffer;
  108         *f = fcexit->to;
  109         *s = fcexit->from;
  110         *time = fcexit->time;
  111         break;
  112     }
  113     case FC_TYPE_MALLOC:
  114     {
  115         FC_CMalloc* fcmalloc = (FC_CMalloc*)buffer;
  116         *where = fcmalloc->where;
  117         *size = fcmalloc->size;
  118         *ptr = fcmalloc->ptr;
  119         break;
  120     }
  121     case FC_TYPE_FREE:
  122     {
  123         FC_CFree* fcfree = (FC_CFree*)buffer;
  124         *ptr = fcfree->ptr;
  125         *where = fcfree->where;
  126         break;
  127     }
  128     case FC_TYPE_REALLOC:
  129     {
  130         FC_CRealloc* fcrealloc = (FC_CRealloc*)buffer;
  131         *where = fcrealloc->where;
  132         *size = fcrealloc->size;
  133         *ptr = fcrealloc->ptr;
  134         *incoming = fcrealloc->old;
  135         break;
  136     }
  137     case FC_TYPE_MEMALIGN:
  138     {
  139         FC_CMemalign* fcmemalign = (FC_CMemalign*)buffer;
  140         *where = fcmemalign->where;
  141         *size = fcmemalign->size;
  142         *align = fcmemalign->align;
  143         *ptr = fcmemalign->ptr;
  144         break;
  145     }
  146     case FC_TYPE_DLSYM:
  147     {
  148         FC_CDlsym* fcdlsym = (FC_CDlsym*)buffer;
  149         *ptr = fcdlsym->fnc;
  150         *incoming = fcdlsym->handle;
  151         sprintf(name, "%s", fcdlsym->name); /* name must be large enough */
  152         break;
  153     }
  154     case FC_TYPE_DLOPEN:
  155     {
  156         FC_CDlopen* fcdlopen = (FC_CDlopen*)buffer;
  157         *ptr = fcdlopen->handle;
  158         sprintf(name, "%s", fcdlopen->name); /* name must be large enough */
  159         /* fcdlopen.flag  -> not used */
  160         break;
  161     }
  162     case FC_TYPE_DLCLOSE:
  163     {
  164         FC_CDlclose* fcdlclose = (FC_CDlclose*)buffer;
  165         *incoming = fcdlclose->handle;
  166         break;
  167     }
  168     case FC_TYPE_FORK:
  169     {
  170         FC_CFork* fcfork = (FC_CFork*)buffer;
  171         *parent = fcfork->child;
  172         *time = fcfork->time;
  173         break;
  174     }
  175     case FC_TYPE_THREAD:
  176     {
  177         FC_CThread* fcthread = (FC_CThread*)buffer;
  178         *parent = fcthread->thread;
  179         *time = fcthread->time;
  180         break;
  181     }
  182     case FC_TYPE_QUIT:
  183     {
  184         FC_CQuit* fcquit = (FC_CQuit*)buffer;
  185         *time = fcquit->time;
  186         break;
  187     }
  188     case FC_TYPE_PARENT:
  189     {
  190         FC_CParent* fcparent = (FC_CParent*)buffer;
  191         *parent = fcparent->parent;
  192         break;
  193     }
  194     case FC_TYPE_TIME:
  195     {
  196         FC_CTime* fctime = (FC_CTime*)buffer;
  197         *time = fctime->time;
  198         break;
  199     }
  200     default:
  201     {
  202         fc_message("invalid message, type: 0x%02x. Very bad...", *type);
  203         break;
  204     }
  205     }
  206 
  207 CLEANUP:
  208 
  209     fc_fifo_read_single_done(fc_com_fifo, fc_com_pid);
  210     
  211     return 1;
  212 }
  213 
  214 /* init the communication process and start the manager */
  215 int fc_mcom_init(int shmid, int *id, FC_INIT *init)
  216 {
  217     fc_com_pid = getpid();
  218 
  219     /* connect to the FIFO */
  220     fc_com_fifo = fc_fifo_connect(shmid);
  221     fc_com_fifo_id = shmid;
  222 
  223     if (fc_com_fifo == FC_FIFO_NDEF)
  224     {
  225         fc_message("cannot connect to the FIFO.");
  226         return (0);
  227     }
  228 
  229     fc_debug("shared buffer '%d' mapped at address %p", shmid, fc_com_fifo);
  230 
  231     /* read the init message */
  232     if ( fc_fifo_read_init(fc_com_fifo, init, sizeof(FC_INIT), fc_com_pid) == 0 )
  233     {
  234         fc_message("end of FIFO while reading initialization message!");
  235         return (0);
  236     }
  237 
  238     /* set the pointers */
  239     if (init->mode == FC_MODE_SINGLE)
  240     {
  241         fc_mcom_mode = FC_MODE_SINGLE;
  242     }
  243     else
  244         if (init->mode == FC_MODE_FORK)
  245     {
  246         fc_mcom_mode = FC_MODE_FORK;
  247     }
  248     else
  249         if (init->mode == FC_MODE_THREAD)
  250     {
  251         fc_mcom_mode = FC_MODE_THREAD;
  252     }
  253     else
  254     {
  255         fc_message("invalid mode for communication initialisation (%d).\n");
  256     }
  257 
  258     /* store the default PID */
  259     fc_default_pid = init->first_pid;
  260     *id = init->first_pid;
  261 
  262     /* ok */
  263     return (1);
  264 }
  265 
  266 /* read a dynamic lib entry */
  267 int fc_mcom_read_lib(FC_LDYN *ldyn)
  268 {
  269     if ( fc_fifo_read_ldyn(fc_com_fifo, ldyn, sizeof (FC_LDYN), fc_com_pid) == 0 )
  270     {
  271         fc_message("end of FIFO while reading dynamic l ibrary list!");
  272         return 0;
  273     }
  274 
  275     return 1;
  276 }
  277 
  278 /* stop the communication process */
  279 int fc_mcom_fini(unsigned int id)
  280 {
  281   /* close the com */
  282   fc_fifo_close(fc_com_fifo, id, 1 /* delete shm if reference count is 0, no process or manager needs it */);
  283 
  284   return(1);
  285 }
  286 
  287 
  288