github.com/undoio/delve@v1.9.0/pkg/proc/native/proc_freebsd.c (about)

     1  #include <sys/param.h>
     2  #include <sys/mount.h>
     3  #include <sys/queue.h>
     4  #include <sys/sysctl.h>
     5  #include <sys/user.h>
     6  #include <sys/types.h>
     7  
     8  #include <libprocstat.h>
     9  #include <libutil.h>
    10  #include <stdlib.h>
    11  #include <string.h>
    12  #include <errno.h>
    13  
    14  #include "proc_freebsd.h"
    15  
    16  /*
    17   * Returns the absolute pathname of the process's executable, if one was found.
    18   * Must be freed by the caller. Sets errno on failure.
    19   */
    20  char * find_executable(int pid) {
    21  	struct procstat *ps;
    22  	struct kinfo_proc *kp;
    23  	char *pathname;
    24  
    25  	/*
    26  	 * procstat_open_sysctl is for running processes. For core files, use
    27  	 * procstat_open_core
    28  	 */
    29  	ps = procstat_open_sysctl();
    30  	kp = kinfo_getproc(pid);
    31  	pathname = malloc(MNAMELEN);
    32  	if (ps && kp && pathname)
    33  		procstat_getpathname(ps, kp, pathname, MNAMELEN);
    34  	free(kp);
    35  	procstat_close(ps);
    36  	return (pathname);
    37  }
    38  
    39  /*
    40   * Returns the comm value of the process, which is usually the basename of its
    41   * executable. Must be freed by the caller.  Sets errno on failure.
    42   */
    43  char * find_command_name(int pid) {
    44  	char *command_name = NULL;
    45  	struct kinfo_proc *kinfo;
    46  
    47  	kinfo = kinfo_getproc(pid);
    48  	if (kinfo != NULL) {
    49  		command_name = malloc(COMMLEN + 1);
    50  		if (command_name != NULL)
    51  			strlcpy(command_name, kinfo->ki_comm, COMMLEN + 1);
    52  		free(kinfo);
    53  	}
    54  
    55  	return (command_name);
    56  }
    57  
    58  int find_status(int pid){
    59  	char status;
    60  	struct kinfo_proc *kinfo;
    61  	kinfo = kinfo_getproc(pid);
    62  	if (kinfo != NULL)
    63  		status = kinfo->ki_stat;
    64  	else
    65  		status = '?';
    66  	free(kinfo);
    67  
    68  	return (status);
    69  }
    70  
    71  uintptr_t get_entry_point(int pid) {
    72      void *ep = NULL;
    73  
    74      errno = EINVAL;
    75  
    76      struct procstat *ps = procstat_open_sysctl();
    77      if (ps == NULL)
    78          return 0;
    79  
    80      uint cnt = 0;
    81      struct kinfo_proc *kipp = procstat_getprocs(ps, KERN_PROC_PID, pid, &cnt);
    82      if (cnt == 0)
    83          return 0;
    84  
    85      Elf_Auxinfo *auxv = procstat_getauxv(ps, kipp, &cnt);
    86      if (auxv == NULL)
    87          return 0;
    88  
    89      for (int i = 0; i < cnt; i++) {
    90          if (auxv[i].a_type == AT_ENTRY) {
    91              ep = auxv[i].a_un.a_ptr;
    92              break;
    93          }
    94      }
    95      procstat_freeauxv(ps, auxv);
    96      errno = 0;
    97      return (uintptr_t)ep;
    98  }