Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
little sentence to hint about the location of missing documentation
[simgrid.git] / tools / gras / struct_diff.c
1 /* struct_diff -- little tool to see which structure's field are modified on mc_diff */
2
3 /* Copyright (c) 2012. The SimGrid Team. All rights reserved.               */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16
17 #include "xbt.h"
18 #include "xbt/datadesc.h"
19 #include "../../src/xbt/datadesc/datadesc_private.h" // RAAAAH! ugly relative path, but it's late, I want it to be done NOW.
20 #include "simix/datatypes.h"
21 #include "../../src/simix/smx_smurf_private.h" // RAAAAH! ugly relative path, but it's even later and it still doesn't work
22
23 static void define_types(void) {
24   xbt_datadesc_type_t desc;
25
26   /* typedef struct xbt_swag_hookup {
27    *    void *next;
28    *    void *prev;
29    *  } s_xbt_swag_hookup_t; */
30   desc = xbt_datadesc_struct("s_xbt_swag_hookup_t");
31   xbt_datadesc_struct_append(desc,"next",xbt_datadesc_by_name("data pointer"));
32   xbt_datadesc_struct_append(desc,"prev",xbt_datadesc_by_name("data pointer"));
33   xbt_datadesc_struct_close(desc);
34
35   /* FIXME: maybe we should provide the internal details */
36   xbt_datadesc_copy("smx_host_t",xbt_datadesc_by_name("data pointer"));
37   xbt_datadesc_copy("smx_context_t",xbt_datadesc_by_name("data pointer"));
38   xbt_datadesc_copy("smx_action_t",xbt_datadesc_by_name("data pointer"));
39   xbt_datadesc_copy("xbt_dict_t",xbt_datadesc_by_name("data pointer"));
40   xbt_datadesc_copy("xbt_fifo_t",xbt_datadesc_by_name("data pointer"));
41   xbt_datadesc_copy("smx_process_t",xbt_datadesc_by_name("data pointer"));
42
43   /* typedef struct {
44    *   char *msg;
45    *   xbt_errcat_t category;
46
47    *   int value;
48    *   short int remote;
49    *   char *host;
50    *   char *procname;
51    *
52    *   int pid;
53    *   char *file;
54    *   int line;
55    *   char *func;
56
57    *   int used;
58    *   char **bt_strings;
59    *   void *bt[XBT_BACKTRACE_SIZE];
60    * } xbt_ex_t;   */
61   desc = xbt_datadesc_struct("xbt_ex_t");
62   xbt_datadesc_struct_append(desc,"msg",xbt_datadesc_by_name("string"));
63
64   xbt_assert(sizeof(xbt_errcat_t)==sizeof(int),
65       "I was assuming that xbt_errcat_t is of the same size than integer, but it's not true: it's %zu (int:%zu)\n",
66       sizeof(xbt_errcat_t),sizeof(int));
67   xbt_datadesc_struct_append(desc,"category",xbt_datadesc_by_name("short int"));
68
69   xbt_datadesc_struct_append(desc,"value",xbt_datadesc_by_name("short int"));
70   xbt_datadesc_struct_append(desc,"remote",xbt_datadesc_by_name("short int"));
71   xbt_datadesc_struct_append(desc,"host",xbt_datadesc_by_name("string"));
72   xbt_datadesc_struct_append(desc,"procname",xbt_datadesc_by_name("string"));
73   xbt_datadesc_struct_append(desc,"pid",xbt_datadesc_by_name("int"));
74   xbt_datadesc_struct_append(desc,"file",xbt_datadesc_by_name("string"));
75   xbt_datadesc_struct_append(desc,"line",xbt_datadesc_by_name("int"));
76   xbt_datadesc_struct_append(desc,"func",xbt_datadesc_by_name("string"));
77   xbt_datadesc_struct_append(desc,"used",xbt_datadesc_by_name("int"));
78   xbt_datadesc_struct_append(desc,"bt_strings",xbt_datadesc_by_name("data pointer"));
79   xbt_datadesc_struct_append(desc,"bt",xbt_datadesc_array_fixed("xbt_bt",xbt_datadesc_by_name("data pointer"),XBT_BACKTRACE_SIZE));
80   xbt_datadesc_struct_close(desc);
81
82   /* typedef struct {
83    *   __ex_mctx_t *ctx_mctx;
84    *   volatile int ctx_caught;
85    *   volatile xbt_ex_t exception;
86    * } xbt_running_ctx_t;   */
87   desc = xbt_datadesc_struct("xbt_running_ctx_t");
88   xbt_datadesc_struct_append(desc,"ctx_mctx",xbt_datadesc_by_name("data pointer"));
89   xbt_datadesc_struct_append(desc,"ctx_caught",xbt_datadesc_by_name("int"));
90   xbt_datadesc_struct_append(desc,"exception",xbt_datadesc_by_name("int"));
91   xbt_datadesc_struct_close(desc);
92
93   xbt_assert(sizeof(e_smx_simcall_t)==sizeof(int),
94       "I was assuming that e_smx_simcall_t is of the same size than integer, but it's not true: it's %zu (int:%zu)\n",
95       sizeof(e_smx_simcall_t),sizeof(int));
96   xbt_datadesc_copy("e_smx_simcall_t", xbt_datadesc_by_name("int"));
97 }
98
99 static void parse_from_file(const char *name){
100   int in=open(name,O_RDONLY);
101   if (in==-1){
102     fprintf(stderr,"Cannot read structure file from %s: %s\n",name,strerror(errno));
103     exit(1);
104   }
105
106   xbt_strbuff_t strbuff = xbt_strbuff_new();
107   int r;
108   do {
109     char buffer[1024];
110     r = read(in,&buffer,1023);
111     buffer[r] = '\0';
112     xbt_strbuff_append(strbuff,buffer);
113   } while (r!=0);
114
115   printf("File %s content:\n%s\n",name,strbuff->data);
116   xbt_datadesc_parse(name,strbuff->data);
117
118 }
119
120 int main(int argc, char** argv) {
121   xbt_init(&argc, argv);
122   define_types();
123
124   if (argc<3) {
125     fprintf(stderr,"Usage: %s struct-file offset1 offset2 ...\n",argv[0]);
126     exit(1);
127   }
128
129   parse_from_file("s_smx_simcall_t");
130   parse_from_file(argv[1]);
131
132   int cpt;
133   xbt_datadesc_type_t type = xbt_datadesc_by_name(argv[1]);
134   for (cpt=2;cpt<argc;cpt++) {
135     int offset = atoi(argv[cpt]);
136     unsigned int cursor;
137     xbt_dd_cat_field_t field;
138     int found = 0;
139
140     printf ("Offset: %d in struct %s (there is %lu fields)\n",
141         offset, type->name, xbt_dynar_length(type->category.struct_data.fields));
142     xbt_dynar_foreach(type->category.struct_data.fields,cursor,field) {
143       if (field->offset[GRAS_THISARCH]<= offset&&
144           field->offset[GRAS_THISARCH]+field->type->size[GRAS_THISARCH] >= offset) {
145         found = 1;
146         printf("This offset is somewhere in field %s, which starts at offset %zu and is of size %zu\n",
147               field->name,field->offset[GRAS_THISARCH],field->type->size[GRAS_THISARCH]);
148
149       }
150     }
151     if (!found) {
152
153       printf("Damnit, the structure is too short to find the the field (last field %s was at offset %zu, with size %zu). Weird.\n",
154           field->name,field->offset[GRAS_THISARCH],field->type->size[GRAS_THISARCH]);
155     }
156   }
157   return 0;
158 }
159
160
161