tools/counter/counter_watch_events.c

Source file repositories/reference/linux-study-clean/tools/counter/counter_watch_events.c

File Facts

System
Linux kernel
Corpus path
tools/counter/counter_watch_events.c
Extension
.c
Size
11742 bytes
Lines
412
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

switch (c) {
		case 'd':
			debug = 1;
			break;
		case 'h':
			print_usage();
			return EXIT_SUCCESS;
		case 'n':
			dev_num = strtoul(optarg, NULL, 10);
			if (errno) {
				perror("strtol failed: --device-num <n>\n");
				return EXIT_FAILURE;
			}
			break;
		case 'l':
			loop = strtol(optarg, NULL, 10);
			if (errno) {
				perror("strtol failed: --loop <n>\n");
				return EXIT_FAILURE;
			}
			break;
		case 'w':
			nwatch++;
			break;
		default:
			return EXIT_FAILURE;
		}
	}

	if (nwatch) {
		watches = calloc(nwatch, sizeof(*watches));
		if (!watches) {
			perror("Error allocating watches\n");
			return EXIT_FAILURE;
		}
	} else {
		/* default to simple watch example */
		watches = simple_watch;
		nwatch = ARRAY_SIZE(simple_watch);
	}

	/* 2nd pass: parse watch sub-options to fill in watch array */
	optind = 1;
	i = 0;
	while ((c = getopt_long(argc, argv, "dhn:l:w:", longopts, NULL)) != -1) {
		switch (c) {
		case 'w':
			subopts = optarg;
			while (*subopts != '\0') {
				ret = getsubopt(&subopts, counter_watch_subopts, &value);
				switch (ret) {
				case WATCH_SCOPE_DEVICE:
				case WATCH_SCOPE_SIGNAL:
				case WATCH_SCOPE_COUNT:
					/* match with counter_scope */
					watches[i].component.scope = ret;
					break;
				case WATCH_COMPONENT_NONE:
				case WATCH_COMPONENT_SIGNAL:
				case WATCH_COMPONENT_COUNT:
				case WATCH_COMPONENT_FUNCTION:
				case WATCH_COMPONENT_SYNAPSE_ACTION:
				case WATCH_COMPONENT_EXTENSION:
					/* match counter_component_type: subtract enum value */
					ret -= WATCH_COMPONENT_NONE;
					watches[i].component.type = ret;
					break;
				case WATCH_EVENT_OVERFLOW:
				case WATCH_EVENT_UNDERFLOW:
				case WATCH_EVENT_OVERFLOW_UNDERFLOW:
				case WATCH_EVENT_THRESHOLD:
				case WATCH_EVENT_INDEX:
				case WATCH_EVENT_CHANGE_OF_STATE:
				case WATCH_EVENT_CAPTURE:
				case WATCH_EVENT_DIRECTION_CHANGE:
					/* match counter_event_type: subtract enum value */
					ret -= WATCH_EVENT_OVERFLOW;
					watches[i].event = ret;
					break;
				case WATCH_CHANNEL:
					if (!value) {
						fprintf(stderr, "Invalid chan=<number>\n");
						rc = EXIT_FAILURE;
						goto err_free_watches;
					}
					watches[i].channel = strtoul(value, NULL, 10);
					if (errno) {
						perror("strtoul failed: chan=<number>\n");
						rc = EXIT_FAILURE;
						goto err_free_watches;

Annotation

Implementation Notes