interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / zero_cuda_tool_stub.c

69 lines · 1955 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Stand-in for ldd and nm. The zero-CUDA argv test copies this binary onto PATH
3 * under both names. It records argv and prints a CUDA line only when the named
4 * file itself contains a marker. It does not run a shell. */
5#include "omega_path_check.h"
6
7#include <fcntl.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <unistd.h>
12
13static const char *base_name(const char *path) {
14 const char *slash = strrchr(path, '/');
15 return slash ? slash + 1 : path;
16}
17
18static void record_argv(int argc, char **argv) {
19 const char *env = getenv("ZERO_CUDA_ARGV_LOG");
20 char log_path[4096];
21 int fd;
22 FILE *log;
23 int i;
24
25 if (omega_path_copy(log_path, sizeof log_path, env) != 0) return;
26 fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC | O_NOFOLLOW, 0600);
27 if (fd < 0) return;
28 log = fdopen(fd, "a");
29 if (!log) {
30 close(fd);
31 return;
32 }
33 fprintf(log, "argc %d\n", argc);
34 for (i = 0; i < argc; i++) {
35 size_t n = strlen(argv[i]);
36 fprintf(log, "arg %d %zu\n", i, n);
37 fwrite(argv[i], 1, n, log);
38 fputc('\n', log);
39 }
40 fclose(log);
41}
42
43int main(int argc, char **argv) {
44 const char *tool = base_name(argv[0]);
45 const char *path = NULL;
46 FILE *f;
47 char buf[4096];
48 size_t n;
49
50 record_argv(argc, argv);
51 if (strcmp(tool, "nm") == 0) {
52 if (argc >= 3) path = argv[2];
53 } else if (argc >= 2) {
54 path = argv[1];
55 }
56 if (!path) return 0;
57 f = fopen(path, "rb");
58 if (!f) return 0;
59 n = fread(buf, 1, sizeof(buf) - 1, f);
60 fclose(f);
61 buf[n] = '\0';
62 if (strcmp(tool, "ldd") == 0 && strstr(buf, "LIBCUDA_NEEDED")) {
63 printf("\tlibcuda.so.1 => /tmp/libcuda.so.1 (0x0000)\n");
64 }
65 if (strcmp(tool, "nm") == 0 && strstr(buf, "CU_SYMBOL")) {
66 printf(" U cuInit\n");
67 }
68 return 0;
69}