示例#1
0
文件: main.c 项目: sujay219/freebsd
static int
kvmd_init(void)
{
	char errbuf[_POSIX2_LINE_MAX];

	if (kvmd != NULL)
		return (0);

	kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
	setgid(getgid());

	if (kvmd == NULL) {
		warnx("kvm not available: %s", errbuf);
		return (-1);
	}

	if (kvm_nlist(kvmd, nl) < 0) {
		if (nlistf)
			errx(1, "%s: kvm_nlist: %s", nlistf,
			     kvm_geterr(kvmd));
		else
			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
	}

	if (nl[0].n_type == 0) {
		if (nlistf)
			errx(1, "%s: no namelist", nlistf);
		else
			errx(1, "no namelist");
	}

	return (0);
}
示例#2
0
void
kgdb_trgt_fetch_registers(int regno __unused)
{
#ifndef CROSS_DEBUGGER
	struct kthr *kt;
	struct pcb pcb;
	int i, reg;

	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
	if (kt == NULL)
		return;
	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
		warnx("kvm_read: %s", kvm_geterr(kvm));
		memset(&pcb, 0, sizeof(pcb));
	}
	for (i = ARM_A1_REGNUM + 8; i <= ARM_SP_REGNUM; i++) {
		supply_register(i, (char *)&pcb.un_32.pcb32_r8 +
		    (i - (ARM_A1_REGNUM + 8 )) * 4);
	}
	if (pcb.un_32.pcb32_sp != 0) {
		for (i = 0; i < 4; i++) {
			if (kvm_read(kvm, pcb.un_32.pcb32_sp + (i) * 4,
			    &reg, 4) != 4) {
				warnx("kvm_read: %s", kvm_geterr(kvm));
				break;
			}
			supply_register(ARM_A1_REGNUM + 4 + i, (char *)&reg);
		}
		if (kvm_read(kvm, pcb.un_32.pcb32_sp + 4 * 4, &reg, 4) != 4)
			warnx("kvm_read :%s", kvm_geterr(kvm));
		else
			supply_register(ARM_PC_REGNUM, (char *)&reg);
	}
