int main (int argc, char** argv) { if (argc != 2) { puts("Usage: cc <file>"); return 1; } output = fopen("a.s", "w"); lex_init(argv[1], 256); sym_init(256); //No arrays? Fine! A 0xFFFFFF terminated string of null terminated strings will do. //A negative-terminated null-terminated strings string, if you will char* std_fns = "malloc\0calloc\0free\0atoi\0fopen\0fclose\0fgetc\0ungetc\0feof\0fputs\0fprintf\0puts\0printf\0" "isalpha\0isdigit\0isalnum\0strlen\0strcmp\0strchr\0strcpy\0strdup\0\xFF\xFF\xFF\xFF"; //Remember that mini-c is typeless, so this is both a byte read and a 4 byte read. //(char) 0xFF == -1, (int) 0xFFFFFF == -1 while (std_fns[0] != -1) { new_fn(strdup(std_fns)); std_fns = std_fns+strlen(std_fns)+1; } program(); return errors != 0; }
int main(int argc,char *argv[]) { unsigned long long totalTime; unsigned long tid; bool haveFile = false; FILE *f = stdin; int parser = -1; if(argc < 3) { fprintf(stderr,"Usage: %s <format> <input> [<symbolFile>...]\n",argv[0]); return EXIT_FAILURE; } for(size_t i = 0; i < sizeof(parsers) / sizeof(parsers[0]); i++) { if(strcmp(argv[1],parsers[i].name) == 0) { parser = i; break; } } if(parser == -1) { fprintf(stderr,"'%s' is no known format. Use 'i586' or 'mmix'.\n",argv[1]); return EXIT_FAILURE; } if(strcmp(argv[2],"-") != 0) { haveFile = true; f = fopen(argv[2],"r"); if(!f) perror("fopen"); } sym_init(); for(int i = 3; i < argc; i++) sym_addFile(argv[i]); parsers[parser].parse(f); if(haveFile) fclose(f); /* calculate total time and leave all functions that have not been left so far (e.g. main) */ totalTime = 0; for(tid = 0; tid < contextSize; tid++) { if(contexts[tid].current) { leaveFuncs(contexts[tid].root); totalTime += contexts[tid].root->time; } } /* print header */ printf("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); printf("<functionCalls>\n"); printf(" <fileName>dummyFile</fileName>\n"); printf(" <totalTime>%Lu</totalTime>\n",totalTime); printf(" <totalMem>0</totalMem>\n"); for(tid = 0; tid < contextSize; tid++) { if(contexts[tid].current) printFunc(contexts[tid].root,1); } printf("</functionCalls>\n"); return EXIT_SUCCESS; }
int main(int argc, char** argv) { // Provide argc and argv to interested modules sys_argc = argc; sys_argv = argv; // Initialize memory system mem_init(); // Initialize stack and exception system stack_init(); // Initialize static symbol table sym_init(); // Initialize value klass system klass_init(); // Phase 1 stack_annot_push("init1_Function"); init1_Function(); stack_annot_pop(); stack_annot_push("init1_Object"); init1_Object(); stack_annot_pop(); ripe_module1a(); ripe_module1b(); // Phase 1.5 stack_annot_push("phase 1.5"); common_init_phase15(); klass_init_phase15(); stack_annot_pop(); // Phase 2 stack_annot_push("init2_Function"); init2_Function(); stack_annot_pop(); stack_annot_push("init2_Object"); init2_Object(); stack_annot_pop(); ripe_module2(); ripe_module3(); // Call main. Value rv = ripe_main(); if (is_int64(rv)){ return unpack_int64(rv); } mem_deinit(); return 0; }
static void parse_spec (char *spec, Sym *sym) { char *colon; sym_init (sym); colon = strrchr (spec, ':'); if (colon) { *colon = '\0'; if (colon > spec) { sym->file = source_file_lookup_name (spec); if (!sym->file) sym->file = &non_existent_file; } spec = colon + 1; if (strlen (spec)) { if (ISDIGIT (spec[0])) sym->line_num = atoi (spec); else sym->name = spec; } } else if (strlen (spec)) { /* No colon: spec is a filename if it contains a dot. */ if (strchr (spec, '.')) { sym->file = source_file_lookup_name (spec); if (!sym->file) sym->file = &non_existent_file; } else if (ISDIGIT (*spec)) { sym->line_num = atoi (spec); } else if (strlen (spec)) { sym->name = spec; } } }
Object * clone(Objid pid) { Object *o; int top; if (pid.server != 0 || pid.id < 0) { return 0; } else { top = dbtop() + 1; set_dbtop(top); o = new_object(); o->id.server = 0; o->id.id = top; sym_init(o); o->parents = list_new(1); o->parents->el[0].type = OBJ; o->parents->el[0].v.obj = pid; assign_object(o->id, o); return o; } }
void core_create_line_syms (void) { char *prev_name, *prev_filename; unsigned int prev_name_len, prev_filename_len; bfd_vma vma, min_vma = ~(bfd_vma) 0, max_vma = 0; Sym *prev, dummy, *sym; const char *filename; int prev_line_num; Sym_Table ltab; bfd_vma vma_high; /* Create symbols for functions as usual. This is necessary in cases where parts of a program were not compiled with -g. For those parts we still want to get info at the function level. */ core_create_function_syms (); /* Pass 1: count the number of symbols. */ /* To find all line information, walk through all possible text-space addresses (one by one!) and get the debugging info for each address. When the debugging info changes, it is time to create a new symbol. Of course, this is rather slow and it would be better if BFD would provide an iterator for enumerating all line infos. */ prev_name_len = PATH_MAX; prev_filename_len = PATH_MAX; prev_name = (char *) xmalloc (prev_name_len); prev_filename = (char *) xmalloc (prev_filename_len); ltab.len = 0; prev_line_num = 0; vma_high = core_text_sect->vma + bfd_get_section_size (core_text_sect); for (vma = core_text_sect->vma; vma < vma_high; vma += min_insn_size) { unsigned int len; if (!get_src_info (vma, &filename, &dummy.name, &dummy.line_num) || (prev_line_num == dummy.line_num && prev_name != NULL && strcmp (prev_name, dummy.name) == 0 && filename_cmp (prev_filename, filename) == 0)) continue; ++ltab.len; prev_line_num = dummy.line_num; len = strlen (dummy.name); if (len >= prev_name_len) { prev_name_len = len + 1024; free (prev_name); prev_name = (char *) xmalloc (prev_name_len); } strcpy (prev_name, dummy.name); len = strlen (filename); if (len >= prev_filename_len) { prev_filename_len = len + 1024; free (prev_filename); prev_filename = (char *) xmalloc (prev_filename_len); } strcpy (prev_filename, filename); min_vma = MIN (vma, min_vma); max_vma = MAX (vma, max_vma); } free (prev_name); free (prev_filename); /* Make room for function symbols, too. */ ltab.len += symtab.len; ltab.base = (Sym *) xmalloc (ltab.len * sizeof (Sym)); ltab.limit = ltab.base; /* Pass 2 - create symbols. */ /* We now set is_static as we go along, rather than by running through the symbol table at the end. The old way called symtab_finalize before the is_static pass, causing a problem since symtab_finalize uses is_static as part of its address conflict resolution algorithm. Since global symbols were prefered over static symbols, and all line symbols were global at that point, static function names that conflicted with their own line numbers (static, but labeled as global) were rejected in favor of the line num. This was not the desired functionality. We always want to keep our function symbols and discard any conflicting line symbols. Perhaps symtab_finalize should be modified to make this distinction as well, but the current fix works and the code is a lot cleaner now. */ prev = 0; for (vma = core_text_sect->vma; vma < vma_high; vma += min_insn_size) { sym_init (ltab.limit); if (!get_src_info (vma, &filename, <ab.limit->name, <ab.limit->line_num) || (prev && prev->line_num == ltab.limit->line_num && strcmp (prev->name, ltab.limit->name) == 0 && filename_cmp (prev->file->name, filename) == 0)) continue; /* Make name pointer a malloc'ed string. */ ltab.limit->name = xstrdup (ltab.limit->name); ltab.limit->file = source_file_lookup_path (filename); ltab.limit->addr = vma; /* Set is_static based on the enclosing function, using either: 1) the previous symbol, if it's from the same function, or 2) a symtab lookup. */ if (prev && ltab.limit->file == prev->file && strcmp (ltab.limit->name, prev->name) == 0) { ltab.limit->is_static = prev->is_static; } else { sym = sym_lookup(&symtab, ltab.limit->addr); if (sym) ltab.limit->is_static = sym->is_static; } prev = ltab.limit; DBG (AOUTDEBUG, printf ("[core_create_line_syms] %lu %s 0x%lx\n", (unsigned long) (ltab.limit - ltab.base), ltab.limit->name, (unsigned long) ltab.limit->addr)); ++ltab.limit; } /* Copy in function symbols. */ memcpy (ltab.limit, symtab.base, symtab.len * sizeof (Sym)); ltab.limit += symtab.len; if ((unsigned int) (ltab.limit - ltab.base) != ltab.len) { fprintf (stderr, _("%s: somebody miscounted: ltab.len=%d instead of %ld\n"), whoami, ltab.len, (long) (ltab.limit - ltab.base)); done (1); } /* Finalize ltab and make it symbol table. */ symtab_finalize (<ab); free (symtab.base); symtab = ltab; }
void core_create_function_syms (void) { bfd_vma min_vma = ~ (bfd_vma) 0; bfd_vma max_vma = 0; int cxxclass; long i; struct function_map * found = NULL; int core_has_func_syms = 0; switch (core_bfd->xvec->flavour) { default: break; case bfd_target_coff_flavour: case bfd_target_ecoff_flavour: case bfd_target_xcoff_flavour: case bfd_target_elf_flavour: case bfd_target_nlm_flavour: case bfd_target_som_flavour: core_has_func_syms = 1; } /* Pass 1 - determine upper bound on number of function names. */ symtab.len = 0; for (i = 0; i < core_num_syms; ++i) { if (!core_sym_class (core_syms[i])) continue; /* Don't create a symtab entry for a function that has a mapping to a file, unless it's the first function in the file. */ if (symbol_map_count != 0) { /* Note: some systems (SunOS 5.8) crash if bsearch base argument is NULL. */ found = (struct function_map *) bsearch (core_syms[i]->name, symbol_map, symbol_map_count, sizeof (struct function_map), search_mapped_symbol); } if (found == NULL || found->is_first) ++symtab.len; } if (symtab.len == 0) { fprintf (stderr, _("%s: file `%s' has no symbols\n"), whoami, a_out_name); done (1); } symtab.base = (Sym *) xmalloc (symtab.len * sizeof (Sym)); /* Pass 2 - create symbols. */ symtab.limit = symtab.base; for (i = 0; i < core_num_syms; ++i) { asection *sym_sec; cxxclass = core_sym_class (core_syms[i]); if (!cxxclass) { DBG (AOUTDEBUG, printf ("[core_create_function_syms] rejecting: 0x%lx %s\n", (unsigned long) core_syms[i]->value, core_syms[i]->name)); continue; } if (symbol_map_count != 0) { /* Note: some systems (SunOS 5.8) crash if bsearch base argument is NULL. */ found = (struct function_map *) bsearch (core_syms[i]->name, symbol_map, symbol_map_count, sizeof (struct function_map), search_mapped_symbol); } if (found && ! found->is_first) continue; sym_init (symtab.limit); /* Symbol offsets are always section-relative. */ sym_sec = core_syms[i]->section; symtab.limit->addr = core_syms[i]->value; if (sym_sec) symtab.limit->addr += bfd_get_section_vma (sym_sec->owner, sym_sec); if (found) { symtab.limit->name = found->file_name; symtab.limit->mapped = 1; } else { symtab.limit->name = core_syms[i]->name; symtab.limit->mapped = 0; } /* Lookup filename and line number, if we can. */ { const char * filename; const char * func_name; if (get_src_info (symtab.limit->addr, & filename, & func_name, & symtab.limit->line_num)) { symtab.limit->file = source_file_lookup_path (filename); /* FIXME: Checking __osf__ here does not work with a cross gprof. */ #ifdef __osf__ /* Suppress symbols that are not function names. This is useful to suppress code-labels and aliases. This is known to be useful under DEC's OSF/1. Under SunOS 4.x, labels do not appear in the symbol table info, so this isn't necessary. */ if (strcmp (symtab.limit->name, func_name) != 0) { /* The symbol's address maps to a different name, so it can't be a function-entry point. This happens for labels, for example. */ DBG (AOUTDEBUG, printf ("[core_create_function_syms: rej %s (maps to %s)\n", symtab.limit->name, func_name)); continue; } #endif } } symtab.limit->is_func = (!core_has_func_syms || (core_syms[i]->flags & BSF_FUNCTION) != 0); symtab.limit->is_bb_head = TRUE; if (cxxclass == 't') symtab.limit->is_static = TRUE; /* Keep track of the minimum and maximum vma addresses used by all symbols. When computing the max_vma, use the ending address of the section containing the symbol, if available. */ min_vma = MIN (symtab.limit->addr, min_vma); if (sym_sec) max_vma = MAX (bfd_get_section_vma (sym_sec->owner, sym_sec) + bfd_section_size (sym_sec->owner, sym_sec) - 1, max_vma); else max_vma = MAX (symtab.limit->addr, max_vma); DBG (AOUTDEBUG, printf ("[core_create_function_syms] %ld %s 0x%lx\n", (long) (symtab.limit - symtab.base), symtab.limit->name, (unsigned long) symtab.limit->addr)); ++symtab.limit; } symtab.len = symtab.limit - symtab.base; symtab_finalize (&symtab); }
void core_create_syms_from (const char * sym_table_file) { const int BUFSIZE = 1024; char * buf = (char *) xmalloc (BUFSIZE); char * address = (char *) xmalloc (BUFSIZE); char type; char * name = (char *) xmalloc (BUFSIZE); bfd_vma min_vma = ~(bfd_vma) 0; bfd_vma max_vma = 0; FILE * f; f = fopen (sym_table_file, "r"); if (!f) { fprintf (stderr, _("%s: could not open %s.\n"), whoami, sym_table_file); done (1); } /* Pass 1 - determine upper bound on number of function names. */ symtab.len = num_of_syms_in (f); if (symtab.len == 0) { fprintf (stderr, _("%s: file `%s' has no symbols\n"), whoami, sym_table_file); done (1); } symtab.base = (Sym *) xmalloc (symtab.len * sizeof (Sym)); /* Pass 2 - create symbols. */ symtab.limit = symtab.base; if (fseek (f, 0, SEEK_SET) != 0) { perror (sym_table_file); done (1); } while (!feof (f) && fgets (buf, BUFSIZE - 1, f)) { if (sscanf (buf, "%s %c %s", address, &type, name) == 3) if (type != 't' && type != 'T') continue; sym_init (symtab.limit); sscanf (address, "%" BFD_VMA_FMT "x", &(symtab.limit->addr) ); symtab.limit->name = (char *) xmalloc (strlen (name) + 1); strcpy ((char *) symtab.limit->name, name); symtab.limit->mapped = 0; symtab.limit->is_func = TRUE; symtab.limit->is_bb_head = TRUE; symtab.limit->is_static = (type == 't'); min_vma = MIN (symtab.limit->addr, min_vma); max_vma = MAX (symtab.limit->addr, max_vma); ++symtab.limit; } fclose (f); symtab.len = symtab.limit - symtab.base; symtab_finalize (&symtab); free (buf); free (address); free (name); }
/* * The main function. */ int main(int argc, char *argv[]) { int ret = 1, debug_level = 0; FILE *log = NULL, *dump = NULL; char c; /* * The numatop requires some authorities to setup perf and increase the hard limit of * fd. So either the user runs as root or following condtions need to be satisfied. * * 1. Set "-1" in /proc/sys/kernel/perf_event_paranoid to let the non-root be able to * setup perf. e.g. * echo -1 > /proc/sys/kernel/perf_event_paranoid * * 2. The CAP_SYS_RESOURCE capability is required to user for increasing the hard * limit of fd. */ g_sortkey = SORT_KEY_CPU; g_precise = PRECISE_NORMAL; g_numatop_pid = getpid(); g_run_secs = TIME_NSEC_MAX; optind = 1; opterr = 0; /* * Parse command line arguments. */ while ((c = getopt(argc, argv, "d:l:o:f:t:hf:s:")) != EOF) { switch (c) { case 'h': print_usage(argv[0]); ret = 0; goto L_EXIT0; case 'l': debug_level = atoi(optarg); if ((debug_level < 0) || (debug_level > 2)) { stderr_print("Invalid log_level %d.\n", debug_level); print_usage(argv[0]); goto L_EXIT0; } break; case 'f': if (optarg == NULL) { stderr_print("Invalid output file.\n"); goto L_EXIT0; } if ((log = fopen(optarg, "w")) == NULL) { stderr_print("Cannot open '%s' for writing.\n", optarg); goto L_EXIT0; } break; case 's': if (optarg == NULL) { print_usage(argv[0]); goto L_EXIT0; } if (strcasecmp(optarg, "high") == 0) { g_precise = PRECISE_HIGH; break; } if (strcasecmp(optarg, "low") == 0) { g_precise = PRECISE_LOW; break; } if (strcasecmp(optarg, "normal") == 0) { g_precise = PRECISE_NORMAL; break; } stderr_print("Invalid sampling_precision '%s'.\n", optarg); print_usage(argv[0]); goto L_EXIT0; case 'd': if (optarg == NULL) { stderr_print("Invalid dump file.\n"); goto L_EXIT0; } if ((dump = fopen(optarg, "w")) == NULL) { stderr_print("Cannot open '%s' for dump.\n", optarg); goto L_EXIT0; } break; case 't': g_run_secs = atoi(optarg); if (g_run_secs <= 0) { stderr_print("Invalid run time %d.\n", g_run_secs); print_usage(argv[0]); goto L_EXIT0; } break; case ':': stderr_print("Missed argument for option %c.\n", optopt); print_usage(argv[0]); goto L_EXIT0; case '?': stderr_print("Unrecognized option %c.\n", optopt); print_usage(argv[0]); goto L_EXIT0; } } if (plat_detect() != 0) { stderr_print("CPU is not supported!\n"); ret = 2; goto L_EXIT0; } /* * It could be failed if user doesn't have authority. */ (void) ulimit_expand(PERF_FD_NUM); /* * Get the number of online cores in system. */ if ((g_ncpus = sysfs_online_ncpus()) == -1) { stderr_print("Platform is not supported " "(numatop supports up to %d CPUs)\n", NCPUS_MAX); goto L_EXIT0; } pagesize_init(); gettimeofday(&g_tvbase, 0); if (debug_init(debug_level, log) != 0) { goto L_EXIT1; } debug_print(NULL, 2, "Detected %d online CPU.\n", g_ncpus); log = NULL; sym_init(); if (dump_init(dump) != 0) { goto L_EXIT2; } dump = NULL; /* * Calculate how many nanoseconds for a TSC cycle. */ calibrate(); /* * Initialize for the "window-switching" table. */ switch_table_init(); if (proc_group_init() != 0) { goto L_EXIT3; } if (node_group_init() != 0) { stderr_print("The node/cpu number is out of range, \n" "numatop supports up to %d nodes and %d CPUs\n", NNODES_MAX, NCPUS_MAX); goto L_EXIT4; } if (disp_cons_ctl_init() != 0) { goto L_EXIT5; } /* * Catch signals from terminal. */ if ((signal(SIGINT, sigint_handler) == SIG_ERR) || (signal(SIGHUP, sigint_handler) == SIG_ERR) || (signal(SIGQUIT, sigint_handler) == SIG_ERR) || (signal(SIGTERM, sigint_handler) == SIG_ERR) || (signal(SIGPIPE, sigint_handler) == SIG_ERR)) { goto L_EXIT6; } /* * Initialize the perf sampling facility. */ if (perf_init() != 0) { debug_print(NULL, 2, "perf_init() is failed\n"); goto L_EXIT6; } /* * Initialize for display and create console thread & display thread. */ if (disp_init() != 0) { perf_fini(); goto L_EXIT6; } /* * Wait the disp thread to exit. The disp thread would * exit when user hits the hotkey 'Q' or press "CTRL+C". */ disp_dispthr_quit_wait(); /* * Notify cons thread to exit. */ disp_consthr_quit(); disp_fini(); stderr_print("NumaTOP is exiting ...\n"); (void) fflush(stdout); ret = 0; L_EXIT6: disp_cons_ctl_fini(); L_EXIT5: node_group_fini(); L_EXIT4: proc_group_fini(); L_EXIT3: dump_fini(); L_EXIT2: sym_fini(); debug_fini(); L_EXIT1: exit_msg_print(); L_EXIT0: if (dump != NULL) { (void) fclose(dump); } if (log != NULL) { (void) fclose(log); } return (ret); }
/* * On the Alpha we can only detect PC relative calls, which are * usually generated for calls to functions within the same * object file only. This is still better than nothing, however. * (In particular it should be possible to find functions that * potentially call integer division routines, for example.) */ void alpha_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc) { bfd_vma pc, dest_pc; unsigned int insn; Sym *child; if (indirect_child.name == NULL) { sym_init (&indirect_child); indirect_child.name = _("<indirect child>"); indirect_child.cg.prop.fract = 1.0; indirect_child.cg.cyc.head = &indirect_child; } DBG (CALLDEBUG, printf (_("[find_call] %s: 0x%lx to 0x%lx\n"), parent->name, (unsigned long) p_lowpc, (unsigned long) p_highpc)); for (pc = (p_lowpc + 3) & ~(bfd_vma) 3; pc < p_highpc; pc += 4) { insn = bfd_get_32 (core_bfd[0], ((unsigned char *) core_text_space + pc - core_text_sect->vma)); switch (insn & (0x3f << 26)) { case OP_Jxx << 26: /* * There is no simple and reliable way to determine the * target of a jsr (the hint bits help, but there aren't * enough bits to get a satisfactory hit rate). Instead, * for any indirect jump we simply add an arc from PARENT * to INDIRECT_CHILD---that way the user it at least able * to see that there are other calls as well. */ if ((insn & (3 << 14)) == Jxx_FUNC_JSR << 14 || (insn & (3 << 14)) == Jxx_FUNC_JSR_COROUTINE << 14) { DBG (CALLDEBUG, printf (_("[find_call] 0x%lx: jsr%s <indirect_child>\n"), (unsigned long) pc, ((insn & (3 << 14)) == Jxx_FUNC_JSR << 14 ? "" : "_coroutine"))); arc_add (parent, &indirect_child, (unsigned long) 0); } break; case OP_BSR << 26: DBG (CALLDEBUG, printf (_("[find_call] 0x%lx: bsr"), (unsigned long) pc)); /* * Regular PC relative addressing. Check that this is the * address of a function. The linker sometimes redirects * the entry point by 8 bytes to skip loading the global * pointer, so we allow for either address: */ dest_pc = pc + 4 + (((bfd_signed_vma) (insn & 0x1fffff) ^ 0x100000) - 0x100000); if (hist_check_address (dest_pc)) { child = sym_lookup (&symtab, dest_pc); if (child) { DBG (CALLDEBUG, printf (" 0x%lx\t; name=%s, addr=0x%lx", (unsigned long) dest_pc, child->name, (unsigned long) child->addr)); if (child->addr == dest_pc || child->addr == dest_pc - 8) { DBG (CALLDEBUG, printf ("\n")); /* a hit: */ arc_add (parent, child, (unsigned long) 0); continue; } } } /* * Something funny going on. */ DBG (CALLDEBUG, printf ("\tbut it's a botch\n")); break; default: break; } } }