Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

evdev.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2015 Etienne Gemsa <etienne.gemsa@lse.epita.fr>
  3. * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
  4. * Copyright (c) 2015-2020 The strace developers.
  5. * All rights reserved.
  6. *
  7. * SPDX-License-Identifier: LGPL-2.1-or-later
  8. */
  9. #include "defs.h"
  10. #include "xlat/evdev_abs.h"
  11. #include "xlat/evdev_ev.h"
  12. #ifdef HAVE_LINUX_INPUT_H
  13. # include <linux/ioctl.h>
  14. # include "types/evdev.h"
  15. # include "xlat/evdev_autorepeat.h"
  16. # include "xlat/evdev_ff_status.h"
  17. # include "xlat/evdev_ff_types.h"
  18. # include "xlat/evdev_keycode.h"
  19. # include "xlat/evdev_leds.h"
  20. # include "xlat/evdev_misc.h"
  21. # include "xlat/evdev_mtslots.h"
  22. # include "xlat/evdev_prop.h"
  23. # include "xlat/evdev_relative_axes.h"
  24. # include "xlat/evdev_snd.h"
  25. # include "xlat/evdev_switch.h"
  26. /** Added by Linux commit v2.6.38-rc1~247^2~1^2~2^2~5 */
  27. # ifndef INPUT_PROP_MAX
  28. # define INPUT_PROP_MAX 0x1f
  29. # endif
  30. # ifndef SYN_MAX
  31. # define SYN_MAX 0xf
  32. # endif
  33. /*
  34. * Has to be included after struct_* type definitions, since _IO* macros
  35. * used in fallback definitions require them for sizeof().
  36. */
  37. # define XLAT_MACROS_ONLY
  38. # include "xlat/evdev_ioctl_cmds.h"
  39. # undef XLAT_MACROS_ONLY
  40. # ifndef EVIOCGPROP
  41. # define EVIOCGPROP(len) _IOR('E', 0x09, len)
  42. # endif
  43. # ifndef EVIOCGMTSLOTS
  44. # define EVIOCGMTSLOTS(len) _IOR('E', 0x0a, len)
  45. # endif
  46. # ifndef EVIOCGSW
  47. # define EVIOCGSW(len) _IOR('E', 0x1b, len)
  48. # endif
  49. static int
  50. abs_ioctl(struct tcb *const tcp, const unsigned int code,
  51. const kernel_ulong_t arg)
  52. {
  53. static const size_t orig_sz = offsetofend(struct_input_absinfo, flat);
  54. static const size_t res_sz = offsetofend(struct_input_absinfo,
  55. resolution);
  56. struct_input_absinfo absinfo;
  57. size_t sz = _IOC_SIZE(code);
  58. size_t read_sz = MIN(sz, sizeof(absinfo));
  59. if (sz < orig_sz)
  60. return RVAL_DECODED;
  61. tprints(", ");
  62. if (umoven_or_printaddr(tcp, arg, read_sz, &absinfo))
  63. return RVAL_IOCTL_DECODED;
  64. tprintf("{value=%u"
  65. ", minimum=%u, ",
  66. absinfo.value,
  67. absinfo.minimum);
  68. if (!abbrev(tcp)) {
  69. tprintf("maximum=%u"
  70. ", fuzz=%u"
  71. ", flat=%u",
  72. absinfo.maximum,
  73. absinfo.fuzz,
  74. absinfo.flat);
  75. if (sz >= res_sz) {
  76. tprintf(", resolution=%u%s",
  77. absinfo.resolution,
  78. sz > res_sz ? ", ..." : "");
  79. } else if (sz > orig_sz) {
  80. tprints(", ...");
  81. }
  82. } else {
  83. tprints("...");
  84. }
  85. tprints("}");
  86. return RVAL_IOCTL_DECODED;
  87. }
  88. static int
  89. keycode_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
  90. {
  91. tprints(", ");
  92. unsigned int keycode[2];
  93. if (!umove_or_printaddr(tcp, arg, &keycode)) {
  94. tprintf("[%u, ", keycode[0]);
  95. printxval(evdev_keycode, keycode[1], "KEY_???");
  96. tprints("]");
  97. }
  98. return RVAL_IOCTL_DECODED;
  99. }
  100. static int
  101. keycode_V2_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
  102. {
  103. tprints(", ");
  104. struct_input_keymap_entry ike;
  105. if (umove_or_printaddr(tcp, arg, &ike))
  106. return RVAL_IOCTL_DECODED;
  107. tprintf("{flags=%" PRIu8
  108. ", len=%" PRIu8 ", ",
  109. ike.flags,
  110. ike.len);
  111. if (!abbrev(tcp)) {
  112. unsigned int i;
  113. tprintf("index=%" PRIu16 ", keycode=", ike.index);
  114. printxval(evdev_keycode, ike.keycode, "KEY_???");
  115. tprints(", scancode=[");
  116. for (i = 0; i < ARRAY_SIZE(ike.scancode); i++) {
  117. if (i > 0)
  118. tprints(", ");
  119. tprintf("%" PRIx8, ike.scancode[i]);
  120. }
  121. tprints("]");
  122. } else {
  123. tprints("...");
  124. }
  125. tprints("}");
  126. return RVAL_IOCTL_DECODED;
  127. }
  128. static int
  129. getid_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
  130. {
  131. tprints(", ");
  132. struct input_id id;
  133. if (!umove_or_printaddr(tcp, arg, &id))
  134. tprintf("{ID_BUS=%" PRIu16
  135. ", ID_VENDOR=%" PRIu16
  136. ", ID_PRODUCT=%" PRIu16
  137. ", ID_VERSION=%" PRIu16 "}",
  138. id.bustype,
  139. id.vendor,
  140. id.product,
  141. id.version);
  142. return RVAL_IOCTL_DECODED;
  143. }
  144. static int
  145. decode_bitset(struct tcb *const tcp, const kernel_ulong_t arg,
  146. const struct xlat *decode_nr, const unsigned int max_nr,
  147. const char *const dflt)
  148. {
  149. tprints(", ");
  150. unsigned int size;
  151. unsigned int size_bits;
  152. if ((kernel_ulong_t) tcp->u_rval > max_nr / 8)
  153. size_bits = max_nr;
  154. else
  155. size_bits = tcp->u_rval * 8;
  156. size = ROUNDUP(ROUNDUP_DIV(size_bits, 8), current_wordsize);
  157. if (syserror(tcp) || !size) {
  158. printaddr(arg);
  159. return RVAL_IOCTL_DECODED;
  160. }
  161. char decoded_arg[size];
  162. if (umove_or_printaddr(tcp, arg, &decoded_arg))
  163. return RVAL_IOCTL_DECODED;
  164. if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_RAW) {
  165. tprints("[");
  166. int bit_displayed = 0;
  167. int i = next_set_bit(decoded_arg, 0, size_bits);
  168. if (i < 0) {
  169. tprints(" 0 ");
  170. } else {
  171. printxval(decode_nr, i, dflt);
  172. while ((i = next_set_bit(decoded_arg, i + 1,
  173. size_bits)) > 0) {
  174. if (abbrev(tcp) && bit_displayed >= 3) {
  175. tprints(", ...");
  176. break;
  177. }
  178. tprints(", ");
  179. printxval(decode_nr, i, dflt);
  180. bit_displayed++;
  181. }
  182. }
  183. tprints("]");
  184. }
  185. if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
  186. tprints(" /* ");
  187. if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV) {
  188. print_local_array_ex(tcp, decoded_arg, size / current_wordsize,
  189. current_wordsize, print_xlong_array_member,
  190. NULL, 0, NULL, NULL);
  191. }
  192. if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE)
  193. tprints(" */");
  194. return RVAL_IOCTL_DECODED;
  195. }
  196. static int
  197. mtslots_ioctl(struct tcb *const tcp, const unsigned int code,
  198. const kernel_ulong_t arg)
  199. {
  200. tprints(", ");
  201. const size_t size = _IOC_SIZE(code) / sizeof(int);
  202. if (!size) {
  203. printaddr(arg);
  204. return RVAL_IOCTL_DECODED;
  205. }
  206. int buffer[size];
  207. if (umove_or_printaddr(tcp, arg, &buffer))
  208. return RVAL_IOCTL_DECODED;
  209. tprints("{code=");
  210. printxval(evdev_mtslots, buffer[0], "ABS_MT_???");
  211. tprints(", values=[");
  212. unsigned int i;
  213. for (i = 1; i < ARRAY_SIZE(buffer); i++)
  214. tprintf("%s%d", i > 1 ? ", " : "", buffer[i]);
  215. tprints("]}");
  216. return RVAL_IOCTL_DECODED;
  217. }
  218. static int
  219. repeat_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
  220. {
  221. tprints(", ");
  222. printpair_int(tcp, arg, "%u");
  223. return RVAL_IOCTL_DECODED;
  224. }
  225. static int
  226. bit_ioctl(struct tcb *const tcp, const unsigned int ev_nr,
  227. const kernel_ulong_t arg)
  228. {
  229. switch (ev_nr) {
  230. case 0:
  231. return decode_bitset(tcp, arg, evdev_ev,
  232. EV_MAX, "EV_???");
  233. case EV_KEY:
  234. return decode_bitset(tcp, arg, evdev_keycode,
  235. KEY_MAX, "KEY_???");
  236. case EV_REL:
  237. return decode_bitset(tcp, arg, evdev_relative_axes,
  238. REL_MAX, "REL_???");
  239. case EV_ABS:
  240. return decode_bitset(tcp, arg, evdev_abs,
  241. ABS_MAX, "ABS_???");
  242. case EV_MSC:
  243. return decode_bitset(tcp, arg, evdev_misc,
  244. MSC_MAX, "MSC_???");
  245. case EV_SW:
  246. return decode_bitset(tcp, arg, evdev_switch,
  247. SW_MAX, "SW_???");
  248. case EV_LED:
  249. return decode_bitset(tcp, arg, evdev_leds,
  250. LED_MAX, "LED_???");
  251. case EV_SND:
  252. return decode_bitset(tcp, arg, evdev_snd,
  253. SND_MAX, "SND_???");
  254. case EV_REP:
  255. return decode_bitset(tcp, arg, evdev_autorepeat,
  256. REP_MAX, "REP_???");
  257. case EV_FF:
  258. return decode_bitset(tcp, arg, evdev_ff_types,
  259. FF_MAX, "FF_???");
  260. case EV_PWR:
  261. tprints(", ");
  262. printnum_int(tcp, arg, "%d");
  263. return RVAL_IOCTL_DECODED;
  264. case EV_FF_STATUS:
  265. return decode_bitset(tcp, arg, evdev_ff_status,
  266. FF_STATUS_MAX, "FF_STATUS_???");
  267. default:
  268. tprints(", ");
  269. printaddr(arg);
  270. return RVAL_IOCTL_DECODED;
  271. }
  272. }
  273. static int
  274. evdev_read_ioctl(struct tcb *const tcp, const unsigned int code,
  275. const kernel_ulong_t arg)
  276. {
  277. /* fixed-number fixed-length commands */
  278. switch (code) {
  279. case EVIOCGVERSION:
  280. tprints(", ");
  281. printnum_int(tcp, arg, "%#x");
  282. return RVAL_IOCTL_DECODED;
  283. case EVIOCGEFFECTS:
  284. tprints(", ");
  285. printnum_int(tcp, arg, "%u");
  286. return RVAL_IOCTL_DECODED;
  287. case EVIOCGID:
  288. return getid_ioctl(tcp, arg);
  289. case EVIOCGREP:
  290. return repeat_ioctl(tcp, arg);
  291. case EVIOCGKEYCODE:
  292. return keycode_ioctl(tcp, arg);
  293. case EVIOCGKEYCODE_V2:
  294. return keycode_V2_ioctl(tcp, arg);
  295. }
  296. /* fixed-number variable-length commands */
  297. switch (_IOC_NR(code)) {
  298. case _IOC_NR(EVIOCGMTSLOTS(0)):
  299. return mtslots_ioctl(tcp, code, arg);
  300. case _IOC_NR(EVIOCGNAME(0)):
  301. case _IOC_NR(EVIOCGPHYS(0)):
  302. case _IOC_NR(EVIOCGUNIQ(0)):
  303. tprints(", ");
  304. if (syserror(tcp))
  305. printaddr(arg);
  306. else
  307. printstrn(tcp, arg, tcp->u_rval);
  308. return RVAL_IOCTL_DECODED;
  309. case _IOC_NR(EVIOCGPROP(0)):
  310. return decode_bitset(tcp, arg, evdev_prop,
  311. INPUT_PROP_MAX, "PROP_???");
  312. case _IOC_NR(EVIOCGSND(0)):
  313. return decode_bitset(tcp, arg, evdev_snd,
  314. SND_MAX, "SND_???");
  315. case _IOC_NR(EVIOCGSW(0)):
  316. return decode_bitset(tcp, arg, evdev_switch,
  317. SW_MAX, "SW_???");
  318. case _IOC_NR(EVIOCGKEY(0)):
  319. return decode_bitset(tcp, arg, evdev_keycode,
  320. KEY_MAX, "KEY_???");
  321. case _IOC_NR(EVIOCGLED(0)):
  322. return decode_bitset(tcp, arg, evdev_leds,
  323. LED_MAX, "LED_???");
  324. }
  325. /* multi-number fixed-length commands */
  326. if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
  327. return abs_ioctl(tcp, code, arg);
  328. /* multi-number variable-length commands */
  329. if ((_IOC_NR(code) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
  330. return bit_ioctl(tcp, _IOC_NR(code) & EV_MAX, arg);
  331. return 0;
  332. }
  333. static int
  334. evdev_write_ioctl(struct tcb *const tcp, const unsigned int code,
  335. const kernel_ulong_t arg)
  336. {
  337. /* fixed-number fixed-length commands */
  338. switch (code) {
  339. case EVIOCSREP:
  340. return repeat_ioctl(tcp, arg);
  341. case EVIOCSKEYCODE:
  342. return keycode_ioctl(tcp, arg);
  343. case EVIOCSKEYCODE_V2:
  344. return keycode_V2_ioctl(tcp, arg);
  345. case EVIOCRMFF:
  346. tprintf(", %d", (int) arg);
  347. return RVAL_IOCTL_DECODED;
  348. case EVIOCGRAB:
  349. case EVIOCREVOKE:
  350. tprintf(", %" PRI_klu, arg);
  351. return RVAL_IOCTL_DECODED;
  352. case EVIOCSCLOCKID:
  353. tprints(", ");
  354. printnum_int(tcp, arg, "%u");
  355. return RVAL_IOCTL_DECODED;
  356. }
  357. int rc = evdev_write_ioctl_mpers(tcp, code, arg);
  358. if (rc != RVAL_DECODED)
  359. return rc;
  360. /* multi-number fixed-length commands */
  361. if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0)))
  362. return abs_ioctl(tcp, code, arg);
  363. return 0;
  364. }
  365. void
  366. print_evdev_ff_type(const kernel_ulong_t val)
  367. {
  368. printxval(evdev_ff_types, val, "FF_???");
  369. }
  370. int
  371. evdev_ioctl(struct tcb *const tcp,
  372. const unsigned int code, const kernel_ulong_t arg)
  373. {
  374. switch (_IOC_DIR(code)) {
  375. case _IOC_READ:
  376. if (entering(tcp))
  377. return 0;
  378. return evdev_read_ioctl(tcp, code, arg);
  379. case _IOC_WRITE:
  380. return evdev_write_ioctl(tcp, code, arg) | RVAL_DECODED;
  381. default:
  382. return RVAL_DECODED;
  383. }
  384. }
  385. #endif /* HAVE_LINUX_INPUT_H */