#endif
}
示例#3
0
static void
fill_pcpu(struct pcpu ***pcpup, int* maxcpup)
{
	struct pcpu **pcpu;
	
	int maxcpu, i;

	*pcpup = NULL;
	
	if (kd == NULL)
		return;

	maxcpu = kvm_getmaxcpu(kd);
	if (maxcpu < 0)
		errx(1, "kvm_getmaxcpu: %s", kvm_geterr(kd));

	pcpu = calloc(maxcpu, sizeof(struct pcpu *));
	if (pcpu == NULL)
		err(1, "calloc");

	for (i = 0; i < maxcpu; i++) {
		pcpu[i] = kvm_getpcpu(kd, i);
		if (pcpu[i] == (struct pcpu *)-1)
			errx(1, "kvm_getpcpu: %s", kvm_geterr(kd));
	}

	*maxcpup = maxcpu;
	*pcpup = pcpu;
}
示例#4
0
static void
fstat_kvm(int what, int arg)
{
	struct kinfo_proc *p, *plast;
	char buf[_POSIX2_LINE_MAX];
	int cnt;

	ALLOC_OFILES(256);	/* reserve space for file pointers */

	/*
	 * Discard setgid privileges if not the running kernel so that bad
	 * guys can't print interesting stuff from kernel memory.
	 */
	if (nlistf != NULL || memf != NULL)
		setgid(getgid());

	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
		errx(1, "%s", buf);
	setgid(getgid());
#ifdef notdef
	if (kvm_nlist(kd, nl) != 0)
		errx(1, "no namelist: %s", kvm_geterr(kd));
#endif
	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
		errx(1, "%s", kvm_geterr(kd));
	print_header();
	for (plast = &p[cnt]; p < plast; ++p) {
		if (p->ki_stat == SZOMB)
			continue;
		dofiles(p);
		if (mflg)
			dommap(p);
	}
}
示例#5
0
文件: trpt.c 项目: SylvestreG/bitrig
void
dotrace(caddr_t tcpcb)
{
	struct tcp_debug *td;
	int prev_debx = tcp_debx;
	int i;

 again:
	if (--tcp_debx < 0)
		tcp_debx = TCP_NDEBUG - 1;
	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
		td = &tcp_debug[i];
		if (tcpcb && td->td_tcb != tcpcb)
			continue;
		ntime = ntohl(td->td_time);
		tcp_trace(td->td_act, td->td_ostate,
		    &td->td_cb, &td->td_ti,
		    &td->td_ti6, td->td_req);
		if (i == tcp_debx)
			goto done;
	}
	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
		td = &tcp_debug[i];
		if (tcpcb && td->td_tcb != tcpcb)
			continue;
		ntime = ntohl(td->td_time);
		tcp_trace(td->td_act, td->td_ostate,
		    &td->td_cb, &td->td_ti,
		    &td->td_ti6, td->td_req);
	}
 done:
	if (follow) {
		prev_debx = tcp_debx + 1;
		if (prev_debx >= TCP_NDEBUG)
			prev_debx = 0;
		do {
			sleep(1);
			if (kvm_read(kd, nl[N_TCP_DEBX].n_value,
			    (char *)&tcp_debx, sizeof(tcp_debx)) !=
			    sizeof(tcp_debx))
				errx(3, "tcp_debx: %s", kvm_geterr(kd));
		} while (tcp_debx == prev_debx);

		if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
		    sizeof(tcp_debug)) != sizeof(tcp_debug))
			errx(3, "tcp_debug: %s", kvm_geterr(kd));

		goto again;
	}
}
示例#6
0
void
kgdb_trgt_fetch_registers(int regno __unused)
{
#ifndef	CROSS_DEBUGGER
	struct kthr *kt;
	struct pcb pcb;

	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
	if (kt == NULL)
		return;
	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
		warnx("kvm_read: %s", kvm_geterr(kvm));
		memset(&pcb, 0, sizeof(pcb));
	}

	supply_register(MIPS_S0_REGNUM, (char *)&pcb.pcb_context[PCB_REG_S0]);
	supply_register(MIPS_S1_REGNUM, (char *)&pcb.pcb_context[PCB_REG_S1]);
	supply_register(MIPS_S2_REGNUM, (char *)&pcb.pcb_context[PCB_REG_S2]);
	supply_register(MIPS_S3_REGNUM, (char *)&pcb.pcb_context[PCB_REG_S3]);
	supply_register(MIPS_S4_REGNUM, (char *)&pcb.pcb_context[PCB_REG_S4]);
	supply_register(MIPS_S5_REGNUM, (char *)&pcb.pcb_context[PCB_REG_S5]);
	supply_register(MIPS_S6_REGNUM, (char *)&pcb.pcb_context[PCB_REG_S6]);
	supply_register(MIPS_S7_REGNUM, (char *)&pcb.pcb_context[PCB_REG_S7]);
	supply_register(MIPS_SP_REGNUM, (char *)&pcb.pcb_context[PCB_REG_SP]);
	supply_register(MIPS_FP_REGNUM, (char *)&pcb.pcb_context[PCB_REG_GP]);
	supply_register(MIPS_RA_REGNUM, (char *)&pcb.pcb_context[PCB_REG_RA]);
	supply_register(MIPS_EMBED_PC_REGNUM, (char *)&pcb.pcb_context[PCB_REG_PC]);
#endif
}
示例#7
0
static kvm_t *
kopen(char const *memf)
{
	kvm_t	*kvmd = NULL;
	char	 errbuf[_POSIX2_LINE_MAX];

	kvmd = kvm_openfiles(NULL, memf, NULL, O_RDONLY, errbuf);
	if (setgid(getgid()) != 0)
		err(1, "setgid");
	if (kvmd == NULL) {
		warnx("kvm_openfiles: %s", errbuf);
		return (NULL);
	}

	if (kvm_nlist(kvmd, nl) < 0) {
		warnx("kvm_nlist: %s", kvm_geterr(kvmd));
		goto fail;
	}

	if (nl[0].n_type == 0) {
		warnx("kvm_nlist: no namelist");
		goto fail;
	}

	return (kvmd);
fail:
	kvm_close(kvmd);

	return (NULL);
} /* kopen */
示例#8
0
文件: iostat.c 项目: tomtor/freebsd
static int
readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len)
{
    if (kd != NULL) {
        ssize_t nbytes;

        nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len);

        if (nbytes < 0) {
            warnx("kvm_read(%s): %s", namelist[nlid].n_name,
                  kvm_geterr(kd));
            return (1);
        } else if ((size_t)nbytes != len) {
            warnx("kvm_read(%s): expected %zu bytes, got %zd bytes",
                  namelist[nlid].n_name, len, nbytes);
            return (1);
        }
    } else {
        size_t nlen = len;

        if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
            warn("sysctl(%s...) failed", name);
            return (1);
        }
        if (nlen != len) {
            warnx("sysctl(%s...): expected %lu, got %lu", name,
                  (unsigned long)len, (unsigned long)nlen);
            return (1);
        }
    }
    return (0);
}
示例#9
0
void
kgdb_trgt_fetch_registers(int regno __unused)
{
	struct kthr *kt;
	struct pcb pcb;
	struct gdbarch_tdep *tdep;
	int i;

	tdep = gdbarch_tdep (current_gdbarch);

	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
	if (kt == NULL)
		return;
	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
		warnx("kvm_read: %s", kvm_geterr(kvm));
		memset(&pcb, 0, sizeof(pcb));
	}

	/*
	 * r14-r31 are saved in the pcb
	 */
	for (i = 14; i <= 31; i++) {
		supply_register(tdep->ppc_gp0_regnum + i,
		    (char *)&pcb.pcb_context[i]);
	}

	/* r1 is saved in the sp field */
	supply_register(tdep->ppc_gp0_regnum + 1, (char *)&pcb.pcb_sp);

	supply_register(tdep->ppc_lr_regnum, (char *)&pcb.pcb_lr);
	supply_register(tdep->ppc_cr_regnum, (char *)&pcb.pcb_cr);
}
示例#10
0
int
main(int ac, char **av)
{
    const char *corefile = NULL;
    const char *sysfile = NULL;
    struct kinfo_proc *kp;
    kvm_t *kd;
    int ch;
    int i;
    int nprocs;

    while ((ch = getopt(ac, av, "M:N:v")) != -1) {
	switch(ch) {
	case 'v':
	    ++verboseopt;
	    break;
	case 'M':
	    corefile = optarg;
	    break;
	case 'N':
	    sysfile = optarg;
	    break;
	default:
	    fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
	    exit(1);
	}
    }
    ac -= optind;
    av += optind;

    if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
	perror("kvm_open");
	exit(1);
    }

    if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nprocs)) == NULL)
	errx(1, "%s", kvm_geterr(kd));

    fprintf(stdout, "%-6s %-6s %-20s %-10s %-5s\n",
	"PID",
	"PPID",
	"COMMAND",
	"LOGIN",
	"NICE");

    for (i = 0; i < nprocs; i++) {
	    fprintf(stdout, "%-6d %-6d %-20s %-10s %-5d\n",
		kp[i].kp_pid,
		kp[i].kp_ppid,
		kp[i].kp_comm,
		kp[i].kp_login,
		kp[i].kp_nice);
    }

    kvm_close(kd);

    return 0;

}
示例#11
0
/*
 * Reset the kernel profiling date structures.
 */
