debug.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S/390 debug facility
  4. *
  5. * Copyright IBM Corp. 1999, 2020
  6. *
  7. * Author(s): Michael Holzheu (holzheu@de.ibm.com),
  8. * Holger Smolinski (Holger.Smolinski@de.ibm.com)
  9. *
  10. * Bugreports to: <Linux390@de.ibm.com>
  11. */
  12. #define KMSG_COMPONENT "s390dbf"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/stddef.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/slab.h>
  18. #include <linux/ctype.h>
  19. #include <linux/string.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/export.h>
  23. #include <linux/init.h>
  24. #include <linux/fs.h>
  25. #include <linux/minmax.h>
  26. #include <linux/debugfs.h>
  27. #include <asm/debug.h>
  28. #define DEBUG_PROLOG_ENTRY -1
  29. #define ALL_AREAS 0 /* copy all debug areas */
  30. #define NO_AREAS 1 /* copy no debug areas */
  31. /* typedefs */
  32. typedef struct file_private_info {
  33. loff_t offset; /* offset of last read in file */
  34. int act_area; /* number of last formated area */
  35. int act_page; /* act page in given area */
  36. int act_entry; /* last formated entry (offset */
  37. /* relative to beginning of last */
  38. /* formated page) */
  39. size_t act_entry_offset; /* up to this offset we copied */
  40. /* in last read the last formated */
  41. /* entry to userland */
  42. char temp_buf[2048]; /* buffer for output */
  43. debug_info_t *debug_info_org; /* original debug information */
  44. debug_info_t *debug_info_snap; /* snapshot of debug information */
  45. struct debug_view *view; /* used view of debug info */
  46. } file_private_info_t;
  47. typedef struct {
  48. char *string;
  49. /*
  50. * This assumes that all args are converted into longs
  51. * on L/390 this is the case for all types of parameter
  52. * except of floats, and long long (32 bit)
  53. *
  54. */
  55. long args[];
  56. } debug_sprintf_entry_t;
  57. /* internal function prototyes */
  58. static int debug_init(void);
  59. static ssize_t debug_output(struct file *file, char __user *user_buf,
  60. size_t user_len, loff_t *offset);
  61. static ssize_t debug_input(struct file *file, const char __user *user_buf,
  62. size_t user_len, loff_t *offset);
  63. static int debug_open(struct inode *inode, struct file *file);
  64. static int debug_close(struct inode *inode, struct file *file);
  65. static debug_info_t *debug_info_create(const char *name, int pages_per_area,
  66. int nr_areas, int buf_size, umode_t mode);
  67. static void debug_info_get(debug_info_t *);
  68. static void debug_info_put(debug_info_t *);
  69. static int debug_prolog_level_fn(debug_info_t *id,
  70. struct debug_view *view, char *out_buf);
  71. static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
  72. struct file *file, const char __user *user_buf,
  73. size_t user_buf_size, loff_t *offset);
  74. static int debug_prolog_pages_fn(debug_info_t *id,
  75. struct debug_view *view, char *out_buf);
  76. static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
  77. struct file *file, const char __user *user_buf,
  78. size_t user_buf_size, loff_t *offset);
  79. static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
  80. struct file *file, const char __user *user_buf,
  81. size_t user_buf_size, loff_t *offset);
  82. static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
  83. char *out_buf, const char *in_buf);
  84. static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
  85. char *out_buf, const char *inbuf);
  86. static void debug_areas_swap(debug_info_t *a, debug_info_t *b);
  87. static void debug_events_append(debug_info_t *dest, debug_info_t *src);
  88. /* globals */
  89. struct debug_view debug_hex_ascii_view = {
  90. "hex_ascii",
  91. NULL,
  92. &debug_dflt_header_fn,
  93. &debug_hex_ascii_format_fn,
  94. NULL,
  95. NULL
  96. };
  97. EXPORT_SYMBOL(debug_hex_ascii_view);
  98. static struct debug_view debug_level_view = {
  99. "level",
  100. &debug_prolog_level_fn,
  101. NULL,
  102. NULL,
  103. &debug_input_level_fn,
  104. NULL
  105. };
  106. static struct debug_view debug_pages_view = {
  107. "pages",
  108. &debug_prolog_pages_fn,
  109. NULL,
  110. NULL,
  111. &debug_input_pages_fn,
  112. NULL
  113. };
  114. static struct debug_view debug_flush_view = {
  115. "flush",
  116. NULL,
  117. NULL,
  118. NULL,
  119. &debug_input_flush_fn,
  120. NULL
  121. };
  122. struct debug_view debug_sprintf_view = {
  123. "sprintf",
  124. NULL,
  125. &debug_dflt_header_fn,
  126. &debug_sprintf_format_fn,
  127. NULL,
  128. NULL
  129. };
  130. EXPORT_SYMBOL(debug_sprintf_view);
  131. /* used by dump analysis tools to determine version of debug feature */
  132. static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
  133. /* static globals */
  134. static debug_info_t *debug_area_first;
  135. static debug_info_t *debug_area_last;
  136. static DEFINE_MUTEX(debug_mutex);
  137. static int initialized;
  138. static int debug_critical;
  139. static const struct file_operations debug_file_ops = {
  140. .owner = THIS_MODULE,
  141. .read = debug_output,
  142. .write = debug_input,
  143. .open = debug_open,
  144. .release = debug_close,
  145. };
  146. static struct dentry *debug_debugfs_root_entry;
  147. /* functions */
  148. /*
  149. * debug_areas_alloc
  150. * - Debug areas are implemented as a threedimensonal array:
  151. * areas[areanumber][pagenumber][pageoffset]
  152. */
  153. static debug_entry_t ***debug_areas_alloc(int pages_per_area, int nr_areas)
  154. {
  155. debug_entry_t ***areas;
  156. int i, j;
  157. areas = kmalloc_array(nr_areas, sizeof(debug_entry_t **), GFP_KERNEL);
  158. if (!areas)
  159. goto fail_malloc_areas;
  160. for (i = 0; i < nr_areas; i++) {
  161. /* GFP_NOWARN to avoid user triggerable WARN, we handle fails */
  162. areas[i] = kmalloc_array(pages_per_area,
  163. sizeof(debug_entry_t *),
  164. GFP_KERNEL | __GFP_NOWARN);
  165. if (!areas[i])
  166. goto fail_malloc_areas2;
  167. for (j = 0; j < pages_per_area; j++) {
  168. areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
  169. if (!areas[i][j]) {
  170. for (j--; j >= 0 ; j--)
  171. kfree(areas[i][j]);
  172. kfree(areas[i]);
  173. goto fail_malloc_areas2;
  174. }
  175. }
  176. }
  177. return areas;
  178. fail_malloc_areas2:
  179. for (i--; i >= 0; i--) {
  180. for (j = 0; j < pages_per_area; j++)
  181. kfree(areas[i][j]);
  182. kfree(areas[i]);
  183. }
  184. kfree(areas);
  185. fail_malloc_areas:
  186. return NULL;
  187. }
  188. /*
  189. * debug_info_alloc
  190. * - alloc new debug-info
  191. */
  192. static debug_info_t *debug_info_alloc(const char *name, int pages_per_area,
  193. int nr_areas, int buf_size, int level,
  194. int mode)
  195. {
  196. debug_info_t *rc;
  197. /* alloc everything */
  198. rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
  199. if (!rc)
  200. goto fail_malloc_rc;
  201. rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
  202. if (!rc->active_entries)
  203. goto fail_malloc_active_entries;
  204. rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
  205. if (!rc->active_pages)
  206. goto fail_malloc_active_pages;
  207. if ((mode == ALL_AREAS) && (pages_per_area != 0)) {
  208. rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
  209. if (!rc->areas)
  210. goto fail_malloc_areas;
  211. } else {
  212. rc->areas = NULL;
  213. }
  214. /* initialize members */
  215. spin_lock_init(&rc->lock);
  216. rc->pages_per_area = pages_per_area;
  217. rc->nr_areas = nr_areas;
  218. rc->active_area = 0;
  219. rc->level = level;
  220. rc->buf_size = buf_size;
  221. rc->entry_size = sizeof(debug_entry_t) + buf_size;
  222. strscpy(rc->name, name, sizeof(rc->name));
  223. memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
  224. memset(rc->debugfs_entries, 0, DEBUG_MAX_VIEWS * sizeof(struct dentry *));
  225. refcount_set(&(rc->ref_count), 0);
  226. return rc;
  227. fail_malloc_areas:
  228. kfree(rc->active_pages);
  229. fail_malloc_active_pages:
  230. kfree(rc->active_entries);
  231. fail_malloc_active_entries:
  232. kfree(rc);
  233. fail_malloc_rc:
  234. return NULL;
  235. }
  236. /*
  237. * debug_areas_free
  238. * - free all debug areas
  239. */
  240. static void debug_areas_free(debug_info_t *db_info)
  241. {
  242. int i, j;
  243. if (!db_info->areas)
  244. return;
  245. for (i = 0; i < db_info->nr_areas; i++) {
  246. for (j = 0; j < db_info->pages_per_area; j++)
  247. kfree(db_info->areas[i][j]);
  248. kfree(db_info->areas[i]);
  249. }
  250. kfree(db_info->areas);
  251. db_info->areas = NULL;
  252. }
  253. /*
  254. * debug_info_free
  255. * - free memory debug-info
  256. */
  257. static void debug_info_free(debug_info_t *db_info)
  258. {
  259. debug_areas_free(db_info);
  260. kfree(db_info->active_entries);
  261. kfree(db_info->active_pages);
  262. kfree(db_info);
  263. }
  264. /*
  265. * debug_info_create
  266. * - create new debug-info
  267. */
  268. static debug_info_t *debug_info_create(const char *name, int pages_per_area,
  269. int nr_areas, int buf_size, umode_t mode)
  270. {
  271. debug_info_t *rc;
  272. rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
  273. DEBUG_DEFAULT_LEVEL, ALL_AREAS);
  274. if (!rc)
  275. goto out;
  276. rc->mode = mode & ~S_IFMT;
  277. refcount_set(&rc->ref_count, 1);
  278. out:
  279. return rc;
  280. }
  281. /*
  282. * debug_info_copy
  283. * - copy debug-info
  284. */
  285. static debug_info_t *debug_info_copy(debug_info_t *in, int mode)
  286. {
  287. unsigned long flags;
  288. debug_info_t *rc;
  289. int i, j;
  290. /* get a consistent copy of the debug areas */
  291. do {
  292. rc = debug_info_alloc(in->name, in->pages_per_area,
  293. in->nr_areas, in->buf_size, in->level, mode);
  294. spin_lock_irqsave(&in->lock, flags);
  295. if (!rc)
  296. goto out;
  297. /* has something changed in the meantime ? */
  298. if ((rc->pages_per_area == in->pages_per_area) &&
  299. (rc->nr_areas == in->nr_areas)) {
  300. break;
  301. }
  302. spin_unlock_irqrestore(&in->lock, flags);
  303. debug_info_free(rc);
  304. } while (1);
  305. if (mode == NO_AREAS)
  306. goto out;
  307. for (i = 0; i < in->nr_areas; i++) {
  308. for (j = 0; j < in->pages_per_area; j++)
  309. memcpy(rc->areas[i][j], in->areas[i][j], PAGE_SIZE);
  310. }
  311. out:
  312. spin_unlock_irqrestore(&in->lock, flags);
  313. return rc;
  314. }
  315. /*
  316. * debug_info_get
  317. * - increments reference count for debug-info
  318. */
  319. static void debug_info_get(debug_info_t *db_info)
  320. {
  321. if (db_info)
  322. refcount_inc(&db_info->ref_count);
  323. }
  324. /*
  325. * debug_info_put:
  326. * - decreases reference count for debug-info and frees it if necessary
  327. */
  328. static void debug_info_put(debug_info_t *db_info)
  329. {
  330. if (!db_info)
  331. return;
  332. if (refcount_dec_and_test(&db_info->ref_count))
  333. debug_info_free(db_info);
  334. }
  335. /*
  336. * debug_format_entry:
  337. * - format one debug entry and return size of formated data
  338. */
  339. static int debug_format_entry(file_private_info_t *p_info)
  340. {
  341. debug_info_t *id_snap = p_info->debug_info_snap;
  342. struct debug_view *view = p_info->view;
  343. debug_entry_t *act_entry;
  344. size_t len = 0;
  345. if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
  346. /* print prolog */
  347. if (view->prolog_proc)
  348. len += view->prolog_proc(id_snap, view, p_info->temp_buf);
  349. goto out;
  350. }
  351. if (!id_snap->areas) /* this is true, if we have a prolog only view */
  352. goto out; /* or if 'pages_per_area' is 0 */
  353. act_entry = (debug_entry_t *) ((char *)id_snap->areas[p_info->act_area]
  354. [p_info->act_page] + p_info->act_entry);
  355. if (act_entry->clock == 0LL)
  356. goto out; /* empty entry */
  357. if (view->header_proc)
  358. len += view->header_proc(id_snap, view, p_info->act_area,
  359. act_entry, p_info->temp_buf + len);
  360. if (view->format_proc)
  361. len += view->format_proc(id_snap, view, p_info->temp_buf + len,
  362. DEBUG_DATA(act_entry));
  363. out:
  364. return len;
  365. }
  366. /*
  367. * debug_next_entry:
  368. * - goto next entry in p_info
  369. */
  370. static inline int debug_next_entry(file_private_info_t *p_info)
  371. {
  372. debug_info_t *id;
  373. id = p_info->debug_info_snap;
  374. if (p_info->act_entry == DEBUG_PROLOG_ENTRY) {
  375. p_info->act_entry = 0;
  376. p_info->act_page = 0;
  377. goto out;
  378. }
  379. if (!id->areas)
  380. return 1;
  381. p_info->act_entry += id->entry_size;
  382. /* switch to next page, if we reached the end of the page */
  383. if (p_info->act_entry > (PAGE_SIZE - id->entry_size)) {
  384. /* next page */
  385. p_info->act_entry = 0;
  386. p_info->act_page += 1;
  387. if ((p_info->act_page % id->pages_per_area) == 0) {
  388. /* next area */
  389. p_info->act_area++;
  390. p_info->act_page = 0;
  391. }
  392. if (p_info->act_area >= id->nr_areas)
  393. return 1;
  394. }
  395. out:
  396. return 0;
  397. }
  398. /*
  399. * debug_output:
  400. * - called for user read()
  401. * - copies formated debug entries to the user buffer
  402. */
  403. static ssize_t debug_output(struct file *file, /* file descriptor */
  404. char __user *user_buf, /* user buffer */
  405. size_t len, /* length of buffer */
  406. loff_t *offset) /* offset in the file */
  407. {
  408. size_t count = 0;
  409. size_t entry_offset;
  410. file_private_info_t *p_info;
  411. p_info = (file_private_info_t *) file->private_data;
  412. if (*offset != p_info->offset)
  413. return -EPIPE;
  414. if (p_info->act_area >= p_info->debug_info_snap->nr_areas)
  415. return 0;
  416. entry_offset = p_info->act_entry_offset;
  417. while (count < len) {
  418. int formatted_line_residue;
  419. int formatted_line_size;
  420. int user_buf_residue;
  421. size_t copy_size;
  422. formatted_line_size = debug_format_entry(p_info);
  423. formatted_line_residue = formatted_line_size - entry_offset;
  424. user_buf_residue = len-count;
  425. copy_size = min(user_buf_residue, formatted_line_residue);
  426. if (copy_size) {
  427. if (copy_to_user(user_buf + count, p_info->temp_buf
  428. + entry_offset, copy_size))
  429. return -EFAULT;
  430. count += copy_size;
  431. entry_offset += copy_size;
  432. }
  433. if (copy_size == formatted_line_residue) {
  434. entry_offset = 0;
  435. if (debug_next_entry(p_info))
  436. goto out;
  437. }
  438. }
  439. out:
  440. p_info->offset = *offset + count;
  441. p_info->act_entry_offset = entry_offset;
  442. *offset = p_info->offset;
  443. return count;
  444. }
  445. /*
  446. * debug_input:
  447. * - called for user write()
  448. * - calls input function of view
  449. */
  450. static ssize_t debug_input(struct file *file, const char __user *user_buf,
  451. size_t length, loff_t *offset)
  452. {
  453. file_private_info_t *p_info;
  454. int rc = 0;
  455. mutex_lock(&debug_mutex);
  456. p_info = ((file_private_info_t *) file->private_data);
  457. if (p_info->view->input_proc) {
  458. rc = p_info->view->input_proc(p_info->debug_info_org,
  459. p_info->view, file, user_buf,
  460. length, offset);
  461. } else {
  462. rc = -EPERM;
  463. }
  464. mutex_unlock(&debug_mutex);
  465. return rc; /* number of input characters */
  466. }
  467. /*
  468. * debug_open:
  469. * - called for user open()
  470. * - copies formated output to private_data area of the file
  471. * handle
  472. */
  473. static int debug_open(struct inode *inode, struct file *file)
  474. {
  475. debug_info_t *debug_info, *debug_info_snapshot;
  476. file_private_info_t *p_info;
  477. int i, rc = 0;
  478. mutex_lock(&debug_mutex);
  479. debug_info = file_inode(file)->i_private;
  480. /* find debug view */
  481. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  482. if (!debug_info->views[i])
  483. continue;
  484. else if (debug_info->debugfs_entries[i] == file->f_path.dentry)
  485. goto found; /* found view ! */
  486. }
  487. /* no entry found */
  488. rc = -EINVAL;
  489. goto out;
  490. found:
  491. /* Make snapshot of current debug areas to get it consistent. */
  492. /* To copy all the areas is only needed, if we have a view which */
  493. /* formats the debug areas. */
  494. if (!debug_info->views[i]->format_proc && !debug_info->views[i]->header_proc)
  495. debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
  496. else
  497. debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
  498. if (!debug_info_snapshot) {
  499. rc = -ENOMEM;
  500. goto out;
  501. }
  502. p_info = kmalloc(sizeof(file_private_info_t), GFP_KERNEL);
  503. if (!p_info) {
  504. debug_info_free(debug_info_snapshot);
  505. rc = -ENOMEM;
  506. goto out;
  507. }
  508. p_info->offset = 0;
  509. p_info->debug_info_snap = debug_info_snapshot;
  510. p_info->debug_info_org = debug_info;
  511. p_info->view = debug_info->views[i];
  512. p_info->act_area = 0;
  513. p_info->act_page = 0;
  514. p_info->act_entry = DEBUG_PROLOG_ENTRY;
  515. p_info->act_entry_offset = 0;
  516. file->private_data = p_info;
  517. debug_info_get(debug_info);
  518. nonseekable_open(inode, file);
  519. out:
  520. mutex_unlock(&debug_mutex);
  521. return rc;
  522. }
  523. /*
  524. * debug_close:
  525. * - called for user close()
  526. * - deletes private_data area of the file handle
  527. */
  528. static int debug_close(struct inode *inode, struct file *file)
  529. {
  530. file_private_info_t *p_info;
  531. p_info = (file_private_info_t *) file->private_data;
  532. if (p_info->debug_info_snap)
  533. debug_info_free(p_info->debug_info_snap);
  534. debug_info_put(p_info->debug_info_org);
  535. kfree(file->private_data);
  536. return 0; /* success */
  537. }
  538. /* Create debugfs entries and add to internal list. */
  539. static void _debug_register(debug_info_t *id)
  540. {
  541. /* create root directory */
  542. id->debugfs_root_entry = debugfs_create_dir(id->name,
  543. debug_debugfs_root_entry);
  544. /* append new element to linked list */
  545. if (!debug_area_first) {
  546. /* first element in list */
  547. debug_area_first = id;
  548. id->prev = NULL;
  549. } else {
  550. /* append element to end of list */
  551. debug_area_last->next = id;
  552. id->prev = debug_area_last;
  553. }
  554. debug_area_last = id;
  555. id->next = NULL;
  556. debug_register_view(id, &debug_level_view);
  557. debug_register_view(id, &debug_flush_view);
  558. debug_register_view(id, &debug_pages_view);
  559. }
  560. /**
  561. * debug_register_mode() - creates and initializes debug area.
  562. *
  563. * @name: Name of debug log (e.g. used for debugfs entry)
  564. * @pages_per_area: Number of pages, which will be allocated per area
  565. * @nr_areas: Number of debug areas
  566. * @buf_size: Size of data area in each debug entry
  567. * @mode: File mode for debugfs files. E.g. S_IRWXUGO
  568. * @uid: User ID for debugfs files. Currently only 0 is supported.
  569. * @gid: Group ID for debugfs files. Currently only 0 is supported.
  570. *
  571. * Return:
  572. * - Handle for generated debug area
  573. * - %NULL if register failed
  574. *
  575. * Allocates memory for a debug log.
  576. * Must not be called within an interrupt handler.
  577. */
  578. debug_info_t *debug_register_mode(const char *name, int pages_per_area,
  579. int nr_areas, int buf_size, umode_t mode,
  580. uid_t uid, gid_t gid)
  581. {
  582. debug_info_t *rc = NULL;
  583. /* Since debugfs currently does not support uid/gid other than root, */
  584. /* we do not allow gid/uid != 0 until we get support for that. */
  585. if ((uid != 0) || (gid != 0))
  586. pr_warn("Root becomes the owner of all s390dbf files in sysfs\n");
  587. BUG_ON(!initialized);
  588. /* create new debug_info */
  589. rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
  590. if (rc) {
  591. mutex_lock(&debug_mutex);
  592. _debug_register(rc);
  593. mutex_unlock(&debug_mutex);
  594. } else {
  595. pr_err("Registering debug feature %s failed\n", name);
  596. }
  597. return rc;
  598. }
  599. EXPORT_SYMBOL(debug_register_mode);
  600. /**
  601. * debug_register() - creates and initializes debug area with default file mode.
  602. *
  603. * @name: Name of debug log (e.g. used for debugfs entry)
  604. * @pages_per_area: Number of pages, which will be allocated per area
  605. * @nr_areas: Number of debug areas
  606. * @buf_size: Size of data area in each debug entry
  607. *
  608. * Return:
  609. * - Handle for generated debug area
  610. * - %NULL if register failed
  611. *
  612. * Allocates memory for a debug log.
  613. * The debugfs file mode access permissions are read and write for user.
  614. * Must not be called within an interrupt handler.
  615. */
  616. debug_info_t *debug_register(const char *name, int pages_per_area,
  617. int nr_areas, int buf_size)
  618. {
  619. return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
  620. S_IRUSR | S_IWUSR, 0, 0);
  621. }
  622. EXPORT_SYMBOL(debug_register);
  623. /**
  624. * debug_register_static() - registers a static debug area
  625. *
  626. * @id: Handle for static debug area
  627. * @pages_per_area: Number of pages per area
  628. * @nr_areas: Number of debug areas
  629. *
  630. * Register debug_info_t defined using DEFINE_STATIC_DEBUG_INFO.
  631. *
  632. * Note: This function is called automatically via an initcall generated by
  633. * DEFINE_STATIC_DEBUG_INFO.
  634. */
  635. void debug_register_static(debug_info_t *id, int pages_per_area, int nr_areas)
  636. {
  637. unsigned long flags;
  638. debug_info_t *copy;
  639. if (!initialized) {
  640. pr_err("Tried to register debug feature %s too early\n",
  641. id->name);
  642. return;
  643. }
  644. copy = debug_info_alloc("", pages_per_area, nr_areas, id->buf_size,
  645. id->level, ALL_AREAS);
  646. if (!copy) {
  647. pr_err("Registering debug feature %s failed\n", id->name);
  648. /* Clear pointers to prevent tracing into released initdata. */
  649. spin_lock_irqsave(&id->lock, flags);
  650. id->areas = NULL;
  651. id->active_pages = NULL;
  652. id->active_entries = NULL;
  653. spin_unlock_irqrestore(&id->lock, flags);
  654. return;
  655. }
  656. /* Replace static trace area with dynamic copy. */
  657. spin_lock_irqsave(&id->lock, flags);
  658. debug_events_append(copy, id);
  659. debug_areas_swap(id, copy);
  660. spin_unlock_irqrestore(&id->lock, flags);
  661. /* Clear pointers to initdata and discard copy. */
  662. copy->areas = NULL;
  663. copy->active_pages = NULL;
  664. copy->active_entries = NULL;
  665. debug_info_free(copy);
  666. mutex_lock(&debug_mutex);
  667. _debug_register(id);
  668. mutex_unlock(&debug_mutex);
  669. }
  670. /* Remove debugfs entries and remove from internal list. */
  671. static void _debug_unregister(debug_info_t *id)
  672. {
  673. int i;
  674. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  675. if (!id->views[i])
  676. continue;
  677. debugfs_remove(id->debugfs_entries[i]);
  678. }
  679. debugfs_remove(id->debugfs_root_entry);
  680. if (id == debug_area_first)
  681. debug_area_first = id->next;
  682. if (id == debug_area_last)
  683. debug_area_last = id->prev;
  684. if (id->prev)
  685. id->prev->next = id->next;
  686. if (id->next)
  687. id->next->prev = id->prev;
  688. }
  689. /**
  690. * debug_unregister() - give back debug area.
  691. *
  692. * @id: handle for debug log
  693. *
  694. * Return:
  695. * none
  696. */
  697. void debug_unregister(debug_info_t *id)
  698. {
  699. if (!id)
  700. return;
  701. mutex_lock(&debug_mutex);
  702. _debug_unregister(id);
  703. mutex_unlock(&debug_mutex);
  704. debug_info_put(id);
  705. }
  706. EXPORT_SYMBOL(debug_unregister);
  707. /*
  708. * debug_set_size:
  709. * - set area size (number of pages) and number of areas
  710. */
  711. static int debug_set_size(debug_info_t *id, int nr_areas, int pages_per_area)
  712. {
  713. debug_info_t *new_id;
  714. unsigned long flags;
  715. if (!id || (nr_areas <= 0) || (pages_per_area < 0))
  716. return -EINVAL;
  717. new_id = debug_info_alloc("", pages_per_area, nr_areas, id->buf_size,
  718. id->level, ALL_AREAS);
  719. if (!new_id) {
  720. pr_info("Allocating memory for %i pages failed\n",
  721. pages_per_area);
  722. return -ENOMEM;
  723. }
  724. spin_lock_irqsave(&id->lock, flags);
  725. debug_events_append(new_id, id);
  726. debug_areas_swap(new_id, id);
  727. debug_info_free(new_id);
  728. spin_unlock_irqrestore(&id->lock, flags);
  729. pr_info("%s: set new size (%i pages)\n", id->name, pages_per_area);
  730. return 0;
  731. }
  732. /**
  733. * debug_set_level() - Sets new actual debug level if new_level is valid.
  734. *
  735. * @id: handle for debug log
  736. * @new_level: new debug level
  737. *
  738. * Return:
  739. * none
  740. */
  741. void debug_set_level(debug_info_t *id, int new_level)
  742. {
  743. unsigned long flags;
  744. if (!id)
  745. return;
  746. if (new_level == DEBUG_OFF_LEVEL) {
  747. pr_info("%s: switched off\n", id->name);
  748. } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
  749. pr_info("%s: level %i is out of range (%i - %i)\n",
  750. id->name, new_level, 0, DEBUG_MAX_LEVEL);
  751. return;
  752. }
  753. spin_lock_irqsave(&id->lock, flags);
  754. id->level = new_level;
  755. spin_unlock_irqrestore(&id->lock, flags);
  756. }
  757. EXPORT_SYMBOL(debug_set_level);
  758. /*
  759. * proceed_active_entry:
  760. * - set active entry to next in the ring buffer
  761. */
  762. static inline void proceed_active_entry(debug_info_t *id)
  763. {
  764. if ((id->active_entries[id->active_area] += id->entry_size)
  765. > (PAGE_SIZE - id->entry_size)) {
  766. id->active_entries[id->active_area] = 0;
  767. id->active_pages[id->active_area] =
  768. (id->active_pages[id->active_area] + 1) %
  769. id->pages_per_area;
  770. }
  771. }
  772. /*
  773. * proceed_active_area:
  774. * - set active area to next in the ring buffer
  775. */
  776. static inline void proceed_active_area(debug_info_t *id)
  777. {
  778. id->active_area++;
  779. id->active_area = id->active_area % id->nr_areas;
  780. }
  781. /*
  782. * get_active_entry:
  783. */
  784. static inline debug_entry_t *get_active_entry(debug_info_t *id)
  785. {
  786. return (debug_entry_t *) (((char *) id->areas[id->active_area]
  787. [id->active_pages[id->active_area]]) +
  788. id->active_entries[id->active_area]);
  789. }
  790. /* Swap debug areas of a and b. */
  791. static void debug_areas_swap(debug_info_t *a, debug_info_t *b)
  792. {
  793. swap(a->nr_areas, b->nr_areas);
  794. swap(a->pages_per_area, b->pages_per_area);
  795. swap(a->areas, b->areas);
  796. swap(a->active_area, b->active_area);
  797. swap(a->active_pages, b->active_pages);
  798. swap(a->active_entries, b->active_entries);
  799. }
  800. /* Append all debug events in active area from source to destination log. */
  801. static void debug_events_append(debug_info_t *dest, debug_info_t *src)
  802. {
  803. debug_entry_t *from, *to, *last;
  804. if (!src->areas || !dest->areas)
  805. return;
  806. /* Loop over all entries in src, starting with oldest. */
  807. from = get_active_entry(src);
  808. last = from;
  809. do {
  810. if (from->clock != 0LL) {
  811. to = get_active_entry(dest);
  812. memset(to, 0, dest->entry_size);
  813. memcpy(to, from, min(src->entry_size,
  814. dest->entry_size));
  815. proceed_active_entry(dest);
  816. }
  817. proceed_active_entry(src);
  818. from = get_active_entry(src);
  819. } while (from != last);
  820. }
  821. /*
  822. * debug_finish_entry:
  823. * - set timestamp, caller address, cpu number etc.
  824. */
  825. static inline void debug_finish_entry(debug_info_t *id, debug_entry_t *active,
  826. int level, int exception)
  827. {
  828. unsigned long timestamp;
  829. union tod_clock clk;
  830. store_tod_clock_ext(&clk);
  831. timestamp = clk.us;
  832. timestamp -= TOD_UNIX_EPOCH >> 12;
  833. active->clock = timestamp;
  834. active->cpu = smp_processor_id();
  835. active->caller = __builtin_return_address(0);
  836. active->exception = exception;
  837. active->level = level;
  838. proceed_active_entry(id);
  839. if (exception)
  840. proceed_active_area(id);
  841. }
  842. static int debug_stoppable = 1;
  843. static int debug_active = 1;
  844. #define CTL_S390DBF_STOPPABLE 5678
  845. #define CTL_S390DBF_ACTIVE 5679
  846. /*
  847. * proc handler for the running debug_active sysctl
  848. * always allow read, allow write only if debug_stoppable is set or
  849. * if debug_active is already off
  850. */
  851. static int s390dbf_procactive(const struct ctl_table *table, int write,
  852. void *buffer, size_t *lenp, loff_t *ppos)
  853. {
  854. if (!write || debug_stoppable || !debug_active)
  855. return proc_dointvec(table, write, buffer, lenp, ppos);
  856. else
  857. return 0;
  858. }
  859. static struct ctl_table s390dbf_table[] = {
  860. {
  861. .procname = "debug_stoppable",
  862. .data = &debug_stoppable,
  863. .maxlen = sizeof(int),
  864. .mode = S_IRUGO | S_IWUSR,
  865. .proc_handler = proc_dointvec,
  866. },
  867. {
  868. .procname = "debug_active",
  869. .data = &debug_active,
  870. .maxlen = sizeof(int),
  871. .mode = S_IRUGO | S_IWUSR,
  872. .proc_handler = s390dbf_procactive,
  873. },
  874. };
  875. static struct ctl_table_header *s390dbf_sysctl_header;
  876. /**
  877. * debug_stop_all() - stops the debug feature if stopping is allowed.
  878. *
  879. * Return:
  880. * - none
  881. *
  882. * Currently used in case of a kernel oops.
  883. */
  884. void debug_stop_all(void)
  885. {
  886. if (debug_stoppable)
  887. debug_active = 0;
  888. }
  889. EXPORT_SYMBOL(debug_stop_all);
  890. /**
  891. * debug_set_critical() - event/exception functions try lock instead of spin.
  892. *
  893. * Return:
  894. * - none
  895. *
  896. * Currently used in case of stopping all CPUs but the current one.
  897. * Once in this state, functions to write a debug entry for an
  898. * event or exception no longer spin on the debug area lock,
  899. * but only try to get it and fail if they do not get the lock.
  900. */
  901. void debug_set_critical(void)
  902. {
  903. debug_critical = 1;
  904. }
  905. /*
  906. * debug_event_common:
  907. * - write debug entry with given size
  908. */
  909. debug_entry_t *debug_event_common(debug_info_t *id, int level, const void *buf,
  910. int len)
  911. {
  912. debug_entry_t *active;
  913. unsigned long flags;
  914. if (!debug_active || !id->areas)
  915. return NULL;
  916. if (debug_critical) {
  917. if (!spin_trylock_irqsave(&id->lock, flags))
  918. return NULL;
  919. } else {
  920. spin_lock_irqsave(&id->lock, flags);
  921. }
  922. do {
  923. active = get_active_entry(id);
  924. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  925. if (len < id->buf_size)
  926. memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
  927. debug_finish_entry(id, active, level, 0);
  928. len -= id->buf_size;
  929. buf += id->buf_size;
  930. } while (len > 0);
  931. spin_unlock_irqrestore(&id->lock, flags);
  932. return active;
  933. }
  934. EXPORT_SYMBOL(debug_event_common);
  935. /*
  936. * debug_exception_common:
  937. * - write debug entry with given size and switch to next debug area
  938. */
  939. debug_entry_t *debug_exception_common(debug_info_t *id, int level,
  940. const void *buf, int len)
  941. {
  942. debug_entry_t *active;
  943. unsigned long flags;
  944. if (!debug_active || !id->areas)
  945. return NULL;
  946. if (debug_critical) {
  947. if (!spin_trylock_irqsave(&id->lock, flags))
  948. return NULL;
  949. } else {
  950. spin_lock_irqsave(&id->lock, flags);
  951. }
  952. do {
  953. active = get_active_entry(id);
  954. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  955. if (len < id->buf_size)
  956. memset((DEBUG_DATA(active)) + len, 0, id->buf_size - len);
  957. debug_finish_entry(id, active, level, len <= id->buf_size);
  958. len -= id->buf_size;
  959. buf += id->buf_size;
  960. } while (len > 0);
  961. spin_unlock_irqrestore(&id->lock, flags);
  962. return active;
  963. }
  964. EXPORT_SYMBOL(debug_exception_common);
  965. /*
  966. * counts arguments in format string for sprintf view
  967. */
  968. static inline int debug_count_numargs(char *string)
  969. {
  970. int numargs = 0;
  971. while (*string) {
  972. if (*string++ == '%')
  973. numargs++;
  974. }
  975. return numargs;
  976. }
  977. /*
  978. * debug_sprintf_event:
  979. */
  980. debug_entry_t *__debug_sprintf_event(debug_info_t *id, int level, char *string, ...)
  981. {
  982. debug_sprintf_entry_t *curr_event;
  983. debug_entry_t *active;
  984. unsigned long flags;
  985. int numargs, idx;
  986. va_list ap;
  987. if (!debug_active || !id->areas)
  988. return NULL;
  989. numargs = debug_count_numargs(string);
  990. if (debug_critical) {
  991. if (!spin_trylock_irqsave(&id->lock, flags))
  992. return NULL;
  993. } else {
  994. spin_lock_irqsave(&id->lock, flags);
  995. }
  996. active = get_active_entry(id);
  997. curr_event = (debug_sprintf_entry_t *) DEBUG_DATA(active);
  998. va_start(ap, string);
  999. curr_event->string = string;
  1000. for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
  1001. curr_event->args[idx] = va_arg(ap, long);
  1002. va_end(ap);
  1003. debug_finish_entry(id, active, level, 0);
  1004. spin_unlock_irqrestore(&id->lock, flags);
  1005. return active;
  1006. }
  1007. EXPORT_SYMBOL(__debug_sprintf_event);
  1008. /*
  1009. * debug_sprintf_exception:
  1010. */
  1011. debug_entry_t *__debug_sprintf_exception(debug_info_t *id, int level, char *string, ...)
  1012. {
  1013. debug_sprintf_entry_t *curr_event;
  1014. debug_entry_t *active;
  1015. unsigned long flags;
  1016. int numargs, idx;
  1017. va_list ap;
  1018. if (!debug_active || !id->areas)
  1019. return NULL;
  1020. numargs = debug_count_numargs(string);
  1021. if (debug_critical) {
  1022. if (!spin_trylock_irqsave(&id->lock, flags))
  1023. return NULL;
  1024. } else {
  1025. spin_lock_irqsave(&id->lock, flags);
  1026. }
  1027. active = get_active_entry(id);
  1028. curr_event = (debug_sprintf_entry_t *)DEBUG_DATA(active);
  1029. va_start(ap, string);
  1030. curr_event->string = string;
  1031. for (idx = 0; idx < min(numargs, (int)(id->buf_size / sizeof(long)) - 1); idx++)
  1032. curr_event->args[idx] = va_arg(ap, long);
  1033. va_end(ap);
  1034. debug_finish_entry(id, active, level, 1);
  1035. spin_unlock_irqrestore(&id->lock, flags);
  1036. return active;
  1037. }
  1038. EXPORT_SYMBOL(__debug_sprintf_exception);
  1039. /**
  1040. * debug_register_view() - registers new debug view and creates debugfs
  1041. * dir entry
  1042. *
  1043. * @id: handle for debug log
  1044. * @view: pointer to debug view struct
  1045. *
  1046. * Return:
  1047. * - 0 : ok
  1048. * - < 0: Error
  1049. */
  1050. int debug_register_view(debug_info_t *id, struct debug_view *view)
  1051. {
  1052. unsigned long flags;
  1053. struct dentry *pde;
  1054. umode_t mode;
  1055. int rc = 0;
  1056. int i;
  1057. if (!id)
  1058. goto out;
  1059. mode = (id->mode | S_IFREG) & ~S_IXUGO;
  1060. if (!(view->prolog_proc || view->format_proc || view->header_proc))
  1061. mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
  1062. if (!view->input_proc)
  1063. mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  1064. pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
  1065. id, &debug_file_ops);
  1066. spin_lock_irqsave(&id->lock, flags);
  1067. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  1068. if (!id->views[i])
  1069. break;
  1070. }
  1071. if (i == DEBUG_MAX_VIEWS) {
  1072. rc = -1;
  1073. } else {
  1074. id->views[i] = view;
  1075. id->debugfs_entries[i] = pde;
  1076. }
  1077. spin_unlock_irqrestore(&id->lock, flags);
  1078. if (rc) {
  1079. pr_err("Registering view %s/%s would exceed the maximum "
  1080. "number of views %i\n", id->name, view->name, i);
  1081. debugfs_remove(pde);
  1082. }
  1083. out:
  1084. return rc;
  1085. }
  1086. EXPORT_SYMBOL(debug_register_view);
  1087. /**
  1088. * debug_unregister_view() - unregisters debug view and removes debugfs
  1089. * dir entry
  1090. *
  1091. * @id: handle for debug log
  1092. * @view: pointer to debug view struct
  1093. *
  1094. * Return:
  1095. * - 0 : ok
  1096. * - < 0: Error
  1097. */
  1098. int debug_unregister_view(debug_info_t *id, struct debug_view *view)
  1099. {
  1100. struct dentry *dentry = NULL;
  1101. unsigned long flags;
  1102. int i, rc = 0;
  1103. if (!id)
  1104. goto out;
  1105. spin_lock_irqsave(&id->lock, flags);
  1106. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  1107. if (id->views[i] == view)
  1108. break;
  1109. }
  1110. if (i == DEBUG_MAX_VIEWS) {
  1111. rc = -1;
  1112. } else {
  1113. dentry = id->debugfs_entries[i];
  1114. id->views[i] = NULL;
  1115. id->debugfs_entries[i] = NULL;
  1116. }
  1117. spin_unlock_irqrestore(&id->lock, flags);
  1118. debugfs_remove(dentry);
  1119. out:
  1120. return rc;
  1121. }
  1122. EXPORT_SYMBOL(debug_unregister_view);
  1123. static inline char *debug_get_user_string(const char __user *user_buf,
  1124. size_t user_len)
  1125. {
  1126. char *buffer;
  1127. buffer = kmalloc(user_len + 1, GFP_KERNEL);
  1128. if (!buffer)
  1129. return ERR_PTR(-ENOMEM);
  1130. if (copy_from_user(buffer, user_buf, user_len) != 0) {
  1131. kfree(buffer);
  1132. return ERR_PTR(-EFAULT);
  1133. }
  1134. /* got the string, now strip linefeed. */
  1135. if (buffer[user_len - 1] == '\n')
  1136. buffer[user_len - 1] = 0;
  1137. else
  1138. buffer[user_len] = 0;
  1139. return buffer;
  1140. }
  1141. static inline int debug_get_uint(char *buf)
  1142. {
  1143. int rc;
  1144. buf = skip_spaces(buf);
  1145. rc = simple_strtoul(buf, &buf, 10);
  1146. if (*buf)
  1147. rc = -EINVAL;
  1148. return rc;
  1149. }
  1150. /*
  1151. * functions for debug-views
  1152. ***********************************
  1153. */
  1154. /*
  1155. * prints out actual debug level
  1156. */
  1157. static int debug_prolog_pages_fn(debug_info_t *id, struct debug_view *view,
  1158. char *out_buf)
  1159. {
  1160. return sprintf(out_buf, "%i\n", id->pages_per_area);
  1161. }
  1162. /*
  1163. * reads new size (number of pages per debug area)
  1164. */
  1165. static int debug_input_pages_fn(debug_info_t *id, struct debug_view *view,
  1166. struct file *file, const char __user *user_buf,
  1167. size_t user_len, loff_t *offset)
  1168. {
  1169. int rc, new_pages;
  1170. char *str;
  1171. if (user_len > 0x10000)
  1172. user_len = 0x10000;
  1173. if (*offset != 0) {
  1174. rc = -EPIPE;
  1175. goto out;
  1176. }
  1177. str = debug_get_user_string(user_buf, user_len);
  1178. if (IS_ERR(str)) {
  1179. rc = PTR_ERR(str);
  1180. goto out;
  1181. }
  1182. new_pages = debug_get_uint(str);
  1183. if (new_pages < 0) {
  1184. rc = -EINVAL;
  1185. goto free_str;
  1186. }
  1187. rc = debug_set_size(id, id->nr_areas, new_pages);
  1188. if (rc != 0) {
  1189. rc = -EINVAL;
  1190. goto free_str;
  1191. }
  1192. rc = user_len;
  1193. free_str:
  1194. kfree(str);
  1195. out:
  1196. *offset += user_len;
  1197. return rc; /* number of input characters */
  1198. }
  1199. /*
  1200. * prints out actual debug level
  1201. */
  1202. static int debug_prolog_level_fn(debug_info_t *id, struct debug_view *view,
  1203. char *out_buf)
  1204. {
  1205. int rc = 0;
  1206. if (id->level == DEBUG_OFF_LEVEL)
  1207. rc = sprintf(out_buf, "-\n");
  1208. else
  1209. rc = sprintf(out_buf, "%i\n", id->level);
  1210. return rc;
  1211. }
  1212. /*
  1213. * reads new debug level
  1214. */
  1215. static int debug_input_level_fn(debug_info_t *id, struct debug_view *view,
  1216. struct file *file, const char __user *user_buf,
  1217. size_t user_len, loff_t *offset)
  1218. {
  1219. int rc, new_level;
  1220. char *str;
  1221. if (user_len > 0x10000)
  1222. user_len = 0x10000;
  1223. if (*offset != 0) {
  1224. rc = -EPIPE;
  1225. goto out;
  1226. }
  1227. str = debug_get_user_string(user_buf, user_len);
  1228. if (IS_ERR(str)) {
  1229. rc = PTR_ERR(str);
  1230. goto out;
  1231. }
  1232. if (str[0] == '-') {
  1233. debug_set_level(id, DEBUG_OFF_LEVEL);
  1234. rc = user_len;
  1235. goto free_str;
  1236. } else {
  1237. new_level = debug_get_uint(str);
  1238. }
  1239. if (new_level < 0) {
  1240. pr_warn("%s is not a valid level for a debug feature\n", str);
  1241. rc = -EINVAL;
  1242. } else {
  1243. debug_set_level(id, new_level);
  1244. rc = user_len;
  1245. }
  1246. free_str:
  1247. kfree(str);
  1248. out:
  1249. *offset += user_len;
  1250. return rc; /* number of input characters */
  1251. }
  1252. /*
  1253. * flushes debug areas
  1254. */
  1255. static void debug_flush(debug_info_t *id, int area)
  1256. {
  1257. unsigned long flags;
  1258. int i, j;
  1259. if (!id || !id->areas)
  1260. return;
  1261. spin_lock_irqsave(&id->lock, flags);
  1262. if (area == DEBUG_FLUSH_ALL) {
  1263. id->active_area = 0;
  1264. memset(id->active_entries, 0, id->nr_areas * sizeof(int));
  1265. for (i = 0; i < id->nr_areas; i++) {
  1266. id->active_pages[i] = 0;
  1267. for (j = 0; j < id->pages_per_area; j++)
  1268. memset(id->areas[i][j], 0, PAGE_SIZE);
  1269. }
  1270. } else if (area >= 0 && area < id->nr_areas) {
  1271. id->active_entries[area] = 0;
  1272. id->active_pages[area] = 0;
  1273. for (i = 0; i < id->pages_per_area; i++)
  1274. memset(id->areas[area][i], 0, PAGE_SIZE);
  1275. }
  1276. spin_unlock_irqrestore(&id->lock, flags);
  1277. }
  1278. /*
  1279. * view function: flushes debug areas
  1280. */
  1281. static int debug_input_flush_fn(debug_info_t *id, struct debug_view *view,
  1282. struct file *file, const char __user *user_buf,
  1283. size_t user_len, loff_t *offset)
  1284. {
  1285. char input_buf[1];
  1286. int rc = user_len;
  1287. if (user_len > 0x10000)
  1288. user_len = 0x10000;
  1289. if (*offset != 0) {
  1290. rc = -EPIPE;
  1291. goto out;
  1292. }
  1293. if (copy_from_user(input_buf, user_buf, 1)) {
  1294. rc = -EFAULT;
  1295. goto out;
  1296. }
  1297. if (input_buf[0] == '-') {
  1298. debug_flush(id, DEBUG_FLUSH_ALL);
  1299. goto out;
  1300. }
  1301. if (isdigit(input_buf[0])) {
  1302. int area = ((int) input_buf[0] - (int) '0');
  1303. debug_flush(id, area);
  1304. goto out;
  1305. }
  1306. pr_info("Flushing debug data failed because %c is not a valid "
  1307. "area\n", input_buf[0]);
  1308. out:
  1309. *offset += user_len;
  1310. return rc; /* number of input characters */
  1311. }
  1312. /*
  1313. * prints debug data in hex/ascii format
  1314. */
  1315. static int debug_hex_ascii_format_fn(debug_info_t *id, struct debug_view *view,
  1316. char *out_buf, const char *in_buf)
  1317. {
  1318. int i, rc = 0;
  1319. for (i = 0; i < id->buf_size; i++)
  1320. rc += sprintf(out_buf + rc, "%02x ", ((unsigned char *) in_buf)[i]);
  1321. rc += sprintf(out_buf + rc, "| ");
  1322. for (i = 0; i < id->buf_size; i++) {
  1323. unsigned char c = in_buf[i];
  1324. if (isascii(c) && isprint(c))
  1325. rc += sprintf(out_buf + rc, "%c", c);
  1326. else
  1327. rc += sprintf(out_buf + rc, ".");
  1328. }
  1329. rc += sprintf(out_buf + rc, "\n");
  1330. return rc;
  1331. }
  1332. /*
  1333. * prints header for debug entry
  1334. */
  1335. int debug_dflt_header_fn(debug_info_t *id, struct debug_view *view,
  1336. int area, debug_entry_t *entry, char *out_buf)
  1337. {
  1338. unsigned long sec, usec;
  1339. unsigned long caller;
  1340. unsigned int level;
  1341. char *except_str;
  1342. int rc = 0;
  1343. level = entry->level;
  1344. sec = entry->clock;
  1345. usec = do_div(sec, USEC_PER_SEC);
  1346. if (entry->exception)
  1347. except_str = "*";
  1348. else
  1349. except_str = "-";
  1350. caller = (unsigned long) entry->caller;
  1351. rc += sprintf(out_buf, "%02i %011ld:%06lu %1u %1s %04u %px ",
  1352. area, sec, usec, level, except_str,
  1353. entry->cpu, (void *)caller);
  1354. return rc;
  1355. }
  1356. EXPORT_SYMBOL(debug_dflt_header_fn);
  1357. /*
  1358. * prints debug data sprintf-formated:
  1359. * debug_sprinf_event/exception calls must be used together with this view
  1360. */
  1361. #define DEBUG_SPRINTF_MAX_ARGS 10
  1362. static int debug_sprintf_format_fn(debug_info_t *id, struct debug_view *view,
  1363. char *out_buf, const char *inbuf)
  1364. {
  1365. debug_sprintf_entry_t *curr_event = (debug_sprintf_entry_t *)inbuf;
  1366. int num_longs, num_used_args = 0, i, rc = 0;
  1367. int index[DEBUG_SPRINTF_MAX_ARGS];
  1368. /* count of longs fit into one entry */
  1369. num_longs = id->buf_size / sizeof(long);
  1370. if (num_longs < 1)
  1371. goto out; /* bufsize of entry too small */
  1372. if (num_longs == 1) {
  1373. /* no args, we use only the string */
  1374. strcpy(out_buf, curr_event->string);
  1375. rc = strlen(curr_event->string);
  1376. goto out;
  1377. }
  1378. /* number of arguments used for sprintf (without the format string) */
  1379. num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
  1380. memset(index, 0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
  1381. for (i = 0; i < num_used_args; i++)
  1382. index[i] = i;
  1383. rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
  1384. curr_event->args[index[1]], curr_event->args[index[2]],
  1385. curr_event->args[index[3]], curr_event->args[index[4]],
  1386. curr_event->args[index[5]], curr_event->args[index[6]],
  1387. curr_event->args[index[7]], curr_event->args[index[8]],
  1388. curr_event->args[index[9]]);
  1389. out:
  1390. return rc;
  1391. }
  1392. /*
  1393. * debug_init:
  1394. * - is called exactly once to initialize the debug feature
  1395. */
  1396. static int __init debug_init(void)
  1397. {
  1398. s390dbf_sysctl_header = register_sysctl("s390dbf", s390dbf_table);
  1399. mutex_lock(&debug_mutex);
  1400. debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT, NULL);
  1401. initialized = 1;
  1402. mutex_unlock(&debug_mutex);
  1403. return 0;
  1404. }
  1405. postcore_initcall(debug_init);