- cmd = (struct load_command *) &mh[1];
- for (i = 0; i < mh->ncmds; i++) {
- if (cmd->cmd == LC_SEGMENT) {
- struct segment_command *orig_sg = (struct segment_command *) cmd;
-
- if (strcmp(SEG_TEXT, orig_sg->segname) == 0)
- orig_ts = orig_sg;
- else if (strcmp(SEG_LINKEDIT, orig_sg->segname) == 0)
- orig_le = orig_sg;
- else if (strcmp("", orig_sg->segname) == 0)
- orig_ts = orig_sg; /* kexts have a single unnamed segment */
- }
- else if (cmd->cmd == LC_SYMTAB)
- orig_st = (struct symtab_command *) cmd;
-
- cmd = (struct load_command *) ((caddr_t) cmd + cmd->cmdsize);
- }
-
- if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL))
- return;
-
- sym = (struct nlist *)orig_le->vmaddr;
- strings = ((char *)sym) + orig_st->nsyms * sizeof(struct nlist);
-
- for (i = 0; i < orig_st->nsyms; i++) {
- uint8_t n_type = sym[i].n_type & (N_TYPE | N_EXT);
- char *name = strings + sym[i].n_un.n_strx;
- char *prev_name;
- unsigned long best;
- unsigned int j;
-
- /* Check that the symbol is a global and that it has a name. */
- if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type))
- continue;
-
- if (0 == sym[i].n_un.n_strx) /* iff a null, "", name. */
- continue;
-
- /* Lop off omnipresent leading underscore. */
- if (*name == '_')
- name += 1;
-
- if (strstr(name, "_dtrace_probe$")) {
- sdt_probedesc_t *sdpd = kmem_alloc(sizeof(sdt_probedesc_t), KM_SLEEP);
- int len = strlen(name) + 1;
-
- sdpd->sdpd_name = kmem_alloc(len, KM_SLEEP);
- strncpy(sdpd->sdpd_name, name, len); /* NUL termination is ensured. */
-
- prev_name = "<unknown>";
- best = 0;
- for (j = 0; j < orig_st->nsyms; j++) {
- uint8_t n_type = sym[j].n_type & (N_TYPE | N_EXT);
- char *name = strings + sym[j].n_un.n_strx;
-
- if (((N_SECT | N_EXT) != n_type && (N_ABS | N_EXT) != n_type))
- continue;
-
- if (0 == sym[j].n_un.n_strx) /* iff a null, "", name. */
- continue;
-
- if (*name == '_')
- name += 1;
- if (strstr(name, "_dtrace_probe$"))
- continue;
-
- if (*(unsigned long *)sym[i].n_value <= (unsigned long)sym[j].n_value)
- continue;
-
- if ((unsigned long)sym[j].n_value > best) {
- best = (unsigned long)sym[j].n_value;
- prev_name = name;
- }
- }
-
- sdpd->sdpd_func = kmem_alloc((len = strlen(prev_name) + 1), KM_SLEEP);
- strncpy(sdpd->sdpd_func, prev_name, len); /* NUL termination is ensured. */
-
- sdpd->sdpd_offset = *(unsigned long *)sym[i].n_value;
-
- sdpd->sdpd_next = g_sdt_mach_module.sdt_probes;
- g_sdt_mach_module.sdt_probes = sdpd;
- } else {
- prev_name = name;
+ cmd = (struct load_command*) &mh[1];
+ for (i = 0; i < mh->ncmds; i++) {
+ if (cmd->cmd == LC_SEGMENT_KERNEL) {
+ kernel_segment_command_t *orig_sg = (kernel_segment_command_t *) cmd;
+
+ if (LIT_STRNEQL(orig_sg->segname, SEG_TEXT)) {
+ orig_ts = orig_sg;
+ } else if (LIT_STRNEQL(orig_sg->segname, SEG_LINKEDIT)) {
+ orig_le = orig_sg;
+ } else if (LIT_STRNEQL(orig_sg->segname, "")) {
+ orig_ts = orig_sg; /* kexts have a single unnamed segment */
+ }
+ } else if (cmd->cmd == LC_SYMTAB) {
+ orig_st = (struct symtab_command *) cmd;
+ }
+
+ cmd = (struct load_command *) ((uintptr_t) cmd + cmd->cmdsize);
+ }
+
+ /* Locate DTrace SDT section in the object. */
+ if ((orig_dt = getsectbyname("__DATA", "__sdt")) == NULL) {
+ printf("DTrace section not found.\n");
+ return;
+ }
+
+ if ((orig_ts == NULL) || (orig_st == NULL) || (orig_le == NULL)) {
+ return;
+ }
+
+ sym = (kernel_nlist_t *)(orig_le->vmaddr + orig_st->symoff - orig_le->fileoff);
+ strings = (char *)(orig_le->vmaddr + orig_st->stroff - orig_le->fileoff);
+
+ /*
+ * Iterate over SDT section and establish all SDT probes.
+ */
+ dtrace_sdt_def_t *sdtdef = (dtrace_sdt_def_t *)(orig_dt->addr);
+ for (size_t k = 0; k < orig_dt->size / sizeof(dtrace_sdt_def_t); k++, sdtdef++) {
+ const char *funcname;
+ unsigned long best;
+
+ sdt_probedesc_t *sdpd = kmem_alloc(sizeof(sdt_probedesc_t), KM_SLEEP);
+
+ /* Unescape probe name and keep a note of the size of original memory allocation. */
+ sdpd->sdpd_name = sdt_strdup_name(sdtdef->dsd_name);
+ sdpd->sdpd_namelen = strlen(sdtdef->dsd_name) + 1;
+
+ /* Used only for provider structure lookup so there is no need to make dynamic copy. */
+ sdpd->sdpd_prov = sdtdef->dsd_prov;
+
+ /*
+ * Find the symbol immediately preceding the sdt probe site just discovered,
+ * that symbol names the function containing the sdt probe.
+ */
+ funcname = "<unknown>";
+ for (i = 0; i < orig_st->nsyms; i++) {
+ uint8_t jn_type = sym[i].n_type & N_TYPE;
+ char *jname = strings + sym[i].n_un.n_strx;
+
+ if ((N_SECT != jn_type && N_ABS != jn_type)) {
+ continue;
+ }
+
+ if (0 == sym[i].n_un.n_strx) { /* iff a null, "", name. */
+ continue;
+ }
+
+ if (*jname == '_') {
+ jname += 1;
+ }
+
+ if (sdtdef->dsd_addr <= (unsigned long)sym[i].n_value) {
+ continue;
+ }
+
+ if ((unsigned long)sym[i].n_value > best) {
+ best = (unsigned long)sym[i].n_value;
+ funcname = jname;