void
reset(struct kvmvars *kvp)
{
	char *zbuf;
	u_long biggest;
	int mib[3];

	setprof(kvp, GMON_PROF_OFF);

	biggest = kvp->gpm.kcountsize;
	if (kvp->gpm.fromssize > biggest)
		biggest = kvp->gpm.fromssize;
	if (kvp->gpm.tossize > biggest)
		biggest = kvp->gpm.tossize;
	if ((zbuf = (char *)malloc(biggest)) == NULL)
		errx(12, "cannot allocate zbuf space");
	bzero(zbuf, biggest);
	if (kflag) {
		if (kvm_write(kvp->kd, (u_long)kvp->gpm.kcount, zbuf,
		    kvp->gpm.kcountsize) != (ssize_t)kvp->gpm.kcountsize)
			errx(13, "tickbuf zero: %s", kvm_geterr(kvp->kd));
		if (kvm_write(kvp->kd, (u_long)kvp->gpm.froms, zbuf,
		    kvp->gpm.fromssize) != (ssize_t)kvp->gpm.fromssize)
			errx(14, "froms zero: %s", kvm_geterr(kvp->kd));
		if (kvm_write(kvp->kd, (u_long)kvp->gpm.tos, zbuf,
		    kvp->gpm.tossize) != (ssize_t)kvp->gpm.tossize)
			errx(15, "tos zero: %s", kvm_geterr(kvp->kd));
		return;
	}
	(void)seteuid(0);
	mib[0] = CTL_KERN;
	mib[1] = KERN_PROF;
	mib[2] = GPROF_COUNT;
	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.kcountsize) < 0)
		err(13, "tickbuf zero");
	mib[2] = GPROF_FROMS;
	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.fromssize) < 0)
		err(14, "froms zero");
	mib[2] = GPROF_TOS;
	if (sysctl(mib, 3, NULL, NULL, zbuf, kvp->gpm.tossize) < 0)
		err(15, "tos zero");
	(void)seteuid(getuid());
	free(zbuf);
}
示例#12
0
文件: kthr.c 项目: AhmadTux/freebsd
static void
kgdb_thr_add_procs(uintptr_t paddr)
{
	struct proc p;
	struct thread td;
	struct kthr *kt;
	CORE_ADDR addr;

	while (paddr != 0) {
		if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p)) {
			warnx("kvm_read: %s", kvm_geterr(kvm));
			break;
		}
		addr = (uintptr_t)TAILQ_FIRST(&p.p_threads);
		while (addr != 0) {
			if (kvm_read(kvm, addr, &td, sizeof(td)) !=
			    sizeof(td)) {
				warnx("kvm_read: %s", kvm_geterr(kvm));
				break;
			}
			kt = malloc(sizeof(*kt));
			kt->next = first;
			kt->kaddr = addr;
			if (td.td_tid == dumptid)
				kt->pcb = dumppcb;
			else if (td.td_state == TDS_RUNNING && stoppcbs != 0 &&
			    CPU_ISSET(td.td_oncpu, &stopped_cpus))
				kt->pcb = (uintptr_t)stoppcbs +
				    sizeof(struct pcb) * td.td_oncpu;
			else
				kt->pcb = (uintptr_t)td.td_pcb;
			kt->kstack = td.td_kstack;
			kt->tid = td.td_tid;
			kt->pid = p.p_pid;
			kt->paddr = paddr;
			kt->cpu = td.td_oncpu;
			first = kt;
			addr = (uintptr_t)TAILQ_NEXT(&td, td_plist);
		}
		paddr = (uintptr_t)LIST_NEXT(&p, p_list);
	}
}
示例#13
0
文件: main.c 项目: Alkzndr/freebsd
/*
 * Resolve symbol list, return 0 on success.
 */
