debug.c 38 KB

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