The kvm_getcmd() function constructs a list of string pointers that represent the command arguments and environment that were used to initiate the process specified by proc.
The kd argument is a pointer to a kernel descriptor returned by kvm_open(3KVM). The u argument is a pointer to a copy in the current process' address space of a user structure, obtained, for instance, by a prior kvm_getu() call. If arg is not NULL, the command line arguments are formed into a null-terminated array of string pointers. The address
of the first such pointer is returned in arg. If env is not NULL, then the environment is formed into a null-terminated array
of string pointers. The address of the first of these is returned in env.
The pointers returned in arg and env refer to data allocated by malloc(3C) and should be freed by a call to free() when no longer needed. See malloc(3C) Both the string pointers and the strings themselves are deallocated when freed.
Since the environment and command line arguments may have been modified by the user process, there is no guarantee that it will be possible to reconstruct the original command at all. Thus, kvm_getcmd() will make the best attempt possible, returning -1 if the user process data is unrecognizable.