int
kresolve_list(struct nlist *_nl)
{

	if ((kvmd == NULL) && (kvmd_init() != 0))
		return (-1);

	if (_nl[0].n_type != 0)
		return (0);

	if (kvm_nlist(kvmd, _nl) < 0) {
		if (nlistf)
			errx(1, "%s: kvm_nlist: %s", nlistf,
			     kvm_geterr(kvmd));
		else
			errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
	}

	return (0);
}
示例#14
0
static int kread(u_long addr, void *buf, int size) {
  int status;

  status = kvm_read(kvmd, addr, buf, size);
  if (status != size) {
    ERROR("tcpconns plugin: kvm_read failed (got %i, expected %i): %s\n",
          status, size, kvm_geterr(kvmd));
    return (-1);
  }
  return (0);
} /* int kread */
示例#15
0
文件: main.c 项目: ralphost/NextBSD
/*
 * Wrapper of kvm_dpcpu_setcpu().
 */
void
kset_dpcpu(u_int cpuid)
{

	if ((kvmd == NULL) && (kvmd_init() != 0))
		xo_errx(-1, "%s: kvm is not available", __func__);

	if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
		xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
		    cpuid, kvm_geterr(kvmd)); 
	return;
}
示例#16
0
static void
domemstat_malloc(void)
{
	struct memory_type_list *mtlp;
	struct memory_type *mtp;
	int error, first, i;

	mtlp = memstat_mtl_alloc();
	if (mtlp == NULL) {
		warn("memstat_mtl_alloc");
		return;
	}
	if (kd == NULL) {
		if (memstat_sysctl_malloc(mtlp, 0) < 0) {
			warnx("memstat_sysctl_malloc: %s",
			    memstat_strerror(memstat_mtl_geterror(mtlp)));
			return;
		}
	} else {
		if (memstat_kvm_malloc(mtlp, kd) < 0) {
			error = memstat_mtl_geterror(mtlp);
			if (error == MEMSTAT_ERROR_KVM)
				warnx("memstat_kvm_malloc: %s",
				    kvm_geterr(kd));
			else
				warnx("memstat_kvm_malloc: %s",
				    memstat_strerror(error));
		}
	}
	printf("%13s %5s %6s %7s %8s  Size(s)\n", "Type", "InUse", "MemUse",
	    "HighUse", "Requests");
	for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
	    mtp = memstat_mtl_next(mtp)) {
		if (memstat_get_numallocs(mtp) == 0 &&
		    memstat_get_count(mtp) == 0)
			continue;
		printf("%13s %5" PRIu64 " %5" PRIu64 "K %7s %8" PRIu64 "  ",
		    memstat_get_name(mtp), memstat_get_count(mtp),
		    (memstat_get_bytes(mtp) + 1023) / 1024, "-",
		    memstat_get_numallocs(mtp));
		first = 1;
		for (i = 0; i < 32; i++) {
			if (memstat_get_sizemask(mtp) & (1 << i)) {
				if (!first)
					printf(",");
				printf("%d", 1 << (i + 4));
				first = 0;
			}
		}
		printf("\n");
	}
	memstat_mtl_free(mtlp);
}
示例#17
0
static int
kread(kvm_t *kvmd, u_long addr, char *buffer, int size)
{
	if (kvmd == NULL || buffer == NULL)
		return (-1);

	if (kvm_read(kvmd, addr, buffer, size) != size) {
		warnx("kvm_read: %s", kvm_geterr(kvmd));
		return (-1);
	}

	return (0);
} /* kread */
示例#18
0
GHashTable *
ck_unix_pid_get_env_hash (pid_t pid)
{
        GHashTable       *hash = NULL;
        char            **penv;
        char              errbuf[_POSIX2_LINE_MAX];
        kvm_t            *kd;
        struct kinfo_proc p;
        int               i;

        kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf);
        if (kd == NULL) {
		g_warning ("kvm_openfiles failed: %s", errbuf);
                return NULL;
        }

        if (! get_kinfo_proc (pid, &p)) {
		g_warning ("get_kinfo_proc failed: %s", g_strerror (errno));
		goto fail;
        }

        penv = kvm_getenvv (kd, &p, 0);
        if (penv == NULL) {
		g_warning ("kvm_getenvv failed: %s", kvm_geterr (kd));
		goto fail;
        }

        hash = g_hash_table_new_full (g_str_hash,
                                      g_str_equal,
                                      g_free,
                                      g_free);

        for (i = 0; penv[i] != NULL; i++) {
                char **vals;

                if (!penv[i][0]) continue;

                vals = g_strsplit (penv[i], "=", 2);
                if (vals != NULL) {
                        g_hash_table_insert (hash,
                                             g_strdup (vals[0]),
                                             g_strdup (vals[1]));
                        g_strfreev (vals);
                }
        }

