debug.c 35 KB

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