Debian Codegraph @

src/libsysprof-capture/tests/rewrite-pid.c

raw
1 /* rewrite-pid.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include <gio/gio.h>
24 #include <sysprof-capture.h>
25
26 /* Rewrite a capture file to change PID X to Y */
27
28 static void
29 rewrite_pid (guint8 *data,
30 gsize len,
31 int old_pid,
32 int new_pid)
33 {
34 SysprofCaptureFileHeader header;
35 guint8 *endptr = &data[len];
36 guint8 *frameptr = (guint8 *)&data[sizeof (SysprofCaptureFileHeader)];
37 gboolean swap;
38
39 if (len < sizeof header)
40 return;
41
42 memcpy (&header, data, sizeof header);
43
44 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
45 swap = !header.little_endian;
46 #else
47 swap = header.little_endian;
48 #endif
49
50 if (swap)
51 {
52 old_pid = GUINT16_SWAP_LE_BE (old_pid);
53 new_pid = GUINT16_SWAP_LE_BE (new_pid);
54 }
55
56 while (frameptr < endptr)
57 {
58 SysprofCaptureFrame *frame = (SysprofCaptureFrame *)frameptr;
59
60 if (frame->pid == old_pid)
61 frame->pid = new_pid;
62
63 if (swap)
64 frameptr += GUINT16_SWAP_LE_BE (frame->len);
65 else
66 frameptr += frame->len;
67 }
68 }
69
70 static gboolean
71 parse_args (int argc,
72 char **argv,
73 const char **file,
74 int *old_pid,
75 int *new_pid)
76 {
77 if (argc != 4)
78 return FALSE;
79
80 *file = argv[1];
81
82 if (!g_file_test (*file, G_FILE_TEST_IS_REGULAR))
83 return FALSE;
84
85 *old_pid = g_ascii_strtoll (argv[2], NULL, 10);
86 *new_pid = g_ascii_strtoll (argv[3], NULL, 10);
87
88 return *old_pid != 0 && *new_pid != 0;
89 }
90
91 int main (int argc,
92 char *argv[])
93 {
94 g_autoptr(GBytes) bytes = NULL;
95 g_autoptr(GError) error = NULL;
96 g_autoptr(GFile) gfile = NULL;
97 const char *file;
98 int old;
99 int new;
100
101 if (!parse_args (argc, argv, &file, &old, &new))
102 {
103 g_printerr ("usage: rewrite-pid FILE OLD_PID NEW_PID\n");
104 return 1;
105 }
106
107 gfile = g_file_new_for_commandline_arg (file);
108
109 if (!(bytes = g_file_load_bytes (gfile, NULL, NULL, &error)))
110 {
111 g_printerr ("error: %s\n", error->message);
112 return 1;
113 }
114
115 rewrite_pid ((guint8 *)g_bytes_get_data (bytes, NULL),
116 g_bytes_get_size (bytes),
117 old, new);
118
119 if (!g_file_set_contents (file,
120 (const char *)g_bytes_get_data (bytes, NULL),
121 g_bytes_get_size (bytes),
122 &error))
123 {
124 g_printerr ("error: %s\n", error->message);
125 return 1;
126 }
127
128 return 0;
129 }