1 /* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 pid_t
*ustcmd_get_online_pids(void)
32 struct dirent
*dirent
;
34 unsigned int ret_size
= 1 * sizeof(pid_t
), i
= 0;
36 dir
= opendir(SOCK_DIR
);
41 pid_t
*ret
= (pid_t
*) malloc(ret_size
);
43 while ((dirent
= readdir(dir
))) {
44 if (!strcmp(dirent
->d_name
, ".") ||
45 !strcmp(dirent
->d_name
, "..")) {
50 if (dirent
->d_type
!= DT_DIR
&&
51 !!strcmp(dirent
->d_name
, "ustd")) {
53 sscanf(dirent
->d_name
, "%u", (unsigned int *) &ret
[i
]);
54 if (pid_is_online(ret
[i
])) {
55 ret_size
+= sizeof(pid_t
);
56 ret
= (pid_t
*) realloc(ret
, ret_size
);
62 ret
[i
] = 0; /* Array end */
75 * Sets marker state (USTCMD_MS_ON or USTCMD_MS_OFF).
77 * @param mn Marker name
78 * @param state Marker's new state
79 * @param pid Traced process ID
80 * @return 0 if successful, or errors {USTCMD_ERR_GEN, USTCMD_ERR_ARG}
82 int ustcmd_set_marker_state(const char *mn
, int state
, pid_t pid
)
84 char *cmd_str
[] = {"disable_marker", "enable_marker"};
89 return USTCMD_ERR_ARG
;
92 asprintf(&cmd
, "%s %s", cmd_str
[state
], mn
);
94 result
= ustcmd_send_cmd(cmd
, pid
, NULL
);
97 return USTCMD_ERR_GEN
;
105 * Destroys an UST trace according to a PID.
107 * @param pid Traced process ID
108 * @return 0 if successful, or error USTCMD_ERR_GEN
110 int ustcmd_destroy_trace(pid_t pid
)
114 result
= ustcmd_send_cmd("destroy", pid
, NULL
);
116 return USTCMD_ERR_GEN
;
123 * Starts an UST trace (and setups it) according to a PID.
125 * @param pid Traced process ID
126 * @return 0 if successful, or error USTCMD_ERR_GEN
128 int ustcmd_setup_and_start(pid_t pid
)
132 result
= ustcmd_send_cmd("start", pid
, NULL
);
134 return USTCMD_ERR_GEN
;
141 * Starts an UST trace according to a PID.
143 * @param pid Traced process ID
144 * @return 0 if successful, or error USTCMD_ERR_GEN
146 int ustcmd_start_trace(pid_t pid
)
150 result
= ustcmd_send_cmd("trace_start", pid
, NULL
);
152 return USTCMD_ERR_GEN
;
159 * Stops an UST trace according to a PID.
161 * @param pid Traced process ID
162 * @return 0 if successful, or error USTCMD_ERR_GEN
164 int ustcmd_stop_trace(pid_t pid
)
168 result
= ustcmd_send_cmd("trace_stop", pid
, NULL
);
170 return USTCMD_ERR_GEN
;
177 * Counts newlines ('\n') in a string.
179 * @param str String to search in
180 * @return Total newlines count
182 unsigned int ustcmd_count_nl(const char *str
)
184 unsigned int i
= 0, tot
= 0;
186 while (str
[i
] != '\0') {
187 if (str
[i
] == '\n') {
197 * Frees a CMSF array.
199 * @param cmsf CMSF array to free
200 * @return 0 if successful, or error USTCMD_ERR_ARG
202 int ustcmd_free_cmsf(struct marker_status
*cmsf
)
205 return USTCMD_ERR_ARG
;
209 while (cmsf
[i
].channel
!= NULL
) {
210 free(cmsf
[i
].channel
);
211 free(cmsf
[i
].marker
);
221 * Gets channel/marker/state/format string for a given PID.
223 * @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
224 * frees with `ustcmd_free_cmsf')
225 * @param pid Targeted PID
226 * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_GEN}
228 int ustcmd_get_cmsf(struct marker_status
**cmsf
, const pid_t pid
)
230 char *big_str
= NULL
;
232 struct marker_status
*tmp_cmsf
= NULL
;
233 unsigned int i
= 0, cmsf_ind
= 0;
236 return USTCMD_ERR_ARG
;
238 result
= ustcmd_send_cmd("list_markers", pid
, &big_str
);
240 return USTCMD_ERR_GEN
;
243 if (big_str
== NULL
) {
244 fprintf(stderr
, "ustcmd: error while getting markers list\n");
245 return USTCMD_ERR_GEN
;
248 tmp_cmsf
= (struct marker_status
*) malloc(sizeof(struct marker_status
) *
249 (ustcmd_count_nl(big_str
) + 1));
250 if (tmp_cmsf
== NULL
) {
251 return USTCMD_ERR_GEN
;
254 /* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
255 while (big_str
[i
] != '\0') {
258 sscanf(big_str
+ i
, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
259 &tmp_cmsf
[cmsf_ind
].channel
,
260 &tmp_cmsf
[cmsf_ind
].marker
,
262 &tmp_cmsf
[cmsf_ind
].fs
);
263 tmp_cmsf
[cmsf_ind
].state
= (state
== USTCMD_MS_CHR_ON
?
264 USTCMD_MS_ON
: USTCMD_MS_OFF
); /* Marker state */
266 while (big_str
[i
] != '\n') {
267 ++i
; /* Go to next '\n' */
269 ++i
; /* Skip current pointed '\n' */
272 tmp_cmsf
[cmsf_ind
].channel
= NULL
;
273 tmp_cmsf
[cmsf_ind
].marker
= NULL
;
274 tmp_cmsf
[cmsf_ind
].fs
= NULL
;
283 * Shoots a given command using ustcomm.
285 * @param cmd Null-terminated command to shoot
286 * @param pid Targeted PID
287 * @param reply Pointer to string to be filled with a reply string (must
288 * be NULL if no reply is needed for the given command).
289 * @return 0 if successful, or errors {USTCMD_ERR_ARG, USTCMD_ERR_CONN}
292 int ustcmd_send_cmd(const char *cmd
, const pid_t pid
, char **reply
)
294 struct ustcomm_connection conn
;
297 return USTCMD_ERR_ARG
;
300 if (ustcomm_connect_app(pid
, &conn
)) {
301 fprintf(stderr
, "ustcmd_send_cmd: could not connect to PID %u\n",
303 return USTCMD_ERR_CONN
;
306 ustcomm_send_request(&conn
, cmd
, reply
);
This page took 0.04303 seconds and 4 git commands to generate.