debug.c 36 KB

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