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 11KB

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