fail:
        kvm_close (kd);

        return hash;
}
示例#19
0
/*
 * Get the state of kernel profiling.
 */
int
getprof(struct kvmvars *kvp)
{
	size_t size;
	int mib[3];

	if (kflag) {
		size = kvm_read(kvp->kd, nl[N_GMONPARAM].n_value, &kvp->gpm,
		    sizeof kvp->gpm);
	} else {
		mib[0] = CTL_KERN;
		mib[1] = KERN_PROF;
		mib[2] = GPROF_GMONPARAM;
		size = sizeof kvp->gpm;
		if (sysctl(mib, 3, &kvp->gpm, &size, NULL, 0) < 0)
			size = 0;
	}

	/*
	 * Accept certain undersized "structs" from old kernels.  We need
	 * everything up to hashfraction, and want profrate and
	 * histcounter_type.  Assume that the kernel doesn't put garbage
	 * in any padding that is returned instead of profrate and
	 * histcounter_type.  This is a bad assumption for dead kernels,
	 * since kvm_read() will normally return garbage for bytes beyond
	 * the end of the actual kernel struct, if any.
	 */
	if (size < offsetof(struct gmonparam, hashfraction) +
	    sizeof(kvp->gpm.hashfraction) || size > sizeof(kvp->gpm))
		errx(4, "cannot get gmonparam: %s",
		    kflag ? kvm_geterr(kvp->kd) : strerror(errno));
	bzero((char *)&kvp->gpm + size, sizeof(kvp->gpm) - size);
	if (kvp->gpm.profrate == 0)
		kvp->gpm.profrate = getprofhz(kvp);
#ifdef __i386__
	if (kvp->gpm.histcounter_type == 0) {
		/*
		 * This fixup only works for not-so-old i386 kernels.  The
		 * magic 16 is the kernel FUNCTION_ALIGNMENT.  64-bit
		 * counters are signed; smaller counters are unsigned.
		 */
		kvp->gpm.histcounter_type = 16 /
		    (kvp->gpm.textsize / kvp->gpm.kcountsize) * CHAR_BIT;
		if (kvp->gpm.histcounter_type == 64)
			kvp->gpm.histcounter_type = -64;
	}
#endif

	return (kvp->gpm.state);
}
示例#20
0
/*
 * Read kernel memory, return 0 on success.
 */
int
kread(u_long addr, char *buf, int size)
{
	if (kvmd == NULL) {
		/*
		 * XXX.
		 */
		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
		if (kvmd != NULL) {
			if (kvm_nlist(kvmd, nl) < 0) {
				if(nlistf)
					errx(1, "%s: kvm_nlist: %s", nlistf,
					     kvm_geterr(kvmd));
				else
					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
			}

			if (nl[0].n_type == 0) {
				if(nlistf)
					errx(1, "%s: no namelist", nlistf);
				else
					errx(1, "no namelist");
			}
		} else {
			warnx("kvm not available");
			return(-1);
		}
	}
	if (!buf)
		return (0);
	if (kvm_read(kvmd, addr, buf, size) != size) {
		warnx("%s", kvm_geterr(kvmd));
		return (-1);
	}
	return (0);
}
示例#21
0
static void
kgdb_trgt_close(int quitting)
{

	if (kvm != NULL) {
		inferior_ptid = null_ptid;
		clear_solib();
		if (kvm_close(kvm) != 0)
			warning("cannot close \"%s\": %s", vmcore,
			    kvm_geterr(kvm));
		kvm = NULL;
		xfree(vmcore);
		vmcore = NULL;
	}
}
示例#22
0
文件: main.c 项目: sujay219/freebsd
/*
 * Read kernel memory, return 0 on success.
 */
