Annotation of coherent/d/286_KERNEL/USRSRC/io/tools/unpatch.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * unpatch.c - display value of kernel variable(s)
        !             3:  */
        !             4: #include <stdio.h>
        !             5: #include <l.out.h>
        !             6: #include <string.h>
        !             7: 
        !             8: #define NUMVARS 20     /* number of symbols we can look up */
        !             9: 
        !            10: /*
        !            11:  * Table for namelist.
        !            12:  */
        !            13: struct nlist nl[NUMVARS+1];
        !            14: char suffix[NUMVARS+1][3];     /* keep ":[cils]" for each variable */
        !            15: 
        !            16: /*
        !            17:  * Symbols.
        !            18:  */
        !            19: char   *kfile;         /* Kernel data memory file */
        !            20: char   *nfile;         /* Namelist file */
        !            21: int    kfd;            /* Kernel memory file descriptor */
        !            22: int    pflag;          /* true if patch-compatible output desired */
        !            23: int    var_num;        /* Number of symbols to look up */
        !            24: 
        !            25: main(argc, argv)
        !            26: char *argv[];
        !            27: {
        !            28:        int argn;
        !            29: 
        !            30:        initialise();
        !            31:        /*
        !            32:         * Scan command line.
        !            33:         *
        !            34:         * "-c nfile" means use alternate map file.
        !            35:         * "-p" means output format suitable for patch command
        !            36:         *
        !            37:         * variable names may have ":[cils]" appended to specify
        !            38:         * size of value to be fetched, e.g.
        !            39:         *   unpatch foo:l
        !            40:         * says foo is a long
        !            41:         */
        !            42:        for (argn = 1; argn < argc; argn++) {
        !            43:                if (strcmp(argv[argn], "-c") == 0) {
        !            44:                        if (++argn < argc) {
        !            45:                                nfile = argv[argn];
        !            46:                        } else {
        !            47: fprintf(stderr, "bad kfile spec\n");
        !            48:                                usage();
        !            49:                        }
        !            50:                } else if (strcmp(argv[argn], "-p") == 0) {
        !            51:                        pflag = 1;
        !            52:                } else {
        !            53:                        char p[40], *p2;
        !            54: 
        !            55:                        strcpy(p, argv[argn]);
        !            56:                        if (p2 = strchr(p, ':')){
        !            57:                                if (strlen(p2) == 2) {
        !            58:                                        strcpy(suffix[var_num], p2);
        !            59:                                        *p2 = '\0';
        !            60:                                        strncpy(nl[var_num].n_name, p, NCPLN);
        !            61:                                } else {
        !            62: fprintf(stderr, "bad suffix size\n");
        !            63:                                        usage();
        !            64:                                }
        !            65:                        } else
        !            66:                                strncpy(nl[var_num].n_name, p, NCPLN);
        !            67:                        var_num++;
        !            68:                }
        !            69:        } 
        !            70:        if (var_num == 0) {
        !            71: fprintf(stderr, "no variable to unpatch\n");
        !            72:                usage();
        !            73:        }
        !            74:        execute();
        !            75:        exit(0);
        !            76: }
        !            77: 
        !            78: /*
        !            79:  * Initialise.
        !            80:  */
        !            81: initialise()
        !            82: {
        !            83:        int i;
        !            84: 
        !            85:        for (i = 0; i < NUMVARS+1; i++) {
        !            86:                nl[i].n_name[0] = '\0';
        !            87:                nl[i].n_type = 0;
        !            88:                suffix[i][0] = '\0';
        !            89:                suffix[i][1] = '\0';
        !            90:        }
        !            91:        nfile = "/coherent";
        !            92:        kfile = "/dev/kmem";
        !            93: }
        !            94: 
        !            95: /*
        !            96:  * Print out usage.
        !            97:  */
        !            98: usage()
        !            99: {
        !           100:        panic("Usage: unpatch [-c map_file] [-p] var_name[:cils]...");
        !           101: }
        !           102: 
        !           103: /*
        !           104:  * Display value
        !           105:  */
        !           106: execute()
        !           107: {
        !           108:        int v, size;
        !           109:        long longval, result;
        !           110:        unsigned int intval;
        !           111:        unsigned short shortval;
        !           112:        unsigned char charval;
        !           113: 
        !           114:        if ((kfd = open(kfile, 0)) < 0)
        !           115:                panic("Cannot open %s", kfile);
        !           116:        nlist(nfile, nl);
        !           117:        for (v = 0; v < var_num; v++) {
        !           118:                if (nl[v].n_type == 0)
        !           119: fprintf(stderr, "can't find variable %s in %s\n", nl[v].n_name, nfile);
        !           120:                else {
        !           121:                        switch (suffix[v][1]) {
        !           122:                        case 'c':
        !           123:                                size = sizeof(char);
        !           124:                                kread((long)nl[v].n_value, &charval, size);
        !           125:                                result = (long) charval;
        !           126:                                break;
        !           127:                        case 'i':
        !           128:                        default:
        !           129:                                size = sizeof(int);
        !           130:                                kread((long)nl[v].n_value, &intval, size);
        !           131:                                result = (long) intval;
        !           132:                                break;
        !           133:                        case 'l':
        !           134:                                size = sizeof(long);
        !           135:                                kread((long)nl[v].n_value, &longval, size);
        !           136:                                result = longval;
        !           137:                                break;
        !           138:                        case 's':
        !           139:                                size = sizeof(short);
        !           140:                                kread((long)nl[v].n_value, &shortval, size);
        !           141:                                result = (long) shortval;
        !           142:                                break;
        !           143:                        }
        !           144:                        if (pflag)
        !           145:                                printf("%s=0x%lx%s ", nl[v].n_name,
        !           146:                                  result, suffix[v]);
        !           147:                        else
        !           148:                                printf("%s=0x%lx\n", nl[v].n_name, result);
        !           149:                }
        !           150:        }
        !           151: }
        !           152: 
        !           153: /*
        !           154:  * Read `n' bytes into the buffer `bp' from kernel memory
        !           155:  * starting at seek position `s'.
        !           156:  */
        !           157: kread(s, bp, n)
        !           158: long s;
        !           159: {
        !           160:        lseek(kfd, (long)s, 0);
        !           161:        if (read(kfd, bp, n) != n)
        !           162:                panic("Kernel memory read error");
        !           163: }
        !           164: 
        !           165: /*
        !           166:  * Print out an error message and exit.
        !           167:  */
        !           168: panic(a1)
        !           169: char *a1;
        !           170: {
        !           171:        fflush(stdout);
        !           172:        fprintf(stderr, "%r", &a1);
        !           173:        fprintf(stderr, "\n");
        !           174:        exit(1);
        !           175: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.