debug.c 35 KB

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