int
kread(u_long addr, void *buf, size_t size)
{

	if (kvmd_init() < 0)
		return (-1);

	if (!buf)
		return (0);
	if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
		warnx("%s", kvm_geterr(kvmd));
		return (-1);
	}
	return (0);
}
示例#23
0
static void
domemstat_zone(void)
{
	struct memory_type_list *mtlp;
	struct memory_type *mtp;
	char name[MEMTYPE_MAXNAME + 1];
	int error;

	mtlp = memstat_mtl_alloc();
	if (mtlp == NULL) {
		warn("memstat_mtl_alloc");
		return;
	}
	if (kd == NULL) {
		if (memstat_sysctl_uma(mtlp, 0) < 0) {
			warnx("memstat_sysctl_uma: %s",
			    memstat_strerror(memstat_mtl_geterror(mtlp)));
			return;
		}
	} else {
		if (memstat_kvm_uma(mtlp, kd) < 0) {
			error = memstat_mtl_geterror(mtlp);
			if (error == MEMSTAT_ERROR_KVM)
				warnx("memstat_kvm_uma: %s",
				    kvm_geterr(kd));
			else
				warnx("memstat_kvm_uma: %s",
				    memstat_strerror(error));
		}
	}
	printf("%-20s %6s %6s %8s %8s %8s %4s %4s\n\n", "ITEM", "SIZE",
	    "LIMIT", "USED", "FREE", "REQ", "FAIL", "SLEEP");
	for (mtp = memstat_mtl_first(mtlp); mtp != NULL;
	    mtp = memstat_mtl_next(mtp)) {
		strlcpy(name, memstat_get_name(mtp), MEMTYPE_MAXNAME);
		strcat(name, ":");
		printf("%-20s %6" PRIu64 ", %6" PRIu64 ",%8" PRIu64 ",%8" PRIu64
		    ",%8" PRIu64 ",%4" PRIu64 ",%4" PRIu64 "\n", name,
		    memstat_get_size(mtp), memstat_get_countlimit(mtp),
		    memstat_get_count(mtp), memstat_get_free(mtp),
		    memstat_get_numallocs(mtp), memstat_get_failures(mtp),
		    memstat_get_sleeps(mtp));
	}
	memstat_mtl_free(mtlp);
	printf("\n");
}
示例#24
0
/*
 * kread reads something from the kernel, given its nlist index.
 */
