Add --list-commands option to the snapshot command
[lttng-tools.git] / src / bin / lttng / utils.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include <limits.h>
22
23 #include <common/error.h>
24 #include <common/utils.h>
25
26 #include "conf.h"
27 #include "utils.h"
28 #include "command.h"
29
30 /*
31 * get_session_name
32 *
33 * Return allocated string with the session name found in the config
34 * directory.
35 */
36 char *get_session_name(void)
37 {
38 char *path, *session_name = NULL;
39
40 /* Get path to config file */
41 path = utils_get_home_dir();
42 if (path == NULL) {
43 goto error;
44 }
45
46 /* Get session name from config */
47 session_name = config_read_session_name(path);
48 if (session_name == NULL) {
49 goto error;
50 }
51
52 DBG2("Config file path found: %s", path);
53 DBG("Session name found: %s", session_name);
54 return session_name;
55
56 error:
57 return NULL;
58 }
59
60 /*
61 * list_commands
62 *
63 * List commands line by line. This is mostly for bash auto completion and to
64 * avoid difficult parsing.
65 */
66 void list_commands(struct cmd_struct *commands, FILE *ofp)
67 {
68 int i = 0;
69 struct cmd_struct *cmd = NULL;
70
71 cmd = &commands[i];
72 while (cmd->name != NULL) {
73 fprintf(ofp, "%s\n", cmd->name);
74 i++;
75 cmd = &commands[i];
76 }
77 }
78
79 /*
80 * list_cmd_options
81 *
82 * Prints a simple list of the options available to a command. This is intended
83 * to be easily parsed for bash completion.
84 */
85 void list_cmd_options(FILE *ofp, struct poptOption *options)
86 {
87 int i;
88 struct poptOption *option = NULL;
89
90 for (i = 0; options[i].longName != NULL; i++) {
91 option = &options[i];
92
93 fprintf(ofp, "--%s\n", option->longName);
94
95 if (isprint(option->shortName)) {
96 fprintf(ofp, "-%c\n", option->shortName);
97 }
98 }
99 }
100
101 /*
102 * fls: returns the position of the most significant bit.
103 * Returns 0 if no bit is set, else returns the position of the most
104 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
105 */
106 #if defined(__i386) || defined(__x86_64)
107 static inline
108 unsigned int fls_u32(uint32_t x)
109 {
110 int r;
111
112 asm("bsrl %1,%0\n\t"
113 "jnz 1f\n\t"
114 "movl $-1,%0\n\t"
115 "1:\n\t"
116 : "=r" (r) : "rm" (x));
117 return r + 1;
118 }
119 #define HAS_FLS_U32
120 #endif
121
122 #if defined(__x86_64)
123 static inline
124 unsigned int fls_u64(uint64_t x)
125 {
126 long r;
127
128 asm("bsrq %1,%0\n\t"
129 "jnz 1f\n\t"
130 "movq $-1,%0\n\t"
131 "1:\n\t"
132 : "=r" (r) : "rm" (x));
133 return r + 1;
134 }
135 #define HAS_FLS_U64
136 #endif
137
138 #ifndef HAS_FLS_U64
139 static __attribute__((unused))
140 unsigned int fls_u64(uint64_t x)
141 {
142 unsigned int r = 64;
143
144 if (!x)
145 return 0;
146
147 if (!(x & 0xFFFFFFFF00000000ULL)) {
148 x <<= 32;
149 r -= 32;
150 }
151 if (!(x & 0xFFFF000000000000ULL)) {
152 x <<= 16;
153 r -= 16;
154 }
155 if (!(x & 0xFF00000000000000ULL)) {
156 x <<= 8;
157 r -= 8;
158 }
159 if (!(x & 0xF000000000000000ULL)) {
160 x <<= 4;
161 r -= 4;
162 }
163 if (!(x & 0xC000000000000000ULL)) {
164 x <<= 2;
165 r -= 2;
166 }
167 if (!(x & 0x8000000000000000ULL)) {
168 x <<= 1;
169 r -= 1;
170 }
171 return r;
172 }
173 #endif
174
175 #ifndef HAS_FLS_U32
176 static __attribute__((unused))
177 unsigned int fls_u32(uint32_t x)
178 {
179 unsigned int r = 32;
180
181 if (!x)
182 return 0;
183 if (!(x & 0xFFFF0000U)) {
184 x <<= 16;
185 r -= 16;
186 }
187 if (!(x & 0xFF000000U)) {
188 x <<= 8;
189 r -= 8;
190 }
191 if (!(x & 0xF0000000U)) {
192 x <<= 4;
193 r -= 4;
194 }
195 if (!(x & 0xC0000000U)) {
196 x <<= 2;
197 r -= 2;
198 }
199 if (!(x & 0x80000000U)) {
200 x <<= 1;
201 r -= 1;
202 }
203 return r;
204 }
205 #endif
206
207 static
208 unsigned int fls_ulong(unsigned long x)
209 {
210 #if (CAA_BITS_PER_LONG == 32)
211 return fls_u32(x);
212 #else
213 return fls_u64(x);
214 #endif
215 }
216
217 /*
218 * Return the minimum order for which x <= (1UL << order).
219 * Return -1 if x is 0.
220 */
221 int get_count_order_u32(uint32_t x)
222 {
223 if (!x)
224 return -1;
225
226 return fls_u32(x - 1);
227 }
228
229 /*
230 * Return the minimum order for which x <= (1UL << order).
231 * Return -1 if x is 0.
232 */
233 int get_count_order_u64(uint64_t x)
234 {
235 if (!x)
236 return -1;
237
238 return fls_u64(x - 1);
239 }
240
241 /*
242 * Return the minimum order for which x <= (1UL << order).
243 * Return -1 if x is 0.
244 */
245 int get_count_order_ulong(unsigned long x)
246 {
247 if (!x)
248 return -1;
249
250 return fls_ulong(x - 1);
251 }
This page took 0.033261 seconds and 4 git commands to generate.