qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

linux-user: fix /proc/self/stat handling

In the original bug report long files names in Guix caused
/proc/self/stat be truncated without the trailing ") " as specified in
proc manpage which says:
(2) comm %s
The filename of the executable, in parentheses. This
is visible whether or not the executable is swapped
out.

In the kernel this is currently done by do_task_stat calling
proc_task_name() which uses a structure limited by TASK_COMM_LEN (16).

Additionally it should only be reporting the executable name rather
than the full path. Fix both these failings while cleaning up the code
to use GString to build up the reported values. As the whole function
is cleaned up also adjust the white space to the current coding style.

Message-ID: <fb4c55fa-d539-67ee-c6c9-de8fb63c8488@inria.fr>
Reported-by: Brice Goglin <Brice.Goglin@inria.fr>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20200414200631.12799-10-alex.bennee@linaro.org>

+19 -24
+19 -24
linux-user/syscall.c
··· 7295 7295 { 7296 7296 CPUState *cpu = env_cpu((CPUArchState *)cpu_env); 7297 7297 TaskState *ts = cpu->opaque; 7298 - abi_ulong start_stack = ts->info->start_stack; 7298 + g_autoptr(GString) buf = g_string_new(NULL); 7299 7299 int i; 7300 7300 7301 7301 for (i = 0; i < 44; i++) { 7302 - char buf[128]; 7303 - int len; 7304 - uint64_t val = 0; 7302 + if (i == 0) { 7303 + /* pid */ 7304 + g_string_printf(buf, FMT_pid " ", getpid()); 7305 + } else if (i == 1) { 7306 + /* app name */ 7307 + gchar *bin = g_strrstr(ts->bprm->argv[0], "/"); 7308 + bin = bin ? bin + 1 : ts->bprm->argv[0]; 7309 + g_string_printf(buf, "(%.15s) ", bin); 7310 + } else if (i == 27) { 7311 + /* stack bottom */ 7312 + g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack); 7313 + } else { 7314 + /* for the rest, there is MasterCard */ 7315 + g_string_printf(buf, "0%c", i == 43 ? '\n' : ' '); 7316 + } 7305 7317 7306 - if (i == 0) { 7307 - /* pid */ 7308 - val = getpid(); 7309 - snprintf(buf, sizeof(buf), "%"PRId64 " ", val); 7310 - } else if (i == 1) { 7311 - /* app name */ 7312 - snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]); 7313 - } else if (i == 27) { 7314 - /* stack bottom */ 7315 - val = start_stack; 7316 - snprintf(buf, sizeof(buf), "%"PRId64 " ", val); 7317 - } else { 7318 - /* for the rest, there is MasterCard */ 7319 - snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' '); 7320 - } 7321 - 7322 - len = strlen(buf); 7323 - if (write(fd, buf, len) != len) { 7324 - return -1; 7325 - } 7318 + if (write(fd, buf->str, buf->len) != buf->len) { 7319 + return -1; 7320 + } 7326 7321 } 7327 7322 7328 7323 return 0;