static void
kread(int nlx, void *addr, size_t size)
{
	const char *sym;

	if (namelist[nlx].n_type == 0 || namelist[nlx].n_value == 0) {
		sym = namelist[nlx].n_name;
		if (*sym == '_')
			++sym;
		errx(1, "symbol %s not defined", sym);
	}
	if (kvm_read(kd, namelist[nlx].n_value, addr, size) != (ssize_t)size) {
		sym = namelist[nlx].n_name;
		if (*sym == '_')
			++sym;
		errx(1, "%s: %s", sym, kvm_geterr(kd));
	}
}
示例#25
0
static char *
kgetstr(const char *strp)
{
	int n = 0, size = 1;
	char *ret = NULL;

	do {
		if (size == n + 1) {
			ret = realloc(ret, size);
			if (ret == NULL)
				err(1, "%s: realloc", __func__);
			size *= 2;
		}
		if (kvm_read(kd, (u_long)strp + n, &ret[n], 1) != 1)
			errx(1, "%s: %s", __func__, kvm_geterr(kd));
	} while (ret[n++] != '\0');
	return (ret);
}
示例#26
0
static void
kgdb_trgt_close(int quitting)
{

	if (kvm != NULL) {		
		inferior_ptid = null_ptid;
		CLEAR_SOLIB();
		if (kvm_close(kvm) != 0)
			warning("cannot close \"%s\": %s", vmcore,
			    kvm_geterr(kvm));
		kvm = NULL;
		xfree(vmcore);
		vmcore = NULL;
		if (kgdb_trgt_ops.to_sections) {
			xfree(kgdb_trgt_ops.to_sections);
			kgdb_trgt_ops.to_sections = NULL;
			kgdb_trgt_ops.to_sections_end = NULL;
		}
	}
}
示例#27
0
int
kcore_get_generic(struct kcore_data *kc, struct nlist *nl,
		  void *data, size_t len)
{
	if (kc == NULL)
		kc = &kcore_global;

	if (nl[0].n_value == 0) {
		if ((kvm_nlist(kc->kd, nl) < 0) || (nl[0].n_value == 0)) {
			errno = EOPNOTSUPP;
			return(-1);
		}
	}
	if (kvm_read(kc->kd, nl[0].n_value, data, len) != (int)len) {
		warnx("cannot read %s: %s", nl[0].n_name, kvm_geterr(kc->kd));
		errno = EINVAL;
		return(-1);
	}
	return(0);
}
示例#28
0
文件: kernel.c 项目: Undrizzle/apps
int
klookup(unsigned long off, char *target, int siz)
{
    int             result;
    if (kd == NULL)
        return 0;
    result = kvm_read(kd, off, target, siz);
    if (result != siz) {
#if HAVE_KVM_OPENFILES
        snmp_log(LOG_ERR, "kvm_read(*, %lx, %p, %d) = %d: %s\n", off,
                 target, siz, result, kvm_geterr(kd));
#else
        snmp_log(LOG_ERR, "kvm_read(*, %lx, %p, %d) = %d: ", off, target,
                 siz, result);
        snmp_log_perror("klookup");
#endif
        return 0;
    }
    return 1;
}
示例#29
0
void
kgdb_trgt_fetch_registers(int regno __unused)
{
	struct kthr *kt;
	struct pcb pcb;

	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
	if (kt == NULL)
		return;
	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
		warnx("kvm_read: %s", kvm_geterr(kvm));
		memset(&pcb, 0, sizeof(pcb));
	}

	supply_register(SPARC_SP_REGNUM, (char *)&pcb.pcb_sp);
	sparc_supply_rwindow(current_regcache, pcb.pcb_sp, -1);
	supply_register(SPARC64_PC_REGNUM, (char *)&pcb.pcb_pc);
	pcb.pcb_pc += 4;
	supply_register(SPARC64_NPC_REGNUM, (char *)&pcb.pcb_pc);
}
示例#30
0
void
kgdb_trgt_fetch_registers(int regno __unused)
{
	struct kthr *kt;
	struct pcb pcb;

	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
	if (kt == NULL)
		return;
	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
		warnx("kvm_read: %s", kvm_geterr(kvm));
		memset(&pcb, 0, sizeof(pcb));
	}
	supply_register(I386_EBX_REGNUM, (char *)&pcb.pcb_ebx);
	supply_register(I386_ESP_REGNUM, (char *)&pcb.pcb_esp);
	supply_register(I386_EBP_REGNUM, (char *)&pcb.pcb_ebp);
	supply_register(I386_ESI_REGNUM, (char *)&pcb.pcb_esi);
	supply_register(I386_EDI_REGNUM, (char *)&pcb.pcb_edi);
	supply_register(I386_EIP_REGNUM, (char *)&pcb.pcb_eip);
}