📄 pgcore.c
字号:
ehdr.e_ident[EI_MAG1] = ELFMAG1; ehdr.e_ident[EI_MAG2] = ELFMAG2; ehdr.e_ident[EI_MAG3] = ELFMAG3; ehdr.e_type = ET_CORE; ehdr.e_ident[EI_CLASS] = ELFCLASS32;#if defined(__sparc) ehdr.e_machine = EM_SPARC; ehdr.e_ident[EI_DATA] = ELFDATA2MSB;#elif defined(__i386) || defined(__amd64) ehdr.e_machine = EM_386; ehdr.e_ident[EI_DATA] = ELFDATA2LSB;#else#error "unknown machine type"#endif ehdr.e_ident[EI_VERSION] = EV_CURRENT; ehdr.e_version = EV_CURRENT; ehdr.e_ehsize = sizeof (ehdr); ehdr.e_phentsize = sizeof (Elf32_Phdr); ehdr.e_phnum = (unsigned short)nphdrs; ehdr.e_phoff = ehdr.e_ehsize; if (nshdrs != 0) { ehdr.e_shentsize = sizeof (Elf32_Shdr); ehdr.e_shnum = (unsigned short)nshdrs; ehdr.e_shoff = ehdr.e_phoff + ehdr.e_phentsize * ehdr.e_phnum; ehdr.e_shstrndx = nshdrs - 1; } if (pwrite64(fd, &ehdr, sizeof (ehdr), 0) != sizeof (ehdr)) goto err; poff = ehdr.e_phoff; soff = ehdr.e_shoff + ehdr.e_shentsize; doff = boff = ehdr.e_ehsize + ehdr.e_phentsize * ehdr.e_phnum + ehdr.e_shentsize * ehdr.e_shnum;#ifdef _LP64 } else { Elf64_Ehdr ehdr; bzero(&ehdr, sizeof (ehdr)); ehdr.e_ident[EI_MAG0] = ELFMAG0; ehdr.e_ident[EI_MAG1] = ELFMAG1; ehdr.e_ident[EI_MAG2] = ELFMAG2; ehdr.e_ident[EI_MAG3] = ELFMAG3; ehdr.e_type = ET_CORE; ehdr.e_ident[EI_CLASS] = ELFCLASS64;#if defined(__sparc) ehdr.e_machine = EM_SPARCV9; ehdr.e_ident[EI_DATA] = ELFDATA2MSB;#elif defined(__i386) || defined(__amd64) ehdr.e_machine = EM_AMD64; ehdr.e_ident[EI_DATA] = ELFDATA2LSB;#else#error "unknown machine type"#endif ehdr.e_ident[EI_VERSION] = EV_CURRENT; ehdr.e_version = EV_CURRENT; ehdr.e_ehsize = sizeof (ehdr); ehdr.e_phentsize = sizeof (Elf64_Phdr); ehdr.e_phnum = (unsigned short)nphdrs; ehdr.e_phoff = ehdr.e_ehsize; if (nshdrs != 0) { ehdr.e_shentsize = sizeof (Elf64_Shdr); ehdr.e_shnum = (unsigned short)nshdrs; ehdr.e_shoff = ehdr.e_phoff + ehdr.e_phentsize * ehdr.e_phnum; ehdr.e_shstrndx = nshdrs - 1; } if (pwrite64(fd, &ehdr, sizeof (ehdr), 0) != sizeof (ehdr)) goto err; poff = ehdr.e_phoff; soff = ehdr.e_shoff + ehdr.e_shentsize; doff = boff = sizeof (ehdr) + ehdr.e_phentsize * ehdr.e_phnum + ehdr.e_shentsize * ehdr.e_shnum;#endif /* _LP64 */ } /* * Construct the old-style note header and section. */ if (P->status.pr_dmodel == PR_MODEL_NATIVE) { prpsinfo_t prpsinfo; mkprpsinfo(P, &prpsinfo); if (write_note(fd, NT_PRPSINFO, &prpsinfo, sizeof (prpsinfo_t), &doff) != 0) { goto err; } if (write_note(fd, NT_AUXV, P->auxv, P->nauxv * sizeof (P->auxv[0]), &doff) != 0) { goto err; }#ifdef _LP64 } else { prpsinfo32_t pi32; auxv32_t *av32; size_t size = sizeof (auxv32_t) * P->nauxv; int i; mkprpsinfo32(P, &pi32); if (write_note(fd, NT_PRPSINFO, &pi32, sizeof (prpsinfo32_t), &doff) != 0) { goto err; } if ((av32 = malloc(size)) == NULL) goto err; for (i = 0; i < P->nauxv; i++) { auxv_n_to_32(&P->auxv[i], &av32[i]); } if (write_note(fd, NT_AUXV, av32, size, &doff) != 0) { free(av32); goto err; } free(av32);#endif /* _LP64 */ } if (write_note(fd, NT_PLATFORM, plat, platlen, &doff) != 0) goto err; if (Plwp_iter_all(P, old_per_lwp, &pgc) != 0) goto err; if (P->status.pr_dmodel == PR_MODEL_ILP32) { Elf32_Phdr phdr; bzero(&phdr, sizeof (phdr)); phdr.p_type = PT_NOTE; phdr.p_flags = PF_R; phdr.p_offset = (Elf32_Off)boff; phdr.p_filesz = doff - boff; boff = doff; if (pwrite64(fd, &phdr, sizeof (phdr), poff) != sizeof (phdr)) goto err; poff += sizeof (phdr);#ifdef _LP64 } else { Elf64_Phdr phdr; bzero(&phdr, sizeof (phdr)); phdr.p_type = PT_NOTE; phdr.p_flags = PF_R; phdr.p_offset = boff; phdr.p_filesz = doff - boff; boff = doff; if (pwrite64(fd, &phdr, sizeof (phdr), poff) != sizeof (phdr)) goto err; poff += sizeof (phdr);#endif /* _LP64 */ } /* * Construct the new-style note header and section. */ if (P->status.pr_dmodel == PR_MODEL_NATIVE) { if (write_note(fd, NT_PSINFO, &P->psinfo, sizeof (psinfo_t), &doff) != 0) { goto err; } if (write_note(fd, NT_PSTATUS, &P->status, sizeof (pstatus_t), &doff) != 0) { goto err; } if (write_note(fd, NT_AUXV, P->auxv, P->nauxv * sizeof (P->auxv[0]), &doff) != 0) { goto err; }#ifdef _LP64 } else { psinfo32_t pi32; pstatus32_t ps32; auxv32_t *av32; size_t size = sizeof (auxv32_t) * P->nauxv; int i; psinfo_n_to_32(&P->psinfo, &pi32); if (write_note(fd, NT_PSINFO, &pi32, sizeof (psinfo32_t), &doff) != 0) { goto err; } pstatus_n_to_32(&P->status, &ps32); if (write_note(fd, NT_PSTATUS, &ps32, sizeof (pstatus32_t), &doff) != 0) { goto err; } if ((av32 = malloc(size)) == NULL) goto err; for (i = 0; i < P->nauxv; i++) { auxv_n_to_32(&P->auxv[i], &av32[i]); } if (write_note(fd, NT_AUXV, av32, size, &doff) != 0) { free(av32); goto err; } free(av32);#endif /* _LP64 */ } if (write_note(fd, NT_PLATFORM, plat, platlen, &doff) != 0 || write_note(fd, NT_UTSNAME, &uts, sizeof (uts), &doff) != 0 || write_note(fd, NT_CONTENT, &content, sizeof (content), &doff) != 0) goto err; { prcred_t cred, *cp; size_t size = sizeof (prcred_t); if (Pcred(P, &cred, 0) != 0) goto err; if (cred.pr_ngroups > 0) size += sizeof (gid_t) * (cred.pr_ngroups - 1); if ((cp = malloc(size)) == NULL) goto err; if (Pcred(P, cp, cred.pr_ngroups) != 0 || write_note(fd, NT_PRCRED, cp, size, &doff) != 0) { free(cp); goto err; } free(cp); } { prpriv_t *ppriv; const priv_impl_info_t *pinfo; size_t pprivsz, pinfosz; if ((ppriv = proc_get_priv(P->pid)) == NULL) goto err; pprivsz = PRIV_PRPRIV_SIZE(ppriv); if (write_note(fd, NT_PRPRIV, ppriv, pprivsz, &doff) != 0) { free(ppriv); goto err; } free(ppriv); if ((pinfo = getprivimplinfo()) == NULL) goto err; pinfosz = PRIV_IMPL_INFO_SIZE(pinfo); if (write_note(fd, NT_PRPRIVINFO, pinfo, pinfosz, &doff) != 0) goto err; } if (write_note(fd, NT_ZONENAME, zonename, strlen(zonename) + 1, &doff) != 0) goto err;#if defined(__i386) || defined(__amd64) /* CSTYLED */ { struct ssd *ldtp; size_t size; int nldt; nldt = Pldt(P, NULL, 0); size = sizeof (struct ssd) * nldt; if ((ldtp = malloc(size)) == NULL) goto err; if (Pldt(P, ldtp, nldt) == -1 || write_note(fd, NT_LDT, ldtp, size, &doff) != 0) { free(ldtp); goto err; } free(ldtp); }#endif /* __i386 || __amd64 */ if (Plwp_iter_all(P, new_per_lwp, &pgc) != 0) goto err; if (P->status.pr_dmodel == PR_MODEL_ILP32) { Elf32_Phdr phdr; bzero(&phdr, sizeof (phdr)); phdr.p_type = PT_NOTE; phdr.p_flags = PF_R; phdr.p_offset = (Elf32_Off)boff; phdr.p_filesz = doff - boff; boff = doff; if (pwrite64(fd, &phdr, sizeof (phdr), poff) != sizeof (phdr)) goto err; poff += sizeof (phdr);#ifdef _LP64 } else { Elf64_Phdr phdr; bzero(&phdr, sizeof (phdr)); phdr.p_type = PT_NOTE; phdr.p_flags = PF_R; phdr.p_offset = boff; phdr.p_filesz = doff - boff; boff = doff; if (pwrite64(fd, &phdr, sizeof (phdr), poff) != sizeof (phdr)) goto err; poff += sizeof (phdr);#endif /* _LP64 */ } /* * Construct the headers for each mapping and write out its data * if the content parameter indicates that it should be present * in the core file. */ if (Pmapping_iter(P, dump_map, &pgc) != 0) goto err; if (dump_sections(&pgc) != 0) goto err; if (write_shstrtab(P, &pgc) != 0) goto err; free(pgc.pgc_chunk); return (0);err: /* * Wipe out anything we may have written if there was an error. */ (void) ftruncate64(fd, 0); free(pgc.pgc_chunk); return (-1);}static const char *content_str[] = { "stack", /* CC_CONTENT_STACK */ "heap", /* CC_CONTENT_HEAP */ "shfile", /* CC_CONTENT_SHFILE */ "shanon", /* CC_CONTENT_SHANON */ "text", /* CC_CONTENT_TEXT */ "data", /* CC_CONTENT_DATA */ "rodata", /* CC_CONTENT_RODATA */ "anon", /* CC_CONTENT_ANON */ "shm", /* CC_CONTENT_SHM */ "ism", /* CC_CONTENT_ISM */ "dism", /* CC_CONTENT_DISM */ "ctf", /* CC_CONTENT_CTF */ "symtab", /* CC_CONTENT_SYMTAB */};static uint_t ncontent_str = sizeof (content_str) / sizeof (content_str[0]);#define STREQ(a, b, n) (strlen(b) == (n) && strncmp(a, b, n) == 0)intproc_str2content(const char *str, core_content_t *cp){ const char *cur = str; int add = 1; core_content_t mask, content = 0; for (;;) { for (cur = str; isalpha(*cur); cur++) continue; if (STREQ(str, "default", cur - str)) { mask = CC_CONTENT_DEFAULT; } else if (STREQ(str, "all", cur - str)) { mask = CC_CONTENT_ALL; } else if (STREQ(str, "none", cur - str)) { mask = 0; } else { int i = 0; while (!STREQ(str, content_str[i], cur - str)) { i++; if (i >= ncontent_str) return (-1); } mask = (core_content_t)1 << i; } if (add) content |= mask; else content &= ~mask; switch (*cur) { case '\0': *cp = content; return (0); case '+': add = 1; break; case '-': add = 0; break; default: return (-1); } str = cur + 1; }}static intpopc(core_content_t x){ int i; for (i = 0; x != 0; i++) x &= x - 1; return (i);}intproc_content2str(core_content_t content, char *buf, size_t size){ int nonecnt, defcnt, allcnt; core_content_t mask, bit; int first; uint_t index; size_t n, tot = 0; if (content == 0) return ((int)strlcpy(buf, "none", size)); if (content & ~CC_CONTENT_ALL) return ((int)strlcpy(buf, "<invalid>", size)); nonecnt = popc(content); defcnt = 1 + popc(content ^ CC_CONTENT_DEFAULT); allcnt = 1 + popc(content ^ CC_CONTENT_ALL); if (defcnt <= nonecnt && defcnt <= allcnt) { mask = content ^ CC_CONTENT_DEFAULT; first = 0; tot += (n = strlcpy(buf, "default", size)); if (n > size) n = size; buf += n; size -= n; } else if (allcnt < nonecnt) { mask = content ^ CC_CONTENT_ALL; first = 0; tot += (n = strlcpy(buf, "all", size)); if (n > size) n = size; buf += n; size -= n; } else { mask = content; first = 1; } while (mask != 0) { bit = mask ^ (mask & (mask - 1)); if (!first) { if (size > 1) { *buf = (bit & content) ? '+' : '-'; buf++; size--; } tot++; } index = popc(bit - 1); tot += (n = strlcpy(buf, content_str[index], size)); if (n > size) n = size; buf += n; size -= n; mask ^= bit; first = 0; } return ((int)tot);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -