- if ((entry->pdesc->p_pid == -1)
- && !strncmp(entry->pdesc->p_comm, &p->p_comm[0], sizeof(p->p_comm)))
- {
+ /* By default consider we're using p_comm. */
+ char *pname = p->p_comm;
+
+ /* Already matched with another process. */
+ if ((entry->pdesc->p_pid != -1))
+ continue;
+
+ /* p_comm is too short, use the execpath. */
+ if (entry->pdesc->p_name_length >= MAXCOMLEN) {
+ /*
+ * Retrieve the executable path. After the call, length contains
+ * the length of the string + 1.
+ */
+ int length = sizeof(execpath);
+ if (dtrace_proc_get_execpath(p, execpath, &length) != 0)
+ continue;
+ /* Move the cursor to the position after the last / */
+ pname = &execpath[length - 1];
+ while (pname != execpath && *pname != '/')
+ pname--;
+ pname = (*pname == '/') ? pname + 1 : pname;
+ }
+
+ if (!strcmp(entry->pdesc->p_name, pname)) {