1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
/* Copyright (C) 2009 Pierre-Marc Fournier, Philippe Proulx-Barrette
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include "ustcomm.h"
#include "ustcmd.h"
#include "usterr.h"
pid_t *ustcmd_get_online_pids(void)
{
struct dirent *dirent;
DIR *dir;
unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
dir = opendir(SOCK_DIR);
if (!dir) {
return NULL;
}
pid_t *ret = (pid_t *) malloc(ret_size);
while ((dirent = readdir(dir))) {
if (!strcmp(dirent->d_name, ".") ||
!strcmp(dirent->d_name, "..")) {
continue;
}
if (dirent->d_type != DT_DIR &&
!!strcmp(dirent->d_name, "ustd")) {
sscanf(dirent->d_name, "%u", (unsigned int *) &ret[i]);
if (pid_is_online(ret[i])) {
ret_size += sizeof(pid_t);
ret = (pid_t *) realloc(ret, ret_size);
++i;
}
}
}
ret[i] = 0; /* Array end */
if (ret[0] == 0) {
/* No PID at all */
free(ret);
return NULL;
}
closedir(dir);
return ret;
}
/**
* Sets marker state (USTCMD_MS_ON or USTCMD_MS_OFF).
*
* @param mn Marker name
* @param state Marker's new state
* @param pid Traced process ID
* @return 0 if successful, or errors {USTCMD_ERR_GEN, USTCMD_ERR_ARG}
*/
int ustcmd_set_marker_state(const char *mn, int state, pid_t pid)
{
char *cmd_str [] = {"disable_marker", "enable_marker"};
char *cmd;
int result;
if (mn == NULL) {
return USTCMD_ERR_ARG;
}
asprintf(&cmd, "%s %s", cmd_str[state], mn);
result = ustcmd_send_cmd(cmd, pid, NULL);
if (result) {
free(cmd);
return USTCMD_ERR_GEN;
}
free(cmd);
return 0;
}
/**
* Set subbuffer size.
*
* @param channel_size Channel name and size
* @param pid Traced process ID
* @return 0 if successful, or error
*/
int ustcmd_set_subbuf_size(const char *channel_size, pid_t pid)
{
char *cmd;
int result;
asprintf(&cmd, "%s %s", "set_subbuf_size", channel_size);
result = ustcmd_send_cmd(cmd, pid, NULL);
if (result != 1) {
free(cmd);
return 1;
}
free(cmd);
return 0;
}
/**
* Set subbuffer num.
*
* @param channel_num Channel name and num
* @param pid Traced process ID
* @return 0 if successful, or error
*/
int ustcmd_set_subbuf_num(const char *channel_size, pid_t pid)
{
char *cmd;
int result;
asprintf(&cmd, "%s %s", "set_subbuf_num", channel_size);
result = ustcmd_send_cmd(cmd, pid, NULL);
if (result != 1) {
free(cmd);
return 1;
}
free(cmd);
return 0;
}
/**
* Get subbuffer size.
*
* @param channel Channel name
* @param pid Traced process ID
* @return subbuf size if successful, or error
*/
int ustcmd_get_subbuf_size(const char *channel, pid_t pid)
{
char *cmd, *reply;
int result;
/* format: channel_cpu */
asprintf(&cmd, "%s %s_0", "get_subbuf_size", channel);
result = ustcmd_send_cmd(cmd, pid, &reply);
if (result) {
free(cmd);
free(reply);
return -1;
}
result = atoi(reply);
free(cmd);
free(reply);
return result;
}
/**
* Get subbuffer num.
*
* @param channel Channel name
* @param pid Traced process ID
* @return subbuf cnf if successful, or error
*/
int ustcmd_get_subbuf_num(const char *channel, pid_t pid)
{
char *cmd, *reply;
int result;
/* format: channel_cpu */
asprintf(&cmd, "%s %s_0", "get_n_subbufs", channel);
result = ustcmd_send_cmd(cmd, pid, &reply);
if (result) {
free(cmd);
free(reply);
return -1;
}
result = atoi(reply);
free(cmd);
free(reply);
return result;
}
/**
* Destroys an UST trace according to a PID.
*
* @param pid Traced process ID
* @return 0 if successful, or error USTCMD_ERR_GEN
*/
int ustcmd_destroy_trace(pid_t pid)
{
int result;
result = ustcmd_send_cmd("trace_destroy", pid, NULL);
if (result != 1) {
return USTCMD_ERR_GEN;
}
return 0;
}
/**
* Starts an UST trace (and setups it) according to a PID.
*
* @param pid Traced process ID
* @return 0 if successful, or error USTCMD_ERR_GEN
*/
int ustcmd_setup_and_start(pid_t pid)
{
int result;
result = ustcmd_send_cmd("start", pid, NULL);
if (result != 1) {
return USTCMD_ERR_GEN;
}
return 0;
}
/**
* Creates an UST trace according to a PID.
*
* @param pid Traced process ID
* @return 0 if successful, or error USTCMD_ERR_GEN
*/
int ustcmd_create_trace(pid_t pid)
{
int result;
result = ustcmd_send_cmd("trace_create", pid, NULL);
if (result != 1) {
return USTCMD_ERR_GEN;
}
return 0;
}
/**
* Starts an UST trace according to a PID.
*
* @param pid Traced process ID
* @return 0 if successful, or error USTCMD_ERR_GEN
*/
int ustcmd_start_trace(pid_t pid)
{
int result;
result = ustcmd_send_cmd("trace_start", pid, NULL);
if (result != 1) {
return USTCMD_ERR_GEN;
}
return 0;
}
/**
* Alloc an UST trace according to a PID.
*
* @param pid Traced process ID
* @return 0 if successful, or error USTCMD_ERR_GEN
*/
int ustcmd_alloc_trace(pid_t pid)
{
int result;
result = ustcmd_send_cmd("trace_alloc", pid, NULL);
if (result != 1) {
return USTCMD_ERR_GEN;
}
return 0;
}
/**
* Stops an UST trace according to a PID.
*
* @param pid Traced process ID
* @return 0 if successful, or error USTCMD_ERR_GEN
*/
int ustcmd_stop_trace(pid_t pid)
{
int result;
result = ustcmd_send_cmd("trace_stop", pid, NULL);
if (result != 1) {
return USTCMD_ERR_GEN;
}
return 0;
}
/**
* Counts newlines ('\n') in a string.
*
* @param str String to search in
* @return Total newlines count
*/
unsigned int ustcmd_count_nl(const char *str)
{
unsigned int i = 0, tot = 0;
while (str[i] != '\0') {
if (str[i] == '\n') {
++tot;
}
++i;
}
return tot;
}
/**
* Frees a CMSF array.
*
* @param cmsf CMSF array to free
* @return 0 if successful, or error USTCMD_ERR_ARG
*/
int ustcmd_free_cmsf(struct marker_status *cmsf)
{
if (cmsf == NULL) {
return USTCMD_ERR_ARG;
}
unsigned int i = 0;
while (cmsf[i].channel != NULL) {
free(cmsf[i].channel);
free(cmsf[i].marker);
free(cmsf[i].fs);
++i;
}
free(cmsf);
return 0;
}
/**
* Gets channel/marker/state/format string for a given PID.
*
* @param cmsf Pointer to CMSF array to be filled (callee allocates, caller
* frees with `ustcmd_free_cmsf')
* @param pid Targeted PID
* @return 0 if successful, or -1 on error
*/
int ustcmd_get_cmsf(struct marker_status **cmsf, const pid_t pid)
{
char *big_str = NULL;
int result;
struct marker_status *tmp_cmsf = NULL;
unsigned int i = 0, cmsf_ind = 0;
if (cmsf == NULL) {
return -1;
}
result = ustcmd_send_cmd("list_markers", pid, &big_str);
if (result != 1) {
return -1;
}
if (result != 1) {
ERR("error while getting markers list");
return -1;
}
tmp_cmsf = (struct marker_status *) malloc(sizeof(struct marker_status) *
(ustcmd_count_nl(big_str) + 1));
if (tmp_cmsf == NULL) {
return -1;
}
/* Parse received reply string (format: "[chan]/[mark] [st] [fs]"): */
while (big_str[i] != '\0') {
char state;
sscanf(big_str + i, "marker: %a[^/]/%a[^ ] %c %a[^\n]",
&tmp_cmsf[cmsf_ind].channel,
&tmp_cmsf[cmsf_ind].marker,
&state,
&tmp_cmsf[cmsf_ind].fs);
tmp_cmsf[cmsf_ind].state = (state == USTCMD_MS_CHR_ON ?
USTCMD_MS_ON : USTCMD_MS_OFF); /* Marker state */
while (big_str[i] != '\n') {
++i; /* Go to next '\n' */
}
++i; /* Skip current pointed '\n' */
++cmsf_ind;
}
tmp_cmsf[cmsf_ind].channel = NULL;
tmp_cmsf[cmsf_ind].marker = NULL;
tmp_cmsf[cmsf_ind].fs = NULL;
*cmsf = tmp_cmsf;
free(big_str);
return 0;
}
/**
* Sends a given command to a traceable process
*
* @param cmd Null-terminated command to send
* @param pid Targeted PID
* @param reply Pointer to string to be filled with a reply string (must
* be NULL if no reply is needed for the given command).
* @return -1 if successful, 0 on EOT, 1 on success
*/
int ustcmd_send_cmd(const char *cmd, const pid_t pid, char **reply)
{
struct ustcomm_connection conn;
int retval;
if (ustcomm_connect_app(pid, &conn)) {
ERR("could not connect to PID %u", (unsigned int) pid);
return -1;
}
retval = ustcomm_send_request(&conn, cmd, reply);
ustcomm_close_app(&conn);
return retval;
}
|