+/* Search for something like : .*_.*
+ *
+ * The left side is the name, the right side is the number.
+ */
+
+int get_tracefile_name_number(gchar *raw_name,
+ GQuark *name,
+ guint *num,
+ guint *tid,
+ guint *pgid,
+ guint64 *creation)
+{
+ guint raw_name_len = strlen(raw_name);
+ gchar char_name[PATH_MAX];
+ int i;
+ int underscore_pos;
+ long int cpu_num;
+ gchar *endptr;
+ gchar *tmpptr;
+
+ for(i=raw_name_len-1;i>=0;i--) {
+ if(raw_name[i] == '_') break;
+ }
+ if(i==-1) { /* Either not found or name length is 0 */
+ /* This is a userspace tracefile */
+ strncpy(char_name, raw_name, raw_name_len);
+ char_name[raw_name_len] = '\0';
+ *name = g_quark_from_string(char_name);
+ *num = 0; /* unknown cpu */
+ for(i=0;i<raw_name_len;i++) {
+ if(raw_name[i] == '/') {
+ break;
+ }
+ }
+ i++;
+ for(;i<raw_name_len;i++) {
+ if(raw_name[i] == '/') {
+ break;
+ }
+ }
+ i++;
+ for(;i<raw_name_len;i++) {
+ if(raw_name[i] == '-') {
+ break;
+ }
+ }
+ if(i == raw_name_len) return -1;
+ i++;
+ tmpptr = &raw_name[i];
+ for(;i<raw_name_len;i++) {
+ if(raw_name[i] == '.') {
+ raw_name[i] = ' ';
+ break;
+ }
+ }
+ *tid = strtoul(tmpptr, &endptr, 10);
+ if(endptr == tmpptr)
+ return -1; /* No digit */
+ if(*tid == ULONG_MAX)
+ return -1; /* underflow / overflow */
+ i++;
+ tmpptr = &raw_name[i];
+ for(;i<raw_name_len;i++) {
+ if(raw_name[i] == '.') {
+ raw_name[i] = ' ';
+ break;
+ }
+ }
+ *pgid = strtoul(tmpptr, &endptr, 10);
+ if(endptr == tmpptr)
+ return -1; /* No digit */
+ if(*pgid == ULONG_MAX)
+ return -1; /* underflow / overflow */
+ i++;
+ tmpptr = &raw_name[i];
+ *creation = strtoull(tmpptr, &endptr, 10);
+ if(endptr == tmpptr)
+ return -1; /* No digit */
+ if(*creation == G_MAXUINT64)
+ return -1; /* underflow / overflow */
+ } else {
+ underscore_pos = i;
+
+ cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
+
+ if(endptr == raw_name+underscore_pos+1)
+ return -1; /* No digit */
+ if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
+ return -1; /* underflow / overflow */
+
+ strncpy(char_name, raw_name, underscore_pos);
+ char_name[underscore_pos] = '\0';
+
+ *name = g_quark_from_string(char_name);
+ *num = cpu_num;
+ }
+
+
+ return 0;
+}
+
+
+GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
+{
+ return &trace->tracefiles;
+}
+
+
+void compute_tracefile_group(GQuark key_id,
+ GArray *group,
+ struct compute_tracefile_group_args *args)
+{
+ int i;
+ LttTracefile *tf;
+
+ for(i=0; i<group->len; i++) {
+ tf = &g_array_index (group, LttTracefile, i);
+ if(tf->cpu_online)
+ args->func(tf, args->func_args);
+ }
+}
+
+
+void ltt_tracefile_group_destroy(gpointer data)
+{
+ GArray *group = (GArray *)data;
+ int i;
+ LttTracefile *tf;
+
+ for(i=0; i<group->len; i++) {
+ tf = &g_array_index (group, LttTracefile, i);
+ if(tf->cpu_online)
+ ltt_tracefile_close(tf);
+ }
+ g_array_free(group, TRUE);
+}
+
+gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
+{
+ GArray *group = (GArray *)data;
+ int i;
+ LttTracefile *tf;
+
+ for(i=0; i<group->len; i++) {
+ tf = &g_array_index (group, LttTracefile, i);
+ if(tf->cpu_online) return 1;
+ }
+ return 0;
+}
+
+
+/* Open each tracefile under a specific directory. Put them in a
+ * GData : permits to access them using their tracefile group pathname.
+ * i.e. access control/modules tracefile group by index :
+ * "control/module".
+ *
+ * relative path is the path relative to the trace root
+ * root path is the full path
+ *
+ * A tracefile group is simply an array where all the per cpu tracefiles sits.
+ */
+
+static int open_tracefiles(LttTrace *trace, gchar *root_path,
+ gchar *relative_path)
+{
+ DIR *dir = opendir(root_path);
+ struct dirent *entry;
+ struct stat stat_buf;
+ int ret;
+
+ gchar path[PATH_MAX];
+ int path_len;
+ gchar *path_ptr;
+
+ int rel_path_len;
+ gchar rel_path[PATH_MAX];
+ gchar *rel_path_ptr;
+ LttTracefile tmp_tf;
+
+ if(dir == NULL) {
+ perror(root_path);
+ return ENOENT;
+ }
+
+ strncpy(path, root_path, PATH_MAX-1);
+ path_len = strlen(path);
+ path[path_len] = '/';
+ path_len++;
+ path_ptr = path + path_len;
+
+ strncpy(rel_path, relative_path, PATH_MAX-1);
+ rel_path_len = strlen(rel_path);
+ rel_path[rel_path_len] = '/';
+ rel_path_len++;
+ rel_path_ptr = rel_path + rel_path_len;
+
+ while((entry = readdir(dir)) != NULL) {
+
+ if(entry->d_name[0] == '.') continue;
+
+ strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
+ strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
+
+ ret = stat(path, &stat_buf);
+ if(ret == -1) {
+ perror(path);
+ continue;
+ }
+
+ g_debug("Tracefile file or directory : %s\n", path);
+
+ if(strcmp(rel_path, "/eventdefs") == 0) continue;
+
+ if(S_ISDIR(stat_buf.st_mode)) {
+
+ g_debug("Entering subdirectory...\n");
+ ret = open_tracefiles(trace, path, rel_path);
+ if(ret < 0) continue;
+ } else if(S_ISREG(stat_buf.st_mode)) {
+ GQuark name;
+ guint num, tid, pgid;
+ guint64 creation;
+ GArray *group;
+ num = tid = pgid = 0;
+ creation = 0;
+ if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation))
+ continue; /* invalid name */
+
+ g_debug("Opening file.\n");
+ if(ltt_tracefile_open(trace, path, &tmp_tf)) {
+ g_info("Error opening tracefile %s", path);
+
+ continue; /* error opening the tracefile : bad magic number ? */
+ }
+
+ g_debug("Tracefile name is %s and number is %u",
+ g_quark_to_string(name), num);
+
+ tmp_tf.cpu_online = 1;
+ tmp_tf.cpu_num = num;
+ tmp_tf.name = name;
+ tmp_tf.tid = tid;
+ tmp_tf.pgid = pgid;
+ tmp_tf.creation = creation;
+
+ group = g_datalist_id_get_data(&trace->tracefiles, name);
+ if(group == NULL) {
+ /* Elements are automatically cleared when the array is allocated.
+ * It makes the cpu_online variable set to 0 : cpu offline, by default.
+ */
+ group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
+ g_datalist_id_set_data_full(&trace->tracefiles, name,
+ group, ltt_tracefile_group_destroy);
+ }
+
+ /* Add the per cpu tracefile to the named group */
+ unsigned int old_len = group->len;
+ if(num+1 > old_len)
+ group = g_array_set_size(group, num+1);
+ g_array_index (group, LttTracefile, num) = tmp_tf;
+
+ }
+ }
+
+ closedir(dir);
+
+ return 0;
+}
+
+/* ltt_get_facility_description
+ *
+ * Opens the file corresponding to the requested facility (identified by fac_id
+ * and checksum).
+ *
+ * The name searched is : %trace root%/eventdefs/facname_checksum.xml
+ *
+ * Returns 0 on success, or 1 on failure.
+ */
+
+static int ltt_get_facility_description(LttFacility *f,
+ LttTrace *t,
+ LttTracefile *fac_tf)
+{
+ char desc_file_name[PATH_MAX];
+ const gchar *text;
+ guint textlen;
+ gint err;
+ gint arch_spec;
+ gint fac_name_len;
+
+ text = g_quark_to_string(t->pathname);
+ textlen = strlen(text);
+
+ if(textlen >= PATH_MAX) goto name_error;
+ strcpy(desc_file_name, text);
+
+ text = "/eventdefs/";
+ textlen+=strlen(text);
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+
+ text = g_quark_to_string(f->name);
+ fac_name_len = strlen(text);
+ textlen+=fac_name_len;
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+
+ /* arch specific facilities are named like this : name_arch */
+ if(fac_name_len+1 < sizeof("_arch"))
+ arch_spec = 0;
+ else {
+ if(!strcmp(&text[fac_name_len+1-sizeof("_arch")], "_arch"))
+ arch_spec = 1;
+ else
+ arch_spec = 0;
+ }
+
+#if 0
+ text = "_";
+ textlen+=strlen(text);
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+
+ err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
+ "%u", f->checksum);
+ if(err < 0) goto name_error;
+
+ textlen=strlen(desc_file_name);
+
+#endif //0
+
+ if(arch_spec) {
+ switch(t->arch_type) {
+ case LTT_ARCH_TYPE_I386:
+ text = "_i386";
+ break;
+ case LTT_ARCH_TYPE_PPC:
+ text = "_ppc";
+ break;
+ case LTT_ARCH_TYPE_SH:
+ text = "_sh";
+ break;
+ case LTT_ARCH_TYPE_S390:
+ text = "_s390";
+ break;
+ case LTT_ARCH_TYPE_MIPS:
+ text = "_mips";
+ break;
+ case LTT_ARCH_TYPE_ARM:
+ text = "_arm";
+ break;
+ case LTT_ARCH_TYPE_PPC64:
+ text = "_ppc64";
+ break;
+ case LTT_ARCH_TYPE_X86_64:
+ text = "_x86_64";
+ break;
+ case LTT_ARCH_TYPE_C2:
+ text = "_c2";
+ break;
+ case LTT_ARCH_TYPE_POWERPC:
+ text = "_powerpc";
+ break;
+ default:
+ g_error("Trace from unsupported architecture.");
+ }
+ textlen+=strlen(text);
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+ }
+
+ text = ".xml";
+ textlen+=strlen(text);
+ if(textlen >= PATH_MAX) goto name_error;
+ strcat(desc_file_name, text);
+
+ err = ltt_facility_open(f, t, desc_file_name);
+ if(err) goto facility_error;
+
+ return 0;
+
+facility_error:
+name_error:
+ return 1;
+}
+
+static void ltt_fac_ids_destroy(gpointer data)
+{
+ GArray *fac_ids = (GArray *)data;
+
+ g_array_free(fac_ids, TRUE);
+}
+
+
+/* Presumes the tracefile is already seeked at the beginning. It makes sense,
+ * because it must be done just after the opening */
+static int ltt_process_facility_tracefile(LttTracefile *tf)
+{
+ int err;
+ LttFacility *fac;
+ GArray *fac_ids;
+ guint i;
+ LttEventType *et;
+
+ while(1) {
+ err = ltt_tracefile_read_seek(tf);
+ if(err == EPERM) goto seek_error;
+ else if(err == ERANGE) break; /* End of tracefile */
+
+ err = ltt_tracefile_read_update_event(tf);
+ if(err) goto update_error;
+
+ /* We are on a facility load/or facility unload/ or heartbeat event */
+ /* The rules are :
+ * * facility 0 is hardcoded : this is the core facility. It will be shown
+ * in the facility array though, and is shown as "loaded builtin" in the
+ * trace.
+ * It contains event :
+ * 0 : facility load
+ * 1 : facility unload
+ * 2 : state dump facility load
+ * 3 : heartbeat
+ */
+ if(tf->event.facility_id != LTT_FACILITY_CORE) {
+ /* Should only contain core facility */
+ g_warning("Error in processing facility file %s, "
+ "should not contain facility id %u.", g_quark_to_string(tf->name),
+ tf->event.facility_id);
+ err = EPERM;
+ goto fac_id_error;
+ } else {
+
+ struct LttFacilityLoad *fac_load_data;
+ struct LttStateDumpFacilityLoad *fac_state_dump_load_data;
+ char *fac_name;
+ void *pos;
+
+ // FIXME align
+ switch((enum ltt_core_events)tf->event.event_id) {
+ case LTT_EVENT_FACILITY_LOAD:
+ fac_name = (char*)(tf->event.data);
+ g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
+ fac_name);
+ pos = (tf->event.data + strlen(fac_name) + 1);
+ pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->has_alignment);
+ fac_load_data = (struct LttFacilityLoad *)pos;
+
+ fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
+ ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
+ /* facility may already exist if trace is paused/unpaused */
+ if(fac->exists) continue;
+ fac->name = g_quark_from_string(fac_name);
+ fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_load_data->checksum);
+ fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id);
+ fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_load_data->pointer_size);
+ fac->int_size = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_load_data->int_size);
+ fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_load_data->long_size);
+ fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_load_data->size_t_size);
+ fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_load_data->has_alignment);
+
+ if(ltt_get_facility_description(fac, tf->trace, tf))
+ continue; /* error opening description */
+
+ fac->trace = tf->trace;
+
+ /* Preset the field offsets */
+ for(i=0; i<fac->events->len; i++){
+ et = &g_array_index(fac->events, LttEventType, i);
+ precompute_offsets(fac, et);
+ }
+
+ fac->exists = 1;
+
+ fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
+ fac->name);
+ if(fac_ids == NULL) {
+ fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
+ g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
+ fac->name,
+ fac_ids, ltt_fac_ids_destroy);
+ }
+ g_array_append_val(fac_ids, fac->id);
+
+ break;
+ case LTT_EVENT_FACILITY_UNLOAD:
+ g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
+ /* We don't care about unload : facilities ID are valid for the whole
+ * trace. They simply won't be used after the unload. */
+ break;
+ case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
+ fac_name = (char*)(tf->event.data);
+ g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
+ fac_name);
+ pos = (tf->event.data + strlen(fac_name) + 1);
+ pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->has_alignment);
+ fac_state_dump_load_data = (struct LttStateDumpFacilityLoad *)pos;
+
+ fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
+ ltt_get_uint32(LTT_GET_BO(tf), &fac_state_dump_load_data->id));
+ /* facility may already exist if trace is paused/unpaused */
+ if(fac->exists) continue;
+ fac->name = g_quark_from_string(fac_name);
+ fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_state_dump_load_data->checksum);
+ fac->id = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_state_dump_load_data->id);
+ fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_state_dump_load_data->pointer_size);
+ fac->int_size = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_state_dump_load_data->int_size);
+ fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_state_dump_load_data->long_size);
+ fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_state_dump_load_data->size_t_size);
+ fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
+ &fac_state_dump_load_data->has_alignment);
+ if(ltt_get_facility_description(fac, tf->trace, tf))
+ continue; /* error opening description */
+
+ fac->trace = tf->trace;
+
+ /* Preset the field offsets */
+ for(i=0; i<fac->events->len; i++){
+ et = &g_array_index(fac->events, LttEventType, i);
+ precompute_offsets(fac, et);
+ }
+
+ fac->exists = 1;
+
+ fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
+ fac->name);
+ if(fac_ids == NULL) {
+ fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
+ g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
+ fac->name,
+ fac_ids, ltt_fac_ids_destroy);
+ }
+ g_array_append_val(fac_ids, fac->id);
+
+ break;
+ case LTT_EVENT_HEARTBEAT:
+ break;
+ default:
+ g_warning("Error in processing facility file %s, "
+ "unknown event id %hhu in core facility.",
+ g_quark_to_string(tf->name),
+ tf->event.event_id);
+ err = EPERM;
+ goto event_id_error;
+ }
+ }
+ }
+ return 0;
+
+ /* Error handling */
+event_id_error:
+fac_id_error:
+update_error:
+seek_error:
+ g_warning("An error occured in facility tracefile parsing");
+ return err;
+}
+
+
+LttTrace *ltt_trace_open(const gchar *pathname)