+static CostCentreStack *
+pruneCCSTree( CostCentreStack *ccs )
+{
+ CostCentreStack *ccs1;
+ IndexTable *i, **prev;
+
+ prev = &ccs->indexTable;
+ for (i = ccs->indexTable; i != 0; i = i->next) {
+ if (i->back_edge) { continue; }
+
+ ccs1 = pruneCCSTree(i->ccs);
+ if (ccs1 == NULL) {
+ *prev = i->next;
+ } else {
+ prev = &(i->next);
+ }
+ }
+
+ if ( (RtsFlags.CcFlags.doCostCentres >= COST_CENTRES_ALL
+ /* force printing of *all* cost centres if -P -P */ )
+
+ || ( ccs->indexTable != 0 )
+ || ( ccs->scc_count || ccs->time_ticks || ccs->mem_alloc )
+ ) {
+ return ccs;
+ } else {
+ return NULL;
+ }
+}
+
+/* -----------------------------------------------------------------------------
+ Generate the XML time/allocation profile
+ -------------------------------------------------------------------------- */
+
+void
+gen_XML_logfile( void )
+{
+ fprintf(prof_file, "%d %lu", TIME_UPD_UQ, total_prof_ticks);
+
+ reportCCS_XML(pruneCCSTree(CCS_MAIN));
+
+ fprintf(prof_file, " 0\n");
+
+ fclose(prof_file);
+}
+
+static void
+reportCCS_XML(CostCentreStack *ccs)
+{
+ CostCentre *cc;
+ IndexTable *i;
+
+ if (ccs_to_ignore(ccs)) { return; }
+
+ cc = ccs->cc;
+
+ fprintf(prof_file, " 1 %d %lu %lu %lu",
+ ccs->ccsID, ccs->scc_count, ccs->time_ticks, ccs->mem_alloc);
+
+ for (i = ccs->indexTable; i != 0; i = i->next) {
+ if (!i->back_edge) {
+ reportCCS_XML(i->ccs);
+ }
+ }
+}
+
+void
+print_ccs (FILE *fp, CostCentreStack *ccs)
+{
+ if (ccs == CCCS) {
+ fprintf(fp, "Cost-Centre Stack: ");
+ }
+
+ if (ccs != CCS_MAIN)
+ {
+ print_ccs(fp, ccs->prevStack);
+ fprintf(fp, "->[%s,%s]", ccs->cc->label, ccs->cc->module);
+ } else {
+ fprintf(fp, "[%s,%s]", ccs->cc->label, ccs->cc->module);
+ }
+
+ if (ccs == CCCS) {
+ fprintf(fp, "\n");
+ }
+}
+
+
+#ifdef DEBUG
+static void
+printCCS ( CostCentreStack *ccs )
+{
+ fprintf(stderr,"<");
+ for (; ccs; ccs = ccs->prevStack ) {
+ fprintf(stderr,ccs->cc->label);
+ if (ccs->prevStack) {
+ fprintf(stderr,",");
+ }
+ }
+ fprintf(stderr,">");
+}
+#endif
+